现在的位置: 首页 > 综合 > 正文

perl 监控apache日志

2012年03月07日 ⁄ 综合 ⁄ 共 625字 ⁄ 字号 评论关闭

日志滚动,获取 tail -f 里的信息 如果 存在“Too many open files”那么重启apache

#!/usr/bin/perl
my $now=localtime(time);
&write_log("$now start reahache.pl\n" . '----------------------------------' . "\n");

open Tail, '/usr/bin/tail -f /etc/httpd/logs/error_log |' or die "Pipe failed: $!\n";
while (<Tail>){
         my $in = <Tail>;

                  if($in=~/Too many open files/i){
                        my $get_str_cmd=qx(service httpd restart);
                        my $log_str = "$in\n" . "$get_str_cmd\n";
                        &write_log($log_str);
                        sleep 10;
              }

}
close Tail;

sub write_log {
   open(TEMP_LOG,">>/lcx/restartLog.log") || die"err: $! \n";
                my $write_str=$_[0];
                syswrite(TEMP_LOG,"$write_str\n\n\n");
   close(TEMP_LOG);
}

设置开机运行
[root@2082 etc]# vi rc.local
su root -c "/usr/bin/perl /lcx/reapache.pl &"

抱歉!评论已关闭.