python 飞书告警监控

探测Nginx 服务监控

#coding: utf-8
import requests,json,os,time

def Alarm(img):
    data = {
            "msg_type": "interactive",
            "card": {
                "elements": [{
                    "tag": "div",
                    "text": {
                        "content": "<font color='grey'> **%s** </font>"  %img,
                        "tag": "lark_md",
                    }
                } ,{
                    "tag": "div",
                    "text": {
                        "content": "<font color='grey'> %s-- </font>" %content,
                        "tag": "lark_md"
                    }
                } , {
                    "tag": "div",
                    "text": {
                        "content": "告警平台: 来机房\n告警级别: critical\n运维团队: 影x运维\n故障实例: %s\n描述信息: %s"  %(content,content),
                        "tag": "lark_md"
                    }
                }, ],
                "header": {
                    "title": {
                        "content": "影x科技监控告警通知",
                        "tag": "plain_text"
                    },
                    "template": "red"
                }
            }
    }
    url='https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    header = {
        'Content - Type': 'application / json'
    }
    data = json.dumps(data)
    return requests.post(url,data,header)

def CheckNginx():
  process = os.popen('ps -ef |grep nginx |grep -v "grep"|wc -l')
  process_num = int(process.read())
  print(type(process_num),process_num)
  return process_num
  


if __name__ =="__main__":
  process_num = CheckNginx()
  if process_num == 0:
    content = "Nginx 服务异常"
    img = "Nginx 服务告警信息"
    Alarm(img)

posted @ 2023-04-21 14:31  lixinliang  阅读(229)  评论(0编辑  收藏  举报