hotsanic |
Subversion Repositories: |
Compare with Previous - Blame - Download
package HotSaNICmod::OSdep;
use RRDs;
use lib "../../lib";
use HotSaNIClogparse;
use Time::Local;
@months=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
%MONTHS=(Jan=>0,Feb=>1,Mar=>2,Apr=>3,May=>4,Jun=>5,Jul=>6,Aug=>7,Sep=>8,Oct=>9,Nov=>10,Dec=>11);
my %ERRORTIME;
##### update specific database
sub updatedb {
my ($name,$sampletime,$value)=@_;
my $ERROR;
if (HotSaNIClog::check_debuglevel("MODULE_VERBOSE")) { HotSaNIClog::info(" $name:$value"); }
if ( ! -e "rrd/$name.rrd" ) { system("./makerrd","$name") }
if ((defined $name) && (defined $sampletime) && (defined $value)) {
$ERRORTIME{$name} = 0 if !defined $ERRORTIME{$name};
if ( $sampletime > $ERRORTIME{$name} ) {
RRDs::update "rrd/$name.rrd",$sampletime.":".$value;
if ($ERROR = RRDs::error) {
HotSaNIClog::error("unable to update `$name.rrd': $ERROR");
if (index($ERROR,"last update time") >=0) {
$ERROR =~ /last update time is (\d+)/,$ERRORTIME{$name}=$1;
HotSaNIClog::warn("Not trying to update `$name.rrd' again before $ERRORTIME{$name}");
}
}
}
}
}
sub version {
($VERSION = '$Revision: 1.12 $') =~ s/.*(\d+\.\d+).*/$1/;
return "default.pm $VERSION";
}
sub sample {
my %args=@_;
# check if scan-interval is exceeded
my $now=time;
my $mtime=0;
if (-e "lastcheck.dat") { $mtime=(stat("lastcheck.dat"))[9]; }
if ($mtime+$args{INTERVAL} <= $now) {
# update timestamp on lastcheck file
utime $now,$now,"lastcheck.dat";
my @items=();
foreach (@{$args{ITEM}}) { my ($item)=split /,/; push @items,$item; $COUNT{$item}=0; }
if (HotSaNIClog::check_debuglevel("MODULE_SAMPLING")) { HotSaNIClog::info("sampling: ".join(" ",@items)); }
my ($pos,@parse)=HotSaNIClogparse::findlogs($args{LOGPATH},$args{LOGNAME}.".*","lastcheck.dat");
my $seconds1=0;
foreach $file (@parse) {
if (HotSaNIClog::check_debuglevel("MODULE_SAMPLING")) { HotSaNIClog::info("parsing $file"); }
open FILE,"$file";
$firstline=<FILE>;
if (HotSaNIClog::check_debuglevel("MODULE_SAMPLING")) { HotSaNIClog::info("seeking to pos. $pos"); }
seek FILE,$pos,0;
$pos=0;
while (<FILE>) {
chomp;
my %result=HotSaNIClogparse::parseline_apache($_);
# number of seconds of beginning of the hour in $datestr
$seconds=timelocal(0,0,$result{TShour},$result{TSday},$result{TSmonth},$result{TSyear})-$result{TSzdiff}*36;
# check if another hour started and save sampling results for last hour
if ($seconds1 < $seconds) {
if (HotSaNIClog::check_debuglevel("MODULE_VERBOSE")) { HotSaNIClog::info("$year $mon $day $hour:$min - $seconds $hrcorrection"); }
# if counts for last hour exist, save them
if ($seconds1 > 0) {
if (HotSaNIClog::check_debuglevel("MODULE_VERBOSE")) { HotSaNIClog::info("save for $seconds1:"); }
# store data...
foreach $item (@items) {
updatedb($item,$seconds1,$COUNT{$item});
$COUNT{$item}=0;
}
# store last-check info
open LAST,">lastcheck.dat";
print LAST "$firstline",tell(FILE),"\n",$seconds1,"\n";
close LAST;
# if last stored hour wasnt 1 hour ago, store the hours between as zero.
if ($seconds1+3600 < $seconds) {
for ($secs=$seconds1+3600;$secs<$seconds;$secs+=3600) {
# store data (0)
foreach $item (@items) { updatedb($item,$secs,0); }
}
}
}
$seconds1=$seconds;
}
foreach $item (@items) { if (index($_,"/$item?") >= 0) { $COUNT{$item}++; } }
}
close FILE;
}
}
}
1;