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

# .1.3.6.1.4.1.2604.3.4 2 --> SOPHOS::sophosHwMemoryConsumption     Indicates whether the appliance is consuming excessive memory
# .1.3.6.1.4.1.2604.3.5 2 --> SOPHOS::sophosHwMemoryStatus          Indicates whether the appliance detects less memory than expected
# .1.3.6.1.4.1.2604.3.6 0 --> SOPHOS::sophosHwRaid                  Indicates whether the appliance RAID system is operating normally
# .1.3.6.1.4.1.2604.3.7 2 --> SOPHOS::sophosHwCpuStatus             Indicates whether the appliance CPU is operating normally
# .1.3.6.1.4.1.2604.3.8 0 --> SOPHOS::sophosHwPowerSupplyLeft       Indicates whether the left power supply is operating normally
# .1.3.6.1.4.1.2604.3.9 0 --> SOPHOS::sophosHwPowerSupplyRight      Indicates whether the right power supply is operating normally
# .1.3.6.1.4.1.2604.3.10 0 --> SOPHOS::sophosHwPowerSupplyFanLeft   Indicates whether the left power supply fan is operating normally
# .1.3.6.1.4.1.2604.3.11 0 --> SOPHOS::sophosHwPowerSupplyFanRight  Indicates whether the left power supply fan is operating normally
# .1.3.6.1.4.1.2604.3.12 2 --> SOPHOS::sophosHwSystemFan            Indicates whether all fans are operating normally
# .1.3.6.1.4.1.2604.3.13 2 --> SOPHOS::sophosHwTemperature          Indicates whether the appliance is operating within an acceptable temperature range
# .1.3.6.1.4.1.2604.3.14 2 --> SOPHOS::sophosHwVoltage              Indicates whether the appliance is operating within normal voltage ranges
# .1.3.6.1.4.1.2604.3.16 2 --> SOPHOS::sophosHwPowerSupplies        Indicates whether both power supplies are operating normally


sophos_map_oid = {
    "4"  : ("Memory Consumption", {
                "2" : " (the appliance is consuming normal memory)",
                "3" : " (the appliance is consuming inflated memory)",
                "4" : " (the appliance is consuming excessive memory)",
           }),
    "5"  : ("Memory", {
                "2" : " (the appliance detects memory than expected)",
                "3" : " (the appliance detects less memory than expected)",
                "4" : " (the appliance detects insufficient memory than expected)",
           }),
    "6"  : ("RAID", {
                "2" : " (the appliance RAID system is operating normally)",
                "3" : " (the appliance RAID system is operating abnormally)",
                "4" : " (the appliance RAID system is operating viciously)",
           }),
    "7"  : ("CPU Utilization", {
                "2" : " (the appliance CPU is operating normally)",
                "3" : " (the appliance CPU is operating abnormally)",
                "4" : " (the appliance CPU is operating viciously)",
           }),
    "8"  : ("Power Supply Left", {
                "2" : " (the left power supply is operating normally)",
                "3" : " (the left power supply is operating abnormally)",
                "4" : " (the left power supply is operating viciously)",
           }),
    "9"  : ("Power Supply Right", {
                "2" : " (the right power supply is operating normally)",
                "3" : " (the right power supply is operating abnormally)",
                "4" : " (the right power supply is operating viciously)",
           }),
    "10" : ("Fan Left", {
                "2" : " (the left fan is operating normally)",
                "3" : " (the left fan is operating abnormally)",
                "4" : " (the left fan is operating viciously)",
           }),
    "11" : ("Fan Right", {
                "2" : " (the right fan is operating normally)",
                "3" : " (the right fan is operating abnormally)",
                "4" : " (the right fan is operating viciously)",
           }),
    "12" : ("Fan Summary", {
                "2" : " (all fans are operating normally)",
                "3" : " (all fans are operating abnormally)",
                "4" : " (all fans are operating viciously)",
           }),
    "13" : ("Temperature", {
                "2" : " (the appliance is operating within an acceptable temperature range)",
                "3" : " (the appliance is operating in the margins of an acceptable temperature range)",
                "4" : " (the appliance is operating outside an acceptable temperature range)",
           }),
    "14" : ("Power Supply Voltage", {
                "2" : " (the appliance is operating within a normal voltage range)",
                "3" : " (the appliance is operating in the margins of a normal voltage range)",
                "4" : " (the appliance is operating outside of a normal voltage range)",
           }),
    "16" : ("Power Supply Summary", {
                "2" : " (both power supplies are operating normally)",
                "3" : " (one or both power supplies are operating abnormally)",
                "4" : " (one or both power supplies are operating viciously)",
           }),
}


def inventory_sophos(info):
    inventory = []
    for item_oid, item_state in info:
        if item_oid in sophos_map_oid and item_state in ["2", "3", "4"]:
            inventory.append( (sophos_map_oid[item_oid][0], None) )
    return inventory


def check_sophos(item, _no_params, info):
    sophos_map_state = {
        "0" : (3, "unknown"),
        "1" : (3, "disabled"),
        "2" : (0, "OK"),
        "3" : (1, "warn"),
        "4" : (2, "error"),
    }
    for item_oid, item_state in info:
        item_name, item_info = sophos_map_oid.get(item_oid, (None, None))
        if item_name == item:
            state, state_readable = sophos_map_state[item_state]
            extra_info = item_info.get(item_state, "")
            infotext = "Status: %s%s" % (state_readable, extra_info)
            return state, infotext


check_info['sophos'] = {
    'inventory_function'        : inventory_sophos,
    'check_function'            : check_sophos,
    'service_description'       : '%s',
    'snmp_info'                 : (".1.3.6.1.4.1.2604.3",[ OID_END, "" ]),
    'snmp_scan_function'        : lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.2604",
}
