Monitor system services with Python scripts

Source: Internet
Author: User

Recently, the development of the PC Service version of the virtual machine has begun to use for customers, service is always inexplicable dead, customers keep the phone to me, make me very bad mood, so in an afternoon, calm down, with Python wrote a simple monitoring process of the script, when the discovery process disappeared, Call the service now, turn on the service ...

The script works like this: The script reads the configuration file, read preconfigured call system service path and the service to be monitored in the Process Manager process name, the reason to use the configuration file, is to facilitate the need for friends, you only need to modify the process name and system path, the source code need not be modified. Look at the comments in the code ... The following is the configuration file Config.ini

[Monitorprogrampath]    
Programpath:c:\program files\ssh Communications security\ssh Secure shell\sshclient.exe   
        
[Monitorprocessname]    
ProcessName:SshClient.exe

The above can be based on your requirements to configure a different path and process name, I need to monitor SshClient.exe This program, then configure his calling system path and his process name in the Task Manager.

Let's look at the code here:

#-*-encoding:utf-8-*-Import Logging import WMI import OS import time from Configparser import Con Figparser configfile = ' config.ini ' config = Configparser () config.read (configfile) Programpath = Config.get (' Monitorprogrampath ', ' programpath ') processname = Config.get (' monitorprocessname ', ' processname ') #读取配置文件中的进程名和系统路径, All 2 parameters can be modified in the configuration file Prolist = [] #定义一个列表 C = WMI. WMI () def main (): for-process in c.win32_process (): Prolist.append (str (process). Name) #把所有任务管理器中的进程名添加到列表 if processname in prolist: #判断进程名是否在列表中, if true, the service being monitored is running state, #打印服 Normal run print ' print ' Server is running ... ' print ' else: #如果进程名不在列表中, that is, the monitored service is dead, then the log file Logging #日志文件名是以年月日为文件名 f=open ('. \\log\\ ' +time.strftime ("%y%m%d", Time.localtime ()) + '-exception.txt ', ' A ') print ' server not running,begining to restart server ... ' #打印服务状态 F.WRITE (' \ n ' + ' \ running,begining to restart server ... ' + ' \ n ') f.write (Time.strftime ('%y-%m-%d%h:%m:%s --%a--%c ', Time.localtime ()) + ' \ n ') #写入时间和服务状态到日志文件中 os.startfile (programpath) #调用服务重启 F.wri    
        Te (' Restart Server Success ... ' + ' \ n ') f.write (Time.strftime ('%y-%m-%d%h:%m:%s--%a--%c ', Time.localtime ())) F.close () #关闭文件 print ' Restart Server Success ... ' Print time.strftime ('%y-%m-%d%h:%m:%s --%a--%c ', Time.localtime ()) del prolist[:] #清空列表, otherwise the list will keep adding process name, will occupy system resources if __name__== "__main__ ": While True:main () Time.sleep #每隔10秒调用脚本看下服务是否正常, restart the service if it is not normal, print service is normal if normal

Oh, the script is still very simple, need a friend can take to play, as long as you modify the configuration file can be, do not need to modify the source code, you can run, hope for everyone's work and learning to help, if there is a problem in use, you can give me advice ...

This article from "You are a passer-by or fearless" blog, please be sure to keep this source http://world77.blog.51cto.com/414605/782935

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.