前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >针对业务日志的监控报警设置

针对业务日志的监控报警设置

作者头像
洗尽了浮华
发布2018-01-23 15:29:06
1.7K0
发布2018-01-23 15:29:06
举报
文章被收录于专栏:散尽浮华散尽浮华

需求说明对线上业务日志进行监控,当日志中出现的ERROR条数超过30条时立即报警! 监控脚本部署到qd-inf-logcollector01服务器上了(需要提前做好qd-inf-logcollector01到业务部署机器的ssh无密码信任关系)

1)qd-inf-logcollector01 机器上的操作记录:

代码语言:javascript
复制
[root@qd-inf-logcollector01 ~]$ cd /app/script/
[root@qd-inf-logcollector01 script]$ ll
total 372
-rwxr-xr-x 1 root root    446 Oct 13 13:02 celery-antiwater_ERROR_monit.sh
-rwxr-xr-x 1 root root    442 Oct 13 13:04 celery-dislike_ERROR_monit.sh
-rwxr-xr-x 1 root root    443 Oct 13 13:58 celery-flower_ERROR_monit.sh
-rwxr-xr-x 1 root root    436 Oct 13 14:01 celery-report_ERROR_monit.sh
-rwxr-xr-x 1 root root    436 Oct 13 14:03 consumer01_ERROR_monit.sh
-rwxr-xr-x 1 root root    436 Oct 13 14:04 consumer02_ERROR_monit.sh
-rwxr-xr-x 1 root root    437 Oct 13 14:06 imgsearch01_ERROR_monit.sh
-rwxr-xr-x 1 root root    437 Oct 13 14:07 imgsearch02_ERROR_monit.sh
-rwxr-xr-x 1 work work    857 Oct 12 14:31 sms.py                             //这个是短信报警脚本(内部短信脚本,这里就不贴出来了)
-rwxr-xr-x 1 root root    435 Oct 13 14:08 snapshot01_ERROR_monit.sh
-rwxr-xr-x 1 root root    435 Oct 13 14:10 snapshot02_ERROR_monit.sh
-rwxr-xr-x 1 root root    433 Oct 13 14:11 xcspam01_ERROR_monit.sh
-rwxr-xr-x 1 root root    433 Oct 13 14:12 xcspam02_ERROR_monit.sh

下面贴出其中两个日志的监控脚本,其他的脚本内容类似:
[root@qd-inf-logcollector01 script]$ cat celery-antiwater_ERROR_monit.sh 
#!/bin/bash 
while true
do
  TIME1=$(date +%Y-%m-%d)
  TIME2=$(date +%H:%M -d'-1 minute')
  NUM=$(ssh root@10.111.232.148 'grep -E '${TIME1}' /app/script/antiwater_ERROR.log|grep '${TIME2}' |wc -l')

  if [[ "${NUM}" -ge 30 ]]
  then
    python /app/script/sms.py antiwater_ERROR.log "qd-vpc-op-rule01 server 在${TIME1} ${TIME2}产生${NUM}个antiwater ERROR!请收到报警后尽快查看并处理!"
    sleep 600
  else
    sleep 30
  fi
done

[root@qd-inf-logcollector01 script]$ cat consumer01_ERROR_monit.sh 
#!/bin/bash 

while true
do
  TIME1=$(date +%Y-%m-%d)
  TIME2=$(date +%H:%M -d'-1 minute')
  NUM=$(ssh root@10.111.232.225 'grep -E '${TIME1}' /app/script/ERROR.log |grep '${TIME2}'|wc -l')

  if [[ "${NUM}" -ge 30 ]]
  then
    python /app/script/sms.py xcspam_error_log "qd-vpc-op-consumer01 server 在${TIME1} ${TIME2}产生${NUM}个xcspam ERROR!请收到报警后尽快查看并处理!"
    sleep 600
  else
    sleep 30 
  fi
done

特别注意:
需要在后台执行这些脚本,因为当ERROR日志信息注入量比较大(比如上万条)的时候,就不适合使用crontab定时执行了。
[如果使用crontab定时执行的话,那么定时时间间隔短的话,比如一分钟执行一次,那么当ERROR比较多的时候,报警信息就会很多!如果定时时间间隔长的话,那么在ERROR信息出现的时候,可能无法第一时间报警,这样就失去了报警的意义了。]
所以还是将脚本放在后台执行比较合理,这样当ERROR条数超过30条时,会立即第一时间发出第一个报警,然后根据脚本中的sleep进行报警频率调整!这样也是为了确保监控报警的时效性!

[root@qd-inf-logcollector01 script]$ nohup /bin/bash -x /app/script/celery-antiwater_ERROR_monit.sh &            //回车,按ctrl+c,放在后台执行
[root@qd-inf-logcollector01 script]$ nohup /bin/bash -x /app/script/consumer01_ERROR_monit.sh & 
[root@qd-inf-logcollector01 script]$ ps -ef|grep monit.sh
root     20574     1  0 14:03 ?        00:00:00 /bin/bash -x /app/script/consumer01_ERROR_monit.sh
root     32568     1  0 13:03 ?        00:00:00 /bin/bash -x /app/script/celery-antiwater_ERROR_monit.sh

2)业务部署机器(也就是监控的目标机器上)的脚本配置,这里选择consumer01机器看下脚本配置,其他机器脚本内容类似!

代码语言:javascript
复制
[root@qd-vpc-op-consumer01 ~]# cd /app/script/
[root@qd-vpc-op-consumer01 script]# ll
total 28
-rwxr-xr-x 1 work work  35 Oct  9 17:02 empty_ERROR_log.sh
-rw-r--r-- 1 work work   0 Oct 13 03:00 ERROR.log
-rwxr-xr-x 1 work work 186 Oct  9 16:34 monit_log.sh

[root@qd-vpc-op-consumer01 script]# cat monit_log.sh
#!/bin/bash
FILE="/data/log/xcspam/xcspam_error.log"
nohup /usr/bin/tail  -f $FILE |grep ERROR >> /app/script/ERROR.log &

首次执行下这个脚本,然后将tail命令的动作放在后台一直执行
这样,一旦xcspam_error.log文件中有ERROR信息输入,就会被打入到ERROR.log中
这样确保了监控的实时性!
[root@qd-vpc-op-consumer01 script]# /bin/bash -x /app/script/monit_log.sh

[root@qd-vpc-op-consumer01 script]# ps -ef|grep tail
root     11337     1  0 Oct09 ?        00:09:01 /usr/bin/tail -f /data/log/xcspam/xcspam_error.log

随着监控的执行,ERROR.log文件也会不断增大,故需要一个清理脚本:
[root@qd-vpc-op-consumer01 script]# cat empty_ERROR_log.sh 
#!/bin/bash
>/app/script/ERROR.log

[root@qd-vpc-op-consumer01 script]# crontab -l
0 3 */2 * * /bin/bash -x /app/script/empty_ERROR_log.sh > /dev/null 2>&1
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-10-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档