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

# <<<websphere_mq_queues>>>
# 0 CD.ISS.CATSOS.REPLY.C000052 5000
# 0 CD.ISS.COBA.REPLY.C000052 5000
# 0 CD.ISS.DEUBA.REPLY.C000052 5000
# 0 CD.ISS.TIQS.REPLY.C000052 5000
# 0 CD.ISS.VWD.REPLY.C000052 5000

# Old output
# <<<websphere_mq_queues>>>
# 0 CD.ISS.CATSOS.REPLY.C000052
# 0 CD.ISS.COBA.REPLY.C000052
# 0 CD.ISS.DEUBA.REPLY.C000052
# 0 CD.ISS.TIQS.REPLY.C000052
# 0 CD.ISS.VWD.REPLY.C000052

websphere_mq_queues_default_levels = {
    'message_count'     : ( 1000, 1200 ),
    'message_count_perc': ( 80.0, 90.0 )
}

def inventory_websphere_mq_queues(info):
    return [ ( x[1], 'websphere_mq_queues_default_levels' ) for x in info  ]

def check_websphere_mq_queues(item, params, info):
    if isinstance(params, tuple):
        params = {'message_count': params,
                  'message_count_perc': websphere_mq_queues_default_levels["message_count_perc"]}

    for line in info:
        queue = line[1]
        if queue == item:
            messages = int(line[0])
            queue_length = None
            if len(line) >= 3:
                queue_length = int(line[2])
                length_info = "/%d" % queue_length
            else:
                length_info = ""
            message = "%d%s messages in queue" % ( messages, length_info )

            warn, crit = params['message_count']
            perf = [ ( "queue", messages, warn, crit, None, queue_length ) ]

            state = 0
            levels_info = []
            if messages >= crit:
                state = 2
            elif messages >= warn:
                state = 1
            if state:
                levels_info.append("Levels at %s/%s" % params["message_count"])

            perc_state = 0
            if queue_length:
                warn_perc, crit_perc = params['message_count_perc']
                used_perc = messages / float(queue_length) * 100
                if used_perc >= crit_perc:
                    perc_state = 2
                elif used_perc >= warn_perc:
                    perc_state = 1
                if perc_state:
                    levels_info.append("Levels at %s%%/%s%%" % params["message_count_perc"])

            levels_text = ""
            if levels_info:
                levels_text = ", (%s)" % ", ".join(levels_info)
            return max(state, perc_state), message + levels_text, perf

check_info["websphere_mq_queues"] = {
    "group"                 : "websphere_mq",
    "check_function"        : check_websphere_mq_queues,
    "inventory_function"    : inventory_websphere_mq_queues,
    "service_description"   : "MQ Queue %s",
    "has_perfdata"          : True,
}

