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

alcatel_power_operstate_map = {
    "1": "up",
    "2": "down",
    "3": "testing",
    "4": "unknown",
    "5": "secondary",
    "6": "not present",
    "7": "unpowered",
    "9": "master",
}

alcatel_power_no_power_supply_info = "no power supply"

alcatel_power_type_map = {
    "0": alcatel_power_no_power_supply_info,
    "1": "AC",
    "2": "DC",
}

AlcatelPowerEntry = collections.namedtuple("AlcatelPowerEntry", [
    "oper_state_readable",
    "power_type",
])


def parse_alcatel_power(info):
    parsed = {}
    for oidend, status, power_type in info:
        parsed.setdefault(
            oidend,
            AlcatelPowerEntry(
                alcatel_power_operstate_map.get(status, 'unknown[%s]' % status),
                alcatel_power_type_map.get(power_type, alcatel_power_no_power_supply_info),
            ))
    return parsed


@discover
def inventory_alcatel_power(_oidend, device):
    return (device.power_type != alcatel_power_no_power_supply_info and
            device.oper_state_readable != 'not present')


@get_parsed_item_data
def check_alcatel_power(item, _no_params, device):
    if device.oper_state_readable == 'up':
        state = 0
    else:
        state = 2
    yield state, "[%s] Operational status: %s" % (device.power_type, device.oper_state_readable)


check_info["alcatel_power"] = {
    "parse_function": parse_alcatel_power,
    "check_function": check_alcatel_power,
    "inventory_function": inventory_alcatel_power,
    "service_description": "Power Supply %s",
    "snmp_scan_function": alcatel_networking_products_scan_function,
    "snmp_info": (
        ".1.3.6.1.4.1.6486.800.1.1.1.1.1.1.1",  # MIB object "chasEntPhysicalEntry"
        [
            OID_END,
            2,  # MIB object "chasEntPhysOperStatus":
            #     up(1), down(2), testing(3), unknown(4),
            #     secondary(5), notPresent(6), unpowered(7), master(9)
            36,  # MIB object "chasEntPhysPowerType":
            #     0 no power supply, 1 ac, 2 dc (not available on old devices)
        ]),
    "includes": ["alcatel.include"],
}
