Perl 监控 tomcat
公司的struts 2 项目,偶尔会出现out of memory异常,导致系统崩溃,所以写了个脚本监控一下。间隔10秒Post一次数据,连续5次不是返回 200 OK,就重启tomcat。
use AnyEvent;
use AnyEvent::HTTP;
use POSIX qw(strftime);
use strict;
use warnings;
my $count = 0; #计数
my $startbat = 'startup.bat'; #启动tomcat的bat
my $stopbat = 'shutdown.bat'; #关闭tomcat的bat
my $link = "http://localhost:8080/GpsOverSpeedSearch/"; #POST的URL
sub checkIsAlive {
http_request POST => $link,
timeout => 30, sub {
my ($body, $hdr) = @_;
if ($hdr->{Status} =~ /^2/) {
$count = 0;
print(strftime("%Y-%m-%d %H:%M:%S ", localtime));
print "tomcat is alive!\n";
} else {
print(strftime("%Y-%m-%d %H:%M:%S ", localtime));
print "lost link for the server!\n";
if($count++ > 3) {
print(strftime("%Y-%m-%d %H:%M:%S ", localtime));
print "it will be restart tomcat!\n";
system($stopbat);
system($startbat);
$count = 0;
}
}
};
}
my $cv = AnyEvent->condvar;
my $time_watcher = AnyEvent->timer (
after => 10,
interval => 10,
cb => \&checkIsAlive
);
$cv->recv;
把代码丢服务器上,可以安心回家过年咯。很少写Perl了,要是脚本异常崩溃别喷我呀。