免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
11 [报告]
发表于 2009-05-30 13:46 |显示全部楼层
原帖由 DQP 于 2009-5-30 13:22 发表
看看 log 没次都死在同一个位置么?
或者每次死的时间一样么



我说的死掉是Perl监控进程死掉,而且log记录的是我要监控的进程重启的记录。

所以楼上说的好像。。。。(没什么用)。要不你再仔细看看代码?

论坛徽章:
0
12 [报告]
发表于 2009-05-30 14:42 |显示全部楼层
原帖由 DQP 于 2009-5-30 14:34 发表
嗯 说的就是这个监控进程
看看死在哪了



请教,怎么看perl监控进程死在哪里呢???急!谢谢!

论坛徽章:
0
13 [报告]
发表于 2009-05-30 14:55 |显示全部楼层
刚才服务器进程又丢失,但perl监控进程也没有起到作用,也不知道什么时候perl进程丢失了。



从程序代码上,哪些地方还可以再改进呢?
谢谢了!

论坛徽章:
0
14 [报告]
发表于 2009-05-30 16:08 |显示全部楼层

回复 #20 Perl_Er 的帖子

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


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

论坛徽章:
0
15 [报告]
发表于 2009-05-30 20:29 |显示全部楼层

回复 #21 gaochong 的帖子

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

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

论坛徽章:
0
16 [报告]
发表于 2009-05-30 23:02 |显示全部楼层

回复 #22 gaochong 的帖子

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

论坛徽章:
0
17 [报告]
发表于 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
18 [报告]
发表于 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
19 [报告]
发表于 2009-05-31 13:04 |显示全部楼层
以上代码还是不行。
Perl进程运行了2小时左右还是丢失了,请各位指教呀。

论坛徽章:
0
20 [报告]
发表于 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