脚本参数:
# 主题
{ALERT.SUBJECT}
# 内容
{ALERT.MESSAGE}
2. 配置–>动作–>创建动作
动作里的操作部分
告警通知: {EVENT.NAME}
告警时间:{EVENT.TIME}
告警项目:{TRIGGER.KEY1}
告警主机: {HOST.NAME}
恢复操作
问题已解决: {EVENT.NAME}
解决时间:{EVENT.RECOVERY.TIME}
主机: {HOST.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}
3. 管理–>用户–>Admin–>报警媒介
收件人随意,脚本选自己创的
vim /usr/lib/zabbix/alertscripts/ding.sh
#!/bin/bash
webhook='https://oapi.dingtalk.com/robot/send?access_token=dd53f2b447606292117415xxxx5e8ef3c2852xxxx8c95c5eedcf5e3e6e35d7e1'
text=$2
title=$1
picture_url='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.sayloving.com%2Fuploads%2Fallimg%2F20171220%2F1513758710130558.jpg&refer=http%3A%2F%2Fwww.sayloving.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1627091632&t=a74822dcc463f70ec32cce3980d5d270'
content_url='http://106.xx.170.xxx/zabbix/hosts.php?ddreset=1'
function rebot() {
curl $webhook -H 'Content-Type: application/json' -d "
{
'msgtype': 'link',
'link': {
'text': '$text',
'title': '$title',
'picUrl': '$picture_url',
'messageUrl': '$content_url'
}
}"
}
rebot
chmod a+x /usr/lib/zabbix/alertscripts/ding.sh
./ding.sh hello 我是内容
title是标题,text是内容,分别使用$1和$2传参,picture_url是图片url,content_url是点击标题可以跳转到对应的url。
这里使用的是钉钉自定义机器人的link类型
其余类型示例:
简单发送消息,发送消息为执行脚本时的$1参数
#!/bin/bash
test=$1
curl 'https://oapi.dingtalk.com/robot/send?access_token=5d68e789f19d90317e8b740afca005235c8cd794xxx521xxx36d508da833f2ef' \
-H 'Content-Type: application/json' \
-d '{"msgtype": "text","text": {"content":"'$test'"}}'
简单发送消息+艾特所有人or指定联系人
‘content’:发送的消息内容
‘at’:需要艾特人时都需要在这个字典里
‘isAtAll’:true艾特所有,flase关闭,为true时会覆盖掉下面单独艾特的指定手机号
‘atMobiles’:指定@手机号,需isAtAll为flase才能触发,也可不加isAtAll这行配置,默认为flase
#!/bin/bash
webhook='https://oapi.dingtalk.com/robot/send?access_token=5d68e789f19d90317e8b74xxxca005235c8cdxxx7575xxx5d36d508da833f2ef'
custom=$1
function rebot() {
curl $webhook -H 'Content-Type: application/json' -d "
{
'msgtype': 'text',
'text': {
'content': '$custom\n报警啦啦啦'
},
'at': {
'isAtAll': true,
'atMobiles':[
"13260xxxxxx"
"17386xxxxxx"
]
}
}"
}
rebot
markdown类型
‘text’:里是markdown格式的消息。
# 是调整字体大小,1个最大,6个最小,用* *括起来的是斜体字,加粗字体是用两个*括起来,测试过没有效果
#!/bin/bash
webhook='https://oapi.dingtalk.com/robot/send?access_token=5d68e789f19d90317e8b740afca005235c8cd794757521b5d36d508da833f2ef'
test_url='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fd.ifengimg.com%2Fw2560_h1440_q70%2Fimg1.ugc.ifeng.com%2Fnewugc%2F20200718%2F10%2Fwemedia%2F7b2e10e55d549a793c143b3e4fccca63c8d2016f_size333_w2560_h1440.jpg&refer=http%3A%2F%2Fd.ifengimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625235016&t=2706f0c8b591db6a476e95661f4dfff8'
function rebot() {
curl $webhook -H 'Content-Type: application/json' -d "
{
'msgtype': 'markdown',
'markdown': {
'title':'标题',
'text': '# 标题 \n > ##### *我是测试内容* \n > \n > #### 明天又是新的一天 *晚安*\n'
}
}"
}
rebot