-
Notifications
You must be signed in to change notification settings - Fork 57
/
check_jstat
executable file
·189 lines (170 loc) · 5.48 KB
/
check_jstat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/sh
#
#
# A simple Nagios command that check some statistics of a JAVA JVM.
#
# It first chech that the process specified by its pid (-p) or its
# service name (-s) (assuming there is a /var/run/<name>.pid file
# holding its pid) is running and is a java process.
# It then call jstat -gc and jstat -gccapacity to catch current and
# maximum 'heap' and 'perm' sizes.
# What is called 'heap' here is the edden + old generation space,
# while 'perm' represents the permanent generation space.
# If specified (with -w and -c options) values can be checked with
# WARNING or CRITICAL thresholds (apply to both heap and perm regions).
# This plugin also attach perfomance data to the output:
# pid=<pid>
# heap=<heap-size-used>;<heap-max-size>;<%ratio>;<warning-threshold-%ratio>;<critical-threshold-%ratio>
# perm=<perm-size-used>;<perm-max-size>;<%ratio>;<warning-threshold-%ratio>;<critical-threshold-%ratio>
#
#
# Created: 2012, June
# By: Eric Blanchard
# License: LGPL v2.1
#
# Usage helper for this script
function usage() {
local prog="${1:-check_jstat}"
echo "Usage: $prog -v";
echo " Print version and exit"
echo "Usage: $prog -h";
echo " Print this help and 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.3'
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=$(jps | grep "$java_name" 2>/dev/null)
else
java=$(jps | grep -v Jps 2>/dev/null)
fi
java_count=$(echo "$java" | wc -l)
if [ -z "$java" -o "$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] seems not to be a JAVA application"
exit 2
fi
gc=$(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=$(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))
heapmx=$(($ygcmx + $ogcmx))
heapratio=$((($heap * 100) / $heapmx))
permratio=$((($pu * 100) / $pgcmx))
#echo "youg+old=${heap}k, (Max=${heapmx}k, current=${heapratio}%)"
#echo "perm=${pu}k, (Max=${pgcmx}k, current=${permratio}%)"
perfdata="pid=$pid heap=$heap;$heapmx;$heapratio;$ws;$cs perm=$pu;$pgcmx;$permratio;$ws;$cs"
if [ $cs -gt 0 -a $permratio -ge $cs ]; then
echo "CRITICAL: jstat process $label critical PermGen (${permratio}% of MaxPermSize)|$perfdata"
exit 2
fi
if [ $cs -gt 0 -a $heapratio -ge $cs ]; then
echo "CRITICAL: jstat process $label critical Heap (${heapratio}% of MaxHeapSize)|$perfdata"
exit 2
fi
if [ $ws -gt 0 -a $permratio -ge $ws ]; then
echo "WARNING: jstat process $label warning PermGen (${permratio}% of MaxPermSize)|$perfdata"
exit 1
fi
if [ $ws -gt 0 -a $heapratio -ge $ws ]; then
echo "WARNING: jstat process $label warning Heap (${heapratio}% of MaxHeapSize)|$perfdata"
exit 1
fi
echo "OK: jstat process $label alive|$perfdata"
exit 0
# That's all folks !