#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# SpearHead Systems
#


flex_blade_bays_module_state = {
 0:   'standby',
 1:   'on',
 2:   'notPresent',
 255: 'notApplicable',
}

def flex_blade_bays_make_item(line):
    # "3.IO Module 5"
    pd, name = line[0].split(".", 1)
    if pd == '2':
        power_domain = 1
    else:
        power_domain = 2
    return "PD%d %s" % (power_domain, name)

def inventory_flex_blade_bays(info):
    # find only modules that are present and switched on or standby
    inventory = []
    for line in info:
        if line[1] in [ '0', '1' ]:
            item = flex_blade_bays_make_item(line)
            inventory.append((item, None))
    return inventory

def check_flex_blade_bays(item, _no_params, info):
    for line in info:
        if item == flex_blade_bays_make_item(line):
            state = saveint(line[1])
            type = line[2].split('(')[0]
            if state == 1:
                return (0, "State %s (Type: %s, ID: %s)" %
                    (flex_blade_bays_module_state.get(state, 'Unhandled'), type, line[3]))
            elif state == 2:
                return (1, "Not present")
            elif state == 3:
                return (1, "Device is switched off")
            elif state == 0:
                return (1, "Device is in standby")
            else:
                return (2, "invalid state %d" % state)
    return (3, "no data for '%s' in SNMP info" % item)

check_info["flex_blade_bays"] = {
    'check_function':          check_flex_blade_bays,
    'inventory_function':      inventory_flex_blade_bays,
    'service_description':     'IBM Flex Bay %s',
    'snmp_info':               (
        ".1.3.6.1.4.1.2.3.51.2.2.10", [
             "2", # powerDomain1
             "3", # powerDomain2
        ], [ "1.1.5", "1.1.6", "1.1.2", "1.1.1" ] ), # BLADE-MIB
    'snmp_scan_function':      \
     lambda oid: re.match('IBM Flex Chassis Management Module', oid(".1.3.6.1.2.1.1.1.0")) != None,
}