钉钉建群

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

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

  点击“值班群”按纽:

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

  拉群至少需要2个人:

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

添加群机器人

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

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

  点击“智能群助手”:

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

  点击“添加机器人”:

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

  点击加号:“+”

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

  点击“自定义”:

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

  点击“添加”按纽:

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

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

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

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

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

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

测试curl命令

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

  执行脚本结果:

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

脚本内容

[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"}

  执行脚本结果:

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

参考文档

https://www.cnblogs.com/xuliuzai/p/11469039.html——shell操作钉钉机器人实现告警提醒

https://www.cnblogs.com/fulu/p/13749745.html——从零开始打造专属钉钉机器人