十七周五次课(4月24日)
20.23/20.24/20.25 告警系统邮件引擎
20.26 运行告警系统
20.23/20.24/20.25 告警系统邮件引擎
编辑mail.sh内容 //其中mail.py内容到这里下载https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D22Z/mail.py
vim mail.sh
log=$1
t_s=`date +%s`
t_s2=`date -d "2 hours ago" +%s`
if [ ! -f /tmp/$log ]
then
echo $t_s2 > /tmp/$log
fi
t_s2=`tail -1 /tmp/$log|awk '{print $1}'`
echo $t_s>>/tmp/$log
v=$[$t_s-$t_s2]
echo $v
if [ $v -gt 3600 ]
then
./mail.py $1 $2 $3
echo "0" > /tmp/$log.txt
else
if [ ! -f /tmp/$log.txt ]
then
echo "0" > /tmp/$log.txt
fi
nu=`cat /tmp/$log.txt`
nu2=$[$nu+1]
echo $nu2>/tmp/$log.txt
if [ $nu2 -gt 10 ]
then
./mail.py $1 "trouble continue 10 min $2" "$3"
echo "0" > /tmp/$log.txt
fi
fi
使用方法:建立1个计划任务
crontab -e //写入如下内容
* * * * * cd /usr/local/sbin/mon/bin; bash main.sh
20.26 运行告警系统
sh –x main.sh //exec表示写到日志里了
[root@aming-02 /usr/local/sbin/mon/bin]# sh -x main.sh
+ export send=1
+ send=1
++ awk '/inet/ {print $2}'
++ /sbin/ifconfig
++ grep -A1 'ens33: '
+ export addr=192.168.37.101
+ addr=192.168.37.101
++ pwd
+ dir=/usr/local/sbin/mon/bin
++ echo /usr/local/sbin/mon/bin
++ awk -F/ '{print $NF}'
+ last_dir=bin
+ '[' bin == bin ']'
+ conf_file=../conf/mon.conf
+ exec
查看日志文件:cat ../log/err.log
[root@aming-02 /usr/local/sbin/mon/bin]# cat ../log/err.log
++ date '+%F %T'
+ echo '2018-04-24 13:04:07 load average'
+ /bin/bash ../shares/load.sh
+ grep -q to_mon_502=1 ../conf/mon.conf
++ grep logfile= ../conf/mon.conf
++ awk -F = '{print $2}'
++ sed 's/ //g'
+ export log=/data/log/xxx.xxx.com/access.log
+ log=/data/log/xxx.xxx.com/access.log
+ $'/bin/bash\302\240\302\240../shares/502.sh'
main.sh:行23: /bin/bash ../shares/502.sh: 没有那个文件或目录
因为我们的502脚本根本不存在,所有要想执行脚本需要
修改mon配置文件:vim ../conf/mon.conf
to_mon_502=0 //502不需要监控
修改脚本文件:vim main.sh
#exec 1>>../log/mon.log 2>>../log/err.log //注释掉这行,不写入日志文件
再次测试:sh -x main.sh
[root@aming-02 /usr/local/sbin/mon/bin]# sh -x main.sh
+ export send=1
+ send=1
++ /sbin/ifconfig
++ grep -A1 'ens33: '
++ awk '/inet/ {print $2}'
+ export addr=192.168.37.101
+ addr=192.168.37.101
++ pwd
+ dir=/usr/local/sbin/mon/bin
++ awk -F/ '{print $NF}'
++ echo /usr/local/sbin/mon/bin
+ last_dir=bin
+ '[' bin == bin ']'
+ conf_file=../conf/mon.conf
++ date '+%F %T'
+ echo '2018-04-24 13:38:38 load average'
2018-04-24 13:38:38 load average
+ /bin/bash ../shares/load.sh
13:38:38 load is 0
+ grep -q to_mon_502=1 ../conf/mon.conf