ImportRequestsImportSYSImportJsondingding_url='https://oapi.dingtalk.com/robot/send?access_token=6d11af3252812ea50410c2ccb861814a6ed11b2306606934a5d4ca9f2ec8c09'Data= {"Msgtype":"markdown","markdown": {"title":"Monitoring","text":"Apche Exception"}}headers= {'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)
#Coding:utf-8ImportPsutilImportRequestsImport TimeImportOSImportJsonmonitor_name= Set (['httpd','Cobblerd'])#user-specified service process name for monitoringproc_dict={}proc_name= Set ()#System-Detected process nameMonitor_map = { 'httpd':'systemctl Restart httpd', 'Cobblerd':'systemctl Restart Cobblerd' #the system restarts automatically after the process is down}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']): proc_dict[proc.info['PID']] = proc.info['name'] Proc_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:p_status='Stop'P_name=P Data= { "Msgtype":"markdown", "markdown": { "title":"Monitoring Information", "text":"# #%s\n"% Time.strftime ("%y-%m-%d%x") +"> # # # # service Name:%s \ n"% p_name +"> # # Status:%s \ n"% p_status +"> # # # is trying to start"},} headers= {'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) Os.system (Monitor_map[p_name])#perform a restart command and then determine if the restart was successfulProc_set =set () forProc_againinchPsutil.process_iter (attrs=['PID','name']): Proc_set.add (proc_again.info['name']) 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 onceP_status ='Success'P_name=P Data= { "Msgtype":"markdown", "markdown": { "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! "},} headers= {'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)Else: P_status='Reboot failed'P_name=P Data= { "Msgtype":"markdown", "markdown": { "title":"Monitoring Information", "text":"# #%s\n"% Time.strftime ("%y-%m-%d%x") +"> # # # # service Name:%s \ n"% p_name +"> # # Status:%s \ n"% p_status +"> # # # Sorry, service startup failed bird! "},} headers= {'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) Time.sleep (5)
python3-implementation of nail alarm