#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-

# 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",
}

