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
相关推荐
赵民勇2 天前
Linux/Unix中install命令全面用法解析
linux·shell
gis分享者2 天前
Shell 脚本中如何使用 trap 命令捕捉和处理信号(中等)
shell·脚本·信号·处理·trap·捕捉
課代表2 天前
bat 批处理中 FOR 命令的变量修饰符
脚本·bat·环境变量·批处理·路径·扩展名·短名称
IT 乔峰3 天前
脚本部署MHA集群
linux·shell
課代表3 天前
PowerShell 目录树生成与递归算法陷阱:目录统计为何从0变多?
脚本·powershell·bat·目录·计数·文件夹·树状结构
岚天start3 天前
【日志监控方案】Python脚本获取关键字日志信息并推送钉钉告警
python·钉钉·日志监控
牛奶咖啡135 天前
shell脚本编程(一)
linux·shell·shell脚本·shell脚本解析·grep命令语法·grep选项详解·正则表达式解析
gis分享者7 天前
请解释 Shell 脚本中的重定向(redirection)操作及其用途(中等)
shell·脚本·重定向·操作·用途·redirection
我是koten7 天前
K8s启动pod失败,日志报非法的Jar包排查思路(Invalid or corrupt jarfile /app/xxxx,jar)
java·docker·容器·kubernetes·bash·jar·shell
lingxiao168888 天前
vs脚本自动复制生成的文件至指定的位置
c#·脚本