Simply copy and paste the following code into a perl script file. A zip version is available also at here.

 

mrtg.daily.pl

 

use File::Copy;

### Get yesterday's file name
# @LT = CurrentTime - 20 hours
@LT = localtime(time-72000);
@LT[5] = @LT[5] + 1900;
@LT[4] = @LT[4] + 1;
$yearstr = sprintf( "%04s", @LT[5] );
$datestr = sprintf( "%04s-%02s-%02s", @LT[5], @LT[4], @LT[3]);

### get the configuration file name and the log file name
$logFile = "mrtg.daily.graph.save.log"; #default log file name
if( scalar(@ARGV)<1 ) {
WriteLog( "Configuration file is needed as command line parameter like." );
WriteLog( "mrtg.daily.graph.save.pl mrtg.cfg" );
exit;
}
$cfgFile = $ARGV[0];
if( uc(substr($cfgFile,length($cfgFile)-4,4))=~/.CFG/ ) {
$logFile = substr($cfgFile,0,length($cfgFile)-4) . ".daily.log";
} else {
$logFile = $cfgFile . ".log";
}

# Process command line switches
for( $i=1; $i<scalar(@ARGV); $i++ ) {
$line = $ARGV[$i];
if( uc(substr($line,0,9))=~/\/DESTDIR:/ ) {
$BaseDestinationDir = substr( $line, 9, length($line)-9 );
} elsif( uc(substr($line,0,12))=~/\/NOSUBFOLDER/ ) {
$NoSubFolder = 1;
} elsif( uc(substr($line,0,6))=~/\/DEBUG/ ) {
$g_Debug = 1;
} elsif( uc(substr($line,0,8))=~/\/PAGETOP/ ) {
$g_Body = substr( $line, 8, length($line)-8 );
}
}

open( INPUT, $cfgFile ) or exit;
@buf = <INPUT>;
chomp( @buf );
close( INPUT );

