#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2017             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.


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,
    'includes': ['mcafee_gateway.include', 'cpu_util.include'],
}
