#!/usr/bin/perl
#
# Copyright (C) 2009 Andrew Chadwick, University of Oxford <andrew.chadwick@ceu.ox.ac.uk>
# Based on work by Rackspace US, Inc. <http://www.rackspace.com>, (C) 2008.
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
#
#
# This plugin will graph the power consumption of a Dell PowerEdge Server
# via the omreport tool. It has been tested on the following chassis:
#
#   PowerEdge R905
#
# To enable, create a link in your plugins directory to wherever you
# installed this file, e.g.:
#
#   cd /etc/munin/plugins
#   ln -s /usr/share/munin/plugins/omreport_pwrmon_power
#
# Configuration parameters for /etc/munin/plugin-conf.d/munin-node
#
# [omreport_*]
#   user         - User that has permissions to run the omreport binary
#   env.omreport - Path to the omreport binary
#
# Parameters:
#
#   config
#   autoconf
#
# Author: Andrew Chadwick <andrew.chadwick@ceu.ox.ac.uk>
# Revision: 0.1  2008-01-28
#
#%# family=auto
#%# capabilities=autoconf


use strict;
my $omreport = $ENV{"omreport"} || "/usr/bin/omreport";

if ($ARGV[0] && $ARGV[0] eq "autoconf") {
    if (-f $omreport) {
        print "yes\n";
    }
    else {
        print "no ($omreport does not exist)\n";
        exit(1);
    }
}
else {
    my @cmd = ($omreport, qw{chassis pwrmonitoring});
    my ($index, %val);
    my $pid = open(my $cmd_out, '-|', @cmd) or die "@cmd: $!\n";
    defined $pid or die "fork() failed: $!\n";
    my $amperage_idx = 0;
    while (<$cmd_out>) {
        s/\s+/\040/g;
        s/\s+$//;
        s/^\s+//;

        /^Power\s+Consumption\b/ .. /^Amperage\b/ or next;

        my ($field, $value) = split(/\s+\:\s+/, $_);
        if ($field eq 'Index') {
            $index = "power_${value}";
            $val{$index} = {};
        }
        elsif ($field eq 'Probe Name') {
            $val{$index}{label} = $value;
        }
        elsif ($field eq 'Reading') {
            $value =~ s{\s+W$}{};
            $value =~ s{\[N/A\]}{}g;
            $val{$index}{value} = $value;
        }
        elsif ($field eq 'Warning Threshold') {
            $value =~ s{\s+W$}{};
            $value =~ s{\[N/A\]}{}g;
            $val{$index}{warning} = $value;
        }
        elsif ($field eq 'Failure Threshold') {
            $value =~ s{\s+W$}{};
            $value =~ s{\[N/A\]}{}g;
            $val{$index}{critical} = $value;
        }
    }
    close $cmd_out;

    if ($ARGV[0] && $ARGV[0] eq "config") {
        print "graph_title OpenManage - Power Monitoring - Power\n";
        print "graph_args --base 1000 -l 0\n";
        print "graph_vlabel Watts\n";
        print "graph_category Sensors\n";
        foreach my $j (sort keys %val) {
            print "$j.label $val{$j}{label}\n";
            if ($val{$j}{warning}) {
                print "$j.warning $val{$j}{warning}\n";
            }
            if ($val{$j}{critical}) {
                print "$j.critical $val{$j}{critical}\n";
            }
        }
    }
    else {
        foreach my $j (sort keys %val) {
            print "$j.value $val{$j}{value}\n";
        }
    }
}
exit(0);
