标签:art iter api emc 状态 需要 stat 机器人 access
import requests import sys import json dingding_url = ‘https://oapi.dingtalk.com/robot/send?access_token=6d11af3252812ea50410c2ccb861814a6ed11b2306606934a5d4ca9f2ec8c09‘ data = {"msgtype": "markdown","markdown": {"title": "监控","text": "apche异常"}} 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-8 import psutil import requests import time import os import json monitor_name = set([‘httpd‘,‘cobblerd‘]) # 用户指定监控的服务进程名称 proc_dict = {} proc_name = set() # 系统检测的进程名称 monitor_map = { ‘httpd‘: ‘systemctl restart httpd‘, ‘cobblerd‘: ‘systemctl restart cobblerd‘ # 系统在进程down掉后,自动重启 } dingding_url = ‘https://oapi.dingtalk.com/robot/send?access_token=b5258c4335ed8ab792075013c965efcbf4f8940f92e7bd936cdc7842d3bf9405‘ # 钉钉机器人token使用参考文档:http://www.pc6.com/infoview/Article_108931.html while True: for proc in psutil.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 # 通过集合的形式来找出停掉的进程名,前者有但是后者没有的 if proc_stop: # 如果确实有监控的进程停掉了,那么我们需要告警以及自动重启功能 for p in proc_stop: p_status = ‘停止‘ p_name = p data = { "msgtype": "markdown", "markdown": { "title": "监控信息", "text": "### %s\n" % time.strftime("%Y-%m-%d %X") + "> #### 服务名:%s \n\n" % p_name + "> #### 状态:%s \n\n" % p_status + "> #### 正在尝试启动" }, } 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]) # 执行重启命令,然后判断是否重启成功 proc_set = set() for proc_again in psutil.process_iter(attrs=[‘pid‘,‘name‘]): proc_set.add(proc_again.info[‘name‘]) if p in proc_set: # 如果进程启动成功,p是以前停掉的进程,proc_set是已经重启过一次后的所有进程集合 p_status = ‘成功‘ p_name = p data = { "msgtype": "markdown", "markdown": { "title": "监控信息", "text": "### %s\n" % time.strftime("%Y-%m-%d %X") + "> #### 服务名:%s \n\n" % p_name + "> #### 状态:%s \n\n" % p_status + "> #### 已经启动成功,服务正在运行!" }, } 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 = ‘重启失败‘ p_name = p data = { "msgtype": "markdown", "markdown": { "title": "监控信息", "text": "### %s\n" % time.strftime("%Y-%m-%d %X") + "> #### 服务名:%s \n\n" % p_name + "> #### 状态:%s \n\n" % p_status + "> #### Sorry,服务启动失败鸟!" }, } 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)
标签:art iter api emc 状态 需要 stat 机器人 access
原文地址:https://www.cnblogs.com/LyShark/p/9111326.html