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
相关推荐
1.01^100012 天前
[2-02-02].第59节:功能函数 - 函数基础
shell
yunson_Liu13 天前
服务器获取外网IP,并发送到钉钉
服务器·钉钉
___波子 Pro Max.13 天前
Shell脚本中&&和||语法解析
shell
lewis_lk15 天前
文本处理三剑客: grep、sed、awk使用指南
后端·shell
lgbisha15 天前
华为云Flexus+DeepSeek征文|体验华为云ModelArts快速搭建Dify-LLM应用开发平台并创建自己dify钉钉群聊机器人
人工智能·ai·语言模型·自然语言处理·机器人·华为云·钉钉
水哥ansys16 天前
ANSYS ACT 格式及文件组成如何?
二次开发·脚本·ansys·workbench·水哥ansys·act
Johny_Zhao16 天前
企业级LDAP-RADIUS深度集成高可用方案
linux·网络·python·网络安全·信息安全·云计算·shell·cisco·系统运维
Johny_Zhao16 天前
基于CentOS Stream 8的物联网平台深度优化方案
linux·网络·网络安全·信息安全·云计算·shell·yum源·系统运维
Johny_Zhao18 天前
CentOS Stream 8 高可用 Kuboard 部署方案
linux·网络·python·网络安全·docker·信息安全·kubernetes·云计算·shell·yum源·系统运维·kuboard
LUCIAZZZ18 天前
钉钉机器人-自定义卡片推送快速入门
java·jvm·spring boot·机器人·钉钉·springboot