Skip to content

Instantly share code, notes, and snippets.

@mallowlabs
Last active January 17, 2018 05:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mallowlabs/bc7572cd91989903973c to your computer and use it in GitHub Desktop.
Save mallowlabs/bc7572cd91989903973c to your computer and use it in GitHub Desktop.
Munin jstat__heap plugin for Oracle Java 7/8
#!/bin/bash
#
# Plugin for monitor JVM activity - Heap Usage -
#
# Usage:
#
# Symlink into /etc/munin/plugins/ and add the monitored
# alias name like :
#
# ln -s /usr/share/munin/plugins/jstat__heap \
# /etc/munin/plugins/jstat_<jvm alias>_heap
# This should, however, be given through autoconf and suggest.
#
# Requirements:
#
# You need to execute your Java program under jsvc provided by
# http://jakarta.apache.org/commons/daemon/
# which enables you to run your Java program with specified
# pid file with -pidfile option.
# A Brief setup documentation is also available at
# http://tomcat.apache.org/tomcat-5.5-doc/setup.html
#
# Target:
#
# Target Java Virtual Machine to monitor are:
# Oracle JDK 7.0 (http://www.oracle.com/technetwork/java/javase/)
# Oracle JDK 8.0 (http://www.oracle.com/technetwork/java/javase/)
#
# Parameters:
#
# config (required)
#
# Config variables:
#
# pidfilepath - Which file path use. Defaults to '/var/run/jsvc.pid'
# javahome - Defaults to '/usr/local/java/jdk'
#
DefaultPidFile="/var/run/jsvc.pid"
DefaultJavaHome="/usr/local/java/jdk"
#
# Environment Variables
#
if [ -z "${pidfilepath}" ]; then
pidfilepath="${DefaultPidFile}"
fi
if [ -z "${graphtitle}" ]; then
graphtitle="${pidfilepath}"
fi
if [ -z "${javahome}" ]; then
JAVA_HOME="${DefaultJavaHome}"
else
JAVA_HOME="${javahome}"
fi
export JAVA_HOME
#
# Functions
#
chk_jdk()
{
Version=`${JAVA_HOME}/bin/java -version 2>&1 | egrep '^java version' | awk '{print $3}' | sed -e 's/\"//g' | cut -d'_' -f 1`
if [ "${Version}" != "1.7.0" ]; then
JDK_TYPE="jdk8"
else
JDK_TYPE="jdk7"
fi
}
config_common()
{
echo "graph_title Heap Usage" $graphtitle
echo "graph_args --base 1024 -l 0"
echo "graph_vlabel Heap Usage(Bytes)"
echo "graph_info Heap Usage"
echo "graph_category JVM"
}
config_oracle_jdk7()
{
config_common
echo "Eden_Used.label Eden_Used"
echo "Eden_Free.label Eden_Free"
echo "Survivor0_Used.label Survivor0_Used"
echo "Survivor0_Free.label Survivor0_Free"
echo "Survivor1_Used.label Survivor1_Used"
echo "Survivor1_Free.label Survivor1_Free"
echo "Old_Used.label Old_Used"
echo "Old_Free.label Old_Free"
echo "Permanent_Used.label Permanent_Used"
echo "Permanent_Free.label Permanent_Free"
echo "Eden_Used.draw AREA"
echo "Eden_Free.draw STACK"
echo "Survivor0_Used.draw STACK"
echo "Survivor0_Free.draw STACK"
echo "Survivor1_Used.draw STACK"
echo "Survivor1_Free.draw STACK"
echo "Old_Used.draw STACK"
echo "Old_Free.draw STACK"
echo "Permanent_Used.draw STACK"
echo "Permanent_Free.draw STACK"
}
config_oracle_jdk8()
{
config_common
echo "Eden_Used.label Eden_Used"
echo "Eden_Free.label Eden_Free"
echo "Survivor0_Used.label Survivor0_Used"
echo "Survivor0_Free.label Survivor0_Free"
echo "Survivor1_Used.label Survivor1_Used"
echo "Survivor1_Free.label Survivor1_Free"
echo "Old_Used.label Old_Used"
echo "Old_Free.label Old_Free"
echo "Metaspace_Used.label Metaspace_Used"
echo "Metaspace_Free.label Metaspace_Free"
echo "Compressed_Class_Space_Capacity_Used.label Compressed_Class_Space_Capacity_Used"
echo "Compressed_Class_Space_Capacity_Free.label Compressed_Class_Space_Capacity_Free"
echo "Eden_Used.draw AREA"
echo "Eden_Free.draw STACK"
echo "Survivor0_Used.draw STACK"
echo "Survivor0_Free.draw STACK"
echo "Survivor1_Used.draw STACK"
echo "Survivor1_Free.draw STACK"
echo "Old_Used.draw STACK"
echo "Old_Free.draw STACK"
echo "Metaspace_Used.draw STACK"
echo "Metaspace_Free.draw STACK"
echo "Compressed_Class_Space_Capacity_Used.draw STACK"
echo "Compressed_Class_Space_Capacity_Free.draw STACK"
}
print_oracle_jdk7_stats()
{
${JAVA_HOME}/bin/jstat -gc ${PidNum} | tail -1 | awk \
'{\
S0C = $1; \
S1C = $2; \
S0U = $3; \
S1U = $4; \
EC = $5; \
EU = $6; \
OC = $7; \
OU = $8; \
PC = $9; \
PU = $10; \
\
S0F = S0C - S0U; \
S1F = S1C - S1U; \
EF = EC - EU; \
OF = OC - OU; \
PF = PC - PU; \
\
print "Eden_Used.value " EU * 1024; \
print "Eden_Free.value " EF * 1024; \
print "Survivor0_Used.value " S0U * 1024; \
print "Survivor0_Free.value " S0F * 1024; \
print "Survivor1_Used.value " S1U * 1024; \
print "Survivor1_Free.value " S1F * 1024; \
print "Old_Used.value " OU * 1024; \
print "Old_Free.value " OF * 1024; \
print "Permanent_Used.value " PU * 1024; \
print "Permanent_Free.value " PF * 1024; \
}'
}
print_oracle_jdk8_stats()
{
${JAVA_HOME}/bin/jstat -gc ${PidNum} | tail -1 | awk \
'{\
S0C = $1; \
S1C = $2; \
S0U = $3; \
S1U = $4; \
EC = $5; \
EU = $6; \
OC = $7; \
OU = $8; \
MC = $9; \
MU = $10; \
CCSC = $11; \
CCSU = $12; \
\
S0F = S0C - S0U; \
S1F = S1C - S1U; \
EF = EC - EU; \
OF = OC - OU; \
MF = MC - MU; \
CCSF = CCSC - CCSU; \
\
print "Eden_Used.value " EU * 1024; \
print "Eden_Free.value " EF * 1024; \
print "Survivor0_Used.value " S0U * 1024; \
print "Survivor0_Free.value " S0F * 1024; \
print "Survivor1_Used.value " S1U * 1024; \
print "Survivor1_Free.value " S1F * 1024; \
print "Old_Used.value " OU * 1024; \
print "Old_Free.value " OF * 1024; \
print "Metaspace_Used.value " MU * 1024; \
print "Metaspace_Free.value " MF * 1024; \
print "Compressed_Class_Space_Capacity_Used.value " CCSU * 1024; \
print "Compressed_Class_Space_Capacity_Free.value " CCSF * 1024; \
}'
}
#
# common for all argument
#
PidNum=`cat ${pidfilepath}`
chk_jdk
#
# autoconf
#
if [ "$1" = "autoconf" ]; then
if [ ! -x "${JAVA_HOME}/bin/jstat" ]; then
echo "no (No jstat found in ${JAVA_HOME}/bin)"
exit 1
fi
if [ ! -f "${pidfilepath}" -o ! -r "${pidfilepath}" ]; then
echo "no (No such file ${pidfilepath} or cannot read ${pidfilepath}"
exit 1
fi
echo "yes"
exit 0
fi
#
# config
#
if [ "$1" = "config" ]; then
if [ "${JDK_TYPE}" == "jdk8" ]; then
config_oracle_jdk8
else
config_oracle_jdk7
fi
exit 0
fi
#
# Main
#
if [ "${JDK_TYPE}" == "jdk8" ]; then
print_oracle_jdk8_stats
else
print_oracle_jdk7_stats
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment