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 天前
钉钉重走“取经路”
钉钉
需要点灵感1 天前
# 从身份证读卡到钉钉同步:C# WinForms企业级应用开发实战
开发语言·c#·钉钉
是Winky啊1 天前
【OpenClaw】钉钉数据分析bot
钉钉·agent·openclaw
天草二十六_简村人1 天前
阿里云DMS工单审批对接钉钉应用的实践示例
运维·数据库·后端·阿里云·云原生·云计算·钉钉
java资料站2 天前
钉钉远程一键执行服务器启动脚本
运维·服务器·钉钉
一直会游泳的小猫3 天前
USB设备历史清理工具使用手册
脚本·注册表清理·usb记录
翼龙云_cloud4 天前
阿里云代理商:轻量服务器部署 OpenClaw 集成钉钉实现自动化办公
服务器·人工智能·阿里云·钉钉·openclaw
LilySesy4 天前
【案例总结】震撼巨作——SAP连接钉钉WEBHOOK
运维·人工智能·ai·钉钉·sap·abap·webhook
狂龙骄子5 天前
CentOS查看压缩包文件列表
shell·rar·7-zip·cygwin·7z·压缩包文件列表·查看压缩包
李小白杂货铺5 天前
网络测速脚本(MacOS和Linux平台可用)
linux·macos·脚本·curl·ping·网络测速·网络测速脚本