#!/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.

#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.2.1 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.2.2 2
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.2.3 3
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.2.4 4
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.2.5 5
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.3.1 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.3.2 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.3.3 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.3.4 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.3.5 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.4.1 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.4.2 3
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.4.3 255
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.4.4 0
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.4.5 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.5.1 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.5.2 12
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.5.3 9
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.5.4 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.5.5 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.6.1 ESX1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.6.2 ESX109
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.6.3 ESX110
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.6.4 ESX137
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.6.5 ESX138



def inventory_blade_blades(info):
    for line in info:
        if line[2] == '1':
            yield (line[0], None)


def check_blade_blades(item, _no_params, info):
    map_exists = {
        '0'     : (2, "false"),
        '1'     : (0, "true")
    }

    map_power = {
        '0'     : (2, "off"),
        '1'     : (0, "on"),
        '3'     : (1, "standby"),
        '4'     : (1, "hibernate"),
        '255'   : (3, "unknown"),
    }

    map_health = {
        '0'      : (3, "unknown"),
        '1'      : (0, "good"),
        '2'      : (1, "warning"),
        '3'      : (2, "critical"),
        '4'      : (1, "kernel mode"),
        '5'      : (0, "discovering"),
        '6'      : (2, "communications error"),
        '7'      : (2, "no power"),
        '8'      : (1, "flashing"),
        '9'      : (2, "initialization Failure"),
        '10'     : (2, "insuffiecient power"),
        '11'     : (2, "power denied"),
        '12'     : (1, "maintenance mode"),
        '13'     : (1, "firehose dump"),
    }

    for line in info:
        if line[0] == item:
            # name
            yield 0, line[4]
            # exist_state
            state, state_readable = map_exists[line[1]]
            yield state, "Exists: %s" % state_readable
            # power_state
            state, state_readable = map_power[line[2]]
            yield state, "Power: %s" % state_readable
            # health_state
            state, state_readable = map_health[line[3]]
            yield state, "Health: %s" % state_readable


check_info["blade_blades"] = {
    'inventory_function'    : inventory_blade_blades,
    'check_function'        : check_blade_blades,
    'service_description'   : "Blade %s",
    'snmp_info'             : (".1.3.6.1.4.1.2.3.51.2.22.1.5.1.1", [ # BLADE-MIB
                                                2, # bladeId
                                                3, # bladeExists
                                                4, # bladePowerState
                                                5, # bladeHealthState
                                                6, # bladeName
                              ]),
    'snmp_scan_function'    : lambda oid: re.match('(BladeCenter|BladeCenter Advanced|IBM Flex Chassis|Lenovo Flex Chassis) Management Module', \
                                oid(".1.3.6.1.2.1.1.1.0")) != None,
}
