Shell脚本一键推送到钉钉告警并@指定人

打印 上一主题 下一主题

主题 1023|帖子 1023|积分 3069

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
 1. Shell脚本

  1.   cat    /opt/monitor/device/device.sh
复制代码
  1. #!/bin/bash
  2. # 域名列表文件绝对路径
  3. text_file="/opt/monitor/device/device.txt"
  4. #PG数据库密码环境变量
  5. export PGPASSWORD='8888888888888888'
  6. #结果为0代表正常设备,非0代表有异常设备
  7. sql_cmd="select count(1) from aio_brm.device where (org_id = 99999999999999999 or org_id = 988888888888888881) and del_flag = '0' and status = '0'"
  8. psql -U database_user -d  database_name  -p 88888 -h sh-postgres-8888888888.sql.tencentcdb.com   -c  "$sql_cmd" > $text_file
  9. #钉钉webhook
  10. prod_webhook_url="https://oapi.dingtalk.com/robot/send?access_token=9999999999999999999999999999999999999"
  11. test_webhook_url="https://oapi.dingtalk.com/robot/send?access_token=9999999999999999999888888888888888888"
  12. MESSAGE=$(cat "$text_file")
  13. # 要@的用户手机号(多个用户用逗号分隔)
  14. AT_MOBILES="13088888888,13099999999"
  15. #JSON_DATA=$(cat <<EOF { "msgtype": "text", "text": { "content": "$MESSAGE" } } EOF )
  16. #curl -s -X POST -H 'Content-Type: application/json' -d "$JSON_DATA" "$webhook_url"
  17. #钉钉推送告警信息
  18. prod_dingding_push(){
  19.       curl      -H "Content-Type: application/json" \
  20.                 -d '{
  21.                     "msgtype": "text",
  22.                     "text": {
  23.                         "content": "告警内容:【生产环境告警,请及时处理】\n执行语句:【select count(1) from aio_brm.device where (org_id = 99999999999999999 or org_id = 988888888888888881) and del_flag = '0' and status = '0'】"
  24.                     },
  25.                     'at': {
  26.                          'atMobiles': ['19099999999','180888888888'],
  27.                          'isAtAll': false
  28.                     }
  29.                 }' $prod_webhook_url
  30. }
  31. test_dingding_push(){
  32.       curl      -H "Content-Type: application/json" \
  33.                 -d '{
  34.                     "msgtype": "text",
  35.                     "text": {
  36.                         "content": "【生产环境告警,请及时处理】\n[0代表正常设备;大于0代表异常设备]:\n '"$MESSAGE"' \n"
  37.                     },
  38.                     'at': {
  39.                          'atMobiles': ['19099999999','180888888888'],
  40.                          'isAtAll': false
  41.                     }
  42.                 }' $test_webhook_url
  43. }
  44. #
  45. MySQL_Results=$( cat "$text_file"   |  sed   -n   3p)
  46. if [ $MySQL_Results  == 0 ] ; then
  47.     echo  "Device  is online"
  48. else
  49.     prod_dingding_push   &&  test_dingding_push
  50. fi
复制代码
2. 根据业务需求配置定时使命

  1. [root@localhost]# crontab  -l
  2. 30  08,16,23   *   *   *    /opt/monitor/device/device.sh  >  /dev/null  2>&1
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

老婆出轨

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表