Révision b6c93cc7
fixed bug if a dir/filename contains the text total
| plugins/other/dirsizes | ||
|---|---|---|
| 1 |
#!/usr/bin/perl |
|
| 2 |
# -*- perl -*- |
|
| 3 |
# |
|
| 4 |
# ############################################################################## # |
|
| 5 |
# |
|
| 6 |
# This munin plugin watches the sizes of the given directories. |
|
| 7 |
# @author Kevin Fischer |
|
| 8 |
# @version 2010/08/05 |
|
| 9 |
# @website http://kevin-fischer.de |
|
| 10 |
# |
|
| 11 |
# Copy this to your node's config file (default: plugin-conf.d/munin-node): |
|
| 12 |
# [dirsizes] |
|
| 13 |
# user root |
|
| 14 |
# env.watchdirs /var/www,/tmp |
|
| 15 |
# |
|
| 16 |
# Change the env.watchdirs-variable according to your wishes. |
|
| 17 |
# DONT FORGET TO RUN AS ROOT! |
|
| 18 |
# |
|
| 19 |
# You can test this plugin by calling it with params "test" and your watchdirs: |
|
| 20 |
# ./dirsizes test /dir1,/tmp/dir2 |
|
| 21 |
# |
|
| 22 |
# ############################################################################## # |
|
| 23 |
|
|
| 24 |
use strict; |
|
| 25 |
|
|
| 26 |
my @watchdirs; |
|
| 27 |
|
|
| 28 |
# Normal mode or test mode? |
|
| 29 |
if(exists $ARGV[0] and $ARGV[0] ne "test") |
|
| 30 |
{
|
|
| 31 |
# If no dirs are given, exit. |
|
| 32 |
if(! defined($ENV{"watchdirs"}))
|
|
| 33 |
{
|
|
| 34 |
die "No directories given! See the manual at top of this plugin file."; |
|
| 35 |
} |
|
| 36 |
|
|
| 37 |
# Split the watchdirs string |
|
| 38 |
@watchdirs = split(",", $ENV{"watchdirs"});
|
|
| 39 |
} |
|
| 40 |
# Test mode |
|
| 41 |
elsif(exists $ARGV[0] and $ARGV[0] eq "test") |
|
| 42 |
{
|
|
| 43 |
# Split the watchdirs string |
|
| 44 |
@watchdirs = split(",", $ARGV[1]);
|
|
| 45 |
} |
|
| 46 |
|
|
| 47 |
# Config or read request? |
|
| 48 |
if(exists $ARGV[0] and $ARGV[0] eq "config") |
|
| 49 |
{
|
|
| 50 |
# Munin basic info |
|
| 51 |
print "graph_title Directory sizes\n"; |
|
| 52 |
print "graph_args --base 1024 --lower-limit 0\n"; |
|
| 53 |
print "graph_vlabel directory size\n"; |
|
| 54 |
print "graph_info Displays the sizes of all configured directories.\n"; |
|
| 55 |
print "graph_category disk\n"; |
|
| 56 |
print "graph_total Total\n"; |
|
| 57 |
|
|
| 58 |
# All available directories |
|
| 59 |
foreach my $dir (@watchdirs) {
|
|
| 60 |
# Remove illegal characters |
|
| 61 |
my $label = $dir; |
|
| 62 |
$label =~ s@[\/-]@_@g; |
|
| 63 |
|
|
| 64 |
# Print name |
|
| 65 |
print "dir", $label, ".label ", $dir, "\n"; |
|
| 66 |
} |
|
| 67 |
} |
|
| 68 |
# Read request, output the directory sizes |
|
| 69 |
else |
|
| 70 |
{
|
|
| 71 |
# All available directories |
|
| 72 |
foreach my $dir (@watchdirs) {
|
|
| 73 |
# Remove illegal characters |
|
| 74 |
my $label = $dir; |
|
| 75 |
$label =~ s@[\/-]@_@g; |
|
| 76 |
|
|
| 77 |
# Get the dirsize |
|
| 78 |
my $dirsize = getSize($dir); |
|
| 79 |
|
|
| 80 |
# Get the label |
|
| 81 |
my $label = niceLabelname($dir); |
|
| 82 |
|
|
| 83 |
# Print name |
|
| 84 |
print "dir", $label, ".value ", $dirsize, ".0\n"; |
|
| 85 |
} |
|
| 86 |
} |
|
| 87 |
|
|
| 88 |
|
|
| 89 |
# Function: getSize($dir) |
|
| 90 |
sub getSize |
|
| 91 |
{
|
|
| 92 |
my($dir) = @_; |
|
| 93 |
|
|
| 94 |
# Get the size via `du` |
|
| 95 |
my @dirsize = split(' ', `du -cb $dir | grep "total"`);
|
|
| 96 |
return @dirsize[0]; |
|
| 97 |
} |
|
| 98 |
# Remove illegal characters |
|
| 99 |
sub niceLabelname |
|
| 100 |
{
|
|
| 101 |
my($label) = @_; |
|
| 102 |
|
|
| 103 |
$label =~ s@[\/-]@_@g; |
|
| 104 |
return $label; |
|
| 105 |
} |
|
| 106 |
|
|
| 107 |
exit 0; |
|
| 1 |
#!/usr/bin/perl |
|
| 2 |
# -*- perl -*- |
|
| 3 |
# |
|
| 4 |
# |
|
| 5 |
############################################################################## |
|
| 6 |
# |
|
| 7 |
# |
|
| 8 |
# This munin plugin watches the sizes of the given directories. |
|
| 9 |
# @author Kevin Fischer |
|
| 10 |
# @version 2010/08/05 |
|
| 11 |
# @website http://kevin-fischer.de |
|
| 12 |
# |
|
| 13 |
# Copy this to your node's config file (default: plugin-conf.d/munin-node): |
|
| 14 |
# [dirsizes] |
|
| 15 |
# user root |
|
| 16 |
# env.watchdirs /var/www,/tmp |
|
| 17 |
# |
|
| 18 |
# Change the env.watchdirs-variable according to your wishes. |
|
| 19 |
# DONT FORGET TO RUN AS ROOT! |
|
| 20 |
# |
|
| 21 |
# You can test this plugin by calling it with params "test" and your watchdirs: |
|
| 22 |
# ./dirsizes test /dir1,/tmp/dir2 |
|
| 23 |
# |
|
| 24 |
# |
|
| 25 |
############################################################################## |
|
| 26 |
# |
|
| 27 |
|
|
| 28 |
use strict; |
|
| 29 |
my @watchdirs; |
|
| 30 |
|
|
| 31 |
if ( exists $ARGV[0] and $ARGV[0] eq "test" ) {
|
|
| 32 |
|
|
| 33 |
# Split the watchdirs string |
|
| 34 |
@watchdirs = split( ",", $ARGV[1] ); |
|
| 35 |
} |
|
| 36 |
else {
|
|
| 37 |
|
|
| 38 |
# If no dirs are given, exit. |
|
| 39 |
if ( !defined( $ENV{"watchdirs"} ) ) {
|
|
| 40 |
die "No directories given! See the manual at top of this plugin file."; |
|
| 41 |
} |
|
| 42 |
|
|
| 43 |
# Split the watchdirs string |
|
| 44 |
@watchdirs = split( ",", $ENV{"watchdirs"} );
|
|
| 45 |
} |
|
| 46 |
|
|
| 47 |
# Config or read request? |
|
| 48 |
if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {
|
|
| 49 |
|
|
| 50 |
# Munin basic info |
|
| 51 |
print "graph_title Directory sizes\n"; |
|
| 52 |
print "graph_args --base 1024 --lower-limit 0\n"; |
|
| 53 |
print "graph_vlabel directory size\n"; |
|
| 54 |
print "graph_info Displays the sizes of all configured directories.\n"; |
|
| 55 |
print "graph_category disk\n"; |
|
| 56 |
print "graph_total Total\n"; |
|
| 57 |
|
|
| 58 |
# All available directories |
|
| 59 |
foreach my $dir (@watchdirs) {
|
|
| 60 |
|
|
| 61 |
# Remove illegal characters |
|
| 62 |
my $label = $dir; |
|
| 63 |
$label =~ s@[\/-]@_@g; |
|
| 64 |
|
|
| 65 |
# Print name |
|
| 66 |
print "dir", $label, ".label ", $dir, "\n"; |
|
| 67 |
} |
|
| 68 |
} |
|
| 69 |
|
|
| 70 |
# Read request, output the directory sizes |
|
| 71 |
else {
|
|
| 72 |
|
|
| 73 |
# All available directories |
|
| 74 |
foreach my $dir (@watchdirs) {
|
|
| 75 |
|
|
| 76 |
# Remove illegal characters |
|
| 77 |
my $label = $dir; |
|
| 78 |
$label =~ s@[\/-]@_@g; |
|
| 79 |
|
|
| 80 |
# Get the dirsize |
|
| 81 |
my $dirsize = getSize($dir); |
|
| 82 |
|
|
| 83 |
# Get the label |
|
| 84 |
my $label = niceLabelname($dir); |
|
| 85 |
|
|
| 86 |
# Print name |
|
| 87 |
print "dir", $label, ".value ", $dirsize, ".0\n"; |
|
| 88 |
} |
|
| 89 |
} |
|
| 90 |
|
|
| 91 |
# Function: getSize($dir) |
|
| 92 |
sub getSize {
|
|
| 93 |
my ($dir) = @_; |
|
| 94 |
|
|
| 95 |
# Get the size via `du` |
|
| 96 |
my @dirsize = split( ' ', `du -cb $dir | grep "total" | tail -1 ` ); |
|
| 97 |
return @dirsize[0]; |
|
| 98 |
} |
|
| 99 |
|
|
| 100 |
# Remove illegal characters |
|
| 101 |
sub niceLabelname {
|
|
| 102 |
my ($label) = @_; |
|
| 103 |
|
|
| 104 |
$label =~ s@[\/-]@_@g; |
|
| 105 |
return $label; |
|
| 106 |
} |
|
| 107 |
|
|
| 108 |
exit 0; |
|
| 109 |
|
|
Formats disponibles : Unified diff