--- projects/cms/source/reports/rrdgraphing/graph.pl 2002/03/18 13:24:31 1.1 +++ projects/cms/source/reports/rrdgraphing/graph.pl 2002/05/20 16:11:23 1.3 @@ -1,5 +1,24 @@ #!/usr/bin/perl -w +# +# i-scream central monitoring system +# Copyright (C) 2000-2002 i-scream +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# + # ----------------------------------------------------------- # i-scream graph generation script # http://www.i-scream.org.uk @@ -7,7 +26,7 @@ # Generates graphs from rrd databases for i-scream data. # # $Author: tdb $ -# $Id: graph.pl,v 1.1 2002/03/18 13:24:31 tdb Exp $ +# $Id: graph.pl,v 1.3 2002/05/20 16:11:23 tdb Exp $ #------------------------------------------------------------ ## TODO @@ -31,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); @@ -49,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); @@ -143,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"; } }