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


def inventory_h3c_lanswitch_sensors(info):
    return [(h3c_lanswitch_genitem(line[0]), None) for line in info if line[1] in ["1", "2"]]


def check_h3c_lanswitch_sensors(item, params, info):
    # the values are:   active     (1), deactive   (2), not-install  (3), unsupport    (4)
    for (endoid, status) in info:
        if h3c_lanswitch_genitem(endoid) == item:
            if status == "2":
                return (2, "Sensor %s status is %s" % (item, status))
            elif status == "1":
                return (0, "Sensor %s status is %s" % (item, status))
            return (1, "Sensor % status is %s" % (item, status))
    return (3, "Sensor %s not found" % item)


check_info["h3c_lanswitch_sensors"] = (
    check_h3c_lanswitch_sensors,
    "%s",
    0,
    inventory_h3c_lanswitch_sensors,
)


def h3c_lanswitch_genitem(endoid):
    deviceclass, _one, id_ = endoid.split(".")
    if deviceclass == "1":
        hwLswdev = "Fan"
    else:
        hwLswdev = "Powersupply"
    unitid = int(id_) // 65536
    num = int(id_) % 65536
    return "Unit %d %s %d" % (unitid, hwLswdev, num)


# mib: A3COM-HUAWEI-LswDEVM-MIB
# fan table: A3COM-HUAWEI-OID-MIB::lswCommon.9.1.1.1.1.65536 = INTEGER: 65536
# fan status: A3COM-HUAWEI-OID-MIB::lswCommon.9.1.1.1.2.65536 = INTEGER: 1
# hwdevMPowerStatusTable ( hwDevMPowerNum, hwDevMPowerStatus )
# hwdevMFanStatusTable
#
#
# finally,just check fan status
snmp_info["h3c_lanswitch_sensors"] = (
    ".1.3.6.1.4.1.43.45.1.2.23.1.9.1",
    ["1.1", "2.1"],
    [OID_END, "2"],
)

snmp_scan_functions["h3c_lanswitch_sensors"] = (
    lambda oid: "3com s" in oid(".1.3.6.1.2.1.1.1.0").lower()
)
