#!/usr/bin/perl
# -*- perl -*-
#
# $Id: df_inode.in 1589 2008-04-12 23:37:11Z matthias $
#
# Plugin to monitor inode-usage. Rewrite based on original shell-based
# version
#
# Parameters understood:
#
#       config   (required)
#       autoconf (optional - used by munin-config)
#
# Environment
#       exclude: space separated list if fs types to exclude.
#                iso9660 by default
#
# $Id: df_inode.in 1589 2008-04-12 23:37:11Z matthias $
#
# Magic markers (optional - used by munin-config and installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf

use strict;
use Munin::Plugin;

# Read /proc/mounts
my %mounts;
open (MOUNTS,"/proc/mounts") or die "Could not /proc/mounts for reading.";
while (<MOUNTS>) {
    # Does perl really not have any shorthand for this? I guess it has.
    if ( /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/ ) {
	$mounts{$2}=$3;
    }
}
close MOUNTS;

my $exclude=$ENV{'exclude'} || 'none unknown iso9660 squashfs udf romfs ramfs';
my $warning=$ENV{'warning'} || 92;
my $critical=$ENV{'critical'} || 98;
my $dfopts = "-P -l -i ".join(' -x ',('',split('\s+',$exclude)));

sub print_values() {

    # Read from df
    open (DF,"df $dfopts |") or die "Could not open pipe from df: $!";
    <DF>; # Skip the header
    while (<DF>) {
	next if /\/\//;
	
	# Parse the output
	if ( /^(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)/ ) {
	    my $fs=$mounts{$6};
	    next if $fs eq "reiserfs";

	    my $ps=$5;
	    my $name=clean_fieldname($1);

	    $ps =~ s/\%//;

	    print $name .  ".value  " . $ps . "\n";
	}
    }
    close DF;
    die "Error executing df. Exit code $?\n" if $?;
}

if ( $ARGV[0] eq "autoconf" ) {
  if (`/usr/bin/perl $0` eq "" ) {
    print "no\n";
    exit 1;
  } else {
    print "yes\n";
    exit 0;
  }
}

if ( $ARGV[0] eq "config" ) {

    # The headers
    print "graph_title Inode 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 |") or die "Unable to open pipe from df: $!";
    <DF>; # Skip the header
    while (<DF>) {
	next if /\/\//;
	
	# Parse the output
	if ( /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/ ) {
	    my $fs=$mounts{$6};
	    next if $fs eq "reiserfs";

	    my $dir=$6;
	    my $name=clean_fieldname($1);

	    # Create and print labels
	    print "$name.label $dir\n";
	    print "$name.warning $warning\n";
	    print "$name.critical $critical\n";
	}
    }
    close DF;
    die "Error executing df. Exit code $?\n" if $?;
    exit 0;
}

print_values();
