#!/usr/bin/perl -w
#此脚本会读取config_file文件,此文件中每行一条记录,每个记录由三项组成,(业务名 告警日志全路径 告警时间阀值) #脚本会读取每一条记录,然后检查日志文件最后一次更改到检查为止的时间是否大于告警时间值 #如果大于告警时间值,说明最近日志没有写入告警,业务系统正常。 #如果小于告警时间值,说明最近写入告警日志,业务系统不正常。
use DBI; use strict; use warnings; my ($dbname, $user, $passwd); $dbname="MONITOR_10.1.7.14"; $user="jk_ebip"; $passwd="jk_ebip";
my $dbh; my $config_file="D:/config.txt";
my $true=1; my $false=0;
my $insert_t; my $check_t; my $last_modify_t;
while(1){
open CONFIG, $config_file or die "Can't read config file ($!)"; while (<CONFIG>) { chomp; my @args=split; my $result=if_new_log($args[1],$args[2]); $dbh = DBI->connect("dbi:Oracle:$dbname",$user,$passwd,{RaiseError => 1, AutoCommit => 0})||die "Catabase Connect not made : $DBI::errstr";
my $deletestring="delete from T_JK_TYDL_STATUS where yyname='$args[0]'"; $dbh->do( $deletestring );
$insert_t=get_time(); my $insertstring="insert into T_JK_TYDL_STATUS(inserttime,checktime,lastmtime,yyname,status) values ('$insert_t','$check_t','$last_modify_t','$args[0]','$result')";
$dbh->do( $insertstring );
$dbh->disconnect; }
sleep (10); }
sub if_new_log { if (-e $_[0]){ my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)=stat($_[0]); my $check_time=time; $check_t=get_time();
my $last_mtime=$mtime; $last_modify_t=get_time($mtime); my $subtime=$check_time - $last_mtime; if ($subtime < $_[1]) { $true; } else { $false; } } }
sub get_time { my $tm=""; if (@_ == 1){ $tm=$_[0]; } else { $tm=time; } my ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = localtime($tm); $year=$year + 1900; $mon=$mon + 1; if ($mon < 10){ $mon="0"."${mon}"; } if ($day < 10){ $day="0"."$day"; } if ($hour < 10){ $hour="0"."$hour"; } if ($min < 10){ $min="0"."$min"; } if ($sec < 10){ $sec="0"."$sec"; }
my $cur_time="$year"."$mon"."$day"."$hour"."$min"."$sec"; return $cur_time; }
|