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