bin/java-heap-pcnt.sh in sensu-plugins-java-1.2.0 vs bin/java-heap-pcnt.sh in sensu-plugins-java-1.3.0
- old
+ new
@@ -4,21 +4,25 @@
# This was forked from Sensu Community Plugins
# Date: 2013-9-30
# Modified: Mario Harvey - badmadrad.com
+# Date: 2018-08-30
+# Modified: Juan Moreno Martinez - Change MAX HEAP instead Current HEAP
+
# You must have openjdk-7-jdk and openjdk-7-jre packages installed
# http://openjdk.java.net/install/
# Also make sure the user "sensu" can sudo without password
# #RED
-while getopts 'w:c:n:hp' OPT; do
+while getopts 'w:c:n:l:hp' OPT; do
case $OPT in
w) WARN=$OPTARG;;
c) CRIT=$OPTARG;;
n) NAME=$OPTARG;;
+ l) HEAP_MAX=$OPTARG;;
h) hlp="yes";;
p) perform="yes";;
*) unknown="yes";;
esac
done
@@ -30,10 +34,20 @@
-n --> Name of JVM process < value
-w --> Warning Percentage < value
-c --> Critical Percentage < value
-p --> print out performance data
-h --> print this help screen
+ -l --> limit, valid value max or current (default current)
+ current: when -Xms and -Xmx same value
+ max: when -Xms and -Xmx have different values
+
+Requirement: User that launch script must be permisions in sudoers for jps,jstat,jmap
+sudoers lines suggested:
+----------
+sensu ALL=(ALL) NOPASSWD: /usr/bin/jps, /usr/bin/jstat, /usr/bin/jmap
+Defaults:sensu !requiretty
+----------
"
if [ "$hlp" = "yes" ]; then
echo "$HELP"
exit 0
@@ -51,11 +65,17 @@
echo "$COUNT java process(es) found with name $NAME"
exit 3
fi
#Get heap capacity of JVM
-TotalHeap=$(sudo jstat -gccapacity $PID | tail -n 1 | awk '{ print ($4 + $5 + $6 + $10) / 1024 }')
-
+if [ "$HEAP_MAX" == "" ] || [ "$HEAP_MAX" == "current" ]; then
+ TotalHeap=$(sudo ${JAVA_BIN}jstat -gccapacity $PID | tail -n 1 | awk '{ print ($4 + $5 + $6 + $10) / 1024 }')
+elif [[ "$HEAP_MAX" == "max" ]]; then
+ TotalHeap=$(sudo ${JAVA_BIN}jmap -heap $PID 2> /dev/null | grep MaxHeapSize | tr -s " " | tail -n1 | awk '{ print $3 /1024 /1024 }')
+else
+ echo "limit options must be max or current"
+ exit 1
+fi
#Determine amount of used heap JVM is using
UsedHeap=$(sudo jstat -gc $PID | tail -n 1 | awk '{ print ($3 + $4 + $6 + $8 + $10) / 1024 }')
#Get heap usage percentage
HeapPer=$(echo "scale=3; $UsedHeap / $TotalHeap * 100" | bc -l| cut -d "." -f1)