Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / disk / dirsizes @ ef960abc

Historique | Voir | Annoter | Télécharger (2,43 ko)

1 b6c93cc7 ian dobson
#!/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;