Storing Java GC statistics to an RRD file

From OpenNMS
Jump to: navigation, search

Here is a simple perl script that I wrote to parse the data out of a output.log with verbosegc turned on. It ssh's into a box running opennms and grabs the data and writes into a rrdfile.

#!/usr/bin/perl

($host, $nodeid, $maxheap) = @ARGV;

$fetchCmd="ssh opennms\@$host tail -24 /var/log/opennms/output.log | grep GC | tail -1";

while (1) {

$time = `date +%s`;
chop $time;
($wasusedK, $usedK, $committedK) = ( `$fetchCmd` =~ m/^\[GC (\d+)K->(\d+)K\((\d+)K\), ([0-9.]+) secs\]/);
unless ($wasusedK > 0) {
	($wasusedK, $usedK, $committedK) = ( `$fetchCmd` =~ m/^\d+\.\d+: \[GC \d+\.\d+: \[[^\]]+\] (\d+)K->(\d+)K\((\d+)K\), [0-9.]+ secs\]/);
}

unless ($wasusedK > 0) {
	($wasusedK, $usedK, $committedK) = ( `$fetchCmd` =~ m/^\d+\.\d+: \[GC \[[^\]]+\] (\d+)K->(\d+)K\((\d+)K\), [0-9.]+ secs\]/);
}

$used = $usedK * 1024;
$committed = $committedK * 1024;

print "$time:$used $time:$committed\n";

if ($maxheap > 0) {
	print `sudo rrdtool update /var/lib/opennms/rrd/snmp/$nodeid/memHeapMaxSize.rrd $time:$maxheap`;
}
if ($time > 0) {
	print `sudo rrdtool update /var/lib/opennms/rrd/snmp/$nodeid/memHeapUsed.rrd $time:$used`;
}

if ($committed > 0) {
	print `sudo rrdtool update /var/lib/opennms/rrd/snmp/$nodeid/memHeapCommitted.rrd $time:$committed`;

}

sleep 300;
}