Skip to content

Instantly share code, notes, and snippets.

@diablowu
Created May 13, 2015 07:13
Show Gist options
  • Select an option

  • Save diablowu/493e123cc7b880b3f4ff to your computer and use it in GitHub Desktop.

Select an option

Save diablowu/493e123cc7b880b3f4ff to your computer and use it in GitHub Desktop.
check tomcat server for nagios plugin
#!/bin/sh
##### Created by Pawan #####
##### Vesion 1.4 #####
# Usage helper for this script
function usage() {
local prog="${1:-check_jstat.sh}"
echo "Usage: $prog -v";
echo " Print version and exit"
echo "Usage: $prog -h";
echo " Print this help nd exit"
echo "Usage: $prog -p <pid> [-w <%ratio>] [-c <%ratio>]";
echo "Usage: $prog -s <service> [-w <%ratio>] [-c <%ratio>]";
echo "Usage: $prog -j <java-name> [-w <%ratio>] [-c <%ratio>]";
echo " -p <pid> the PID of process to monitor"
echo " -s <service> the service name of process to monitor"
echo " -j <java-name> the java app (see jps) process to monitor"
echo " if this name in blank (-j '') any java app is"
echo " looked for (as long there is only one)"
echo " -w <%> the warning threshold ratio current/max in %"
echo " -c <%> the critical threshold ratio current/max in %"
}
#VERSION='1.2'
service=''
pid=''
ws=-1
cs=-1
use_jps=0
while getopts hvp:s:j:w:c: opt ; do
case ${opt} in
v) echo "$0 version $VERSION"
exit 0
;;
h) usage $0
exit 3
;;
p) pid="${OPTARG}"
;;
s) service="${OPTARG}"
;;
j) java_name="${OPTARG}"
use_jps=1
;;
w) ws="${OPTARG}"
;;
c) cs="${OPTARG}"
;;
esac
done
if [ -z "$pid" -a -z "$service" -a $use_jps -eq 0 ] ; then
echo "One of -p, -s or -j parameter must be provided"
usage $0
exit 3
fi
if [ -n "$pid" -a -n "$service" ] ; then
echo "Only one of -p or -s parameter must be provided"
usage $0
exit 3
fi
if [ -n "$pid" -a $use_jps -eq 1 ] ; then
echo "Only one of -p or -j parameter must be provided"
usage $0
exit 3
fi
if [ -n "$service" -a $use_jps -eq 1 ] ; then
echo "Only one of -s or -j parameter must be provided"
usage $0
exit 3
fi
if [ $use_jps -eq 1 ] ; then
if [ -n "$java_name" ] ; then
java=$( sudo -u user jps | grep "$java_name" 2>/dev/null)
else
java=$( sudo -u user jps | grep -v Jps 2>/dev/null)
fi
java_count=$(echo "$java" | wc -l)
if [ "$java_count" != "1" ] ; then
echo "UNKNOWN: No (or multiple) java app found"
exit 3
fi
pid=$(echo "$java" | cut -d ' ' -f 1)
label=${java_name:-$(echo "$java" | cut -d ' ' -f 2)}
elif [ -n "$service" ] ; then
if [ ! -r /var/run/${service}.pid ] ; then
echo "/var/run/${service}.pid not found"
exit 3
fi
pid=$(cat /var/run/${service}.pid)
label=$service
else
label=$pid
fi
if [ ! -d /proc/$pid ] ; then
echo "CRITICAL: process pid[$pid] not found"
exit 2
fi
proc_name=$(cat /proc/$pid/status | grep 'Name:' | sed -e 's/Name:[ \t]*//')
if [ "$proc_name" != "java" ]; then
echo "CRITICAL: process pid[$pid] Tomcat Service Not Running"
exit 2
fi
gc=$( sudo -u user jstat -gc $pid | tail -1 | sed -e 's/[ ][ ]*/ /g')
if [ -z "$gc" ]; then
echo "CRITICAL: Can't get GC statistics"
exit 2
fi
#echo "gc=$gc"
set -- $gc
eu=$(expr "${6}" : '\([0-9]\+\)')
ou=$(expr "${8}" : '\([0-9]\+\)')
pu=$(expr "${10}" : '\([0-9]\+\)')
gccapacity=$( sudo -u user jstat -gccapacity $pid | tail -1 | sed -e 's/[ ][ ]*/ /g')
if [ -z "$gccapacity" ]; then
echo "CRITICAL: Can't get GC capacity"
exit 2
fi
#echo "gccapacity=$gccapacity"
set -- $gccapacity
ygcmx=$(expr "${2}" : '\([0-9]\+\)')
ogcmx=$(expr "${8}" : '\([0-9]\+\)')
pgcmx=$(expr "${12}" : '\([0-9]\+\)')
#echo "eu=${eu}k ygcmx=${ygcmx}k"
#echo "ou=${ou}k ogcmx=${ogcmx}k"
#echo "pu=${pu}k pgcmx=${pgcmx}k"
heap=$((($eu + $ou ) / 1024))
heapmx=$((($ygcmx + $ogcmx) / 1024))
heapratio=$((($heap * 100) / $heapmx))
permratio=$((($pu * 100) / $pgcmx))
pumb=$((($pu ) / 1024))
pgcmxmb=$((($pgcmx ) / 1024))
#echo "youg+old=${heap}k, (Max=${heapmx}k, current=${heapratio}%)"
#echo "perm=${pu}k, (Max=${pgcmx}k, current=${permratio}%)"
#perfdata="pid=$pid heap=$heap MB;$heapmx MB;$heapratio;$ws;$cs perm=$pumb MB;$pgcmxmb MB;$permratio;$ws;$cs"
perfdata="pid=$pid heap=$heap MB;$heapmx MB; $heapratio% perm=$pumb MB;$pgcmxmb MB; $permratio%"
if [ $cs -gt 0 -a $permratio -ge $cs ]; then
echo "CRITICAL: Process Tomcat critical PermGen size - $perfdata"
exit 2
fi
if [ $cs -gt 0 -a $heapratio -ge $cs ]; then
echo "CRITICAL: Process Tomcat critical Heap size - $perfdata"
exit 2
fi
if [ $ws -gt 0 -a $permratio -ge $ws ]; then
echo "WARNING: Process Tomcat warning PermGen size - $perfdata"
exit 1
fi
if [ $ws -gt 0 -a $heapratio -ge $ws ]; then
echo "WARNING: Process Tomcat warning Heap size - $perfdata"
exit 1
fi
echo "OK: Process Tomcat alive - $perfdata"
exit 0
####### Usages of command
###### command[check_tomcat]=/usr/lib64/nagios/plugins/check_tomcat -j Boot -w 80 -c 90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment