--- projects/cms/source/reports/rrdgraphing/graph.pl 2002/05/18 18:15:59 1.2 +++ projects/cms/source/reports/rrdgraphing/graph.pl 2002/05/21 11:37:56 1.4 @@ -26,7 +26,7 @@ # Generates graphs from rrd databases for i-scream data. # # $Author: tdb $ -# $Id: graph.pl,v 1.2 2002/05/18 18:15:59 tdb Exp $ +# $Id: graph.pl,v 1.4 2002/05/21 11:37:56 tdb Exp $ #------------------------------------------------------------ ## TODO @@ -50,7 +50,22 @@ my($rrddir) = "/u1/i-scream/databases"; my($hex_slash) = "_2f"; # _ converted to a decimal then hex'd my($hex_underscore) = "_5f"; - + +# maximum age (last modified) before an rrd or graph get cleaned up +# (in seconds) +my($maxrrdage) = 3600; # 1 hour +my($maximgage) = 3600; # 1 hour + +# delete rrd's when they get cleaned up? +# if unset, will just ignore the rrd's +# - usually best to leave this off, we don't want to delete useful rrds :) +my($deleterrds) = 0; + +# delete graphs when they get cleaned up? +# if unset, won't bother checking at all +# - usually best to leave this on +my($deleteimgs) = 1; + # Read the contents of the base directory # and pull out the list of subdirectories (except . and .. :) opendir(DIR, $rrddir); @@ -68,6 +83,19 @@ foreach my $machine (@rrddirlist) { # See what rrd we have, and generate the graphs accordingly foreach my $rrd (@rrdlist) { chomp $rrd; + # stat the file + my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, + $ctime,$blksize,$blocks) = stat("$rrddir/$machine/$rrd"); + # check if it's old enough to be deleted + if((time - $mtime) > $maxrrdage) { + # do we delete the rrd, or just ignore it? + if($deleterrds) { + # if so, delete it + unlink("$rrddir/$machine/$rrd"); + } + # no more processing required for this rrd + next; + } if($rrd =~ /^(cpu)\.rrd$/) { my(@data); my(@rawdata); @@ -163,7 +191,59 @@ foreach my $machine (@rrddirlist) { &makegraph($machine, $baserrd, $comment, \@data, \@rawdata); } } + # have a last check, maybe we can remove the directory now? + # (only if we're deleting stuff) + if($deleterrds) { + # Read the contents of the directory + opendir(DIR, "$rrddir/$machine"); + my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR); + closedir DIR; + if($#dirlist == -1) { + rmdir "$rrddir/$machine"; + } + } } + +if($deleteimgs) { + # Read the contents of the graphs directory + # and pull out the list of subdirectories (except . and .. :) + opendir(DIR, $imgdir); + my(@imgdirlist) = grep { -d "$imgdir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR); + closedir DIR; + + # look through each directoty, as they might + # contain images for a particular machine + foreach my $machine (@imgdirlist) { + # Read the contents of the directory + opendir(DIR, "$imgdir/$machine"); + my(@imglist) = grep { /\.png$/ && -f "$imgdir/$machine/$_" } readdir(DIR); + closedir DIR; + + # See what rrd we have, and generate the graphs accordingly + foreach my $img (@imglist) { + chomp $img; + # stat the img + my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, + $ctime,$blksize,$blocks) = stat("$imgdir/$machine/$img"); + # check if it's old enough to be deleted + if((time - $mtime) > $maximgage) { + # if so, delete it + unlink("$imgdir/$machine/$img"); + } + } + # have a last check, maybe we can remove the directory now? + # Read the contents of the directory + opendir(DIR, "$imgdir/$machine"); + my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR); + closedir DIR; + if($#dirlist == -1) { + rmdir "$imgdir/$machine"; + } + } +} + +exit(0); + # # subroutine to make some graphs