Projet

Général

Profil

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

root / plugins / disk / du-2 @ ef960abc

Historique | Voir | Annoter | Télécharger (5,3 ko)

1 e430a3e5 ldidry
#!/usr/bin/perl
2
# vim: set filetype=perl sw=4 tabstop=4 expandtab smartindent: #
3
4
=head1 NAME
5
6
  du - Plugin to monitor multiple directories size
7
8 8da25e68 Luc Didry
=head1 AUTHOR AND COPYRIGHT
9 e430a3e5 ldidry
10 8da25e68 Luc Didry
  Copyright 2011-2012 Luc Didry <luc AT didry.org>
11 e430a3e5 ldidry
12
=head1 HOWTO CONFIGURE AND USE :
13
14
=over
15
16
=item - /etc/munin/plugin-conf.d/du_
17
18
     [du]
19
     user root
20
     env.interval 20 # INTERVAL OF DU POLLING IN MINUTES
21
     env.dirs /home/foo /home/bar # DIRECTORIES TO POLL
22
     env.suppr /home/ # PLEASE USE \# INSTEAD OF #
23
     timeout 900 # 15 MINUTES IN SECONDS
24
25
=item - /etc/munin/plugins-enabled
26
27
     ln -svf ../plugins-available/site/du
28
29
30
=item - restart Munin node
31
32
     sudo killall -TERM munin-node
33
34
=back
35
36
=head1 CREDITS
37
38
  Based on the 'du_multidirs-v2' initially written in Bash by Christian Kujau <lists@nerdbynature.de> and modified by dano229.
39
  This script was based on the 'homedirs' plugin, initially written in Perl by Philipp Gruber <pg@flupps.net>
40
41
=head1 MAGIC MARKERS
42
43
  #%# family=auto
44
  #%# capabilities=autoconf
45
46 8da25e68 Luc Didry
=head1 LICENSE
47
48
    This program is free software: you can redistribute it and/or modify
49
    it under the terms of the GNU General Public License as published by
50
    the Free Software Foundation, either version 3 of the License, or
51
    any later version.
52
53
    This program is distributed in the hope that it will be useful,
54
    but WITHOUT ANY WARRANTY; without even the implied warranty of
55
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
56
    GNU General Public License for more details.
57
58
    You should have received a copy of the GNU General Public License
59
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
60
61 e430a3e5 ldidry
=cut
62
63
use warnings;
64
use strict;
65
use Munin::Plugin;
66
use POSIX qw(setsid);
67
68
my $PLUGIN_NAME = "du";
69
my $CACHEFILE="$Munin::Plugin::pluginstatedir/du.cache";
70
my $TEMPFILE="$Munin::Plugin::pluginstatedir/du.tmp";
71
my $LOCKFILE="$Munin::Plugin::pluginstatedir/du.lock";
72
my $TIMEFILE="$Munin::Plugin::pluginstatedir/du.time";
73
74
##### autoconf
75
if( (defined $ARGV[0]) && ($ARGV[0] eq "autoconf") ) {
76
    print "yes\n";
77
    ## Done !
78
    munin_exit_done();
79
}
80
81
## In the parent, it's just a regular munin plugin which reads a file with the infos
82
##### config
83
if( (defined $ARGV[0]) && ($ARGV[0] eq "config") ) {
84
    print "graph_title Directory usage\n";
85
    print "graph_args --base 1024 -l 1\n";
86
    print "graph_vlabel Bytes\n";
87
    print "graph_category disk\n";
88
    print "graph_total total\n";
89
    print "graph_info This graph shows the size of several directories\n";
90
91
    my $foo = 0;
92
    open (FILE, "<", $CACHEFILE) or munin_exit_fail();
93
    while(defined (my $bar = <FILE>)) {
94
        if ($bar =~ m/(\d+)\s+(.+)/) {
95
            my $dir = $2;
96
            clean_path(\$dir);
97
            print "$dir.label $dir\n";
98
            if ($foo++) {
99
                print "$dir.draw STACK\n";
100
            } else {
101
                print "$dir.draw AREA\n";
102
            }
103
        }
104
    }
105
    close(FILE);
106
    ## Done !
107
    munin_exit_done();
108
}
109
110
##### fetch
111
open (FILE, "<", $CACHEFILE) or munin_exit_fail();
112
while(defined (my $foo = <FILE>)) {
113
    if ($foo =~ m/(\d+)\s+(.+)/) {
114
        my ($field, $value) = ($2, $1);
115
        clean_path(\$field);
116 b6f9a54a Luc Didry
        print $field, ".value ", $value, "\n";
117 e430a3e5 ldidry
    }
118
}
119
close(FILE);
120
daemonize();
121
122
#
123
##
124 b6f9a54a Luc Didry
### PUBLIC FONCTIONS
125 e430a3e5 ldidry
###############################################################################
126
## Used to create the fork
127
sub daemonize {
128
    chdir '/'               or die "Can't chdir to /: $!";
129
    defined(my $pid = fork) or die "Can't fork: $!";
130
    munin_exit_done() if $pid;
131
    open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
132
    open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
133
    open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
134
    setsid                  or die "Can't start a new session: $!";
135
    ## In the child, let's get the du infos if necessary
136
    if (cache_is_too_old() && du_not_running()) {
137
        my $dirs = $ENV{dirs};
138
        system("touch $LOCKFILE; du -sb $dirs > $TEMPFILE; cat $TEMPFILE > $CACHEFILE; rm $LOCKFILE; date +%s > $TIMEFILE;");
139
    }
140
    exit;
141
} ## daemonize
142
143
## Used to remove the beginning of the paths if wanted
144
sub clean_path {
145
    my ($path) = @_;
146
    if (defined $ENV{suppr}) {
147
        my $pattern = $ENV{suppr};
148
        $$path =~ s#^($pattern)##;
149
    }
150
} ## clean_path
151
152 b6f9a54a Luc Didry
## Do you really need I told you what this functions are going to check ?
153 e430a3e5 ldidry
sub cache_is_too_old {
154
    return 1 if (! -e $TIMEFILE);
155
    my ($time) = `cat $TIMEFILE`;
156
    chomp $time;
157
    return 1 if ( (time - $time) > ($ENV{interval}*60) );
158
    return 0;
159
} ## cache_is_too_old
160
161
sub du_not_running {
162 a2b3e009 Luc Didry
    if (-e $LOCKFILE) {
163
        my ($time) = `cat $TIMEFILE`;
164
        chomp $time;
165
        if ( (time - $time) > ($ENV{interval}*60*60) ) {
166
            # The cache is really old (60xinterval) => Maybe the lockfile wasn't properly deleted.
167
            # Let's delete it.
168
            system("rm $LOCKFILE;");
169
            return 1;
170
        } else {
171
	        return 0;
172
        }
173
    } else {
174
        return 1;
175
    }
176 e430a3e5 ldidry
}
177 b6f9a54a Luc Didry
178 e430a3e5 ldidry
sub munin_exit_done {
179
    __munin_exit(0);
180
} ## sub munin_exit_done
181
182
sub munin_exit_fail {
183
    __munin_exit(1);
184
} ## sub munin_exit_fail
185
186
#
187
##
188 b6f9a54a Luc Didry
### INTERNALS FONCTIONS
189 e430a3e5 ldidry
###############################################################################
190
sub __munin_exit {
191
    my $exitcode = shift;
192
    exit($exitcode) if(defined $exitcode);
193
    exit(1);
194
} ## sub __munin_exit