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


# Example part from SNMP walk:
# .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

# Legacy default
#brightmail_message_levels = {}

def inventory_sym_brightmail_queues(info):
    inventory = []
    for line in info:
        inventory.append((line[0], {}))
    return inventory


def check_sym_brightmail_queues(item, params, info):
    state = 0

    for line in info:
        if line[0] == item:
            connections, dataRate, deferredMessages, \
            messageRate, queueSize, queuedMessages = line[1:]
            msg = ''
            for label, value, (warn, crit) in [
                    ("connections",      int(connections),      params.get("connections",     (None, None))),
                    ("messageRate",      int(messageRate),      params.get("messageRate",     (None, None))),
                    ("dataRate",         int(dataRate),         params.get("dataRate",        (None, None))),
                    ("queuedMessages",   int(queuedMessages),   params.get("queuedMessages",  (None, None))),
                    # Symantec did not document the Unit for the queue. You can still set
                    # some level if you wish.
                    ("queuesize",        int(queueSize),        params.get("queueSize",       (None, None))),
                    ("deferredMessages", int(deferredMessages), params.get("deferredMessages",(None, None))),
                   ]:

                single_state = 0

                if   crit is not None and value >=  crit:
                    single_state = (max(state, 2))
                elif warn is not None and value >=  warn:
                    single_state = (max(state, 1))
                state = (max(single_state, state))
                msg += "%s:%d" % (label, value) + "!" * single_state + " "
                # Perfdata: maybe one graph per Queue with connections, messageRate, queuedMessages, deferredMessages
                # and another for queuesize and datarate

            return (state, msg)



    return (3, "Queue not found in agent output")


check_info["sym_brightmail_queues"] = {
     "check_function"           : check_sym_brightmail_queues,
     "group"                    : "sym_brightmail_queues",
     "inventory_function"       : inventory_sym_brightmail_queues,
     "service_description"      : "Queue %s",
     "has_perfdata"             : False,
     "snmp_scan_function"       : lambda oid: "el5_sms" 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
     ]),
}
