监视特定string的Apache错误日志

什么是推荐的解决scheme来梳理Apache的前一天的/ var / log / httpd / error_log查找包含特定string和电子邮件时,检测到这样的string的行?

它可以通过cron每小时运行,我的情况下的string将是“sigkill”或“达到maxclients”。

Nagios,仙人掌等将是一个矫枉过正。 我需要一些简单的东西

谢谢

但是,如果你真的喜欢Perl,它比bash更轻一点,但是…

#!/usr/bin/perl -w use strict; my $cachefile="/var/cache/lastpos-apache2-scan4maxclntOrSigKill"; my $logfile="/var/log/apache2/error.log"; my $searchstr="sigkill|reached maxclients"; my $lastpos=0; if (-f $cachefile) { open FH,"<".$cachefile; $lastpos=<FH>; close FH; }; my $newpos=(stat $logfile)[7]; open FH,"<".$logfile; seek FH,$lastpos,0; while (<FH>) { print if /$searchstr/i; }; close FH; open FH,">".$cachefile; print FH $newpos; close FH; 

对于通过cron运行期刊,你可以使用一个caching来存储日志文件中的最后一个位置,而不是grep最新的行:

 #!/bin/bash logfile=/var/log/apache2/error.log searchstr='sigkill\|reached maxclients' cachefile='/var/cache/lastpos-apache2-scan4maxclntOrSigKill' [ -f $cachefile ] && lastpos=$(<$cachefile) [ "$lastpos" ] || lastpos=0 newpos=$(stat -c %s $logfile) [ $lastpos -gt $newpos ] && lastpos=0 tail -c +$lastpos $logfile | grep "$searchstr" echo $newpos >$cachefile 

总是有Perl ,例如:

 perl -ne 'print if m/sigkill|reached maxclients/i' /var/log/apache2/error_log 

Monit是一个非常轻量级的系统监控工具,当某些string出现时,它将监视日志文件并发送警报。 有关使用情况,请参阅http://mmonit.com/monit/documentation/monit.html#file_content_testing上的文档&#x3002;