#!/usr/bin/php5 -q
<?
    /**************************************************************************
    #
    #  check_esxi_hpSmartArrayE200i_over_ssh.php
    #
    #  Copyright 2012 Dariusz Kozlowski <login_16@wp.pl>
    #
    #  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; either version 2 of the License, or
    #  (at your option) any later version.
    #
    #  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., 51 Franklin Street, Fifth Floor, Boston,
    #  MA 02110-1301, USA.
    #
    ############################################################################
    #
    #  need to install:
    #
    #  ESXi 5.0:
    #  - HP ESXi Utilities Offline Bundle for VMware ESXi 5.0 - (/opt/hp/)
    #  - paste public key into /etc/ssh/keys-root/authorized_keys
    #
    #  Linux Server:
    #  - enabled ssh2 support in PHP
    #  - paste ssh keys anywhere, example /etc/nagios3/certs/
    #
    #  Nagios:
    #  define command {
    #     command_name  check_esxi_hp_array
    #     command_line  /usr/lib/nagios/plugins/check_esxi_hpSmartArrayE200i_over_ssh.php $HOSTADDRESS$
    #  }
    #
    /***************************************************************************/

    $states['STATE_OK'] = 0;
    $states['STATE_WARNING'] = 1;
    $states['STATE_CRITICAL'] = 2;
    $states['STATE_UNKNOWN'] = 3;
    $states['STATE_DEPENDENT'] = 4;

    $info = "";
    $perf = "";
    $state = "";

    $connection = ssh2_connect($argv[1], 22, array('hostkey'=>'ssh-rsa'));

    if ( ssh2_auth_pubkey_file($connection, 'root', '/etc/nagios3/certs/vmware.key.pub', '/etc/nagios3/certs/vmware.key', 'secret') ) {

        $outputStream = ssh2_exec($connection, '/opt/hp/hpacucli/bin/hpacucli \'controller slot=0 show config\'');
        $errorStream = ssh2_fetch_stream($outputStream, SSH2_STREAM_STDERR);

        stream_set_blocking($outputStream, true);
        stream_set_blocking($errorStream, true);

        $output = stream_get_contents($outputStream);
        $error = stream_get_contents($errorStream);

        fclose($errorStream);
        fclose($outputStream);

    } else {

        $info = "phpSSH2 :: Public Key Authentication Failed";
        $state = 'STATE_UNKNOWN';

    }

  if ( strlen($output) > 5 ) {

    $lines = explode(chr(0x0A),$output);

    foreach ($lines as $line_num => $line) {

      $line = trim($line);

      if ( strlen($line) > 5 ) {

        if ( stripos($line, "logicaldrive") !== FALSE ) {

            $info = $line;
            $perf = $lines[$line_num+2]."\n"
                   .$lines[$line_num+3];

            if ( stripos($line, "OK") !== FALSE ) {

                $info = "OK - ".$info;
                $state = 'STATE_OK';
                break;

            } else if ( stripos($line, "Interim Recovery Mode") !== FALSE ) {

                $info = "CRITICAL - ".$info;
                $state = 'STATE_CRITICAL';
                break;

            } else if ( stripos($line, "Recovering") !== FALSE ) {

                $info = "WARNING - ".$info;
                $state = 'STATE_WARNING';
                break;

            } else {

                $info = "UNKNOWN - ".$info;
                $state = 'STATE_UNKNOWN';
                break;

            }
        }

      }
    }

  } else {

    if ( $info == "" ) {

        $info = "UNKNOWN - No data";
        $state = 'STATE_UNKNOWN';

    }

  }

  $echo =  $info."|".$perf;
  echo $echo;
  exit($states[$state]);

?>

