天天看点

钉钉机器人的使用——shell脚本发送监控信息

钉钉建群

  在钉钉对话框右上角点击加号,点击“发起群聊”:

钉钉机器人的使用——shell脚本发送监控信息

  点击“值班群”按纽:

钉钉机器人的使用——shell脚本发送监控信息

  拉群至少需要2个人:

钉钉机器人的使用——shell脚本发送监控信息

添加群机器人

  建群成功后,点击右上角的“群设置”按纽:

钉钉机器人的使用——shell脚本发送监控信息

  点击“智能群助手”:

钉钉机器人的使用——shell脚本发送监控信息

  点击“添加机器人”:

钉钉机器人的使用——shell脚本发送监控信息

  点击加号:“+”

钉钉机器人的使用——shell脚本发送监控信息

  点击“自定义”:

钉钉机器人的使用——shell脚本发送监控信息

  点击“添加”按纽:

钉钉机器人的使用——shell脚本发送监控信息

  勾选“自定义关键词”,设置关键词。

       在发送的消息里,必须带有设置的关键词,才能发送成功。

钉钉机器人的使用——shell脚本发送监控信息

  “Webhook”后面的地址,要写进shell脚本:https://oapi.dingtalk.com/robot/send?access_token=76cef610145f21e790973cbbzzzzzzzzzzaea74926c665a476e17998b1425354

钉钉机器人的使用——shell脚本发送监控信息

测试curl命令

curl 'https://oapi.dingtalk.com/robot/send?access_token=76cef610145f21e790973cbbzzzzzzzzzzaea74926c665a476e17998b1425354' \
   -H 'Content-Type: application/json' \
   -d '{"msgtype": "text", 
        "text": {
             "content": "运维监控 中考分流贻害无穷"
        }
      }'      

  执行脚本结果:

脚本内容

[root@iZ2ze7ajq328l8q1ai15szhZ ~]# cat oldboy-44_5.sh
#!/bin/sh

netstat -an|grep "ESTABLISHED"|awk '{print $5}'|grep ":8848$"|sort|uniq -c >ip.txt

exec <ip.txt
while read line
  do
    IP_count=`echo $line|awk '{print $1}'`
    IP=`echo $line|awk '{print $2}'|awk -F : '{print $1}'`
    if [ $IP_count -ge 10 ]
      then

        curl 'https://oapi.dingtalk.com/robot/send?access_token=76cef610145f21e790973cbbzzzzzzzzzzaea74926c665a476e17998b1425354' \
          -H 'Content-Type: application/json' \
          -d "{'msgtype': 'text', 
            'text': {
            'content': '运维监控\nIP地址:$IP请求$IP_count次'
            }
          }"

    fi
done
[root@iZ2ze7ajq328l8q1ai15szhZ ~]# sh oldboy-44_5.sh
{"errcode":0,"errmsg":"ok"}{"errcode":0,"errmsg":"ok"}      

  执行脚本结果:

参考文档