Last active
October 6, 2016 13:48
-
-
Save damc-dev/f8eef396d20392bb0e3a93c7a6627a0a to your computer and use it in GitHub Desktop.
Create CSV of running java processes and their memory usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Description: Create CSV of running java processes and their memory usage | |
# this script must be run as the same user as the java processes you want to capture | |
# | |
# Author: David McElligott<[email protected]> 10/5/2016 | |
# | |
user="$(whoami)" | |
header="" | |
header="Server, App Name, Java Version, Metaspace Used (kb), Metaspace Total (kb), Metaspace Used (%), Permgen Used (kb), Permgen Total (kb), Permgen Used (%), Command Error" | |
echo $header | |
for pid in $(pgrep -x java -u $user); do | |
meta_space_total="" | |
meta_space_used="" | |
meta_space_percentage="" | |
permgen_space_total="" | |
permgen_space_used="" | |
permgen_space_percentage="" | |
run_status="" | |
cmdline="$(cat /proc/$pid/cmdline)" | |
java_home="$(echo $cmdline | grep -aoP '(?<=).*/java../.*?(?=/bin/java|/jre)')" | |
java_home="$(readlink -f $java_home)" | |
java_version="$(basename $java_home)" | |
app_name="$(echo $cmdline | grep -oP '(?<=-Dapp.name=).*?(?=-D)')" | |
if [[ "$app_name" == "" ]]; then | |
app_name="$(echo $cmdline | grep -oP '(?<=-Djbs.name=).*?(?=-D)')" | |
fi | |
row="$HOSTNAME" | |
row="$row, $app_name" | |
row="$row, $java_version" | |
jstat_output=$($java_home/bin/jstat -gc $pid 2>/dev/null) | |
if [ "$?" != "0" ]; then | |
run_status="FAILED: jstat Could not attach to $pid" | |
else | |
jstat_data="$(printf "$jstat_output" | tail -n 1 2> /dev/null)" | |
if [[ "$java_version" == *"jdk1.8"* ]]; then | |
meta_space_total="$(echo $jstat_data | awk '{print $9}')" | |
meta_space_used="$(echo $jstat_data | awk '{print $10}')" | |
meta_space_percentage="$(echo "scale=2; $meta_space_used*100/$meta_space_total" | bc -l)" | |
else | |
permgen_space_total="$(echo $jstat_data | awk '{print $9}')" | |
permgen_space_used="$(echo $jstat_data | awk '{print $10}')" | |
permgen_space_percentage="$(echo "scale=2; $permgen_space_used*100/$permgen_space_total" | bc -l )" | |
fi | |
fi | |
row="$row, $meta_space_used, $meta_space_total, $meta_space_percentage, $permgen_space_used, $permgen_space_total, $permgen_space_percentage, $run_status" | |
echo $row | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment