--- projects/cms/source/reports/rrdgraphing/graph.pl 2002/05/18 18:15:59 1.2 +++ projects/cms/source/reports/rrdgraphing/graph.pl 2002/05/20 16:11:23 1.3 @@ -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.3 2002/05/20 16:11:23 tdb Exp $ #------------------------------------------------------------ ## TODO @@ -50,7 +50,12 @@ 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($maxgraphage) = 3600; # 1 hour + # Read the contents of the base directory # and pull out the list of subdirectories (except . and .. :) opendir(DIR, $rrddir); @@ -68,6 +73,17 @@ 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) { +print "pruning rrd $rrddir/$machine/$rrd\n"; + # if so, delete it + unlink("$rrddir/$machine/$rrd"); + # no more processing required for this rrd + next; + } if($rrd =~ /^(cpu)\.rrd$/) { my(@data); my(@rawdata); @@ -162,6 +178,53 @@ foreach my $machine (@rrddirlist) { $comment = "unknown queue" if not defined $comment; &makegraph($machine, $baserrd, $comment, \@data, \@rawdata); } + } + # have a last check, maybe we can remove the directory now? + # Read the contents of the directory + opendir(DIR, "$rrddir/$machine"); + my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR); + closedir DIR; + if($#dirlist == -1) { +print "pruning rrddir $rrddir/$machine\n"; + rmdir "$rrddir/$machine"; + } +} + +# 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) > $maxgraphage) { +print "pruning img $imgdir/$machine/$img\n"; + # 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) { +print "pruning img dir $imgdir/$machine\n"; + rmdir "$imgdir/$machine"; } }