$pBuf = \@buf;
# search for WorkDir
$i = 0;
while( ($i<=$#$pBuf) and (not defined $WorkDir) ) {
$line = $pBuf->[$i];
if( uc(substr($line,0,8))=~/WORKDIR:/ ) {
$WorkDir = GetSetting( $line );
} else {
$i++;
}
}
# check variables
if( not defined $WorkDir ) {
WriteLog( "Can not find WorkDir setting in " . cfgFile );
exit;
}
if( not defined $BaseDestinationDir ) {
$BaseDestinationDir = $WorkDir;
}

### initialize hashes
%Title = ();
%PageTop = ();

#read in all the targets
$i = 0;
while( $i<=$#$pBuf ) {
$line = $pBuf->[$i];
if( substr($line,0,1)=~/#/ ) { # this is a comment line
;
} elsif( uc(substr($line,0,7))=~/TARGET\[/ ) {
if( defined $TargetName ) {
push( @Target, $TargetName );
if( defined $TitleName ) { $Title{$TargetName} = $TitleName; }
if( defined $PageTopString ) { $PageTop{$TargetName} = $PageTopString; }
#intialize variables for the next target 
undef $TargetName;
undef $TitleName;
undef $PageTopString;
}
$TargetName = GetTargetName($line);
} elsif( (defined $TargetName) and ($line=~/\[$TargetName\]:/) ) {
if( uc(substr($line,0,6))=~/TITLE\[/ ) {
$TitleName = GetSetting( $line );
} elsif( uc(substr($line,0,8))=~/PAGETOP\[/ ) {
$PageTopString = GetSetting( $line );
}
}
$i++;
}
#save the last target
if( defined $TargetName ) {
push( @Target, $TargetName );
if( defined $TitleName ) { $Title{$TargetName} = $TitleName; }
if( defined $PageTopString ) { $PageTop{$TargetName} = $PageTopString; }
#intialize variables for the next target 
undef $TargetName;
undef $TitleName;
undef $PageTopString;
}


# output debug info
if( $g_Debug ) {
WriteLog( "" );
WriteLog( "cfgFile: " . $cfgFile );
WriteLog( "logFile: " . $logFile );
WriteLog( "WorkDir: " . $WorkDir );
WriteLog( "DestinationDir: " . $BaseDestinationDir );
WriteLog( "SubFolder: " . ($NoSubFolder?"NoSubFolder":"With subfolder") );
WriteLog( "Debug: " . ($g_Debug?"on":"off") );
WriteLog( "Glboal PageTop: " . $g_Body );
}


# process each target
$pList = \@Target; 
for( $i=0; $i<=$#$pList; $i++ ) {
$TargetName = $pList->[$i];
$TitleName = (exists $Title{$TargetName}) ? $Title{$TargetName} : $TargetName; 
$PageTopString = (exists $PageTop{$TargetName}) ? $PageTop{$TargetName} : ($TargetName . " Daily Graph History");
$SourceFile = $WorkDir . "\\" . $TargetName . "-day.png";
if( $NoSubFolder ) {
$DestinationFile = $BaseDestinationDir . "\\" . $TargetName . "-day-" . $datestr . ".png";
$htmlFile = $BaseDestinationDir . "\\" . $TargetName . "-day-history.html";
$htmlYearFile = $BaseDestinationDir . "\\" . $TargetName . "-day-" . $yearstr . ".html";
} else {
$DestinationFile = $BaseDestinationDir . "\\" . $TargetName . "\\" . $TargetName . "-day-" . $datestr . ".png";
$htmlFile = $BaseDestinationDir . "\\" . $TargetName . "\\" . $TargetName . "-day-history.html";
$htmlYearFile = $BaseDestinationDir . "\\" . $TargetName . "\\" . $TargetName . "-day-" . $yearstr . ".html";
}

if( $g_Debug ) {
WriteLog( "" );
WriteLog( "Target[" . $TargetName . "]:" );
WriteLog( "Title[" . $TargetName . "]:" . $TitleName );
WriteLog( "PageTop[" . $TargetName . "]:" . $PageTopString );
WriteLog( "SourceFile:" . $SourceFile );
WriteLog( "DestinationFile:" . $DestinationFile );
WriteLog( "htmlYearFile:" . $htmlYearFile );
WriteLog( "htmlFile:" . $htmlFile );
}

open( INPUT, "$htmlYearFile");
$line = <INPUT>;
chomp( $line );
if( (not defined $line) or (substr($line,0,6)!~/<html>/) ) {
push( @htmlBuf, "<html>" );
push( @htmlBuf, '' );
push( @htmlBuf, "<head>" );
push( @htmlBuf, '<meta name="GENERATOR" content="Microsoft FrontPage 4.0">' );
push( @htmlBuf, '<title>' . $TitleName . '</title>' );
push( @htmlBuf, '</head>' );
push( @htmlBuf, '' );
push( @htmlBuf, '<body>' );
push( @htmlBuf, '' );
if( defined $g_Body ) {
push( @htmlBuf, $g_body );
push( @htmlBuf, '' );
}
push( @htmlBuf, '<p><font size="5"><b>' . $PageTopString . '</b></font></p>' );
push( @htmlBuf, '' );
push( @htmlBuf, '<table border="0" width="100%" cellspacing="10">' );
push( @htmlBuf, ' <tr>' );
push( @htmlBuf, ' <td width="100%">' . $datestr . '<br>' );
push( @htmlBuf, ' <img border="0" src="' . $TargetName . '-day-' . $datestr . '.png' . '"></td>' );
push( @htmlBuf, ' </tr>' );
push( @htmlBuf, '</table>' );
push( @htmlBuf, '</body>' );
push( @htmlBuf, '</html>' );
push( @htmlBuf, '' );
} else {
push( @htmlBuf, $line );
while( $line = <INPUT> ) {
chomp( $line );
if( substr($line,0,8)=~/<\/table>/ ) {
push( @htmlBuf, ' <tr>' );
push( @htmlBuf, ' <td width="100%">' . $datestr . '<br>' );
push( @htmlBuf, ' <img border="0" src="' . $TargetName . '-day-' . $datestr . '.png' . '"></td>' );
push( @htmlBuf, ' </tr>' );

push( @htmlBuf, $line );
}
}
close( INPUT );

#copy the file
copy( $SourceFile, $DestinationFile ) or WriteLog( "Cannot copy ".$SourceFile." -> ".$DestinationFile );

#update the html file
$pBuf = \@htmlBuf;
open( OUTPUT, ">$htmlYearFile") or WriteLog( "Cannot generate " . $htmlYearFile );
for( $j=0; $j<=$#$pBuf; $j++ ) {
print OUTPUT $pBuf->[$j], "\n";
}
close( OUTPUT );

#copy $htmlFile to a $-history.html
copy( $htmlYearFile, $htmlFile ) or WriteLog( "Cannot copy ".$htmlYearFile." -> ".$htmlFile );;

#clear the @htmlBuf, so it is ready for next Target
splice @htmlBuf;

}

while( ($k,$v)=each %Title ) { delete( $Title{$k} ); }
while( ($k,$v)=each %PageTop ) { delete( $PageTop{$k} ); }
splice( @buf );

# end of the main program



sub WriteLog($) {
my $line = @_->[0];
my @LT = localtime;
@LT[5] = @LT[5] + 1900;
@LT[4] = @LT[4] + 1;
my $datestr = sprintf( "%04s-%02s-%02s %02s:%02s:%02s --", @LT[5], @LT[4], @LT[3], @LT[2], @LT[1], @LT[0]);
open( OUTPUT, ">>$logFile") or exit;
print OUTPUT "$datestr $line\n";
close( OUTPUT );
}

sub GetTargetName($) {
my $line = @_->[0];
my $index1 = index( $line, "[" );
my $index2 = index( $line, "]" );
return substr( $line, $index1+1, $index2-$index1-1 );
}

# read the line seperated by ":", return the second half of it
sub GetSetting($) {
my @buf;
my $TempStr = @_->[0];
@buf = split( /:/, $TempStr, 2 );
$TempStr = trim( $buf[1] );
splice( @buf );
return $TempStr; 
}

#remove the leading and trailing spaces
sub trim {
my @out = @_;
for (@out) {
s/^\s+//;
s/\s+$//;
}
return wantarray ? @out : $out[0];
}

 

 

Counter

Hosted by www.Geocities.ws

1