<?php
/*
 * LibreNMS
 *
 * Copyright (c) 2016 Peter TKATCHENKO https://github.com/Peter2121/
 *
 * 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 3 of the License, or (at your
 * option) any later version.  Please see LICENSE.txt at the top level of
 * the source code distribution for details.
 */

$oids = snmp_walk($device, 'eqlMemberHealthStatus', '-OQne', 'EQLMEMBER-MIB', 'equallogic');

d_echo('Health oids:');
d_echo($oids."\n");

/*
eqlMemberHealthStatus
    INTEGER     {
                unknown  (0),
                normal   (1),
                warning  (2),
                critical (3)
                }
    DESCRIPTION     The value of this object is determinted by the severity of the
                    health condition state variables. The most severe state will
                    be reflected.

The LibreNMS generic states is derived from Nagios:
    0 = OK
    1 = Warning
    2 = Critical
    3 = Unknown

*/

if (!empty($oids)) {
    $descr='Health';

    $state_name = 'eqlMemberHealthStatus';
    $state_index_id = create_state_index($state_name);

    if ($state_index_id) {
        $states = array(
            array($state_index_id,'unknown',0,0,3) ,
            array($state_index_id,'normal',1,1,0) ,
            array($state_index_id,'warning',1,2,1) ,
            array($state_index_id,'critical',1,3,2)
        );
        foreach ($states as $value) {
            $insert = array(
                'state_index_id' => $value[0],
                'state_descr' => $value[1],
                'state_draw_graph' => $value[2],
                'state_value' => $value[3],
                'state_generic_value' => $value[4]
            );
            dbInsert($insert, 'state_translations');
        }
    }

    foreach (explode("\n", $oids) as $data) {
        $data = trim($data);
        if (!empty($data)) {
            list($oid,$current) = explode(' = ', $data, 2);
            $split_oid        = explode('.', $oid);
            $num_index        = $split_oid[(count($split_oid) - 1)];
            $index            = (int)$num_index+0;
            $low_limit        = 0.5;
            $high_limit       = 2.5;
            discover_sensor($valid['sensor'], 'state', $device, $oid, $index, $state_name, $descr, 1, 1, $low_limit, $low_limit, $high_limit, $high_limit, $current, 'snmp', $index);
            create_sensor_to_state_index($device, $state_name, $index);
        }
    }
}

$oids1 = snmp_walk($device, 'eqlMemberHealthDetailsPowerSupplyName', '-OQn', 'EQLMEMBER-MIB', 'equallogic');

d_echo('PowerSupplyName oids:');
d_echo($oids1."\n");

/*
    .1.3.6.1.4.1.12740.2.1.8.1.2.1.329840783.1 = Power Cooling Module 0
    .1.3.6.1.4.1.12740.2.1.8.1.2.1.329840783.2 = Power Cooling Module 1
**/

$base_oid         = '.1.3.6.1.4.1.12740.2.1.8.1.3.1.'; // eqlMemberHealthDetailsPowerSupplyCurrentState

if (!empty($oids1)) {
/*
eqlMemberHealthDetailsPowerSupplyCurrentState
    INTEGER {
            on-and-operating    (1),
            no-ac-power         (2),
            failed-or-no-data   (3) -- has ac but no dc out or we have no data
    }
*/
    $state_name1 = 'eqlMemberPowerSupplyCurrentState';
    $state_index_id1 = create_state_index($state_name1);

    if ($state_index_id1) {
        $states1 = array(
            array($state_index_id1,'on-and-operating',1,1,0) ,
            array($state_index_id1,'no-ac-power',1,2,1) ,
            array($state_index_id1,'failed-or-no-data',1,3,2)
        );
        foreach ($states1 as $value) {
            $insert = array(
                'state_index_id' => $value[0],
                'state_descr' => $value[1],
                'state_draw_graph' => $value[2],
                'state_value' => $value[3],
                'state_generic_value' => $value[4]
            );
            dbInsert($insert, 'state_translations');
        }
    }

    foreach (explode("\n", $oids1) as $data) {
        $data = trim($data);
        if (!empty($data)) {
            list($oid,$descr) = explode(' = ', $data, 2);
            $split_oid        = explode('.', $oid);
            $num_index        = $split_oid[(count($split_oid) - 1)];
            $index            = (int)$num_index+0;
            $member_id        = $split_oid[(count($split_oid) - 2)];
            $num_index        = $member_id.'.'.$num_index;
            $oid              = $base_oid.$num_index;
            $extra            = snmp_get_multi($device, $oid, '-OQne', 'EQLMEMBER-MIB', 'equallogic');
            d_echo($extra);
            if (!empty($extra)) {
                list($foid,$pstatus) = explode(' = ', $extra, 2);
                $index        = (100 + $index);
                $low_limit    = 0.5;
                $high_limit   = 1.5;
                discover_sensor($valid['sensor'], 'state', $device, $oid, $index, $state_name1, $descr, 1, 1, $low_limit, $low_limit, $high_limit, $high_limit, $pstatus, 'snmp', $index);
                create_sensor_to_state_index($device, $state_name1, $index);
            }
        }//end if
    }//end foreach
}//end if empty oids


