使用shell脚本编写程序定时查询数据库,根据数据库结果发送短信和邮件。定时任务使用crontab实现,具体实现如下。
1. crontab 定时执行任务
crontab -e进行编辑,使用方法类似于vi,
例子:0 * * * * bash /home/work/opt/nginx/sbin/nginx_log.sh
2. 完整脚本
#!/bin/bash
if [ $# -lt 2 ]
then
echo "params error";
exit;
fi
#创建日志文件
LOG_FILE_NAME="/home/work/monitor/xxx"$1".log"
if [ ! -e "$LOG_FILE_NAME" ]
then
touch $LOG_FILE_NAME;
fi
LOG=$(date "+%Y-%m-%d %H:%M:%S");
#当前时间
current_time=$(date +%s);
echo $current_time
#查询开始时间
start_time=$(($current_time-$3))
echo $start_time;
tag=${1}
query_total_num_sql="select count(1) from unionsdkmonitor where tag like '%${tag}%' and timestamp>='${start_time}' and timestamp<='${current_time}'"
total_num=$(/usr/bin/mysql -h10.11.0.90 -P4051 -uuser -ppassword -N gamesdkmonitor -s -e "${query_total_num_sql}");
echo ${total_num};
error_result="0";
query_error_num_sql="select count(1) from unionsdkmonitor where tag like '%${tag}%' and timestamp>='${start_time}' and timestamp<='${current_time}' and result=${error_result}";
error_num=$(/usr/bin/mysql -h10.11.0.90 -P4051 -uuser -ppassword -N gamesdkmonitor -s -e "${query_error_num_sql}");
echo ${error_num}
#总条数小于5条不报警。
if [ 5 -gt ${total_num} ]
then
exit;
fi
error_per=$[error_num*100/total_num];
time=$[$3/60];
LOG=${LOG}" "$2" totalnum="${total_num}" error_num="${error_num}" error_per="${error_per}"%";
#记录日志
echo $LOG >> $LOG_FILE_NAME
#大于阀值,发送短信和邮件
echo $error_per
if [ $error_per -gt 50 ]
then
message="$2接口过去$time分钟访问$total_num次,失败$error_num次,错误率为百分之$error_per,时间"$(date "+%Y-%m-%d %H:%M:%S")".";
#xxx
phones=(xxxx xxxx)
#phones=(15011549635)
for var1 in ${phones[@]}
do
#发送短信
curl "http://ip/message.php?phone=$var1&message=$message"
done
#发送邮件
/home/liuqi/sendEmail -t xxx@xxx.com,xxx@xxx.com -f mail@xxx.com -s smtp.qiye.163.com:25 -xu xxx@xxx.com -xp Lv6bYuMW8a -m $message -u "标题"
echo $message;
fi
exit;
相关推荐
NULL 博文链接:https://woxiangbo.iteye.com/blog/2392852
shell创建db2数据库 ,shell创建db2数据库,shell创建db2数据库,shell创建db2数据库,shell创建db2数据库,shell创建db2数据库
shell定时器与shell发送邮件给到邮箱
shell连接oracle数据库工具脚本:支持select/insert/update/delete 部署位置:/root/sysmonitor db:数据库文件夹 dbconfig.properties:数据库配置文件, dbConnectTest.sh:连接测试文件 dbExecurteSQL.sh:...
Linux下连接金仓数据库(ksql)并完成查询、插入、修改表数据实例,修改后可用于oracle及mysql
可配置化shell脚本监控rabbitmq集群,如果rabbitmq运行异常,发送邮件通知管理员。亲自整理无任何坑。
使用mysql命令创建数据库对于很多人来说非常容易,结合shell脚本一起使用呢? 在shell脚本里面来创建想要使用的数据库,是不是感觉棒棒哒!
2.脚本中的邮件发送者名字根据自己的喜好进行更改,代码的第十行:user_from="<master@linux.com>" #邮件的发送者 实现方案请参考:https://blog.csdn.net/TAB_YLS/article/details/80597773
shell脚本和数据库测试.doc shell脚本和数据库测试.doc
Linux shell发邮件如何才能同时发送邮件给多个人???这里揭晓答案
01-25 ##note: ##fun_num fun函数后台运行次数 ##sql_num 每个函数sql运行次数 ##********************************##数据库变量设置dbhost=192.168.1.182dbbase=rechargedbuser=infosmsdbpass=info
利用LINUX SHELL Script实现数据库系统管理.pdf
shell短信接口开发_shell发送短信验证码通知_shell短信demo示例.docx
防止误删服务器数据库 ,使用Shell脚本备份数据库: ### 1、需求分析: 1)每天凌晨2:10备份数据库atguiguDB到/data/backup/db 2)备份开始和备份结束能够给出相应的提示信息 3)备份后的文件要求以备份时间为...
使用shell 脚本对数据库进行数据库数据备份,加入定时任务可进行定时备份。
主要介绍了Shell脚本实现自动发送邮件的例子,使用.muttrc文件配合shell脚本实现,需要的朋友可以参考下
简单shell脚本,供其他系统调用,也可以操作数据库,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
自己写的几个简单的shell脚本,可用来定时清理数据库的归档文件,并且对数据库定时进行备份操作。
数据库远程管理 shell