| 1 | 1 | simandl | package HotSaNICmod::OSdep; |
| 2 | | | |
| 3 | | | use RRDs; |
| 4 | | | |
| 5 | | | use lib "../../lib"; |
| 6 | | | use HotSaNIClogparse; |
| 7 | | | |
| 8 | | | use Time::Local; |
| 9 | | | |
| 10 | | | @months=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); |
| 11 | | | %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); |
| 12 | | | |
| 13 | | | my %ERRORTIME; |
| 14 | | | |
| 15 | | | ##### update specific database |
| 16 | | | sub updatedb { |
| 17 | | | my ($name,$sampletime,$value)=@_; |
| 18 | | | my $ERROR; |
| 19 | | | if (HotSaNIClog::check_debuglevel("MODULE_VERBOSE")) { HotSaNIClog::info(" $name:$value"); } |
| 20 | | | if ( ! -e "rrd/$name.rrd" ) { system("./makerrd","$name") } |
| 21 | | | if ((defined $name) && (defined $sampletime) && (defined $value)) { |
| 22 | | | $ERRORTIME{$name} = 0 if !defined $ERRORTIME{$name}; |
| 23 | | | if ( $sampletime > $ERRORTIME{$name} ) { |
| 24 | | | RRDs::update "rrd/$name.rrd",$sampletime.":".$value; |
| 25 | | | if ($ERROR = RRDs::error) { |
| 26 | | | HotSaNIClog::error("unable to update `$name.rrd': $ERROR"); |
| 27 | | | if (index($ERROR,"last update time") >=0) { |
| 28 | | | $ERROR =~ /last update time is (\d+)/,$ERRORTIME{$name}=$1; |
| 29 | | | HotSaNIClog::warn("Not trying to update `$name.rrd' again before $ERRORTIME{$name}"); |
| 30 | | | } |
| 31 | | | } |
| 32 | | | } |
| 33 | | | } |
| 34 | | | } |
| 35 | | | |
| 36 | | | sub version { |
| 37 | | | ($VERSION = '$Revision: 1.12 $') =~ s/.*(\d+\.\d+).*/$1/; |
| 38 | | | return "default.pm $VERSION"; |
| 39 | | | } |
| 40 | | | |
| 41 | | | sub sample { |
| 42 | | | |
| 43 | | | my %args=@_; |
| 44 | | | |
| 45 | | | # check if scan-interval is exceeded |
| 46 | | | |
| 47 | | | my $now=time; |
| 48 | | | my $mtime=0; |
| 49 | | | if (-e "lastcheck.dat") { $mtime=(stat("lastcheck.dat"))[9]; } |
| 50 | | | |
| 51 | | | if ($mtime+$args{INTERVAL} <= $now) { |
| 52 | | | |
| 53 | | | # update timestamp on lastcheck file |
| 54 | | | |
| 55 | | | utime $now,$now,"lastcheck.dat"; |
| 56 | | | |
| 57 | | | my @items=(); |
| 58 | | | foreach (@{$args{ITEM}}) { my ($item)=split /,/; push @items,$item; $COUNT{$item}=0; } |
| 59 | | | |
| 60 | | | if (HotSaNIClog::check_debuglevel("MODULE_SAMPLING")) { HotSaNIClog::info("sampling: ".join(" ",@items)); } |
| 61 | | | |
| 62 | | | my ($pos,@parse)=HotSaNIClogparse::findlogs($args{LOGPATH},$args{LOGNAME}.".*","lastcheck.dat"); |
| 63 | | | my $seconds1=0; |
| 64 | | | |
| 65 | | | foreach $file (@parse) { |
| 66 | | | if (HotSaNIClog::check_debuglevel("MODULE_SAMPLING")) { HotSaNIClog::info("parsing $file"); } |
| 67 | | | open FILE,"$file"; |
| 68 | | | $firstline=<FILE>; |
| 69 | | | if (HotSaNIClog::check_debuglevel("MODULE_SAMPLING")) { HotSaNIClog::info("seeking to pos. $pos"); } |
| 70 | | | seek FILE,$pos,0; |
| 71 | | | $pos=0; |
| 72 | | | while (<FILE>) { |
| 73 | | | chomp; |
| 74 | | | |
| 75 | | | my %result=HotSaNIClogparse::parseline_apache($_); |
| 76 | | | |
| 77 | | | # number of seconds of beginning of the hour in $datestr |
| 78 | | | $seconds=timelocal(0,0,$result{TShour},$result{TSday},$result{TSmonth},$result{TSyear})-$result{TSzdiff}*36; |
| 79 | | | |
| 80 | | | # check if another hour started and save sampling results for last hour |
| 81 | | | if ($seconds1 < $seconds) { |
| 82 | | | if (HotSaNIClog::check_debuglevel("MODULE_VERBOSE")) { HotSaNIClog::info("$year $mon $day $hour:$min - $seconds $hrcorrection"); } |
| 83 | | | |
| 84 | | | # if counts for last hour exist, save them |
| 85 | | | if ($seconds1 > 0) { |
| 86 | | | if (HotSaNIClog::check_debuglevel("MODULE_VERBOSE")) { HotSaNIClog::info("save for $seconds1:"); } |
| 87 | | | |
| 88 | | | # store data... |
| 89 | | | foreach $item (@items) { |
| 90 | | | updatedb($item,$seconds1,$COUNT{$item}); |
| 91 | | | $COUNT{$item}=0; |
| 92 | | | } |
| 93 | | | |
| 94 | | | # store last-check info |
| 95 | | | open LAST,">lastcheck.dat"; |
| 96 | | | print LAST "$firstline",tell(FILE),"\n",$seconds1,"\n"; |
| 97 | | | close LAST; |
| 98 | | | |
| 99 | | | # if last stored hour wasnt 1 hour ago, store the hours between as zero. |
| 100 | | | if ($seconds1+3600 < $seconds) { |
| 101 | | | for ($secs=$seconds1+3600;$secs<$seconds;$secs+=3600) { |
| 102 | | | # store data (0) |
| 103 | | | foreach $item (@items) { updatedb($item,$secs,0); } |
| 104 | | | } |
| 105 | | | } |
| 106 | | | } |
| 107 | | | $seconds1=$seconds; |
| 108 | | | } |
| 109 | | | foreach $item (@items) { if (index($_,"/$item?") >= 0) { $COUNT{$item}++; } } |
| 110 | | | } |
| 111 | | | close FILE; |
| 112 | | | } |
| 113 | | | } |
| 114 | | | } |
| 115 | | | |
| 116 | | | 1; |
| 117 | | | |
| 118 | | | |
| 119 | | | |