#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2019             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_aos7_operability_to_status_mapping = {
    '1': "up",
    '2': "down",
    '3': "testing",
    '4': "unknown",
    '5': "secondary",
    '6': "not present",  # no check status required
    '7': "unpowered",
    '8': "master",
    '9': "idle",
    '10': "power save",
}

alcatel_power_aos7_no_power_supply = "no power supply"

alcatel_power_aos7_power_type_mapping = {
    '0': alcatel_power_aos7_no_power_supply,
    '1': "AC",
    '2': "DC",
}

PowerSupplyEntry = collections.namedtuple('PowerSupplyEntry', 'status_readable power_supply_type')


def parse_alcatel_power_aos7(info):
    return {
        item: PowerSupplyEntry(
            alcatel_power_aos7_operability_to_status_mapping[operability_status],
            alcatel_power_aos7_power_type_mapping[power_supply_type],
        ) for (item, operability_status, power_supply_type) in info
    }


@discover
def inventory_alcatel_power_aos7(_oidend, device):
    return (device.power_supply_type != alcatel_power_aos7_no_power_supply and
            device.status_readable != "not present")


@get_parsed_item_data
def check_alcatel_power_aos7(item, _no_params, device):
    if device.status_readable == 'up':
        status = 0
    else:
        status = 2
    yield status, "[%s] Status: %s" % (device.power_supply_type, device.status_readable)


check_info["alcatel_power_aos7"] = {
    "parse_function": parse_alcatel_power_aos7,
    "check_function": check_alcatel_power_aos7,
    "inventory_function": inventory_alcatel_power_aos7,
    "service_description": "Power Supply %s",
    "snmp_scan_function": alcatel_new_networking_products_scan_function,
    "snmp_info": (
        ".1.3.6.1.4.1.6486.801.1.1.1.1.1.1.1",  # MIB object "chasEntPhysicalEntry"
        [
            OID_END,
            2,  # MIB object "chasEntPhysOperStatus"
            35,  # MIB object "chasEntPhysPowerType"
        ]),
    "includes": ["alcatel.include"],
}
