1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-23 06:35:42 +00:00

fixed bug if a dir/filename contains the text total

This commit is contained in:
ian dobson 2011-06-23 21:08:31 +02:00 committed by Steve Schnepp
parent 5332dc60ad
commit b6c93cc77c

View file

@ -1,107 +1,109 @@
#!/usr/bin/perl #!/usr/bin/perl
# -*- perl -*- # -*- perl -*-
# #
# ############################################################################## # #
# ##############################################################################
# This munin plugin watches the sizes of the given directories. #
# @author Kevin Fischer #
# @version 2010/08/05 # This munin plugin watches the sizes of the given directories.
# @website http://kevin-fischer.de # @author Kevin Fischer
# # @version 2010/08/05
# Copy this to your node's config file (default: plugin-conf.d/munin-node): # @website http://kevin-fischer.de
# [dirsizes] #
# user root # Copy this to your node's config file (default: plugin-conf.d/munin-node):
# env.watchdirs /var/www,/tmp # [dirsizes]
# # user root
# Change the env.watchdirs-variable according to your wishes. # env.watchdirs /var/www,/tmp
# DONT FORGET TO RUN AS ROOT! #
# # Change the env.watchdirs-variable according to your wishes.
# You can test this plugin by calling it with params "test" and your watchdirs: # DONT FORGET TO RUN AS ROOT!
# ./dirsizes test /dir1,/tmp/dir2 #
# # 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? use strict;
if(exists $ARGV[0] and $ARGV[0] ne "test") my @watchdirs;
{
# If no dirs are given, exit. if ( exists $ARGV[0] and $ARGV[0] eq "test" ) {
if(! defined($ENV{"watchdirs"}))
{ # Split the watchdirs string
die "No directories given! See the manual at top of this plugin file."; @watchdirs = split( ",", $ARGV[1] );
} }
else {
# Split the watchdirs string
@watchdirs = split(",", $ENV{"watchdirs"}); # If no dirs are given, exit.
} if ( !defined( $ENV{"watchdirs"} ) ) {
# Test mode die "No directories given! See the manual at top of this plugin file.";
elsif(exists $ARGV[0] and $ARGV[0] eq "test") }
{
# Split the watchdirs string # Split the watchdirs string
@watchdirs = split(",", $ARGV[1]); @watchdirs = split( ",", $ENV{"watchdirs"} );
} }
# Config or read request? # Config or read request?
if(exists $ARGV[0] and $ARGV[0] eq "config") if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {
{
# Munin basic info # Munin basic info
print "graph_title Directory sizes\n"; print "graph_title Directory sizes\n";
print "graph_args --base 1024 --lower-limit 0\n"; print "graph_args --base 1024 --lower-limit 0\n";
print "graph_vlabel directory size\n"; print "graph_vlabel directory size\n";
print "graph_info Displays the sizes of all configured directories.\n"; print "graph_info Displays the sizes of all configured directories.\n";
print "graph_category disk\n"; print "graph_category disk\n";
print "graph_total Total\n"; print "graph_total Total\n";
# All available directories # All available directories
foreach my $dir (@watchdirs) { foreach my $dir (@watchdirs) {
# Remove illegal characters
my $label = $dir; # Remove illegal characters
$label =~ s@[\/-]@_@g; my $label = $dir;
$label =~ s@[\/-]@_@g;
# Print name
print "dir", $label, ".label ", $dir, "\n"; # Print name
} print "dir", $label, ".label ", $dir, "\n";
} }
# Read request, output the directory sizes }
else
{ # Read request, output the directory sizes
# All available directories else {
foreach my $dir (@watchdirs) {
# Remove illegal characters # All available directories
my $label = $dir; foreach my $dir (@watchdirs) {
$label =~ s@[\/-]@_@g;
# Remove illegal characters
# Get the dirsize my $label = $dir;
my $dirsize = getSize($dir); $label =~ s@[\/-]@_@g;
# Get the label # Get the dirsize
my $label = niceLabelname($dir); my $dirsize = getSize($dir);
# Print name # Get the label
print "dir", $label, ".value ", $dirsize, ".0\n"; my $label = niceLabelname($dir);
}
} # Print name
print "dir", $label, ".value ", $dirsize, ".0\n";
}
# Function: getSize($dir) }
sub getSize
{ # Function: getSize($dir)
my($dir) = @_; sub getSize {
my ($dir) = @_;
# Get the size via `du`
my @dirsize = split(' ', `du -cb $dir | grep "total"`); # Get the size via `du`
return @dirsize[0]; my @dirsize = split( ' ', `du -cb $dir | grep "total" | tail -1 ` );
} return @dirsize[0];
# Remove illegal characters }
sub niceLabelname
{ # Remove illegal characters
my($label) = @_; sub niceLabelname {
my ($label) = @_;
$label =~ s@[\/-]@_@g;
return $label; $label =~ s@[\/-]@_@g;
} return $label;
}
exit 0;
exit 0;