#!/bin/sh
#
# check_ctmserver plugin for nagios
#
# usage:
#    check_ctmserver controlm_server_user_dir controlm_server_dir
#
# Check if all important Control-M/Server processes are working
# using .ctmprofile and shctm script
#
# Requires permissions to read and execute the Control-M/Server scripts, for example the Nagios user is a member of the Control-M/Server user's group
#
# Use this as example
#
# command[check_ctmserver]=/usr/lib64/nagios/plugins/check_ctmserver /home/ctmuser /home/ctmuser/ctm_server
#
# initial version: 13 November 2014 by Tomasz Wladyczanski
#
# Copyright Notice: GPL
#
# https://www.linkedin.com/in/tomekw
# http://uid-0.blogspot.com
#
#

. ${1}/.ctmprofile
PATH=$PATH:${2}/scripts
SHOWCTM=`shctm`
CTMCE=`echo "$SHOWCTM" | grep "java -Dp_ctm=com.bmc.ctms.ctmce.CtmCe"`
CTMWD=`echo "$SHOWCTM" | grep "p_ctmwd"`
CTMNS=`echo "$SHOWCTM" | grep "p_ctmns"`
CTMSU=`echo "$SHOWCTM" | grep "p_ctmsu"`
CTMRT=`echo "$SHOWCTM" | grep "p_ctmrt"`
CTMTR=`echo "$SHOWCTM" | grep "p_ctmtr"`
REZULTAT=0
if [ "${CTMCE}" = "" ]
then
  CTMCE="CE is OFF"
  REZULTAT=2
else
  CTMCE="CE is ON"
fi
if [ "${CTMWD}" = "" ]
then
  CTMWD="WD is OFF"
  REZULTAT=2
else
  CTMWD="WD is ON"
fi
if [ "${CTMNS}" = "" ]
then
  CTMNS="NS is OFF"
  REZULTAT=2
else
  CTMNS="NS is ON"
fi
if [ "${CTMSU}" = "" ]
then
  CTMSU="SU is OFF"
  REZULTAT=2
else
  CTMSU="SU is ON"
fi
if [ "${CTMRT}" = "" ]
then
  CTMRT="RT is OFF"
  REZULTAT=2
else
  CTMRT="RT is ON"
fi
if [ "${CTMTR}" = "" ]
then
  CTMTR="TR is OFF"
  REZULTAT=2
else
  CTMTR="TR is ON"
fi
if [ $REZULTAT = 2 ]
then
  echo -n "At least some important processes are not working: "
  echo $CTMSU", "$CTMCE", "$CTMWD", "$CTMNS", "$CTMRT", "$CTMTR"."
else
  echo -n "All important processes are working (SU, CE, WD, NS, RT, TR)."
fi
exit $REZULTAT

