#! /usr/bin/perl # Script reads data log produced by APC AP7940 which if ftp'd to $logDir # it finds the last entry read (from status file) and scrolls down through # log file until it finds the next entry. # then summarizes the entries (only happens once an hour) and stores the value # in the out file. # Copyright 2015, Rod, Daily Data, Inc. # Free to use, modify, change, anything you want # You do NOT have to give attribution, but it would be # appreciated. # Also, if you make changes and want to give them back, just send them to # us. Visit the http://www.dailydata.net and select Contact Us my $logDir = '/home/apcpdu'; # location where log is ftp'd to by AP7940 my @PDUNames = ('pdu1','pdu2'); # names of pdu's to look for my $logFileExtension = 'log'; # extension expected for log file name my $statusExtension = 'status'; # extension used for recording last record processed my $outputFileExtension = 'out'; # extension used for most recent summary my $voltage = 208; # could not figure out how to get voltage from pdu, so hard coded my $pf = 1; # ditto power factor foreach my $thisLog ( @PDUNames ) { my $statusFile = $logDir . '/' . $thisLog . '.' . $statusExtension; my $logFile = $logDir . '/' . $thisLog . '.' . $logFileExtension; my $outFile = $logDir . '/' . $thisLog . '.' . $outputFileExtension; my @log; my $status = 0; # print "Processing log from $thisLog\n"; if ( -f $statusFile ) { # print "\tReading $statusFile\n"; open STATUS, "<$statusFile"; $status = ; close STATUS; } if ( -f $logFile ) { # print "\tReading $logFile\n"; open LOG,"<$logFile" or die "Could not read $logFile: $!\n"; my $log = join( '', ); @log = split( "\n", $log ); close LOG; } else { next; } chomp @log; # print "\tTrying to find header\n"; my $line = 0; while ( $line < @log ) { last if $log[$line] =~ m/Date\s+Time\s+I\s+IMax\s+IMin/; $line++; }; # print "\tFinding first line to process\n"; if ( $status ) { while ( $line < @log && $log[$line] ne $status ) { $line++; } } $line++; # we should now be at the first entry AFTER the last one read # print "First line to process would be line $line\n$log[$line]\n"; my $max = 0; my $min = 1000; my $sum = 0; my $count = 0; my $datetime; # last date and time read while ( $line < @log ) { my ($date,$time,$current,$currentMax,$currentMin) = split( /\s+/, $log[$line]); $max = $currentMax if $currentMax > $max; $min = $currentMin if $currentMin < $min; $count++; $sum += $current; $datetime = "$date $time"; $line++; } next unless $count; open LOG,">$outFile" or die "Could not write to $outFile: $!\n"; print LOG "timestamp\t$datetime\n"; print LOG "average\t" . $sum/$count . "\n"; print LOG "max\t$max\n"; print LOG "min\t$min\n"; print LOG "count\t$count\n"; print LOG "voltage\t$voltage\n"; print LOG "pf\t$pf\n"; close LOG; `chmod 666 $outFile`; open STATUS,">$statusFile" or die "Could not write to $statusFile: $!\n"; print STATUS $log[scalar(@log)-1]; close STATUS; `mv $logFile $logFile.old`; } 1;