--- projects/cms/source/reports/rrdgraphing/graph.pl 2002/03/18 13:24:31 1.1 +++ projects/cms/source/reports/rrdgraphing/graph.pl 2002/05/21 11:37:56 1.4 @@ -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.4 2002/05/21 11:37:56 tdb Exp $ #------------------------------------------------------------ ## TODO @@ -31,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); @@ -49,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); @@ -144,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