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

# FRU = Field Replacable Unit

cisco_fru_poweradmin_types = {
  '1': ( 'on',                   0 ),
  '2': ( 'off',                  2 ),
  '3': ( 'inlineAuto',           1 ),
  '4': ( 'inlineOn',             1 ),
  '5': ( 'powerCycle',           1 ),
}

cisco_fru_poweroper_types = {
  '1' : ('offEnvOther',          1 ),
  '2' : ('on',                   0 ),
  '3' : ('offAdmin',             1 ),
  '4' : ('offDenied',            2 ),
  '5' : ('offEnvPower',          2 ),
  '6' : ('offEnvTemp',           2 ),
  '7' : ('offEnvFan',            2 ),
  '8' : ('failed',               2 ),
  '9' : ('onButFanFail',         1 ),
  '10': ('offCooling',           1 ),
  '11': ('offConnectorRating',   1 ),
  '12': ('onButInlinePowerFail', 2 ),
}


def inventory_cisco_fru_power(info):
    # Monitor all devices that are not
    # - with incomplete SNMP data
    # - in state 1:offEnvOther
    # - in state 5:offEnvPower
    return [ (line[0], None)
             for line in info
             if line[2] not in ('', '0', '1', '5') ]


def check_cisco_fru_power(item, _no_params, info):
    for oid_end, admin_state, oper_state in info:
        if oid_end == item:
            worst_state = 0
            infotexts = []

            for (name, state), title in [
              ( cisco_fru_poweroper_types[oper_state],   "Operational state" ),
              ( cisco_fru_poweradmin_types[admin_state], "Administrative state" )]:

                text = title + ": " + name
                worst_state = max(state, worst_state)
                if state == 1:
                    text += '(!)'
                elif state == 2:
                    text += '(!!)'
                infotexts.append(text)

            return worst_state, ", ".join(infotexts)

    return 3, "No FRU with this id found"


check_info["cisco_fru_power"] = {
    'check_function'          : check_cisco_fru_power,
    'inventory_function'      : inventory_cisco_fru_power,
    'service_description'     : 'FRU Power %s',
    'snmp_info'               : ('.1.3.6.1.4.1.9.9.117.1.1.2.1', [ OID_END, 1, 2 ] ),
    'snmp_scan_function'      : lambda oid: "cisco" in oid(".1.3.6.1.2.1.1.1.0").lower(),
}
