ITPub博客

首页 > IT基础架构 > 网络安全 > tomcat watch dog program

tomcat watch dog program

原创 网络安全 作者:wangzh3 时间:2006-03-25 10:06:38 0 删除 编辑

#!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
);
}

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/47869/viewspace-822840/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 今天去扯皮操蛋
下一篇: 宽容
请登录后发表评论 登录
全部评论
  • 博文量
    301
  • 访问量
    10875979