[Python Study Notes] Process information (Tintin software monitoring process, http-post)

Source: Internet
Author: User

"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "", "" "". "" "" "" "" "" "" "" "," "" "" "," "" "" "," "" "," "" ">> Process Information .py>> Author: Liu Yang>> e-mail: [email protected] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "." "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "#!/usr/bin/env python#-*-coding:utf-8-*-import sys,osimport psutil # http Get/postimport requests# processing string import json# Get system time import time# tintin urlurl = ' Https://oapi.dingtalk.com/robot/send? Access_token=3d94c740a27d5f70da6acf2a1c9383b338ad5c562fbaece8d38595c405e7e6a9 ' # converted to the current timestamp Time_now = Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ()) # process to monitor monitor_name = {' UFW '}# the process to be monitored, corresponding to the shell statement, defined as empty Monitor_map ={} #要监控的服务名集合, {' UFW ': ' Service UFW start '}for m in monitor_name:monitor_map[m]= ' service ' +m+ ' Start ' Print (Monitor_map) # print (time_now) # show process, respectively get process dictionary {' PID ': ' Name '} and de-Ganso () def process_message (): # process dictionary, save PID, key value of process name to proc_dict={} # into To save the process name Proc_list=set () # Psutil. Process (pid=4, name= ' SysTem ', started= ' 2018-02-11 16:39:45 ') for P in Psutil.process_iter (attrs=[' pid ', ' Name ']): proc_dict[p.info[' pid '] ]=p.info[' name '] Proc_list.add (p.info[' name ')) print (proc_list) print (proc_dict) proc_stop = Monitor_name -Proc_list #proc_stop: Extract which process does not open if proc_stop under the current operating system: #当前没有启动的这个进程集合不为空, a service has died for P in Proc_stop : Print ("restarting%s"%p) #取出每一个没有启动的服务名 p_status = ' Stop ' p_name = P dat A = {"Msgtype": "Markdown", "markdown": {"title": "Monitoring Message", "Te XT ":" # # # #%s\n "% time_now +" > ##### service Name:%s \ n "% p_name +" > ##### status:%s \ n " % p_status + "> ##### trying to Start"}} headers = {' Content-type ': ' Appl Ication/json; Charset=utf-8 '} send_data = Json.dumps (data). Encode (' Utf-8 ') requests.post (url=url,data=send_data,he Aders=heaDERs) #post提交数据: Submitted address, submitted data if sys.platform.lower () = = ' Linux ': Os.system (monitor_map[p                ]) #函数不会给你返回值 Proc_name = set () for P2 in Psutil.process_iter (attrs=[' pid ', ' Name ']): Proc_name.add (p2.info[' name ') #操作系统下的所有进程名拿出来 if p in Proc_name:prin                    T ("%s started successfully"%p) P_status = ' start ' data = {"Msgtype": "Markdown",                     "Markdown": {"title": "Monitor Message", "text": "# #%s\n"% time_now + "> ##### service Name:%s \ n"% p_name + "> ##### status:%s \ n"% p_status + "& Gt ##### reboot Successful "}} headers = {' Content-type ': ' Application/json; charset=ut F-8 '} send_data = Json.dumps (data). Encode (' Utf-8 ') requests.post (url=url,data=send_data,head        Ers=headers)    Else:print ("%s failed to start"%p) P_status = ' Stop ' data = {' MSgt Ype ":" Markdown "," markdown ": {" title ":" Monitoring Message "," text ":" # # # #%s\ N "% time_now +" > ##### service Name:%s \ n "% p_name +" > ##### status:%s \ n "% P_st ATUs + "> ##### start Failed"}} headers = {' Content-typ E ': ' Application/json; Charset=utf-8 '} send_data = Json.dumps (data). Encode (' Utf-8 ') requests.post (url=url,data=send _data,headers=headers) Time.sleep (2) if __name__ = = ' __main__ ': While True:process_message ()

[Python Study Notes] Process information (Tintin software monitoring process, http-post)

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.