#!/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.cpu_util import *  # pylint: disable=wildcard-import,unused-wildcard-import

# 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.mcafee_gateway import *  # pylint: disable=wildcard-import,unused-wildcard-import


def parse_mcafee_emailgateway_entities(info):
    parsed = {}
    for idx, services in enumerate(
        [
            [
                "Temperature",
                "Voltage",
                "Power Supplies",
                "Cooling",
                "Other Modules",
                "UPS",
                "Bridge",
                "RAID",
            ],
            [
                "AV DAT",
                "AV Engine",
                "Spam DAT",
                "Spam Engine",
                "Config Antirelay",
                "Encryption",
                "SMTP",
                "POP3",
                "EPO",
                "TQM-Server",
                "GTI Message",
                "GTM Feedback",
                "GTI File",
                "RBL",
                "R-Syslog",
                "Remote Syslog",
                "LDAP",
                "Remove LDAP",
                "SNMPd",
                "Remove DNS",
                "NTP",
            ],
            ["WEBMC", "Eventhandler", "SMTP Retryer", "Spam Updater", "Postgres", "RMD Merge"],
        ]
    ):
        parsed.update(dict(zip(services, info[idx][0])))
    return parsed


def inventory_mcafee_emailgateway_entities(parsed):
    for title, dev_state in parsed.items():
        if dev_state not in ["10", "11"]:
            yield title, {}


def check_mcafee_emailgateway_entities(item, params, parsed):
    map_states = {
        "0": (0, "healthy"),
        "1": (1, "operational but requires attention"),
        "2": (1, "requires attention"),
        "3": (1, "end of life reached"),
        "4": (1, "near end of life"),
        "5": (2, "corrupt dats"),
        "6": (2, "corrupt configuration"),
        "7": (2, "requires immediate attention"),
        "8": (2, "critical"),
        "9": (3, "unknown state"),
        "10": (1, "disabled"),
        "11": (1, "not applicable"),
    }

    if item in parsed:
        state, state_readable = map_states[parsed[item]]
        return state, "Status: %s" % state_readable


check_info["mcafee_emailgateway_entities"] = {
    "parse_function": parse_mcafee_emailgateway_entities,
    "inventory_function": inventory_mcafee_emailgateway_entities,
    "check_function": check_mcafee_emailgateway_entities,
    "service_description": "Entity %s",
    "snmp_info": [
        (
            ".1.3.6.1.4.1.1230.2.4.1.2.3.2",
            [
                "1",  # hardware-temperature-state
                "2",  # hardware-voltage-state
                "3",  # hardware-power-supplies-state
                "4",  # hardware-cooling-state
                "5",  # hardware-other-modules-state
                "6",  # ups-state
                "7",  # bridge-state
                "8",  # raid-state
            ],
        ),
        (
            ".1.3.6.1.4.1.1230.2.4.1.2.3.4",
            [
                "1",  # av-dat-state
                "2",  # av-eng-state
                "3",  # spam-dat-state
                "4",  # spam-eng-state
                "5",  # config-antirelay-state
                "6",  # encryption-state
                "7",  # smtp-state
                "8",  # pop3-state
                "9",  # epo-state
                "10",  # tqmserver-state
                "11",  # gti-message-state
                "12",  # gti-feedback-state
                "13",  # gti-file-state
                "14",  # rbl-state
                "15",  # rsyslog-state
                "16",  # remote-syslog-state
                "17",  # ldap-state
                "18",  # remote-ldap-state
                "19",  # snmpd-state
                "20",  # remote-dns-state
                "21",  # ntp-state
            ],
        ),
        (
            ".1.3.6.1.4.1.1230.2.4.1.2.3.5",
            [
                "1",  # webmc-state
                "2",  # evthandler-state
                "3",  # smtp-retryer-state
                "4",  # spam-updater-state
                "5",  # postgres-state
                "6",  # rmdmerge-state
            ],
        ),
    ],
    "snmp_scan_function": scan_mcafee_emailgateway,
}
