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

# <<<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 = ( 1000, 1200 )

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):
    for line in info:
        queue = line[1]
        if queue == item:
            messages = int(line[0])
            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
            perf = [ ( "queue", messages, warn, crit ) ]
            if messages >= crit:
                return 2, message, perf
            if messages >= warn:
                return 1, message, perf
            return 0, message, perf

    return 3, "No message queue named %s in agent output" % item

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

