因为项目需要,要对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;
}
阅读(2567) | 评论(0) | 转发(1) |