#!@@PERL@@ -w
# -*- cperl -*-

=head1 NAME

bdf - Munin plugin to monitor disk inode usage on an HP-UX machine

=head1 APPLICABLE SYSTEMS

Any HP-UX system with commands '/usr/bin/bdf' and '/usr/sbin/fstyp'

=head1 CONFIGURATION

The plugin must be run as the root user to be able to determine
the filesystem type (VXFS, HFS, etc.):

  [bdf*]
    user root

=head1 INTERPRETATION

The plugin shows disk inode usage on HP-UX systems as reported by
the command 'bdf'.

Only locally mounted VXFS or HFS filesystems are reported.

The plugin aims to be a functional equivalent to the Linux df_inode plugin.

=head1 MAGIC MARKERS

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

=head1 VERSION

  $Id$

=head1 BUGS

None known

=head1 AUTHOR

Chris Gardner

=head1 LICENSE

GPLv2

=cut

use Munin::Plugin;
use strict;
$ENV{'PATH'} = '/bin:/usr/bin:/sbin:/usr/sbin';

if(defined($ARGV[0]) and $ARGV[0] eq "autoconf") {
    if(-x '/usr/bin/bdf' && -x '/sbin/fstyp') {
	print "yes\n";
	exit 0;
    } else {
	print "no (no bdf and/or fstyp executable)\n";
	exit 0;
    }
}

# `bdf -i` --> [0]=Filesystem, [1]=kbytes, [2]=used, [3]=avail, [4]=%used, [5]=iused, [6]=ifree, [7]=%iuse, [8]=Mounted on
# Just 1 round of `bdf` is simpler but picks up CDFS cruft. bdf can't accept multiple FS types in 1 command.
my @bdf_hfs = `/usr/bin/bdf -ilt hfs`;
shift(@bdf_hfs);
my @bdf_vxfs = `/usr/bin/bdf -ilt vxfs`;
shift(@bdf_vxfs);
push(my @bdf, (@bdf_hfs, @bdf_vxfs)); # Recombine `bdf` for each FS type.
my $i = 0;
foreach(@bdf) {
    chomp;
    my @cols = split(/\s+/);
    if($#cols == 0) { # `bdf` line wraps if device name > 18 chars. This restores 1 array element per filesystem.
	chomp($_ .= $bdf[$i+1]);
	splice(@bdf, $i+1, 1);
    }
    $i++;
}

my $maxlabel = 20;

if(defined($ARGV[0]) and $ARGV[0] eq "config") {
    print "graph_title Inode usage in percent\n";
    print "graph_args --upper-limit 100 -l 0\n";
    print "graph_vlabel %\n";
    print "graph_category disk\n";
    print "graph_info This graph shows disk usage on the machine.\n";
    foreach(@bdf) {
	(my $fs, my $mount) = (split(/\s+/))[0,8]; # See top comment for array slice description.
	chomp(my $fs_type = `/sbin/fstyp $fs`);
	(my $name = $fs) =~ s|/|_|g;
	print "$name.label ";
	if(length($mount) > $maxlabel) {
	    print "..." . substr($mount, -$maxlabel+3) . "\n";
	} else {
	    print "$mount\n";
	}
	print "$name.info $mount ($fs_type) -> $fs\n";
	print "$name.warning 92\n";
	print "$name.critical 98\n";
    }
    exit 0;
}

foreach(@bdf) {
    (my $fs, my $pct_used) = (split(/\s+/))[0,7];
    (my $name = $fs) =~ s|/|_|g;
    $pct_used =~ s/%//g;
    print "$name.value $pct_used\n";
}

# vim:syntax=perl
