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

# EES-POWER-MIB::SYSTEMSTATUS.0  .1.3.6.1.4.1.6302.2.1.2.1.0
# OPERATIONAL VALUES:
#                (1) UNKNOWN - STATUS HAS NOT YET BEEN DEFINED
#                (2) NORMAL - THERE ARE NO ACTIVATED ALARMS
#                (3) OBSERVATION - OA, LOWEST LEVEL OF 'ABNORMAL' STATUS
#                (4) WARNING - A3
#                (5) MINOR - MA
#                (6) MAJOR - CA, HIGHEST LEVEL OF 'ABNORMAL' STATUS
#                ADMINISTRATIVE VALUES:
#                (7) UNMANAGED
#                (8) RESTRICTED
#                (9) TESTING
#                (10) DISABLED"
#        SYNTAX INTEGER {
#                UNKNOWN(1),
#                NORMAL(2),
#                OBSERVATION(3),
#                WARNING(4),
#                MINOR(5),
#                MAJOR(6),
#                UNMANAGED(7),
#                RESTRICTED(8),
#                TESTING(9),
#                DISABLED(10) }

# the mib is the NetSure_ESNA.mib, which we have received directly
# from a customer, it is named "Emerson Energy Systems (EES) Power MIB"

emerson_stat_default = (0, 0)  # warning / critical, unused


def inventory_emerson_stat(info):
    if info:
        return [(None, "emerson_stat_default")]


def check_emerson_stat(item, params, info):
    if info:
        status_text = {
            1: "unknown",
            2: "normal",
            3: "observation",
            4: "warning - A3",
            5: "minor - MA",
            6: "major - CA",
            7: "unmanaged",
            8: "restricted",
            9: "testing",
            10: "disabled"
        }
        status = saveint(info[0][0])
        infotext = "Status: " + status_text.get(status)

        state = 0
        if status in [5, 6, 10]:
            state = 2
        elif status in [1, 3, 4, 7, 8, 9]:
            state = 1

        return (state, infotext)

    return (3, "Status not found in SNMP output")


check_info['emerson_stat'] = {
    "inventory_function": inventory_emerson_stat,
    "check_function": check_emerson_stat,
    "service_description": "Status",
    "snmp_info": (".1.3.6.1.4.1.6302.2.1.2.1", ["0"]),
    "snmp_scan_function": lambda oid: oid('.1.3.6.1.4.1.6302.2.1.1.1.0', "").startswith(
        'Emerson Network Power'),
}
