20.20 告警系统主脚本
把shell脚本放在 /usr/local/sbin 目录下,方便查找
[root@localhost ~]# cd /usr/local/sbin/ [root@localhost sbin]# mkdir mon/ [root@localhost sbin]# cd mon/ [root@localhost mon]# mkdir bin conf shares mail log [root@localhost mon]# ls bin conf log mail shares [root@localhost mon]# cd bin/ [root@localhost bin]# vim main.sh
主脚本main.sh参数地址:http://note.youdao.com/noteshare?id=ef94586704b208ee6d7c7d1e5f04f644&sub=08E66A3760DF4961B29C74DED78E09F1
export send 是否发送邮件的开关,1为发送,如果是维护状态下,就要关闭告警
addr 本机ip(根据实际网卡名称更改ens33或者其他名称)
last_dir /bin/目录
conf_file ../conf/mon.conf 相对于/bin/的位置
grep -q 只要匹配就返回,用于 if 逻辑判断
mon.conf
## to config the options if to monitor
## 定义mysql的服务器地址、端口以及user、password
to_mon_cdb=0 ##0 or 1, default 0,0 not monitor, 1 monitor
db_ip=10.20.3.13
db_port=3315
db_user=username
db_pass=passwd
## httpd 如果是1则监控,为0不监控
to_mon_httpd=0
## php 如果是1则监控,为0不监控
to_mon_php_socket=0
## http_code_502 需要定义访问日志的路径
to_mon_502=1
logfile=/data/log/xxx.xxx.com/access.log
## request_count 定义日志路径以及域名
to_mon_request_count=0
req_log=/data/log/www.discuz.net/access.log
domainname=www.discuz.net
·load.sh内容
#! /bin/bash
##Writen by alex##
load=`uptime |awk -F 'average:' '{print $2}'|cut -d',' -f1|sed 's/ //g' |cut -d. -f1`
if [ $load -gt 10 ] && [ $send -eq "1" ]
then
echo "$addr `date +%T` load is $load" >../log/load.tmp
/bin/bash ../mail/mail.sh [email protected] "$addr\_load:$load" `cat ../log/load.tmp`
fi
echo "`date +%T` load is $load"
·502.sh内容
#! /bin/bash
d=`date -d "-1 min" +%H:%M`c_502=`grep :$d: $log |grep ' 502 '|wc -l`
if [ $c_502 -gt 10 ] && [ $send == 1 ]; then
echo "$addr $d 502 count is $c_502">../log/502.tmp ##502数量计入日志
/bin/bash ../mail/mail.sh $addr\_502 $c_502 ../log/502.tmp ##发送邮件
fi
echo "`date +%T` 502 $c_502"
disk.sh内容
#! /bin/bash
##Writen by alex##
rm -f ../log/disk.tmp
LANG=en
for r in `df -h |awk -F '[ %]+' '{print $5}'|grep -v Use` ##以一个或多个空格或 % 作为分隔符
do
if [ $r -gt 90 ] && [ $send -eq "1" ]
then
echo "$addr `date +%T` disk useage is $r" >>../log/disk.tmp
fi
if [ -f ../log/disk.tmp ]
then
df -h >> ../log/disk.tmp
/bin/bash ../mail/mail.sh $addr\_disk $r ../log/disk.tmp
echo "`date +%T` disk useage is nook"
else
echo "`date +%T` disk useage is ok"
fi
[ %]+ 表示一个或多个空格或%
[root@localhost shares]# echo "12:aaa#sadfsad:111#3333" |awk -F '[:#]' '{print $3}'
sadfsad ##以:或#为分隔符,所以第三段为sadfsad
[root@localhost shares]# echo "12:aaa#sadfsad:111#3333" |awk -F '[:#]' '{print NF}'
5 ##以:或#为分隔符,有5段
[root@localhost shares]# echo "12:aaa#sadfsad:111##3333" |awk -F '[:#]' '{print NF}'
6 ##以:或#为分隔符,有6段,因为有个##
[root@localhost shares]# echo "12:aaa#sadfsad:111##3333" |awk -F '[:#]+' '{print NF}'
5 ##以一个或多个:或#为分隔符,就只有5段