#!/usr/bin/php
<?php
// Author: Carsten Schoene
// Last change: 2009-07-15 
// required arguments
// $1 = Host / IP
// $2 = read community string
$switchType="SNMPv2-SMI::enterprises.674.10892.2.1.1.2.0";

$grad = iconv("UTF-8","ISO-8859-15","°");
define('MYNAME',"check_snmp_dell_cmc");
define('OK',0);
define('WARNING',1);
define('CRITICAL',2);
define('UNKNOWN',3);
define('DEPENDENT',4);

define_syslog_variables();
openlog(MYNAME,LOG_PID | LOG_ODELAY,LOG_MAIL);

if ( ! extension_loaded("snmp") ) {
        if ( ! dl("snmp") ) {
                syslog(LOG_ERR,"snmp extension not loaded!");
                exit;
        }
}
ini_set("display_errors","on");
// get working directory
define('BASE',dirname(__FILE__));

$cmdlineopt = getopt("H:C:");
if (empty($cmdlineopt)) {
        echo "Usage: " . MYNAME . " -H [<hostname> | <ipaddress>] -C [<community>]\n";
        echo "\t -H\t Hostname or IP address (default: localhost)\n";
        echo "\t -C\t Community (default: public)\n";
        exit(1);
}
if ( isset($cmdlineopt['H']) ) {
	$hostname = $cmdlineopt['H'];
} else {
	$hostname = "localhost";
}

if ( isset($cmdlineopt['C']) ) {
	$community = $cmdlineopt['C'];
} else {
	$community = "public";
}
$swTypeline = @snmpget($hostname,$community,$switchType);
if ( $swTypeline == FALSE ) {
	echo "Can't get switchtype\n";
	exit(UNKNOWN);
} else {
	$swTypearr = explode(": ",$swTypeline);
	$swType = trim(ereg_replace("\"","",$swTypearr[1]));
}

switch ($swType) {
	case "PowerEdge M1000e":
		$cmc['globalState']['oid'] = "SNMPv2-SMI::enterprises.674.10892.2.3.1.1.0";
		$cmc['globalState']['label'] = "Global:";
		$cmc['ioModuleState']['oid'] = "SNMPv2-SMI::enterprises.674.10892.2.3.1.2.0";
		$cmc['ioModuleState']['label'] = "IO:";
		$cmc['kvmState']['oid'] = "SNMPv2-SMI::enterprises.674.10892.2.3.1.3.0";
		$cmc['kvmState']['label'] = "KVM:";
		$cmc['redState']['oid'] = "SNMPv2-SMI::enterprises.674.10892.2.3.1.4.0";
		$cmc['redState']['label'] = "RED:";
		$cmc['powerState']['oid'] = "SNMPv2-SMI::enterprises.674.10892.2.3.1.5.0";
		$cmc['powerState']['label'] = "Power:";
		$cmc['fanState']['oid'] = "SNMPv2-SMI::enterprises.674.10892.2.3.1.6.0";
		$cmc['fanState']['label'] = "Fan:";
		$cmc['bladeState']['oid'] = "SNMPv2-SMI::enterprises.674.10892.2.3.1.7.0";
		$cmc['bladeState']['label'] = "Blade:";
		$cmc['tempState']['oid'] = "SNMPv2-SMI::enterprises.674.10892.2.3.1.8.0";
		$cmc['tempState']['label'] = "Temprature:";
		$cmc['cmcState']['oid'] = "SNMPv2-SMI::enterprises.674.10892.2.3.1.9.0";
		$cmc['cmcState']['label'] = "CMC:";
		define(1,'STATUS_OTHER');
		define(2,'STATUS_UNKNOWN');
		define(3,'STATUS_OK');
		define(4,'STATUS_NONCRITICAL');
		define(5,'STATUS_CRITICAL');
		define(6,'STATUS_NONRECOVERABLE');

		foreach ( $cmc as $key => $state ) {
			$cmc[$key]['reading'] = @snmpget($hostname,$community,$state['oid']);
			$tmp = explode(": ",$cmc[$key]['reading']);
			$cmc[$key]['value'] = $tmp[1];
			$cmc[$key]['state'] = constant($cmc[$key]['value']);

		}
		$output = "";
		foreach ( $cmc as $key => $value ) {
			
			$output .= $cmc[$key]['label'] . " " . $cmc[$key]['state'] . "    ";
		}

		echo $output;	
		if ( ereg('STATUS_NONRECOVERABLE',$output) ) {
			exit(CRITICAL);
		}
		if ( ereg('STATUS_CRITICAL',$output) ) {
			exit(CRITICAL);
		}
		if ( ereg('STATUS_NONCRITICAL',$output) ) {
			exit(WARNGING);
		}
		if ( ereg('STATUS_UNKOWN',$output) ) {
			exit(UNKNOWN);
		}
		if ( ereg('STATUS_OTHER',$output) ) {
			exit(UNKNOWN);
		}
		if ( ereg('STATUS_OK',$output) ) {
			exit(OK);
		}
	break;
	default:
		echo "CMC-type $swType is currently not supported";
}
?>
