#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.cisco_sensor_item import *  # pylint: disable=wildcard-import,unused-wildcard-import
# .1.3.6.1.4.1.9.9.13.1.5.1.2.1 "removed"
# .1.3.6.1.4.1.9.9.13.1.5.1.2.2 "AC Power Supply"
# .1.3.6.1.4.1.9.9.13.1.5.1.3.1 5
# .1.3.6.1.4.1.9.9.13.1.5.1.3.2 1
# .1.3.6.1.4.1.9.9.13.1.5.1.4.1 1
# .1.3.6.1.4.1.9.9.13.1.5.1.4.2 2

cisco_power_states = (
    '',
    'normal',
    'warning',
    'critical',
    'shutdown',
    'not present',
    'not functioning',
)

cisco_power_sources = (
    '',
    'unknown',
    'AC',
    'DC',
    'external power supply',
    'internal redundant',
)


def inventory_cisco_power(info):
    # Note: the name of the power supply is not unique. We have seen
    # a Cisco with four entries in the MIB. So we force uniqueness
    # by appending a "/4" for ID 4 if the name is not unique
    discovered = {}
    for sid, textinfo, state, _source in info:
        if state != '5':
            name = cisco_sensor_item(textinfo, sid)
            discovered.setdefault(name, []).append(sid)

    for name, entries in discovered.items():
        if len(entries) == 1:
            yield name, None
        else:
            for entry in entries:
                yield ("%s %s" % (name, entry)), None


def check_cisco_power(item, no_params, info):
    for sid, textinfo, state, source in info:
        if cisco_sensor_item(textinfo, sid) == item or\
                cisco_sensor_item(textinfo, sid)+" "+sid == item or\
                cisco_sensor_item(textinfo, sid)+"/"+sid == item:
            state = int(state)
            source = int(source)
            output = 'Status: %s, Source: %s' % \
                (cisco_power_states[state], cisco_power_sources[source])
            if state == 1:
                return 0, "%s" % output
            elif state == 2:
                return 1, "%s" % output
            return 2, "%s" % output


check_info["cisco_power"] = {
    'check_function': check_cisco_power,
    'inventory_function': inventory_cisco_power,
    'service_description': 'Power %s',
    'snmp_info': (
        '.1.3.6.1.4.1.9.9.13.1.5.1',
        [  # ciscoEnvMonSupplyStatusEntry
            OID_END,
            2,  # ciscoEnvMonSupplyStatusDescr
            3,  # ciscoEnvMonSupplyState
            4  # ciscoEnvMonSupplySource
        ]),
    'snmp_scan_function': lambda oid: "cisco" in oid(".1.3.6.1.2.1.1.1.0").lower(),
}
