2006 年 03 月 25 日, 星期六

tomcat watch dog program

#!C:/Perl/bin/perl.exe -w
#name:tomcat_watchdog.pl
#function:monitor the tomcat status,if the web service is suspending or stopping,then restart the tomcat
#author: wangzhihong.china@gmail.com wang.zhihong1@zte.com.cn
######################################################
# Copyright wangzhihong 2006 #
######################################################

package tomcat_watchdog;

use strict;
use warnings;
use FileHandle;
use LWP::UserAgent; #LWP: library for www in perl

my $logfilename = "c:/watchdog.log";
my $HLOG = new FileHandle;
$HLOG->open(">>$logfilename")
or die("打开日志文件出错:文件名为 $logfilename ;错误原因为: $!");

my $browser = LWP::UserAgent->new;
$browser->timeout(10); #timeout time is 10 seconds,defualt 180 seconds
my $url = 'http://localhost:8080/index/index.jsp';
my $response = $browser->get($url);

if ( $response->is_success ) {
LOG("web is up!");
}
else {

#再次探测
$response = $browser->get($url);
if ( !$response->is_success ) {

#重启tomcat
LOG("web is down!,restart it");
system("net stop tomcat");
system("net start tomcat");
LOG("web is up now!");
}
}

$HLOG->close();

# the subrution to record the log information
sub LOG {
my ($text) = @_;
my $time = timeString();

# log to stdout.
print "[$time] $textn";

# log to logfile.
print $HLOG "[$time] $textn";
}

#获取当前年月日YYYY-MM-DD HH:MI:ss格式,just for log
sub timeString {
my $year = ( localtime() )[5];
my $mon = ( localtime() )[4];
my $mday = ( localtime() )[3];
my $hour = ( localtime() )[2];
my $min = ( localtime() )[1];
my $sec = ( localtime() )[0];
return sprintf(
"%04d-%02d-%02d %02d:%02d:%02d",
$year + 1900,
$mon + 1, $mday, $hour, $min, $sec
);
}


wangzh3 发表于:2006.03.25 10:06 ::分类: ( 大杂烩 ) ::阅读:(1223次) :: 评论 (2) :: 引用 (0)

haha [回复]

还是南研的

andyloo 评论于:2006.03.29 22:07

哈! [回复]

原来是友商

andyloo 评论于:2006.03.29 21:48

发表评论

标题

在此添加评论

称呼

邮箱地址(可选)

个人主页(可选)