]> git.somenet.org - root/pub/somesible.git/blob - roles/base/munin-node/files/default/plugins.somesible/df
roles/base/munin-node/files
[root/pub/somesible.git] / roles / base / munin-node / files / default / plugins.somesible / df
1 #!/usr/bin/perl -w
2 # -*-  perl -*-
3
4 use strict;
5 use warnings;
6
7 =head1 NAME
8
9 df - Munin plugin to monitor disk usage
10
11 =head1 APPLICABLE SYSTEMS
12
13 Every Linux system with df installed.
14
15 =head1 CONFIGURATION
16
17 The plugin excludes per default the following special, read-only or
18 dynamically allocating file systems from graphing:
19
20   none unknown rootfs iso9660 squashfs udf romfs ramfs debugfs cgroup_root
21
22 To change this set the environment variable "exclude" with a list of
23 space separated fs types.  The environment variables "warning" and
24 "critical" sets the percentage from which Munin starts to warn about
25 the disk usage.
26
27 This configuration snipplet is an example with the defaults:
28
29   [df]
30     env.exclude none unknown rootfs iso9660 squashfs udf romfs ramfs debugfs cgroup_root devtmpfs
31     env.warning 92
32     env.critical 98
33
34 Put it in a file in /etc/munin/plugin-conf.d/ and restart the munin-node.
35
36 You may specify filesystem specific warning and critical levels:
37
38     env._dev_sda2_warning 98
39     env._dev_sda2_critical 99
40
41 Devices can be explicitly included or excluded based on their mountpoint or
42 device name using the include_re and exclude_re environment variables.  These
43 environment variables are parsed as whitespace separated regular expressions.
44 For example, if you wish to ignore the filesystem on /dev/sda2 and all
45 filesystems mounted under /var except /var/tmp, these rules would achieve this:
46
47     env.include_re ^/var/tmp$
48     env.exclude_re /dev/sda2 ^/var/
49
50 Please note that these expressions are tried against both mountpoints and
51 device names, therefore broad matches could potentially filter out desired
52 devices.  Anchoring is also useful for avoiding false positives (as seen in the
53 example), but not strictly necessary.  Testing with munin-run is always a good
54 idea.
55
56 Also note that a mountpoint that is excluded by filesystem type but included by
57 RE will not be included.
58
59 =head1 USAGE
60
61 Link this plugin to /etc/munin/plugins/ and restart the munin-node.
62
63 =head1 MAGIC MARKERS
64
65   #%# family=auto
66   #%# capabilities=autoconf
67
68 =head1 BUGS
69
70 Uses device names instead of mount points to identify mounted
71 filesystems.
72
73 =head1 AUTHOR
74
75 Ingvar Hagelund
76
77 =head1 LICENSE
78
79 GPLv2
80
81 =cut
82
83 use Munin::Plugin;
84
85 # For these devices use the mount point, the device is useless
86 my %usemntpt = ( tmpfs => 1, none => 1, udev => 1, simfs => 1 );
87
88 my $exclude = $ENV{'exclude'} || 'none unknown rootfs iso9660 squashfs udf romfs ramfs debugfs cgroup_root devtmpfs';
89 my $dfopts  = "-P -l ".join(' -x ',('',split('\s+',$exclude)));
90
91 my $mode = ($ARGV[0] or "print");
92
93 # Compile REs from env
94 my @include_re;
95 if (defined $ENV{include_re}) {
96     foreach my $re (split m{\s+}, $ENV{include_re}) {
97         push @include_re, qr/$re/;
98     }
99 }
100 my @exclude_re;
101 if (defined $ENV{exclude_re}) {
102     foreach my $re (split m{\s+}, $ENV{exclude_re}) {
103         push @exclude_re, qr/$re/;
104     }
105 }
106
107 sub skip {
108     my $name = shift;
109     my $mountpt = shift;
110
111     foreach my $re (@include_re) {
112         return 0 if ($name =~ $re or $mountpt =~ $re);
113     }
114
115     foreach my $re (@exclude_re) {
116         return 1 if ($name =~ $re or $mountpt =~ $re);
117     }
118
119     return 0;
120 }
121
122 if ($mode eq 'autoconf' ) {
123     if (`/usr/bin/perl $0` eq '' ) {
124         print "no (no devices to monitor)\n";
125     } else {
126         print "yes\n";
127     }
128     exit 0;
129 }
130
131 if ($mode eq 'config' ) {
132     # The headers
133     print "graph_title Disk usage in percent\n";
134     print "graph_args --upper-limit 100 -l 0\n";
135     print "graph_vlabel %\n";
136     print "graph_scale no\n";
137     print "graph_category disk\n";
138 }
139
140 # Read from df
141 open (DF,"df $dfopts 2>/dev/null | tail -n+2 | sort -k6 |") or die "Unable to open pipe from df: $!";
142 #<DF>; # Skip the header
143 while (<DF>) {
144     next if m{//};
145
146     # Parse the output
147     my ($name, undef, $used, $avail, undef, $mountpt, undef) = split(/\s+/, $_, 7);
148
149     next if skip($name, $mountpt);
150
151     # Calculate percentage used
152     my $ps = 0;
153     $ps = ($used / ($used+$avail)) * 100 if $used;
154
155     $name = $mountpt; # if defined($usemntpt{$name}) && $usemntpt{$name};
156     $name = clean_fieldname($name);
157
158     if($mode eq 'config') {
159         print $name, ".label ", $mountpt, "\n";
160         print_thresholds($name,undef,undef,92,98);
161     } else {
162         print $name, ".value ", $ps, "\n";
163     }
164 }
165 close DF;
166
167 # vim: ft=perl : sw=4 : ts=4 : et