1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-09-19 09:03:20 +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,7 +1,9 @@
#!/usr/bin/perl #!/usr/bin/perl
# -*- perl -*- # -*- perl -*-
# #
# ############################################################################## # #
##############################################################################
#
# #
# This munin plugin watches the sizes of the given directories. # This munin plugin watches the sizes of the given directories.
# @author Kevin Fischer # @author Kevin Fischer
@ -16,92 +18,92 @@
# Change the env.watchdirs-variable according to your wishes. # Change the env.watchdirs-variable according to your wishes.
# DONT FORGET TO RUN AS ROOT! # DONT FORGET TO RUN AS ROOT!
# #
# You can test this plugin by calling it with params "test" and your watchdirs: # You can test this plugin by calling it with params "test" and your watchdirs:
# ./dirsizes test /dir1,/tmp/dir2 # ./dirsizes test /dir1,/tmp/dir2
# #
# ############################################################################## # #
##############################################################################
#
use strict; use strict;
my @watchdirs; my @watchdirs;
# Normal mode or test mode? if ( exists $ARGV[0] and $ARGV[0] eq "test" ) {
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 # Split the watchdirs string
@watchdirs = split(",", $ENV{"watchdirs"}); @watchdirs = split( ",", $ARGV[1] );
} }
# Test mode else {
elsif(exists $ARGV[0] and $ARGV[0] eq "test")
{ # If no dirs are given, exit.
# Split the watchdirs string if ( !defined( $ENV{"watchdirs"} ) ) {
@watchdirs = split(",", $ARGV[1]); 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? # 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
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 # Munin basic info
foreach my $dir (@watchdirs) { print "graph_title Directory sizes\n";
# Remove illegal characters print "graph_args --base 1024 --lower-limit 0\n";
my $label = $dir; print "graph_vlabel directory size\n";
$label =~ s@[\/-]@_@g; print "graph_info Displays the sizes of all configured directories.\n";
print "graph_category disk\n";
print "graph_total Total\n";
# Print name # All available directories
print "dir", $label, ".label ", $dir, "\n"; 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 # Read request, output the directory sizes
else else {
{
# All available directories
foreach my $dir (@watchdirs) {
# Remove illegal characters
my $label = $dir;
$label =~ s@[\/-]@_@g;
# Get the dirsize # All available directories
my $dirsize = getSize($dir); foreach my $dir (@watchdirs) {
# Get the label # Remove illegal characters
my $label = niceLabelname($dir); my $label = $dir;
$label =~ s@[\/-]@_@g;
# Print name # Get the dirsize
print "dir", $label, ".value ", $dirsize, ".0\n"; my $dirsize = getSize($dir);
}
# Get the label
my $label = niceLabelname($dir);
# Print name
print "dir", $label, ".value ", $dirsize, ".0\n";
}
} }
# Function: getSize($dir) # Function: getSize($dir)
sub getSize sub getSize {
{ my ($dir) = @_;
my($dir) = @_;
# Get the size via `du` # Get the size via `du`
my @dirsize = split(' ', `du -cb $dir | grep "total"`); my @dirsize = split( ' ', `du -cb $dir | grep "total" | tail -1 ` );
return @dirsize[0]; return @dirsize[0];
} }
# Remove illegal characters
sub niceLabelname
{
my($label) = @_;
$label =~ s@[\/-]@_@g; # Remove illegal characters
return $label; sub niceLabelname {
my ($label) = @_;
$label =~ s@[\/-]@_@g;
return $label;
} }
exit 0; exit 0;