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

# Mappings for translating SNMP states to nagios states
flex_blade_blades_exists_states = (2, 0)
flex_blade_blades_power_states  = (1, 0)
flex_blade_blades_health_states = (3, 0, 1, 2)

# Name mappings
flex_blade_blades_exists_labels = ('no', 'yes')
flex_blade_blades_power_labels  = ('off', 'on')
flex_blade_blades_health_labels = ('unknown', 'good', 'warning', 'bad')

def inventory_flex_blade_blades(info):
    # find only blades that are powered on
    return [ (line[0], '', line[1]) for line in info if line[2] == '1' ]

def check_flex_blade_blades(item, params, info):
    for line in info:
        if line[0] == item:
            exists, power_state, health_state = map(saveint, line[1:4])
            name = line[4]

            state  = 0
            output = '%s: ' % (name)

            for label, part_state, nag_state, state_label in (
                    ('Exists', exists,       flex_blade_blades_exists_states[exists],        flex_blade_blades_exists_labels[exists]),
                    ('Power',  power_state,  flex_blade_blades_power_states[power_state],    flex_blade_blades_power_labels[power_state]),
                    ('Health', health_state, flex_blade_blades_health_states[health_state],  flex_blade_blades_health_labels[health_state])):
                output += '%s: %s' % (label, state_label)
                if nag_state == 1:
                    output += ' (!)'
                elif nag_state == 2:
                    output += ' (!!)'
                elif nag_state == 3:
                    output += ' (UNKNOWN)'
                state = max(state, nag_state)
                output += ', '

            return (state, output.rstrip(', '))
    return (3, "no data for '%s' in SNMP info" % item)

check_info["flex_blade_blades"] = {
    'check_function':          check_flex_blade_blades,
    'inventory_function':      inventory_flex_blade_blades,
    'service_description':     'Flex 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('IBM Flex Chassis Management Module', oid(".1.3.6.1.2.1.1.1.0")) != None,
}