2

钉钉告警发送

 1 year ago
source link: https://bajie.dev/posts/20221123-dingding/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

钉钉告警发送

2022-11-23 1 分钟阅读
#!/usr/bin/env python
#coding:utf-8

#zabbix钉钉报警
import requests
import json
import sys
import os
import datetime

#这里是自己创建的机器人的webhook
webhook="https://oapi.dingtalk.com/robot/send?access_token=xxxxxx"      

user=sys.argv[1]
text=sys.argv[3]
data={
    "msgtype": "text",
    "text": {
        "content": text
    },
    "at": {
        "atMobiles": [
            user
        ],
        "isAtAll": False
    }
}
headers = {'Content-Type': 'application/json'}
x=requests.post(url=webhook,data=json.dumps(data),headers=headers)
python

下面放shell脚本的

#!/bin/bash
 
function ddmsg() {
    Token="xxxxxxxxxxxx"
    Weburl="https://oapi.dingtalk.com/robot/send?access_token="
    curl -ks -m 2 "${Weburl}${Token}" \
        -H 'Content-Type: application/json;charset=utf-8' \
        -d "{'msgtype': 'text', 
             'text': { 'content': '$*'}
        }" &>/tmp/ddmsg.log
    if [ `grep "errmsg.*ok" /tmp/ddmsg.log |wc -l` -ne 1 ]; then
        echo 'send error !';cat /tmp/ddmsg.log;exit 1;
    fi
}

#测试内容
echo "@警报
主机:$(hostname)
信息:Node test
时间:$(date +"%F %T")
">.msg

cat .msg
ddmsg "`cat .msg`"

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK