#!/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': ( 'inline auto', 1 ),
  '4': ( 'inline on',   1 ),
  '5': ( 'power cycle', 1 ),
}


cisco_fru_poweroper_types = {
  '1' : ('off env other',            1 ),
  '2' : ('on',                       0 ),
  '3' : ('off admin',                1 ),
  '4' : ('off denied',               2 ),
  '5' : ('off env power',            2 ),
  '6' : ('off env temp',             2 ),
  '7' : ('off env fan',              2 ),
  '8' : ('failed',                   2 ),
  '9' : ('on but fan fail',          1 ),
  '10': ('off cooling',              1 ),
  '11': ('off connector rating',     1 ),
  '12': ('on but inline power fail', 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:
            for mapping, status, title in [
              ( cisco_fru_poweroper_types,  oper_state,  "Operational state" ),
              ( cisco_fru_poweradmin_types, admin_state, "Administrative state" )]:
                state_readable, state = mapping.get(status, ("unknown[%s]" % status, 3))
                yield state, "%s: %s" % (title, state_readable)


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(),
}
