6

脚本实现电话短信报警

 1 year ago
source link: https://www.v2ex.com/t/915883
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

运维在日常工作中,会写很多脚本,但是脚本想支持电话短信报警却比较麻烦,要想让脚本支持电话、短信报警,一般要在电话短信运营商平台提交资料审核才可以,有些运营商平台还需要提交企业资质才可以,这就给运维带来了很多门槛,最近发现一个平台不需要任何资质,微信扫码登录就可以实现脚本发短信打电话。

1 、配置电话短信模板

打开推送助手(push.spug.cc) / 消息模版 / 新建 / 选择电话短信 / 配置模板 / 配置通知手机号

push-add1.png
push-add2.png
push-add3.png
push-add4.png

2 、配置完模板,复制模板 URL ,就可以用脚本打电话发短信

push-add4.png

3 、先测试一下是否能正常打电话

# 命令执行 curl 命令测试打电话发短信,key1 和 key2 是模板里面接受的二个变量

[root@manage]# curl "https://push.spug.cc/send/byPj9N3xxxYQ?key1=测试报警&key2=服务器 IPxxxx"
{"code": 200, "msg": "请求成功"}

push-test2.png

push-test3.png

4 、编写脚本监控 Mysql 和主从同步,发生异常推送电话短信报警

[root@manage]# vim mysql_alert.sh

#!/bin/bash
mysql_user="repl"
mysql_pass="repl@password"


mysql_status=`netstat -nl | awk 'NR>2{if ($4 ~ /.*:3306/) {print "Yes";exit 0}}'`

if [ "$mysql_status" == "Yes" ];then
        slave_status=`mysql -u${mysql_user} -p${mysql_pass} -e"show slave status\G" | grep "Running" | awk '{if ($2 != "Yes") {print "No";exit 1}}'`
        if [ "$slave_status" == "No" ];then
                echo "slave is not working!"
                
                # 主从同步异常推送电话短信报警
                curl "https://push.spug.cc/send/byPj9N3xxxYQ?key1=Mysql 主从同步&key2=服务器 IP:192.168.100.1"
        else
                echo "slave is working."
        fi
else
        [ ! -f "/tmp/mysql_down" ] && echo "Mysql Server is down!" | mail -s "Warn!MySQL server is down!" ${email_addr}
        
        # mysql 进程异常电话短信报警
        curl "https://push.spug.cc/send/byPj9N3xxxYQ?key1=Mysql 进程&key2=192.168.100.1"
fi

5 、验证电话短信报警

push-test4.png

6 、支持更多渠道报警发送

如果想让脚本支持邮件、钉钉、飞书、企业微信报警,只需要在推送助手平台上编辑模板,把邮件、钉钉、飞书、企业微信打开配置一下就可以实现不改动脚本的情况下实现。

push-config1.png
push-config2.png
push-config3.png

飞书、钉钉、企业微信、邮件报警模板内容:


## <font color='#f90202'> ${key1} 发生异常 </font> ##
**报警项目:** ${key1} 
**报警主机:** ${key2} 

飞书群收到的报警

push-msg1.png

钉钉群收到的报警

push-msg2.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK