天天看點

釘釘自定義機器人_shell腳本發送

報警媒介類型-->建立媒體類型

腳本參數:

# 主題

{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 我是内容

釘釘自定義機器人_shell腳本發送

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 > ![screenshot](${test_url})\n > #### 明天又是新的一天 *晚安*\n'
                        }
                }"
}

rebot
           
釘釘自定義機器人_shell腳本發送

更多參考釘釘自定義機器人的官方文檔

zabbix釘釘報警

zabbix郵件報警