perl 监控apache日志

日志滚动,获取 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 &"

posted @ 2010-01-05 13:55  大力  阅读(660)  评论(0编辑  收藏  举报