From b6c93cc77ce94a22bf0a8931a79c1b93e225ba3e Mon Sep 17 00:00:00 2001 From: ian dobson Date: Thu, 23 Jun 2011 21:08:31 +0200 Subject: [PATCH] fixed bug if a dir/filename contains the text total --- plugins/other/dirsizes | 216 +++++++++++++++++++++-------------------- 1 file changed, 109 insertions(+), 107 deletions(-) diff --git a/plugins/other/dirsizes b/plugins/other/dirsizes index ff5f4f85..a0c57dda 100755 --- a/plugins/other/dirsizes +++ b/plugins/other/dirsizes @@ -1,107 +1,109 @@ -#!/usr/bin/perl -# -*- perl -*- -# -# ############################################################################## # -# -# This munin plugin watches the sizes of the given directories. -# @author Kevin Fischer -# @version 2010/08/05 -# @website http://kevin-fischer.de -# -# Copy this to your node's config file (default: plugin-conf.d/munin-node): -# [dirsizes] -# user root -# env.watchdirs /var/www,/tmp -# -# Change the env.watchdirs-variable according to your wishes. -# DONT FORGET TO RUN AS ROOT! -# -# You can test this plugin by calling it with params "test" and your watchdirs: -# ./dirsizes test /dir1,/tmp/dir2 -# -# ############################################################################## # - -use strict; - -my @watchdirs; - -# Normal mode or test mode? -if(exists $ARGV[0] and $ARGV[0] ne "test") -{ - # If no dirs are given, exit. - if(! defined($ENV{"watchdirs"})) - { - die "No directories given! See the manual at top of this plugin file."; - } - - # Split the watchdirs string - @watchdirs = split(",", $ENV{"watchdirs"}); -} -# Test mode -elsif(exists $ARGV[0] and $ARGV[0] eq "test") -{ - # Split the watchdirs string - @watchdirs = split(",", $ARGV[1]); -} - -# Config or read request? -if(exists $ARGV[0] and $ARGV[0] eq "config") -{ - # Munin basic info - print "graph_title Directory sizes\n"; - print "graph_args --base 1024 --lower-limit 0\n"; - print "graph_vlabel directory size\n"; - print "graph_info Displays the sizes of all configured directories.\n"; - print "graph_category disk\n"; - print "graph_total Total\n"; - - # All available directories - foreach my $dir (@watchdirs) { - # Remove illegal characters - my $label = $dir; - $label =~ s@[\/-]@_@g; - - # Print name - print "dir", $label, ".label ", $dir, "\n"; - } -} -# Read request, output the directory sizes -else -{ - # All available directories - foreach my $dir (@watchdirs) { - # Remove illegal characters - my $label = $dir; - $label =~ s@[\/-]@_@g; - - # Get the dirsize - my $dirsize = getSize($dir); - - # Get the label - my $label = niceLabelname($dir); - - # Print name - print "dir", $label, ".value ", $dirsize, ".0\n"; - } -} - - -# Function: getSize($dir) -sub getSize -{ - my($dir) = @_; - - # Get the size via `du` - my @dirsize = split(' ', `du -cb $dir | grep "total"`); - return @dirsize[0]; -} -# Remove illegal characters -sub niceLabelname -{ - my($label) = @_; - - $label =~ s@[\/-]@_@g; - return $label; -} - -exit 0; \ No newline at end of file +#!/usr/bin/perl +# -*- perl -*- +# +# +############################################################################## +# +# +# This munin plugin watches the sizes of the given directories. +# @author Kevin Fischer +# @version 2010/08/05 +# @website http://kevin-fischer.de +# +# Copy this to your node's config file (default: plugin-conf.d/munin-node): +# [dirsizes] +# user root +# env.watchdirs /var/www,/tmp +# +# Change the env.watchdirs-variable according to your wishes. +# DONT FORGET TO RUN AS ROOT! +# +# You can test this plugin by calling it with params "test" and your watchdirs: +# ./dirsizes test /dir1,/tmp/dir2 +# +# +############################################################################## +# + +use strict; +my @watchdirs; + +if ( exists $ARGV[0] and $ARGV[0] eq "test" ) { + + # Split the watchdirs string + @watchdirs = split( ",", $ARGV[1] ); +} +else { + + # If no dirs are given, exit. + if ( !defined( $ENV{"watchdirs"} ) ) { + die "No directories given! See the manual at top of this plugin file."; + } + + # Split the watchdirs string + @watchdirs = split( ",", $ENV{"watchdirs"} ); +} + +# Config or read request? +if ( exists $ARGV[0] and $ARGV[0] eq "config" ) { + + # Munin basic info + print "graph_title Directory sizes\n"; + print "graph_args --base 1024 --lower-limit 0\n"; + print "graph_vlabel directory size\n"; + print "graph_info Displays the sizes of all configured directories.\n"; + print "graph_category disk\n"; + print "graph_total Total\n"; + + # All available directories + foreach my $dir (@watchdirs) { + + # Remove illegal characters + my $label = $dir; + $label =~ s@[\/-]@_@g; + + # Print name + print "dir", $label, ".label ", $dir, "\n"; + } +} + +# Read request, output the directory sizes +else { + + # All available directories + foreach my $dir (@watchdirs) { + + # Remove illegal characters + my $label = $dir; + $label =~ s@[\/-]@_@g; + + # Get the dirsize + my $dirsize = getSize($dir); + + # Get the label + my $label = niceLabelname($dir); + + # Print name + print "dir", $label, ".value ", $dirsize, ".0\n"; + } +} + +# Function: getSize($dir) +sub getSize { + my ($dir) = @_; + + # Get the size via `du` + my @dirsize = split( ' ', `du -cb $dir | grep "total" | tail -1 ` ); + return @dirsize[0]; +} + +# Remove illegal characters +sub niceLabelname { + my ($label) = @_; + + $label =~ s@[\/-]@_@g; + return $label; +} + +exit 0; +