#!/usr/bin/perl -w # ----------------------------------------------------------------------- # # Copy the $SOURCEFILE to a new timestamped history file # # and build the history index based on the timestamped # # files we found. Delete files older then max history. # # # # 20080110 support[at]frank4dd[dot]com # # # # we need Date::Calc, i.e. apt-get install libdate-calc-perl (Debian) # # ----------------------------------------------------------------------- # use strict; # force to be strict use Date::Calc qw(:all); # for date calculations # Global Variables use vars qw( $head_html $page_html $WEBDIR $SOURCEFILE $FILEPREFIX $INDEXFILE $fh @today ); # directory to create the index and history pages in $WEBDIR = "/home/htdocs/admin/html/logwatch/frank4dd.com"; # source report to build history from $SOURCEFILE = $WEBDIR."/logwatch.html"; # full path and name of the new index page $INDEXFILE = $WEBDIR."/history-index.html"; # filename prefix for history pages $FILEPREFIX = "logwatch-"; # ----------------------------------------------------------------------- # # this function takes the formerly generated $SOURCEFILE to create a copy # # with the name <$FILEPREFIX>.html for keeping a file history. # # ----------------------------------------------------------------------- # sub timestamp_reportfile { my($DESTFILE) = ""; # get todays date @today = Today(); # add a leading zero (month) if($today[1] < 10) { $today[1] = "0".$today[1]; } # add a leading zero (day) if($today[2] < 10) { $today[2] = "0".$today[2]; } # create the full path to the file we want to create a hardlink for $DESTFILE .= $WEBDIR."/".$FILEPREFIX.$today[0].$today[1].$today[2].".html"; # debug only # print "New Name: $DESTFILE\n"; # should we re-run the sript, we need to delete the former copy if(-r $DESTFILE) { unlink($DESTFILE); } # create the hardlink link($SOURCEFILE, $DESTFILE) || die "Cannot create link for $SOURCEFILE"; } # ----------------------------------------------------------------------- # # this function creates the standard html header + top description tables # # ----------------------------------------------------------------------- # sub create_htmlheader { my($pagetitle) = "Logwatch Report History"; my($hostname) = `hostname`; my($rundate) = `/bin/date \"+%d.%m.%Y %H:%M\"`; $head_html = << "EoText"; $pagetitle

$pagetitle

 Server: $hostname:443  [LogWatch: History]  [Domain: www.frank4dd.com] [Back to Management]   Generated: $rundate JST 

EoText } # ----------------------------------------------------------------------- # # this function creates the history index table and rest of the html body # # ----------------------------------------------------------------------- # sub create_htmlpage { my($histweeks) = 12; my($histdays) = $histweeks * 7; my(@weekdays) = ( '#', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Week' ); my(@weekofyr); my(@daydate); my($weekstr); my($HISTFILE); my($EXPIREFILE); my(@filestat); my($time_now); $page_html = "\n"; $page_html .= ""; # create the X-axis header columns my($j) = 0; while ( $j < 9 ) { $page_html .= ""; $j++; } $page_html .= "\n"; # start creating the data rows my($i) = 0; while ( $i <= $histweeks ) { @today = Today(); @weekofyr = Week_of_Year(Add_Delta_Days(@today, -($i*7))); $weekstr = $weekofyr[1]."/".($weekofyr[0]); $page_html .= ""; # Y-axis startheader shows the history number $page_html .= ""; my($j) = 0; while ( $j < 7 ) { @daydate = Add_Delta_Days(Monday_of_Week(@weekofyr),$j); if($daydate[2] < 10) { $daydate[2] = "0".$daydate[2]; } if($daydate[1] < 10) { $daydate[1] = "0".$daydate[1]; } $HISTFILE = $FILEPREFIX.$daydate[0].$daydate[1].$daydate[2].".html"; $page_html .= ""; $j++; } # Y-axis endheader shows the week of year $page_html .= ""; $page_html .= "\n"; $i++; } # we expire the oldest file in history starting from here: $page_html .= "\n"; $page_html .= "\n"; $page_html .= "\n"; $page_html .= "
"; $page_html .= $weekdays[$j]; $page_html .= "
"; $page_html .= $i; $page_html .= ""; if(-r $WEBDIR."/".$HISTFILE) { $page_html .= ""; $page_html .= $daydate[2].".".$daydate[1].".".$daydate[0]; $page_html .= ""; } else { $page_html .= $daydate[2].".".$daydate[1].".".$daydate[0]; } $page_html .= ""; $page_html .= $weekstr; $page_html .= "
\n"; # expire the file older then in the current view @daydate = Add_Delta_Days(@today, -$histdays); if($daydate[2] < 10) { $daydate[2] = "0".$daydate[2]; } if($daydate[1] < 10) { $daydate[1] = "0".$daydate[1]; } $EXPIREFILE = $FILEPREFIX.$daydate[0].$daydate[1].$daydate[2].".html"; $page_html .= "file expired: ".$EXPIREFILE; # delete the expired file and report about it if(-r $EXPIREFILE) { unlink($EXPIREFILE); $page_html .= "
... file successfully deleted."; } # close the field, row and table $page_html .= "
"; } # ----------------------------------------------------------------------- # # main(), run the functions in order and write the history index file # # ----------------------------------------------------------------------- # timestamp_reportfile(); create_htmlheader(); create_htmlpage(); # For debug, enable terminal output # print $head_html; # print $page_html; # write the index page open( OUT, ">$INDEXFILE" ) or die ("$0: can't open $INDEXFILE for writing: $!\n"); $fh = *OUT; print $fh $head_html; print $fh $page_html; print $fh ""; close( OUT ); # ----------------------------------------------------------------------- # # end of main(), end of program, end of file # # ----------------------------------------------------------------------- #