[mrtg] somewhat convenient script

Denis denisa at mtlfab.com
Fri Jul 26 16:31:00 MEST 2002


Hello, everyone.

I wrote a script (see below) that takes regular mrtg pages specified at 
the command line and groups them into four separate, somewhat condenced 
in layout, pages (by day, week, month, and year correspondingly). I find 
it convenient with it to monitor some devices for the current (usually 
daily) status. This script could be added as a cronjob and keep the info 
up to date along with the mrtg data.
Give me some feedback on this if you wish so.

Best regards,
denis


#--------------------------------------------------------------------------------------------------- 

# Description: -The script generates global html-formatted reports of 
the specified statistics files
#                  created by MRTG. By separating the information by 
day, week, month, and year it
#               allows to see all of the device statistics for a 
specific time frame. The output
#        pages are presented in a bit of a compressed format, which 
might be useful to
#        observe more stats on the same page. There's only one column of 
graphs across the
#        page. This script is for the purpose of simplification of html 
global report page
#        generation.
# Input:       -One or more filenames (i.e. <path>/page.html) of the 
html pages generated by the
#               standard MRTG for separate stats, followed by the output 
file name
#        (i.e. <path>/output_file) all separated by spaces at the 
command line
#        (see example of the command below). The output file extension 
is unnesessary
#        because an html extension is added to the given output file name.
#           -If you would like to specify a keyword that will be used in 
the titles and headings
#        of the generated pages, use - sign before it (i.e. -Traffic). 
If no keyword
#        specified, the output file name is being used for the title and 
heading of the out-
#         put html page. Keyword,    however has to be last in the 
command, i.e after the output
#        file name which is essentially required.
# Output:      -Four html-formatted pages (day, week, month, year) in 
the output location specified
#        with the name specified (i.e. 
<output_path>/output_file-[d|w|m|y].html>).                # 
NOTES:       -It is recommended to specify full path with both the input 
and the output files,
#        especially if this script is being used with [cron] for 
automatic updates. If no
#        path specified with the input files when using cron, cron won't 
know where those
#        files are and will generate an error. In case of no path 
specified with the output
#        file, cron will create an output file in the home directory of 
the executing user.
#--------------------------------------------------------------------------------------------------- 

# Syntax:      perl <this_script_name.pl> <path>/file1.html ... 
[<path>/fileN.html]
#        > <path>/output_file [-Keyword]
#
# Example:     perl report.pl /usr/local/www/html/router1.html 
/usr/local/www/html/output-file
#        > -Ethernet_Traffic
#
# 07/17/02 - Denis Antipov
#--------------------------------------------------------------------------------------------------- 

#!/usr/bin/perl

if($ARGV[$#ARGV] =~ /^-/){
   $keyword = pop @ARGV;
   $keyword =~ s/^-//;}
else{      $keyword = $ARGV[$#ARGV];
   $keyword =~ s/.*\///;    #trims off the possible path
}

#if only 1 input string thus without output file name
if($#ARGV lt 1){ die "Error: Did not provide output file!\a\n";}
else{$outfile = pop @ARGV;}

%content = 
(d=>["<html><head>"],w=>["<html><head>"],m=>["<html><head>"],y=>["<html><head>"]); 


foreach $item (keys %content){
   push @{$content{$item}}, "\n<meta HTTP-EQUIV=\"Refresh\" 
CONTENT=\"300\">",
   "\n<meta HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">",
   "\n<meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; 
charset=iso-8859-1\">","\n<title>";}

push @{$content{d}}, "Daily $keyword 
Report</title></head>\n<body>\n<font size=+1>Daily $keyword 
Report</font>\n";
push @{$content{w}}, "Weekly $keyword 
Report</title></head>\n<body>\n<font size=+1>Weekly $keyword 
Report</font>\n";
push @{$content{m}}, "Monthly $keyword 
Report</title></head>\n<body>\n<font size=+1>Monthly $keyword 
Report</font>\n";
push @{$content{y}}, "Yearly $keyword 
Report</title></head>\n<body>\n<font size=+1>Yearly $keyword 
Report</font>\n";

foreach $item (keys %content){    #this is a link to the main menu page, 
you can get rid of it if you don't have such page in the parent dir
   push @{$content{$item}}, "<a href=\"../index.html\"> -> Back to Main 
</a><br>\n";}

foreach $filename(@ARGV){
   #line flags
   $d=0, $w=0, $m=0, $y=0;
   open(IN, "<$filename");
   while($line = <IN>){
       if($line =~ m/<TITLE>.*/i){
          $line =~ s/<TITLE>/<b>/g;
          $line =~ s/<\/TITLE>/<\/b>/g;
          foreach $item (keys %content){
           push @{$content{$item}}, "\n$line", "<br>\n";}
       }
       elsif($line =~ m/<!-- Begin `Daily' Graph.*/i){$d=1;}
       elsif($line =~ m/<!-- Begin `Weekly' Graph.*/i){$w=1;}
       elsif($line =~ m/<!-- Begin `Monthly' Graph.*/i){$m=1;}
       elsif($line =~ m/<!-- Begin `Yearly' Graph.*/i){$y=1;}
       elsif($line =~ m/<!-- End `Daily' Graph.*/i){$d=0;}
       elsif($line =~ m/<!-- End `Weekly' Graph.*/i){$w=0;}
       elsif($line =~ m/<!-- End `Monthly' Graph.*/i){$m=0;}
       elsif($line =~ m/<!-- End `Yearly' Graph.*/i){$y=0;}
       #skip the lines above the graph
       next if($line =~ /^<B>/ || $line=~ /^<BR>/ || $line=~/^<HR>/);

       if($d eq 1){push @{$content{d}}, $line;}
       elsif($w eq 1){push @{$content{w}}, $line;}
       elsif($m eq 1){push @{$content{m}}, $line;}
       elsif($y eq 1){push @{$content{y}}, $line;}
   }
   close(IN);
}

foreach $item (keys %content){
   push @{$content{$item}}, "\n<br><a href=\"../index.html\"> -> Main 
</a>\n</body>\n</html>";}

foreach $key (keys %content){
   open(OUT, ">$outfile-$key.html") or die "cannot open 
$outfile-$key.html file!";
   flock(OUT, 2);
   print OUT @{$content{$key}};
   flock(OUT, 8);
   close(OUT);
}




--
Unsubscribe mailto:mrtg-request at list.ee.ethz.ch?subject=unsubscribe
Archive     http://www.ee.ethz.ch/~slist/mrtg
FAQ         http://faq.mrtg.org    Homepage     http://www.mrtg.org
WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi



More information about the mrtg mailing list