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

1. Shell脚本

bash 复制代码
  cat    /opt/monitor/device/device.sh
bash 复制代码
#!/bin/bash

# 域名列表文件绝对路径

text_file="/opt/monitor/device/device.txt"

#PG数据库密码环境变量
export PGPASSWORD='8888888888888888'


#结果为0代表正常设备,非0代表有异常设备
sql_cmd="select count(1) from aio_brm.device where (org_id = 99999999999999999 or org_id = 988888888888888881) and del_flag = '0' and status = '0'"


psql -U database_user -d  database_name  -p 88888 -h sh-postgres-8888888888.sql.tencentcdb.com   -c  "$sql_cmd" > $text_file



#钉钉webhook
prod_webhook_url="https://oapi.dingtalk.com/robot/send?access_token=9999999999999999999999999999999999999"
test_webhook_url="https://oapi.dingtalk.com/robot/send?access_token=9999999999999999999888888888888888888"


MESSAGE=$(cat "$text_file")



# 要@的用户手机号(多个用户用逗号分隔)
AT_MOBILES="13088888888,13099999999"

#JSON_DATA=$(cat <<EOF { "msgtype": "text", "text": { "content": "$MESSAGE" } } EOF )


#curl -s -X POST -H 'Content-Type: application/json' -d "$JSON_DATA" "$webhook_url"

#钉钉推送告警信息
prod_dingding_push(){
      curl      -H "Content-Type: application/json" \
                -d '{
                    "msgtype": "text",
                    "text": {
                        "content": "告警内容:【生产环境告警,请及时处理】\n执行语句:【select count(1) from aio_brm.device where (org_id = 99999999999999999 or org_id = 988888888888888881) and del_flag = '0' and status = '0'】"
                    },
                    'at': {
                         'atMobiles': ['19099999999','180888888888'],
                         'isAtAll': false
                    }
                }' $prod_webhook_url
}


test_dingding_push(){
      curl      -H "Content-Type: application/json" \
                -d '{
                    "msgtype": "text",
                    "text": {
                        "content": "【生产环境告警,请及时处理】\n[0代表正常设备;大于0代表异常设备]:\n '"$MESSAGE"' \n"
                    },
                    'at': {
                         'atMobiles': ['19099999999','180888888888'],
                         'isAtAll': false
                    }
                }' $test_webhook_url
}


#
MySQL_Results=$( cat "$text_file"   |  sed   -n   3p)
if [ $MySQL_Results  == 0 ] ; then
    echo  "Device  is online"
else
    prod_dingding_push   &&  test_dingding_push
fi

2. 根据业务需求配置定时任务

bash 复制代码
[root@localhost]# crontab  -l
30  08,16,23   *   *   *    /opt/monitor/device/device.sh  >  /dev/null  2>&1
相关推荐
pingzhuyan13 小时前
linux运维异常(总) - 排查与修复(系统yum,docker,网络dns解析等)
linux·运维·docker·centos·shell
问道飞鱼13 小时前
【Linux知识】Shell 脚本参数详解:从基础到高级应用
linux·运维·服务器·shell
小嘟嘟1317 小时前
第3章 Shell 条件判断:解决 90% 的分支逻辑问题
linux·运维·shell
小嘟嘟1318 小时前
第1章 Shell 脚本入门:从 “Hello World“ 到自动化执行的完整路径
linux·运维·自动化·shell
小嘟嘟1319 小时前
第2章 Shell 变量与参数传递:3 种定义方式 + 避坑指南
linux·运维·shell
IT 乔峰1 天前
脚本自动部署本地YUM源+Apache配置
shell
哈哈~haha2 天前
Step 12: Shell Control as Container && Step 13:Margins and Paddings 使用CSS类对页面美化
shell·margin·ui5·paddings
小兔薯了2 天前
3.Shell 变量基础知识
shell
pingzhuyan2 天前
linux常规(shell脚本)-启动java程序-实现快捷git拉取,maven打包,nohup发布(无dockerfile版)
java·linux·git·maven·shell
一路向前的月光2 天前
Uniapp实现钉钉小程序前期基础配置
uni-app·钉钉