#!/usr/bin/perl -w
#
# Copyright (C) 2006 Lars Strand
#
# Munin-plugin to monitor temperature on HP-servers.
# Uses SNMP, and needs hpasmd.
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
# $Log$
#
#
#
#%# family=snmpauto
#%# capabilities=snmpconf

use strict;
use Net::SNMP;

my $DEBUG = 0;
my $MAXLABEL = 20;

my $host      = $ENV{host}      || undef;
my $port      = $ENV{port}      || 161;
my $community = $ENV{community} || "public";

# Taken from CPQHLTH-MIB.mib
my %locations = (
		 1 => "other",
		 2 => "unknown",
		 3 => "system",
		 4 => "systemBoard",
		 5 => "ioBoard",
		 6 => "cpu",
		 7 => "memory",
		 8 => "storage",
		 9 => "removableMedia",
		 10 => "powerSupply",
		 11 => "ambient",
		 12 => "chassis",
		 13 => "bridgeCard"
		 );

#my $response;

if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
{
	print "index  1.3.6.1.4.1.232.6.2.6.8.1.\n";
	print "require 1.3.6.1.4.1.232.6.2.6.8.1.4.1.1. [1-4]\n";
	exit 0;
}

if ($0 =~ /^(?:|.*\/)snmp_([^_]+)_HP_temp$/)
{
	$host  = $1;
	if ($host =~ /^([^:]+):(\d+)$/)
	{
		$host = $1;
		$port = $2;
	}
}
elsif (!defined($host))
{
	print "# Debug: $0 -- $1\n" if $DEBUG;
	die "# Error: couldn't understand what I'm supposed to monitor.";
}

# Sensor locations
my $cpqHeTemperatureLocale = "1.3.6.1.4.1.232.6.2.6.8.1.3.1.";

# Temperatures
my $cpqHeTemperatureCelsius = "1.3.6.1.4.1.232.6.2.6.8.1.4.1.";

# Temperatures thresholds
my $cpqHeTemperatureThreshold = "1.3.6.1.4.1.232.6.2.6.8.1.5.1.";


my ($session, $error) = Net::SNMP->session(
		-hostname  => $host,
		-community => $community,
		-port      => $port
	);

if (!defined ($session)) {
	die "Croaking: $error";
}

# Fetch the values.
my $temp_locales   = $session->get_table($cpqHeTemperatureLocale);

if (!defined ($temp_locales)) {
    die "Croaking: $error";
}

my @i = ();

if (defined $ARGV[0] and $ARGV[0] eq "config") {
    #print "host_name $host\n";
    print "graph_title Temperature (in C)\n";
    print "graph_category sensors\n";
    print "graph_args --upper-limit 100 -l 0\n";
    print "graph_vlabel C\n";
    print "graph_info This graph shows the temperatures on a HP server.\n";

    while (my ($oid, $loc_id) = each (%$temp_locales)) {
	@i = split(/\./, $oid);
	my $t = $session->get_request($cpqHeTemperatureThreshold . $i[-1]);
        while (my ($ooid, $threshold) = each(%$t)) {
	    print "$locations{$loc_id}$i[-1].label $locations{$loc_id}\n";
	    print "$locations{$loc_id}$i[-1].info Temperature sensor on $locations{$loc_id}\n";
	    print "$locations{$loc_id}$i[-1].critical $threshold\n";
	}
    }
    exit 0;
}

# Fetch values
while (my ($oid, $loc_id) = each (%$temp_locales)) {
    @i = split(/\./, $oid);
    my $t = $session->get_request($cpqHeTemperatureCelsius . $i[-1]);
    while (my ($ooid, $temperature) = each(%$t)) {
	print "$locations{$loc_id}$i[-1].value $temperature\n";
    }
}

