2018-4-26 python enables monitoring of service processes, alarms to nail bots and restart tasks

Source: Internet
Author: User

recently saw a nail software robot, feel very good, compared to mail, alarm, more comfortable, special offer code content.

[[email protected] ~]# nohup python test.py & # can refer to the next article, the Python process constitutes a systemd service background run, monitor better every 5 seconds

1 #Coding:utf-82 ImportPsutil3 ImportRequests4 Import Time5 ImportOS6 ImportJSON7 8Monitor_name = Set (['httpd','Cobblerd'])#user-specified service process name for monitoring9 TenProc_dict = {} OneProc_name = set ()#System-Detected process name AMonitor_map = { -     'httpd':'systemctl Restart httpd', -     'Cobblerd':'systemctl Restart Cobblerd'  #the system restarts automatically after the process is down the } -  -Dingding_url ='https://oapi.dingtalk.com/robot/send?access_token= b5258c4335ed8ab792075013c965efcbf4f8940f92e7bd936cdc7842d3bf9405' - #Nail Robot Token use reference document: Http://www.pc6.com/infoview/Article_108931.html +  -  whileTrue: +      forProcinchPsutil.process_iter (attrs=['PID','name']): Aproc_dict[proc.info['PID']] = proc.info['name'] atProc_name.add (proc.info['name']) -  -Proc_stop = Monitor_name-proc_name#The name of the stopped process is found in the form of a collection, but the latter is not. -  -     ifProc_stop:#if the process of monitoring is stopped, then we need the alarm and auto restart function. -          forPinchProc_stop: inP_status ='Stop' -P_name =P todata = { +                 "Msgtype":"markdown", -                 "markdown": { the                     "title":"Monitoring Information", *                     "text":"# #%s\n"% Time.strftime ("%y-%m-%d%x") + $                     "> # # # # service Name:%s \ n"% p_name +Panax Notoginseng                     "> # # Status:%s \ n"% p_status + -                     "> # # # is trying to start" the                 }, +             } Aheaders = {'Content-type':'Application/json;charset=utf-8'} theSend_data = json.dumps (data). Encode ('Utf-8') +Requests.post (url=dingding_url,data=send_data,headers=headers) -  $Os.system (Monitor_map[p_name])#perform a restart command and then determine if the restart was successful $Proc_set =set () -              forProc_againinchPsutil.process_iter (attrs=['PID','name']): -Proc_set.add (proc_again.info['name']) the  -             ifPinchProc_set:#If the process starts successfully, p is a previously stopped process, and Proc_set is a collection of all processes that have been restarted onceWuyiP_status ='Success' theP_name =P -data = { Wu                     "Msgtype":"markdown", -                     "markdown": { About                         "title":"Monitoring Information", $                         "text":"# #%s\n"% Time.strftime ("%y-%m-%d%x") + -                         "> # # # # service Name:%s \ n"% p_name + -                         "> # # Status:%s \ n"% p_status + -                         "> # # # has started successfully and the service is running! " A                     }, +                 } theheaders = {'Content-type':'Application/json;charset=utf-8'} -Send_data = json.dumps (data). Encode ('Utf-8') $Requests.post (url=dingding_url,data=send_data,headers=headers) the             Else: theP_status ='Reboot failed' theP_name =P thedata = { -                     "Msgtype":"markdown", in                     "markdown": { the                         "title":"Monitoring Information", the                         "text":"# #%s\n"% Time.strftime ("%y-%m-%d%x") + About                         "> # # # # service Name:%s \ n"% p_name + the                         "> # # Status:%s \ n"% p_status + the                         "> # # # Sorry, service startup failed bird! " the                     }, +                 } -headers = {'Content-type':'Application/json;charset=utf-8'} theSend_data = json.dumps (data). Encode ('Utf-8')BayiRequests.post (url=dingding_url,data=send_data,headers=headers) theTime.sleep (5)

nail Software Results Picture

2018-4-26 python enables monitoring of service processes, alarms to nail bots and restart tasks

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.