#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk 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 in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# Author: Lars Michelsen <lm@mathias-kettner.de>

hp_sts_drvbox_type_map = {
    '1': 'other',
    '2': 'ProLiant Storage System',
    '3': 'ProLiant-2 Storage System',
    '4': 'internal ProLiant-2 Storage System',
    '5': 'proLiant2DuplexTop',
    '6': 'proLiant2DuplexBottom',
    '7': 'proLiant2InternalDuplexTop',
    '8': 'proLiant2InternalDuplexBottom',
}

hp_sts_drvbox_cond_map = {
    '1': (3, 'other'),
    '2': (0, 'ok'),
    '3': (1, 'degraded'),
    '4': (2, 'failed'),
}

hp_sts_drvbox_fan_map = {
    '1': (3,    'other'),
    '2': (0,    'ok'),
    '3': (2,    'failed'),
    '4': (None, 'noFan'),
    '5': (1,    'degraded'),
}

hp_sts_drvbox_temp_map = {
    '1': (3,    'other'),
    '2': (0,    'ok'),
    '3': (1,    'degraded'),
    '4': (2,    'failed'),
    '5': (None, 'noTemp'),
}

hp_sts_drvbox_sp_map = {
    '1': (3,    'other'),
    '2': (0,    'sidePanelInPlace'),
    '3': (2,    'sidePanelRemoved'),
    '4': (None, 'noSidePanelStatus'),
}

hp_sts_drvbox_power_map = {
    '1': (3,    'other'),
    '2': (0,    'ok'),
    '3': (1,    'degraded'),
    '4': (2,    'failed'),
    '5': (None, 'noFltTolPower'),
}

def inventory_hp_sts_drvbox(info):
    if info:
        return [ (line[0] + '/' + line[1], None)
                  for line in info
                  if line[3] != '' ] # only inventorize rows with "model" set

def check_hp_sts_drvbox(item, _no_params, info):
    for line in info:
        if line[0] + '/' + line[1] == item:
            c_index, b_index, ty, model, fan_status, cond, \
              temp_status, sp_status, pwr_status, serial, loc = line

            sum_state = 0
            output = []

            for val, label, map in [ (fan_status,  'Fan-Status',       hp_sts_drvbox_fan_map),
                                     (cond,        'Condition',        hp_sts_drvbox_cond_map),
                                     (temp_status, 'Temp-Status',      hp_sts_drvbox_temp_map),
                                     (sp_status,   'Sidepanel-Status', hp_sts_drvbox_sp_map),
                                     (pwr_status,  'Power-Status',     hp_sts_drvbox_power_map), ]:
                this_state = map[val][0]
                if this_state is None:
                    continue # skip unsupported checks
                state_txt = ''
                if this_state == 1:
                    state_txt = ' (!)'
                elif this_state == 2:
                    state_txt = ' (!!)'
                sum_state = max(sum_state, this_state)
                output.append('%s: %s%s' % (label, map[val][1], state_txt))

            output.append('(Type: %s, Model: %s, Serial: %s, Location: %s)' %
                       (hp_sts_drvbox_type_map.get(ty, 'unknown'), model, serial, loc))

            return (sum_state, ', '.join(output))
    return (3, "Controller not found in snmp data")

check_info["hp_sts_drvbox"] = {
    'check_function':          check_hp_sts_drvbox,
    'inventory_function':      inventory_hp_sts_drvbox,
    'service_description':     'Drive Box %s',
    'snmp_info':               (
        '.1.3.6.1.4.1.232.8.2.1.1', [
             '1', # cpqSsBoxCntlrIndex
             '2', # cpqSsBoxBusIndex
             '3', # cpqSsBoxType
             '4', # cpqSsBoxModel
             '7', # cpqSsBoxFanStatus
             '8', # cpqSsBoxCondition
             '9', # cpqSsBoxTempStatus
            '10', # cpqSsBoxSidePanelStatus
            '11', # cpqSsBoxFltTolPwrSupplyStatus
            '17', # cpqSsBoxSerialNumber
            '23', # cpqSsBoxLocationString
        ]),
    'snmp_scan_function':      \
     lambda oid: "proliant" in oid(".1.3.6.1.4.1.232.2.2.4.2.0", "").lower(),
}
