需要监控服务器的python3

一个数据处理进程 ,一个简单的shell脚本

#! /bin/sh  

proc_name=\"python3\"

proc_num() 
{
    num=`ps -ef | grep $proc_name | grep -v grep | wc -l`
    return $num
}

proc_num
number=$?
if [ $number -eq 0 ]
then
    cd /home/admin/monitor_course && python monitor_datahub.py
fi
~   

proc_name : 进程名

ps -ef | grep $xxxx_name | grep -v grep | wc -l           # 进程数量,  grep -v 是反向查找 grep -v grep  查找不含有 grep 字段行

使用webhook 接口发送钉玎消息:

  • 首先在钉玎群新建机器人 获取 webhook 
  • 新建monitor_datahub.py
import json
import requests

headers = {\'Content-Type\': \'application/json;charset=utf-8\', \'Connection\': \'close\'}  # 头部信息,Zabbix官方文档写法,可以查看zabbix官方文档
url = \"https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxx\" 这里的url就是webhook的Api接口


def message(text):  # 定义信息函数
    text_info = {  # 编写规则可以查看Zabbix官方文档的Zabbix Api
        \"msgtype\": \"text\",
        \"at\": {
            \"atMobiles\": [
                \"176xxxxxx\", \"133xxxxxxxx\"
            ],
            \"isAtAll\": False
        },
        \"text\": {
            \"content\": text
        }
    }

    print (requests.post(url,data=json.dumps(text_info),headers=headers,).content)  # 将返回的数据编码成 JSON 字符串


msg = \'****服务器进程监测******datahub  python3 数据处理进程异常\'
message(msg)

参数:

\"atMobiles\":        需要@钉玎群中人员的的电话号码,可添加多个
\"isAtAll\": False   是否@全部人员  默认false
\"content\": text    钉玎消息内容

收藏 打印