-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava-pcnt.sh
More file actions
101 lines (85 loc) · 3.11 KB
/
Copy pathjava-pcnt.sh
File metadata and controls
101 lines (85 loc) · 3.11 KB
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
#!/usr/bin/env bash
# Converted by Beau Bilyeu (beau.bilyeu@gmail.com) to cover all use
# cases for polling graphite-bound Java stats
#
# Collect metrics on your JVM and allow you to trace usage in graphite
# Modified: Mario Harvey - badmadrad.com
#
# You must have openjdk-7-jdk and openjdk-7-jre packages installed
# http://openjdk.java.net/install/
#
# Example Use: ./java-pct -t systemd -w 90 -c 95 -n jira.somedomain.com
# Ex Output: HEAP OK - jvm heap usage: 73% | 4499.92 MB out of 6144 MB
# Also make sure the user "sensu" can ${SUDO} without password
while getopts 't:w:c:n:hp' OPT; do
case $OPT in
t) TYPE=$OPTARG;;
w) WARN=$OPTARG;;
c) CRIT=$OPTARG;;
n) NAME=$OPTARG;;
h) hlp="yes";;
*) unknown="yes";;
esac
done
# usage
HELP="
usage: $0 [ -t type -n value -w value -c value -p -h ]
-n --> Name of JVM process < value
-w --> Warning Percentage < value
-c --> Critical Percentage < value
-h --> print this help screen
"
if [ "$hlp" == "yes" ]; then
echo "$HELP"
exit 0
fi
## ensure we have a valid type
if [ "$TYPE" != "command" ] && [ "$TYPE" != "daemontools" ] && [ "$TYPE" != "systemd" ]; then
echo -e "Invalid type. Type must be 'command', 'daemontools', or 'systemd'.$HELP"
exit 1
fi
[ -r /etc/profile.d/java.sh ] && . /etc/profile.d/java.sh
export PATH=${PATH}:/usr/local/bin
NAME=${NAME:=0}
PID=0
RETVAL=""
# Get the absolute path to needed binaries to avoid sudo pathing weirdness later on
svstat=$(command -v svstat 2>/dev/null)
jstat=$(command -v jstat 2>/dev/null)
if [ -z "$svstat" ] && [ -z "$jstat" ]; then
echo -e "ERROR: jstat and svstat not installed\n"
exit 1
fi
# Get PID of JVM.
case $TYPE in
command) PID=$(ps aux | grep "${NAME}" | grep -v grep | grep -v "${0}" | awk '{print $2}');;
daemontools) PID=$(${svstat} /service/j2ee_${NAME}/ 2>/dev/null | grep -oP "\(pid \d*\)" | grep -oP "\d+");;
systemd) PID=$(systemctl status -n 0 j2ee_${NAME}.service 2>/dev/null | grep PID | awk '{ print $3 }');;
esac
# Get the UID & associated user running the JVM.
PUID=$(grep Uid /proc/${PID}/status | awk '{print $2}')
USER=$(getent passwd ${PUID} | awk -F: '{ print $1 }')
# Get heap capacity of JVM
TotalHeap=$(su ${USER} -c "${jstat} -gccapacity $PID | tail -n 1 | awk '{ print (\$4 + \$5 + \$6 + \$10) / 1024 }'" 2> /dev/null)
UsedHeap=$(su ${USER} -c "${jstat} -gc ${PID} | tail -n 1 | awk '{ print (\$3 + \$4 + \$6 + \$8) / 1024 }'" 2> /dev/null)
HeapPer=$(echo "scale=3; $UsedHeap / $TotalHeap * 100" | bc -l| cut -d "." -f1)
WarnTotalHeap=$(echo "scale=3; $TotalHeap * $WARN * 0.01" | bc -l| cut -d "." -f1)
CritTotalHeap=$(echo "scale=3; $TotalHeap * $CRIT * 0.01" | bc -l| cut -d "." -f1)
if [ "$HeapPer" = "" ]; then
echo "HEAP UNKNOWN -"
exit 3
fi
## output line
output="jvm heap usage: $HeapPer% | heap_usage="$HeapPer"%;$WARN;$CRIT;0 heap_mb=${UsedHeap}MB;$WarnTotalHeap;$CritTotalHeap;0;$TotalHeap"
if (( $HeapPer >= $CRIT )); then
echo "HEAP CRITICAL - $output"
exit 2
elif (( $HeapPer >= $WARN )); then
echo "HEAP WARNING - $output"
exit 1
else
echo "HEAP OK - $output"
exit 0
fi
# exit normally
exit 0