#!/usr/bin/php
<?php
// Author: Carsten Schoene
// $LastChangedDate: 2015-02-02 07:59:29 +0100 (Mo, 02. Feb 2015) $
// Rev: $Rev: 9778 $
// required arguments
// $1 = Host / IP
// $2 = read community string

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

openlog(MYNAME,LOG_PID | LOG_ODELAY,LOG_MAIL);

if ( ! extension_loaded("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";
}


// get product type -> .1.3.6.1.4.1.1991.1.1.1.1.26.0
/*
0 - invalid
1 - mg8
2 - ni40G
3 - imr
4 - biRx800
5 - niXmr16000
6 - biRx400
7 - niXmr8000
8 - biRx200
9 - niXmr4000
10 - niMlx16
11 - niMlx8
12 - niMlx4
13 - niMlx32
14 - niXmr32000
15 - biRx32
16 - niCES2000Series
17 - niCER2000Series
18 - brMlxE4
19 - brMlxE8
20 - brMlxE16
21 - brMlxE32
50 - biNI2
66 - biBB
77 - biM4
78 - biNI
83 - biSLB
87 - biWG
*/
$prod = @snmpget($hostname,$community,".1.3.6.1.4.1.1991.1.1.1.1.26.0");
if (empty($prod) ) {
  print "No brocade device found\n";
  exit(UNKNOWN);
} else {
  $prodarr = explode(": ",$prod);
  $product_id = $prodarr[1];
}
switch ($product_id) {
  case "18":
    // temperature
    $chassis = "";
    $chassistemp = $chassis . "";
    $chassiswarntemp = $chassis . "";
    $chassisshutdowntemp = $chassis . "";
    $chassistempfactor = "0.5";

    // power supply
    $psu = ".1.3.6.1.4.1.1991.1.1.1.2.1.1";
    $psuidx = $psu . ".1";
    $psudscr = $psu . ".2";
    $psustat = $psu . ".3";
    $psustates = array(1 => "other", 2 => "normal", 3 => "failure");

    // fans
    $fan = ".1.3.6.1.4.1.1991.1.1.1.3.1.1";
    $fanidx = $fan . ".1";
    $fandscr = $fan . ".2";
    $fanstat = $fan . ".3";
    $fanstates = array(1 => "other", 2 => "normal", 3 => "failure");
  break;
  case "66":
    // temperature
    $chassis = ".1.3.6.1.4.1.1991.1.1.1.1";
    $chassistemp = $chassis . ".18";
    $chassiswarntemp = $chassis . ".19";
    $chassisshutdowntemp = $chassis . ".20";
    $chassistempfactor = "0.5";

    // power supply
    $psu = ".1.3.6.1.4.1.1991.1.1.1.2.1.1";
    $psuidx = $psu . ".1";
    $psudscr = $psu . ".2";
    $psustat = $psu . ".3";
    $psustates = array(1 => "other", 2 => "normal", 3 => "failure");

    // fans
    $fan = ".1.3.6.1.4.1.1991.1.1.1.3.1.1";
    $fanidx = $fan . ".1";
    $fandscr = $fan . ".2";
    $fanstat = $fan . ".3";
    $fanstates = array(1 => "other", 2 => "normal", 3 => "failure");
  break;
  default:
    exit();
}


// check temperature
if ( !empty($chassistemp) ) {
  $curtemp = @snmpget($hostname,$community,$chassistemp.".0");
}
$output = $tempoutput = $fanoutput = $psuoutput = $tempstate = $psustate = $fanstate = "";

if (!empty($curtemp)) {
  $curtemparr = explode(": ",$curtemp);
  $curtemp = $curtemparr[1];
  $warntemp = @snmpget($hostname,$community,$chassiswarntemp.".0");
  $warntemparr = explode(": ",$warntemp);
  $warntemp = $warntemparr[1];
  $shuttemp = @snmpget($hostname,$community,$chassisshutdowntemp.".0");
  $shuttemparr = explode(": ",$shuttemp);
  $shuttemp = $shuttemparr[1];
  if ( is_numeric($chassistempfactor) ) {
    $curtemp  = $curtemp * $chassistempfactor;
    $warntemp = $warntemp * $chassistempfactor;
    $shuttemp = $shuttemp * $chassistempfactor;
  }
  $crittemp = $shuttemp - 2;
  $lowcrittemp = $shuttemp - 10;
  if ( $curtemp < $crittemp  && $curtemp > $lowcrittemp &&  $curtemp > $warntemp ) {
    $tempstate = CRITICAL;
    $tempoutput = "Chassis Temperature " . $curtemp . $grad."C is near shutdown level (".$shuttemp.$grad."C / ".$crittemp.$grad."C)";
  }elseif ( $curtemp <= $lowcrittemp  &&  $curtemp > $warntemp ) {
    $tempstate = WARNING;
    $tempoutput = "Chassis Temperature " . $curtemp . $grad. "C has reached warnging level (".$warntemp.$grad."C)";
  }elseif ( $curtemp <= $warntemp ) {
    $tempstate = OK;
    $tempoutput = "Chassis Temperature " . $curtemp . $grad. "C is normal";
  } else {
    $tempstate = UNKNOWN;
    $tempoutput = "Chassis Temperature is unknown";
  }
} else {
    $tempstate = OK;
    $tempoutput = "No temprature sensor found";
}


// check power supply
if ( !empty($psuidx) ) {
  $psusensorCount = count(@snmpwalk($hostname,$community,$psuidx));
} else {
  $psusensorCount = 0;
  $psustate = OK;
  $psuoutput = "";
}
if ( $psusensorCount > 0 ) {
  foreach ( snmpwalk($hostname,$community,$psuidx) as $psusensor ) {
      $psusensorarr = explode(": ",$psusensor);
      $a = $psusensorarr[1];
      $psuNameline = @snmpget($hostname,$community,$psudscr . ".". $a);
      $psuNameArr = explode("\"", $psuNameline);
      $psuStateline = @snmpget($hostname,$community,$psustat . ".". $a);
      $psuStatArr = explode(": ",$psuStateline);
      $psustatus = $psustates[$psuStatArr[1]];
      if ( $a == 1 ) {
	$psuoutput = "PSU${a} => $psustatus";
      } else {
	$psuoutput .= ", PSU${a} => $psustatus";
      }
  }
  if ( preg_match('/failure/i',$psuoutput) ) {
      $psustate = CRITICAL;
  } elseif( preg_match('/other/i', $psuoutput) ) {
      $psustate = WARNING;
  } elseif( preg_match('/normal/i', $psuoutput) ) {
      $psustate = OK;
  } else {
      $psustate = UNKNOWN;
  }
}

// check fans
if ( !empty($fanidx) ) {
  $fansensorCount = count(@snmpwalk($hostname,$community,$fanidx));
} else {
  $fansensorCount = 0;
  $fanstate = OK;
  $fanoutput = "";
}
if ( $fansensorCount > 0 ) {
  foreach ( @snmpwalk($hostname,$community,$fanidx) as $fansensor ) {
      $fansensorarr = explode(": ",$fansensor);
      $a = $fansensorarr[1];
      $fanNameline = @snmpget($hostname,$community,$fandscr . ".".$a);
      $fanNameArr = explode("\"", $fanNameline);
      $fanStateline = @snmpget($hostname,$community,$fanstat . ".".$a);
      $fanStatArr = explode(": ",$fanStateline);
      $fanstatus = $fanstates[$fanStatArr[1]];
      if ( $a == 1 ) {
	$fanoutput = "FAN${a} => $fanstatus";
      } else {
	$fanoutput .= ", FAN${a} => $fanstatus";
      }
  }
  if ( preg_match('/failure/i',$fanoutput) ) {
      $fanstate = CRITICAL;
  } elseif( preg_match('/other/i', $fanoutput) ) {
      $fanstate = WARNING;
  } elseif( preg_match('/normal/i', $fanoutput) ) {
      $fanstate = OK;
  } else {
      $fanstate = UNKNOWN;
  }
}

$output = $tempoutput . ", " . $psuoutput . ", " . $fanoutput;
print $output . "\n";
if ( $tempstate == CRITICAL  ||  $psustate == CRITICAL  ||  $fanstate == CRITICAL ) {
    exit(CRITICAL);
} elseif ( $tempstate == WARNING  ||  $psustate == WARNING  ||  $fanstate == WARNING ) {
    exit(WARNING);
} elseif ( $tempstate == UNKNOWN  ||  $psustate == UNKNOWN  ||  $fanstate == UNKNOWN ) {
    exit(UNKNOWN);
} else {
    exit(OK);
}
