#!/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.


# .1.3.6.1.4.1.393.200.130.2.1.2.1 = INTEGER: 1
# .1.3.6.1.4.1.393.200.130.2.1.2.2 = INTEGER: 1
# .1.3.6.1.4.1.393.200.130.2.2.1.1.1.1 = INTEGER: 1
# .1.3.6.1.4.1.393.200.130.2.2.1.1.1.2 = INTEGER: 2
# .1.3.6.1.4.1.393.200.130.2.2.1.1.1.3 = INTEGER: 3
# .1.3.6.1.4.1.393.200.130.2.2.1.1.2.1 = STRING: "delivery"
# .1.3.6.1.4.1.393.200.130.2.2.1.1.2.2 = STRING: "inbound"
# .1.3.6.1.4.1.393.200.130.2.2.1.1.2.3 = STRING: "outbound"
# .1.3.6.1.4.1.393.200.130.2.2.1.1.3.1 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.3.2 = Gauge32: 1
# .1.3.6.1.4.1.393.200.130.2.2.1.1.3.3 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.4.1 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.4.2 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.4.3 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.5.1 = Gauge32: 4
# .1.3.6.1.4.1.393.200.130.2.2.1.1.5.2 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.5.3 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.6.1 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.6.2 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.6.3 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.7.1 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.7.2 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.7.3 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.8.1 = Gauge32: 5
# .1.3.6.1.4.1.393.200.130.2.2.1.1.8.2 = Gauge32: 0
# .1.3.6.1.4.1.393.200.130.2.2.1.1.8.3 = Gauge32: 0


def parse_sym_brightmail_queues(info):
    parsed = {}
    for descr, connections, dataRate, deferredMessages, \
        messageRate, queueSize, queuedMessages in info:
        for k, v in [
            ("connections",      connections),
            ("dataRate",         dataRate),
            ("deferredMessages", deferredMessages),
            ("messageRate",      messageRate),
            ("queueSize",        queueSize),
            ("queuedMessages",   queuedMessages),
        ]:
            try:
                parsed.setdefault(descr, {}).setdefault(k, int(v))
            except ValueError:
                pass
    return parsed


def inventory_sym_brightmail_queues(parsed):
    for descr in parsed:
        yield descr, {}


def check_sym_brightmail_queues(item, params, parsed):
    if item not in parsed:
        yield
        return

    data = parsed[item]
    for key, title in [
        ("connections",      "Connections"),
        ("dataRate",         "Data rate"),
        ("deferredMessages", "Deferred messages"),
        ("messageRate",      "Message rate"),
        # Symantec did not document the Unit for the queue. You can still set
        # some level if you wish.
        ("queueSize",        "Queue size"),
        ("queuedMessages",   "Queued messages"),
    ]:
        warn, crit = params.get(key, (None, None))
        value = data.get(key)
        if value is None:
            continue

        infotext = "%s: %s" % (title, value)
        state = 0
        if crit is not None and crit >= crit:
            state = 2
        elif warn is not None and warn >= warn:
            state = 1
        if state:
            infotext += " (warn/crit at %s/%s)" % (warn, crit)
        yield state, infotext


check_info["sym_brightmail_queues"] = {
     "parse_function"       : parse_sym_brightmail_queues,
     "inventory_function"   : inventory_sym_brightmail_queues,
     "check_function"       : check_sym_brightmail_queues,
     "service_description"  : "Queue %s",
     "snmp_scan_function"   : lambda oid: "el5_sms" in oid(".1.3.6.1.2.1.1.1.0").lower() or \
                                          "el6" in oid(".1.3.6.1.2.1.1.1.0").lower(),
     "snmp_info"            : (".1.3.6.1.4.1.393.200.130.2", [
                "2.1.1.2", # InstanceDescr
                "2.1.1.3", # Connections
                "2.1.1.4", # dataRate
                "2.1.1.5", # deferedMessages
                "2.1.1.6", # messageRate
                "2.1.1.7", # queueSize
                "2.1.1.8", # queuedMessages
     ]),
     "group"                : "sym_brightmail_queues",
}
