#!/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 output from agent:
# [[SINGLE_ITEM_EXPORT_int_jens]]
# 0 0 0 0
# [[SPRINGAPP-COMMAND-INBOX-DEV]]
# 0 0 15 15
# [[SINGLE_ITEM_EXPORT_INT_jens]]
# 0 0 0 0
# [[DEBITOR_LOCATION]]
# 0 1 84 84
# [[EDATA_SERIALNUMBERQUERY_INBOX]]
# 0 0 0 0


mq_queues_default_levels = {
    "size" : (None, None),
    "consumerCount" : (None, None),
}

def inventory_mq_queues(info):
    inventory = []
    for line in info:
        if line[0].startswith('[['):
            item = line[0][2:-2]
            inventory.append((item, mq_queues_default_levels))
    return inventory

def check_mq_queues(item, params, info):
    found = False
    for line in info:
        if found == True:
            size, consumerCount, enqueueCount, dequeueCount = map(int, line)
            msg = ""
            state = 0
            warn, crit = params['consumerCount']
            if crit and consumerCount < crit:
                state = 2
                label = "(!!)"
            elif warn and consumerCount < warn:
                state = 1
                label = "(!)"
            if state > 0:
                msg = "%s consuming connections " % consumerCount
                msg += "(Levels Warn/Crit below %s/%s)%s, " % (warn, crit, label)


            label = ""
            warn, crit = params['size']
            if crit and size >= crit:
                state = 2
                label = "(!!)"
            elif warn and size >= warn:
                state = max(state, 1)
                label = "(!)"
            msg += "Queue Size: %s" %  size
            if label != "":
                msg += "(Levels Warn/Crit at %s/%s)%s" % (warn, crit, label)
            msg += ", Enqueue Count: %s, Dequeue Count: %s" % (enqueueCount, dequeueCount )


            perf = [("queue", size, warn, crit), ("enque", enqueueCount), ("deque", dequeueCount) ]
            return state, msg, perf
        if line[0].startswith('[[') and line[0][2:-2] == item:
            found = True
    return 2, "Queue not found"

check_info["mq_queues"] = {
    "check_function"        : check_mq_queues,
    "inventory_function"    : inventory_mq_queues,
    "service_description"   : "Queue %s",
    "has_perfdata"          : True,
    "group"                 : "mq_queues",
}

