免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: gaochong
打印 上一主题 下一主题

根据日志文件监控进程(已初步解决,附named-nanny.pl供各位参考) [复制链接]

论坛徽章:
0
21 [报告]
发表于 2009-05-30 16:08 |只看该作者

回复 #20 Perl_Er 的帖子

谢谢!
这个帖子我会仔细看看!


但是针对我目前的代码,怎么改进呢?你的帖子有不少看不懂,说实话。

论坛徽章:
0
22 [报告]
发表于 2009-05-30 20:29 |只看该作者

回复 #21 gaochong 的帖子

我的正式服务器上的service进程一般情况下是正常的。也就是说,for循环中if ($mtime1 eq $mtime2)这个if语句99%是假的,会到next继续这个if。

但为什么不到2小时Perl进程就丢失了呢?真是想不明白,请各位指教呀。

论坛徽章:
0
23 [报告]
发表于 2009-05-30 23:02 |只看该作者

回复 #22 gaochong 的帖子

还是IRC好一点儿,可惜英文不够好。

论坛徽章:
0
24 [报告]
发表于 2009-05-30 23:07 |只看该作者

回复 #23 gaochong 的帖子

修改了部分代码,其中grep 和 wc 部分还需要再改进。
修改后的代码如下:


#!/usr/bin/perl -w


use strict;
use POSIX qw(strftime);

$ENV{'LD_LIBRARY_PATH'}="../commonlib:/ldap/ds5/lib:/usr/local/mysql/lib/mysql:

my $file="
/ldap/logs/log.dat";
my $r_log="
/export/home/g/log";

for (;;) {
        my $mtime1=localtime ((stat($file))[9]);
        print "
$mtime1\n";
        sleep 5;
        my $mtime2=localtime ((stat($file))[9]);
        print "
$mtime2\n";
        if ($mtime1 eq $mtime2) {
                chomp (my $proc=`ps -ef | grep -i "
ua_aaaService -r -l -b -f ../conf/config.xml" | grep -v grep | wc -l`);
                if ($proc==1) {
                foreach (`ps -ef | grep -i "
ua_aaaService -r -l -b -f ../conf/config.xml" | grep -v grep`) {
                        my($pid)=/\s+\S+\s+(\S+)\s+.*/;
                        kill 9,$pid;
                        my $date=strftime "
%Y%m%d%H%M%S",localtime;
                        open FILE,"
>>",$r_log
                                or die "
$!";
                        printf FILE "
$date restart-radius(pid alive)\n";
                        close FILE;
                        chdir "
/export/home/boss/ua/bin/" or die "$1";
                        system ("
./ua_aaaService -r -l -b -f ../conf/config.xml");
                        }
                } elsif ($proc==0) {
                        print "
no pid\n";
                        my $date2=strftime "
%Y%m%d%H%M%S",localtime;
                        open FILE,"
>>",$r_log
                                or die "
$!";
                        printf FILE "
$date2 restart-radius(no pid)\n";
                        close FILE;
                        chdir "
/export/home/boss/ua/bin/" or die "$1";
                        system ("
./ua_aaaService -r -l -b -f ../conf/config.xml


[ 本帖最后由 gaochong 于 2009-5-30 23:10 编辑 ]

论坛徽章:
0
25 [报告]
发表于 2009-05-31 10:10 |只看该作者
ps -ef | grep -i "ua_aaaService -r -l -b -f ../conf/config.xml" | grep -v grep | wc -l
eq
ps -ef | grep -ic "ua_aaaService -r -l -b -f ../conf/config.xml"

论坛徽章:
0
26 [报告]
发表于 2009-05-31 10:20 |只看该作者
又修改过了,主要就是把shell相关的命令替换为perl自带的。 是irc上的人建议的。
已经在服务器上跑,看看perl进程能运行多久。

代码如下:

#!/usr/bin/perl -w


use strict;
use POSIX qw(strftime);


$ENV{'PATH'}=":/sbin:/bin:/usr/sbin:/usr/bin:

my $file="
logs/log.dat";
my $r_log="
/export/home/g/log";
my $radius='./ua_aaaService -l -b -r -f ../conf/config.xml';

for (;;) {
        my $mtime1=localtime ((stat($file))[9]);
#        print "
$mtime1\n";
        sleep 30;
        my $mtime2=localtime ((stat($file))[9]);
#        print "
$mtime2\n";
        if ($mtime1 eq $mtime2) {
                my @proc=`ps -ef`;
                my @r_proc=grep /$radius/, @proc;
                print "
@r_proc";
                if (scalar @r_proc==1) {
                        foreach (@r_proc) {
                                my $pid=$1 if (/\s+\S+\s+(\S+)\s+.*/);
                                print "
$pid\n";
                                kill -9,$pid;
                                my $date=strftime "
%Y%m%d%H%M%S",localtime;
                                open FILE,"
>>",$r_log
                                        or die "
$!";
                                printf FILE "
$date restart-radius(pid alive)\n";
                                close FILE;
                                chdir "
/export/home/boss/ua/bin/" or die "$1";
                                system ("
./ua_aaaService -l -b -r -f ../conf/config.xml");
                        }
                } elsif (scalar @r_proc==0) {
#                                print "
no pid\n";
                                my $date2=strftime "
%Y%m%d%H%M%S",localtime;
                                open FILE,"
>>",$r_log
                                        or die "
$!";
                                printf FILE "
$date2 restart-radius(no pid)\n";
                                close FILE;
                                chdir "
/export/home/boss/ua/bin/" or die "$1";
                                system ("
./ua_aaaService -l -b -r -f ../conf/config.xml);
                } else {}
        }
next;
}


[ 本帖最后由 gaochong 于 2009-5-31 13:03 编辑 ]

论坛徽章:
0
27 [报告]
发表于 2009-05-31 13:04 |只看该作者
以上代码还是不行。
Perl进程运行了2小时左右还是丢失了,请各位指教呀。

论坛徽章:
0
28 [报告]
发表于 2009-05-31 15:16 |只看该作者
原帖由 gaochong 于 2009-5-31 13:04 发表
以上代码还是不行。
Perl进程运行了2小时左右还是丢失了,请各位指教呀。



nohup ./yourscript &

这样还丢?

论坛徽章:
0
29 [报告]
发表于 2009-05-31 15:19 |只看该作者

倒啊,不能把问题简单化么

#!/usr/bin/perl -w

#Description: The Log File Monitor.


use strict;
use warnings;

my $old_md5hash;
my $new_md5hash;

sub getLogFileMd5(){
    my $LogFile = "/path/log/log.txt";
    my $md5hash;
    my $filename;
    ($md5hash,$filename) = split(/ /,`md5sum $LogFile`);
    return $md5hash;

}
while(1){
    $old_md5hash = &getLogFileMd5();

    sleep 10;

    $new_md5hash = &getLogFileMd5();
        
    if ($old_md5hash !~ /$new_md5hash/ ){
        system("killall -9 Service");
        system("/path/Service");
    }   
}


[ 本帖最后由 upkiller 于 2009-5-31 15:22 编辑 ]

论坛徽章:
0
30 [报告]
发表于 2009-05-31 16:55 |只看该作者

回复 #29 upkiller 的帖子

谢谢!
但和我的代码没多大区别,你是检查md5sum,我是检查时间,我想运行效果没区别吧?


我就多了一个找出PID并kill的过程。


我就想不明白为什么不到2小时Perl进程就丢失了呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP