#!/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 all available temperature probes in the storage
# array enclosures of a Dell PowerEdge Server via the omreport tool. It has
# been tested on the following combinations:
#
#   PowerEdge R905 + PERC 6/E + MD1120
#
# 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_storage_temp
#
# 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


sub get_controllers {
    my $omreport = shift;
    my %ctrlrs;
    my @cmd = ($omreport, 'storage', 'controller');
    my $pid = open(my $cmd_out, '-|', @cmd) or die "@cmd: $!\n";
    defined $pid or die "fork() failed: $!\n";
    my $id;
    while (<$cmd_out>) {
        s/\s+/\040/g;
        s/\s+$//;
        s/^\s+//;
        my ($field, $value) = split(/\s+\:\s+/, $_);
        defined $value or next;
        if ($field eq 'ID') {
            $id = $value;
        }
        elsif ($field eq 'Name') {
            defined $id or next;
            $ctrlrs{$id} = $value;
        }
    }
    close $cmd_out;
    return %ctrlrs;
}


sub get_controller_enclosures {
    my ($omreport, $ctrlr) = @_;
    my %encs;
    my @cmd = ($omreport, 'storage', 'enclosure', "controller=$ctrlr");
    my $pid = open(my $cmd_out, '-|', @cmd) or die "@cmd: $!\n";
    defined $pid or die "fork() failed: $!\n";
    my $id;
    while (<$cmd_out>) {
        s/\s+/\040/g;
        s/\s+$//;
        s/^\s+//;
        my ($field, $value) = split(/\s+\:\s+/, $_);
        defined $value or next;
        if ($field eq 'ID') {
            $id = $value;
        }
        elsif ($field eq 'Name') {
            defined $id or next;
            $encs{$id} = $value;
        }
    }
    close $cmd_out;
    return %encs;
}

sub get_enclosure_temps {
    my ($omreport, $ctrlr, $enc) = @_;
    my %temp_hashes;
    my @cmd = ($omreport, 'storage', 'enclosure', "controller=$ctrlr",
               "enclosure=$enc", "info=temps");
    my $pid = open(my $cmd_out, '-|', @cmd) or die "@cmd: $!\n";
    defined $pid or die "fork() failed: $!\n";
    my $idx;
    while (<$cmd_out>) {
        s/\s+/\040/g;
        s/\s+$//;
        s/^\s+//;
        my ($field, $value) = split(/\s+\:\s+/, $_);
        defined $value or next;
        if ($field eq 'ID') {
            $idx = $value;
            $temp_hashes{$idx} = {
                id => $value,
            };
        }
        elsif ($field eq 'Name') {
            defined $idx or next;
            $temp_hashes{$idx}{name} = $value;
        }
        elsif ($field eq 'Reading') {
            defined $idx or next;
            $value =~ s{\s+C$}{};
            $temp_hashes{$idx}{value} = $value;
        }
        elsif ($field eq 'Maximum Warning Threshold') {
            defined $idx or next;
            $value =~ s{\s+C$}{};
            $temp_hashes{$idx}{warning} = $value;
        }
        elsif ($field eq 'Maximum Failure Threshold') {
            defined $idx or next;
            $value =~ s{\s+C$}{};
            $temp_hashes{$idx}{critical} = $value;
        }
    }
    close $cmd_out;
    return %temp_hashes;
}


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 %ctrlrs = get_controllers($omreport);
    my $is_config_pass = $ARGV[0] && $ARGV[0] eq "config";
    if ($is_config_pass) {
        print "graph_title OpenManage - Storage - Temperatures\n";
        print "graph_args --base 1000 -l 0\n";
        print "graph_vlabel Temp in Degrees Celsius\n";
        print "graph_category Sensors\n";
        print "graph_info Temperature sensors within storage enclosures on the system, typically external ones managed via PERC 6/E adapters.\n";
    }
    foreach my $c (sort keys %ctrlrs) {
        my $c_name = $ctrlrs{$c};
        my %encs = get_controller_enclosures($omreport, $c);
        foreach my $e (sort keys %encs) {
            my $e_name = $encs{$e};
            my %temp_hashes = get_enclosure_temps($omreport, $c, $e);
            foreach my $t (sort keys %temp_hashes) {
                my $t_hash = $temp_hashes{$t};
                my ($t_name, $val, $warn, $crit) = @$t_hash{qw(
                    name value warning critical
                )};
                $c =~ s{\W+}{_}g;
                $e =~ s{\W+}{_}g;
                $t =~ s{\W+}{_}g;
                my $g = "ctrlr${c}_enc${e}_temp${t}";
                if ($is_config_pass) {
                    print "$g.label $c_name\: $e_name\: $t_name\n";
                    print "$g.warning $warn\n";
                    print "$g.critical $crit\n";
                }
                else {
                    print "$g.value $val\n";
                }
            }
        }
    }
}
exit(0);