$oids_disks = snmp_walk($device, 'eqlDiskSerialNumber', '-OQn', 'EQLDISK-MIB', 'equallogic');

d_echo('Disk Serials oids:' . PHP_EOL);
d_echo($oids_disks."\n");

$disks_base_oid         = '.1.3.6.1.4.1.12740.3.1.1.1.8.1.'; // eqlDiskStatus

if (!empty($oids_disks)) {
    $disks_state_name = 'eqlDiskStatus';
    $disks_state_index_id = create_state_index($disks_state_name);

    if ($disks_state_index_id) {
        $disk_states = array(
            array($disks_state_index_id,'on-line',1,1,0),
            array($disks_state_index_id,'spare',1,2,0),
            array($disks_state_index_id,'failed',1,3,2),
            array($disks_state_index_id,'off-line',1,4,1),
            array($disks_state_index_id,'alt-sig',1,5,1),
            array($disks_state_index_id,'too-small',1,6,2),
            array($disks_state_index_id,'history-of-failures',1,7,1),
            array($disks_state_index_id,'unsupported-version',1,8,1),
        );
        foreach ($disk_states as $value) {
            $insert = array(
                'state_index_id' => $value[0],
                'state_descr' => $value[1],
                'state_draw_graph' => $value[2],
                'state_value' => $value[3],
                'state_generic_value' => $value[4]
            );
            dbInsert($insert, 'state_translations');
        }
    }

    foreach (explode("\n", $oids_disks) as $data) {
        $data = trim($data);
        if (!empty($data)) {
            list($oid,$descr) = explode(' = ', $data, 2);
            $split_oid        = explode('.', $oid);
            $disk_index        = $split_oid[(count($split_oid) - 1)];
            $member_id        = $split_oid[(count($split_oid) - 2)];
            $num_index        = $member_id.'.'.$disk_index;
            $oid              = $disks_base_oid.$num_index;
            $extra            = snmp_get($device, $oid, '-OQne', 'EQLDISK-MIB', 'equallogic');
            d_echo($extra);
            if (!empty($extra)) {
                list($foid,$pstatus) = explode(' = ', $extra, 2);
                $index        = 'eqlDiskStatus.'.$disk_index;
                $low_limit    = 0.5;
                $high_limit   = 1.5;
                discover_sensor($valid['sensor'], 'state', $device, $oid, $index, $disks_state_name, "Disk $disk_index - $descr", 1, 1, $low_limit, $low_limit, $high_limit, $high_limit, $pstatus, 'snmp', $index);
                create_sensor_to_state_index($device, $disks_state_name, $index);
                unset(
                    $index,
                    $low_limit,
                    $high_limit
                );
            }
            unset(
                $split_oid,
                $disk_index,
                $index,
                $member_id,
                $num_index,
                $oid,
                $extra
            );
        }//end if
        unset(
            $data
        );
    }//end foreach
}//end if empty oids

unset(
    $oid_disks,
    $disks_base_oid,
    $disks_state_name,
    $disks_state_index_id,
    $disk_states,
    $insert
);
