Projet

Général

Profil

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

root / plugins / disk / du-2 @ 8589c6df

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