Nail Monitoring Service

Requests Import 
Import OS
Import Time
Import JSON
Import psutil
Import Socket
the while True:
# access to the local computer name
hostname = socket.gethostname ()
# access to the local IP
IP = socket.gethostbyname (hostname)
TIME_NOW The time.strftime = ( 'the Y% - M-% D%% H:% M:% S ')
URL =' https://oapi.dingtalk.com/robot/send?access_token=91a38bc1a6afc21b23bf450a058baa9baa6d3beb7fcad1b431db18c97c258ce4 '
DEF Process ():
proc_dict = {}
proc_name = SET ( )
# to monitor service set
monitor_name = { 'the httpd'}
# to monitor the service
server = { 'httpd': ' systemctl start httpd'}
P in psutil.process_iter for (attrs = [ 'PID', 'name']):
# psutil.process_iter: Returns the operating system to wash all of the process name and ID
proc_dict [p.info [ 'PID']] = p.info [ 'name']
proc_name.add (p.info [ 'name'])
proc_stop = monitor_name - proc_name
'' '>>> A = {'. 1 '} >>> B = {' 2 ','. 3 ', '4'} >>> a - b { '1'} to monitoring service - service system = dead '' '
IF proc_stop:
for P1 in proc_stop:
p_status =' stop '
Data = { "MsgType": "text "," text ": {" content ":" host ip:% s \ n service:% s \ n status:% s \ n% s " % (ip, p1, p_status, time_now)}," at ":{ "atMobiles": [], "isAtAll": False}}
headers = { "the Content-the Type": "file application / json", "Chartset": "UTF-. 8"}
# string format the data into json
= json.dumps the send_data (Data)
# The post request is sent to request the interface nail
Response = requests.post (URL = URL, headers = headers, the send_data = Data)
# Returns stress after successful data
content = response.content. decode ()
the os.system (Server [P1])
proc_a = SET ()
for I in psutil.process_iter (attrs = [ 'PID', 'name']):
proc_a.add (i.info [ 'name'])
IF P1 in proc_a:
p_status = 'start'
Data = { "MsgType": "host ip::% s \ n service:% s \ n status:" text "," text " : {" content "% s \ n % s "% (ip, p1 , p_status, time_now)}," at ": {"atMobiles": [], "isAtAll": False}}
= json.dumps the send_data (Data)
Response = requests.post (URL = URL, headers = headers, the send_data = Data)
Content = response.content.decode ()

the else:
p_status = 'stop'
Data = # of this message type { a fixed text
"MsgType": "text", "text": {# message content
"content": "host ip:% s \ n service:% s \ n status:% s \ n% s" % (ip , P1, p_status, TIME_NOW)},
"AT": {# @ person phone number is
"atMobiles": [], "isAtAll":}} False
the send_data = json.dumps (Data)
Response = Requests.post(url=url, headers=headers, data=send_data)
content = response.content.decode()

time.sleep(5)
process()

Guess you like

Origin www.cnblogs.com/ZZYMiss/p/11502584.html