#!/bin/sh
# Monitor Websphere MQ
# WWI  Version 18.05.2016
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | 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.

# plugin for websphere_mq_* checks

if [ "$1" = "" ]
then
    # wwi --------------------------------------
    # deletion of "-"
    # preventing a faulty "TCP Connection" section of the main agent
    # by using the "-" option, the AIX environment show's the message "[YOU HAVE NEW MAIL]"
    # before the section start: <<<websphere_mq_channels>>>
    # so the next section will be useless for the OMD server check.
    #    su - mqm -c "/usr/lib/check_mk_agent/plugins/websphere_mq run"
    su mqm -c "/usr/lib/check_mk_agent/plugins/websphere_mq run"

else
    # Loop over all local mq instances
    for QM in $( ps -ef|grep [/]usr/mqm/bin/runmqlsr|awk -v FS="-m" '{print $2}'|awk '{print $1}'| uniq)
    do
        echo '<<<websphere_mq_channels>>>'
        for i in `echo " display CHANNEL (*) TYPE (SDR) " | /usr/bin/runmqsc $QM | grep CHLTYPE | \
            grep -v SYSTEM | awk '{print $1}'`; do

            j=`echo "display $i " | /usr/bin/runmqsc $QM | grep XMITQ | tr " " "\n" | \
                grep XMITQ | sed '1,$s/(/ /g' | sed '1,$s/)/ /g'| awk '{print $2 }'`

            a=`echo " display qlocal ($j) CURDEPTH " | /usr/bin/runmqsc $QM | grep CURDEPTH | \
                tr " " "\n" | grep CURDEPTH | sed '1,$s/(/ /g' | sed '1,$s/)/ /g'| \
                awk '{print $2 }' | tr "\n" " "`

            c=`echo " display qlocal ($j) MAXDEPTH  " | /usr/bin/runmqsc $QM | grep MAXDEPTH | \
                tr " " "\n" | grep MAXDEPTH | sed '1,$s/(/ /g' | sed '1,$s/)/ /g'| \
                awk '{print $2 }' | tr "\n" " "`

            l=`echo $i | sed '1,$s/(/ /g' | sed '1,$s/)/ /g'| awk '{print $2 }'`

            s=`echo " display chstatus($l)" | /usr/bin/runmqsc $QM | grep STATUS | tail -1 | \
               sed '1,$s/(/ /g' | sed '1,$s/)/ /g'| awk '{print $NF }'`

            if [ "$s" = "" ]
            then
                s="Unknown"
            fi
            echo "$a  $i $c $s"
        done

        echo '<<<websphere_mq_queues>>>'
        for t in `echo " display queue (*) where (USAGE EQ NORMAL) " | /usr/bin/runmqsc $QM | \
            grep -v SYSTEM | grep -v MQMON | grep -v MONITOR | grep -v _T0 |  grep -v _T1 |  \
            grep -v _T2 | grep -v _T3 | grep -v mqtest | grep QUEUE | awk '{ print $1 }' | \
            sed '1,$s/(/ /g' | sed '1,$s/)/ /g'| awk '{print $2 }'`; do

            # wwi MQ admin change, to get more queues which are needed
            a=`echo " display qlocal ($t) CURDEPTH  " | /usr/bin/runmqsc $QM | grep CURDEPTH  | \
                tr " " "\n" | grep CURDEPTH  | sed '1,$s/(/ /g' | sed '1,$s/)/ /g'| \
                awk '{print $2 }' | tr "\n" " "`

            b=`echo " display qlocal ($t) MAXDEPTH  " | /usr/bin/runmqsc $QM | grep MAXDEPTH | \
                tr " " "\n" | grep MAXDEPTH | sed '1,$s/(/ /g' | sed '1,$s/)/ /g'| \
                awk '{print $2 }' | tr "\n" " "`

            c=`echo " dis qs($t) monitor " | /usr/bin/runmqsc $QM | grep -e LGETDATE -e LGETTIME | \
                tr '\n' ' '| awk -v FS="LPUTDATE" '{print $1}'|sed 's/ //g'`

            NOW=$(date +%Y_%m_%d"-"%H_%M_%S)

            # Muster: Anzahl eingehender Messages $a auf $t Max-Queues $b
            # wwi -------------------------

            if [ "$a" == "" ] || [ "$a" == " " ]; then
               a=" 0"
               t=" $t"
               c="CURDEPTH(0)LGETDATE()LGETTIME()"
            fi

            if [ "$b" == "" ]|| [ "$b" == " " ]; then
               b=0
               c="CURDEPTH(0)LGETDATE()LGETTIME()"
            fi

            if [ "$c" == "" ]|| [ "$c" == " " ]; then
               c="CURDEPTH(0)LGETDATE()LGETTIME()"
            fi

            echo "$a $t $b $c $NOW"
        done # for t
    done # for QM
fi


if type dspmq > /dev/null; then
    echo "<<<websphere_mq_instance:sep(41)>>>"
    dspmq -x
    echo "MQv"$(dspmqver|grep -e Version -e ^Level -e Mode|awk -v FS=":" '{print $2","}'|tr -d ' '|tr -d '\n'|sed 's/,$/;/g')
    dspmq -o all
fi
