#!@@PERL@@ -w
# -*-  perl -*-

=head1 NAME

df - Munin plugin to monitor disk usage

=head1 APPLICABLE SYSTEMS

Every Linux system with df installed.

=head1 CONFIGURATION

The plugin excludes per default the following special, read-only or
dynamically allocating file systems from graphing:

  none unknown rootfs iso9660 squashfs udf romfs ramfs debugfs cgroup_root

To change this set the environment variable "exclude" with a list of
space separated fs types.  The environment variables "warning" and
"critical" sets the percentage from which Munin starts to warn about
the disk usage.

This configuration snipplet is an example with the defaults:

  [df]
    env.exclude none unknown rootfs iso9660 squashfs udf romfs ramfs debugfs cgroup_root devtmpfs
    env.warning 92
    env.critical 98

Put it in a file in @@CONFDIR@@/plugin-conf.d/ and restart the munin-node.

You may specify filesystem specific warning and critical levels:

    env._dev_sda2_warning 98
    env._dev_sda2_critical 99

Devices can be explicitly included or excluded based on their mountpoint or
device name using the include_re and exclude_re environment variables.  These
environment variables are parsed as whitespace separated regular expressions.
For example, if you wish to ignore the filesystem on /dev/sda2 and all
filesystems mounted under /var except /var/tmp, these rules would achieve this:

    env.include_re ^/var/tmp$
    env.exclude_re /dev/sda2 ^/var/

Please note that these expressions are tried against both mountpoints and
device names, therefore broad matches could potentially filter out desired
devices.  Anchoring is also useful for avoiding false positives (as seen in the
example), but not strictly necessary.  Testing with munin-run is always a good
idea.

Also note that a mountpoint that is excluded by filesystem type but included by
RE will not be included.

=head1 USAGE

Link this plugin to @@CONFDIR@@/plugins/ and restart the munin-node.

=head1 MAGIC MARKERS

  #%# family=auto
  #%# capabilities=autoconf

=head1 BUGS

Uses device names instead of mount points to identify mounted
filesystems.

=head1 VERSION

  $Id$

=head1 AUTHOR

Ingvar Hagelund

=head1 LICENSE

GPLv2

=cut

use strict;
use Munin::Plugin;

# For these devices use the mount point, the device is useless
my %usemntpt = ( tmpfs => 1, none => 1, udev => 1, simfs => 1 );

my $exclude = $ENV{'exclude'} || 'none unknown rootfs iso9660 squashfs udf romfs ramfs debugfs cgroup_root devtmpfs';
my $dfopts  = "-P -l ".join(' -x ',('',split('\s+',$exclude)));

my $mode = ($ARGV[0] or "print");

# Compile REs from env
my @include_re;
if (defined $ENV{include_re}) {
    foreach my $re (split m{\s+}, $ENV{include_re}) {
        push @include_re, qr/$re/;
    }
}
my @exclude_re;
if (defined $ENV{exclude_re}) {
    foreach my $re (split m{\s+}, $ENV{exclude_re}) {
        push @exclude_re, qr/$re/;
    }
}

sub skip {
    my $name = shift;
    my $mountpt = shift;

    foreach my $re (@include_re) {
        return 0 if ($name =~ $re or $mountpt =~ $re);
    }

    foreach my $re (@exclude_re) {
        return 1 if ($name =~ $re or $mountpt =~ $re);
    }

    return 0;
}

if ($mode eq 'autoconf' ) {
    if (`@@PERL@@ $0` eq '' ) {
        print "no (no devices to monitor)\n";
    } else {
        print "yes\n";
    }
    exit 0;
}

if ($mode eq 'config' ) {
    # The headers
    print "graph_title Disk usage in percent\n";
    print "graph_args --upper-limit 100 -l 0\n";
    print "graph_vlabel %\n";
    print "graph_scale no\n";
    print "graph_category disk\n";
}

# Read from df
open (DF,"df $dfopts 2>/dev/null |") or die "Unable to open pipe from df: $!";
<DF>; # Skip the header
while (<DF>) {
    next if m{//};

    # Parse the output
    my ($name, undef, $used, $avail, undef, $mountpt, undef) = split(/\s+/, $_, 7);

    next if skip($name, $mountpt);

    # Calculate percentage used
    my $ps = 0;
    $ps = ($used / ($used+$avail)) * 100 if $used;

    $name = $mountpt if defined($usemntpt{$name}) && $usemntpt{$name};
    $name = clean_fieldname($name);

    if($mode eq 'config') {
        print $name, ".label ", $mountpt, "\n";
        print_thresholds($name,undef,undef,92,98);
    } else {
        print $name, ".value ", $ps, "\n";
    }
}
close DF;

# vim: ft=perl : sw=4 : ts=4 : et
