# File:    $Id$
# License: OSI Artistic License
#          http://www.opensource.org/licenses/artistic-license.php
# Author:  (c) 2010 Matthew Wall
#
# this map file catches output from any nagios plugin that emits performance
# data according to the standard format.

# avoid unuseful timeout and errors
/output:CHECK_NRPE: Socket timeout/
and return ('ignore');

/output:NRPE: Unable to read output/
and return ('ignore');

/output:CRITICAL - Socket timeout after/
and return ('ignore');

/output:Connection refused/
and return ('ignore');

/output:CRITICAL - Plugin timed out after/
and return ('ignore');

/output:.*plugin may be missing/
and return ('ignore');


# default rule.  if none of the other rules did anything, then check for
# perfdata that meets the standard format.
#
#   label=value[UOM];[warn];[crit];[min];[max]
#
# if units are KB, MB, TB, PB then convert to B
# if units are ms or us then convert to s
# if units is counter, then use type DERIVE (DS min will be 0)
#
# warn and crit have range format:
#
# 10         < 0 or > 10
# 10:        < 10
# ~:10       > 10
# 10:20      < 10 or > 20
# @10:20     >= 10 and <= 20
# 
(! @s || $#s < 0) and /perfdata:(.+)/ and do {
    my $pd = $1;
    while ( $pd =~ s/([^=]+)=(\S+)// ) {
        my ($n,$y) = ($1,$2);
        next if (!defined $n);
        $n =~ s/^\s+//g;
        $n =~ s/\s+$//g;
        $n =~ s/^\'//g;
        $n =~ s/\'$//g;
        $n =~ s/\'\'/\'/g;
        next if ($n eq q());
        my ($x,$w,$c,$min,$max) = split /;/, $y;
        my ($v,$u);
        if ($x =~ /([-0-9.]+)([^-0-9.]+)/) {
            $v = $1; $u = $2;
        } else {
            $v = $x;
        }
        my ($wlo, $whi);
        if (defined $w && $w ne q()) {
            if ($w =~ /([-0-9.]+):([-0-9.]+)/) {
                $wlo = $1; $whi = $2; $w = q();
            } elsif ($w =~ /([-0-9.]+):/) {
                $whi = $1; $w = q();
            } elsif ($w =~ /:([-0-9.]+)/) {
                $wlo = $1; $w = q();
            }
        }
        my ($clo, $chi);
        if (defined $c && $c ne q()) {
            if ($c =~ /([-0-9.]+):([-0-9.]+)/) {
                $clo = $1; $chi = $2; $c = q();
            } elsif ($c =~ /([-0-9.]+):/) {
                $chi = $1; $c = q();
            } elsif ($c =~ /:([-0-9.]+)/) {
                $clo = $1; $c = q();
            }
        }
        my $t = 'GAUGE';
        if (defined $u) {
            my $mult = 1;
            if ($u eq 'c') {
                $t = 'DERIVE';
            } elsif ($u eq 's') {
                $mult = 1;
            } elsif ($u eq 'ms') {
                $mult = 1 / 1_000;
            } elsif ($u eq 'us') {
                $mult = 1 / 1_000_000;
            } elsif ($u eq 'B') {
                $mult = 1;
            } elsif ($u eq 'KB' || $u eq 'K' || $u eq 'kB') {
                $mult = 1024;
            } elsif ($u eq 'MB' || $u eq 'M' || $u eq 'mB') {
                $mult = 1024 * 1024;
            } elsif ($u eq 'GB' || $u eq 'G' || $u eq 'gB') {
                $mult = 1024 * 1024 * 1024;
            } elsif ($u eq 'TB' || $u eq 'T' || $u eq 'tB') {
                $mult = 1024 * 1024 * 1024 * 1024;
            } elsif ($u eq 'PB' || $u eq 'P' || $u eq 'pB') {
                $mult = 1024 * 1024 * 1024 * 1024 * 1024;
            }
            if ($mult != 1) {
                if ( defined $v && $v ne q()) { $v *= $mult; }
                if ( defined $w && $w ne q()) { $w *= $mult; }
                if ( defined $c && $c ne q()) { $c *= $mult; }
                if ( defined $min && $min ne q()) { $min *= $mult; }
                if ( defined $max && $max ne q()) { $max *= $mult; }
                if ( defined $wlo && $wlo ne q()) { $wlo *= $mult; }
                if ( defined $whi && $whi ne q()) { $whi *= $mult; }
                if ( defined $clo && $clo ne q()) { $clo *= $mult; }
                if ( defined $chi && $chi ne q()) { $chi *= $mult; }
            }
        }
        my @x;
        push @x, [ 'data', $t, $v ] if defined $v && $v ne q();
        push @x, [ 'warn', $t, $w ] if defined $w && $w ne q();
        push @x, [ 'crit', $t, $c ] if defined $c && $c ne q();
        push @x, [ 'min', $t, $min ] if defined $min && $min ne q();
        push @x, [ 'max', $t, $max ] if defined $max && $max ne q();
        push @x, [ 'warn_lo', $t, $wlo ] if defined $wlo && $wlo ne q();
        push @x, [ 'warn_hi', $t, $whi ] if defined $whi && $whi ne q();
        push @x, [ 'crit_lo', $t, $clo ] if defined $clo && $clo ne q();
        push @x, [ 'crit_hi', $t, $chi ] if defined $chi && $chi ne q();
        push @s, [ $n, @x ] if @x;
    }
};
