#!/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_hp_webmgmt_status(info):
    for index, _health in info[0]:
        yield index, None


def check_hp_webmgmt_status(item, _no_params, info):
    status_map = {
        "1": (3, "unknown"),
        "2": (3, "unused"),
        "3": (0, "ok"),
        "4": (1, "warning"),
        "5": (2, "critical"),
        "6": (2, "non-recoverable"),
    }

    device_model = info[1][0][0]
    serial_number = info[2][0][0]
    for index, health in info[0]:
        if index == item:
            status, status_msg = status_map[health]
            infotext = "Device status: %s" % status_msg
            if device_model and serial_number:
                infotext += " [Model: %s, Serial Number: %s]" % (device_model, serial_number)
            return status, infotext


check_info["hp_webmgmt_status"] = {
    "check_function": check_hp_webmgmt_status,
    "inventory_function": inventory_hp_webmgmt_status,
    "service_description": "Status %s",
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.11")
    and oid(".1.3.6.1.4.1.11.2.36.1.1.5.1.1.*"),
    "snmp_info": [
        (".1.3.6.1.4.1.11.2.36.1.1.5.1.1", [1, 3]),  # hpHttpMgDeviceIndex  # hpHttpMgDeviceHealth
        (".1.3.6.1.4.1.11.2.36.1.1.5.1.1.9", [1]),  # Device Model
        (".1.3.6.1.4.1.11.2.36.1.1.5.1.1.10", [1]),  # Device Serial Number
    ],
}
