Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2116397
  • 博文数量: 227
  • 博客积分: 10521
  • 博客等级: 上将
  • 技术积分: 3452
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-20 14:59
个人简介

低调做人,高调做事!

文章分类

全部博文(227)

文章存档

2013年(4)

2012年(8)

2011年(16)

2010年(24)

2009年(92)

2008年(83)

分类: LINUX

2009-08-01 00:24:18

因为项目需要,要对windows下面的多个进程进行监控,当进程死掉的时候,自动启动该进程,记录下log,并通知系统管理员。

其实该脚本主要是使用了cpan的 Win32::Process::List 模块.

脚本如下:


#!C:/perl/bin/perl.exe

use strict;
use warnings;
use Net::SMTP_auth;
use POSIX "strftime";
use Win32::Process::List;


my $mailhost = 'smtp.163.com';
my $mailfrom = 'xxxx@163.com';
my @mailto   = 'xxxx@163.com';
my $subject  = '';

my $user   = 'xxxx';
my $passwd = 'xxxx';


my $err_log = 'C:/monitor_process/process_err_log';


use constant PROCESSLIST => ('notepad.exe',
                             'firefox.exe');


foreach my $process (PROCESSLIST) {
    if ( !&check_process_alive( $process ) ) {

         print  "$process is fail!" . "\n";

         open FH, ">> $err_log" or die $!;

         print FH "-" x 80 . ">", "\n";
         my $check_time = ¤t_time();
         print FH  "check at $check_time\n";
         print FH  "$process is fail!" . "\n";
        
         sleep 1;
         system('start ' .$process);

         $subject = "$process is fail";
         &SendMail();

    }
}






##############################
# current time
##############################
sub current_time() {

    my $time_now = POSIX::strftime("[%Y-%m-%d %H:%M:%S]", localtime);
    return $time_now;
}




###############################
# Server process is alive check
###############################
sub check_process_alive {
    my ( $process ) = @_;
    my $P = Win32::Process::List->new();

    if ( !$P->GetProcessPid("$process") ) {
        return 0;
    }
    return 1;
}



##############################
# Send notice mail
##############################
sub SendMail() {

    my $smtp = Net::SMTP_auth->new( $mailhost, Timeout => 120, Debug => 1 )
      or die "Error.\n";
    $smtp->auth( 'LOGIN', $user, $passwd );

    foreach my $mailto (@mailto) {
        $smtp->mail($mailfrom);
        $smtp->to($mailto);
        $smtp->data();
        $smtp->datasend("To: $mailto\n");
        $smtp->datasend("From:$mailfrom\n");
        $smtp->datasend("Subject: $subject\n");
        $smtp->datasend("\n");
        $smtp->dataend();
    }

    $smtp->quit;
}
阅读(2528) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~