2019独角兽企业重金招聘Python工程师标准>>>
写一个shell脚本,每隔30秒查看一次日记文件/data/logs/error.log,看有没有新的日记,如果有,需要发邮件告警。
判断时间点,如果是 0 点 0 分需考虑特殊情况,记录上一次日记的行数,对比本次日记行数和上一次日记行数;
假设:1) 该日记0点0分会清空 2) 邮件脚本路径/usr/local/sbin/mail.py
思路:用 while 循环, sleep 30,
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26[root@localhost_002 shell]# cat 12.sh #!/bin/bash while : do t=`date +%H%M` if [ $t == "00" ] #判断是否是00: then rm -fr /tmp/log_line.txt sleep 60 fi if [ ! -f /tmp/log_line.txt ] then touch /tmp/log_line.txt wc -l /data/log/error.log|awk '{print $1}' >> /tmp/log_line.txt fi last_n=`tail -1 /tmp/log_line.txt` #新增日记的行; n=`wc -l /data/logs/error.log|awk '{print $1}'` #总日记的行; echo $n >> /tmp/log_line.txt if [ $n -gt $loat_n ] then python /usr/local/sbin/mail.py yuanhh@163.com "错误日记增加" “tail /data/log/errlor.log” fi sleep 30 done
第二个方法:自己整理:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#!/bin/bash mail=yuanhh@163.com logfile=/data/logs/error.log t1=`date +%H` t1=`date +%M` n=`cat $logfile |wc -l` while : do n1=`cat $logfile |wc -l` if [ -z $n ] || [ -z $n1 ] then echo "The value none" exit fi if [ $n1 -gt $n ] then /usr/local/sbin/mail.py $mail "error.log " "errorlog 增加" fi if [ $t1 == "00" -a $t2 == "00" ] then echo $n > /tmp/1.log echo $n1 >> /tmp/1.log fi sleep 30 done
转载于:https://my.oschina.net/yuanhaohao/blog/2997158
最后
以上就是粗犷月亮最近收集整理的关于shell_08:error_log日记增加则告警的全部内容,更多相关shell_08内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复