Skip to content

Commit e55d6b8

Browse files
committed
Add JVM heap info plugin
Change-Id: I621fffa01012c4dc6c29ef572bf9fc1d0087ac0e
1 parent d4e9b0b commit e55d6b8

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

minecraft_jvm.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/php
2+
<?php
3+
/*
4+
LIZARDNET FASTLIZARD4/MUNIN-PLUGINS/MINECRAFT <https://fastlizard4.org/wiki/Download:Minecraft_Munin_plugins>
5+
by FastLizard4 and the LizardNet Munin Plugins Development Team <https://gerrit.fastlizard4.org/r/#/admin/groups/17,members>
6+
7+
Copyright (C) 2023 by FastLizard4 and the LizardNet Munin Plugins Development Team. Some rights reserved.
8+
9+
License GPLv3+: GNU General Public License version 3 or later (at your choice): <http://gnu.org/licenses/gpl.html>.
10+
This is free software: you are free to change and redistribute it at your will provided that your redistribution,
11+
with or without modifications, is also licensed under the GNU GPL.
12+
13+
There is NO WARRANTY FOR THIS SOFTWARE to the extent permitted by law.
14+
15+
This is an open source project. The source Git repositories, which you are welcome to contribute to, can be
16+
found here: <https://gerrit.fastlizard4.org/r/gitweb?p=munin-plugins/minecraft.git;a=summary>
17+
18+
Gerrit Code Review for the project: <https://gerrit.fastlizard4.org/r/#/q/project:munin-plugins/minecraft,n,z>
19+
20+
=====
21+
22+
minecraft_memory.php: Returns RRDTool-parsable data about the memory status of a Minecraft server.
23+
24+
DEPENDS ON THE LAGMETER PLUGIN FOR THE CRAFTBUKKIT MINECRAFT SERVER DISTRIBUTION. CraftBukkit can be downloaded
25+
at <http://bukkit.org>, and LagMeter can be downloaded at <http://dev.bukkit.org/bukkit-plugins/lagmeter/>.
26+
*/
27+
28+
/*
29+
* This one isn't really Minecraft specific since it's just examining JVM heap information, but it doesn't seem like the
30+
* output of `jcmd` is standardized so this is likely system- and configuration-specific.
31+
*/
32+
33+
/* An example of the jcmd output we're working with:
34+
6698:
35+
garbage-first heap total 6291456K, used 1410621K [0x0000000680000000, 0x0000000800000000)
36+
region size 8192K, 71 young (581632K), 1 survivors (8192K)
37+
Metaspace used 143774K, committed 145792K, reserved 1179648K
38+
class space used 20375K, committed 21184K, reserved 1048576K
39+
*/
40+
41+
if ($argv[1] == "config") {
42+
echo "graph_args --base 1024 -l 0\n";
43+
echo "graph_title " . ((empty($_ENV['customTitle'])) ? "Minecraft JVM Heap Info" : $_ENV['customTitle'] . " JVM Heap Info") . "\n";
44+
echo "graph_vlabel Bytes (B)\n";
45+
echo "graph_category minecraft\n";
46+
47+
// Order of the graph elements is important as that determines how things are stacked!
48+
echo "metaspace_class_used.label Class space used\n";
49+
echo "metaspace_class_used.draw AREASTACK\n";
50+
echo "metaspace_class_used.info Portion of metaspace used for classes\n";
51+
echo "metaspace_used.label Other metaspace used\n";
52+
echo "metaspace_used.draw AREASTACK\n";
53+
echo "metaspace_used.info Other metaspace used, excluding class space\n";
54+
echo "metaspace_class_committed.label Class space committed\n";
55+
echo "metaspace_class_committed.draw AREASTACK\n";
56+
echo "metaspace_class_committed.info Portion of metaspace committed for classes but not used\n";
57+
echo "metaspace_committed.label Other metaspace committed\n";
58+
echo "metaspace_committed.draw AREASTACK\n";
59+
echo "metaspace_committed.info Other metaspace committed but not used, excluding class space\n";
60+
echo "metaspace_class_reserved.label Class space reserved\n";
61+
echo "metaspace_class_reserved.draw AREASTACK\n";
62+
echo "metaspace_class_reserved.info Portion of metaspace reserved for classes but not committed\n";
63+
echo "metaspace_reserved.label Other metaspace reserved\n";
64+
echo "metaspace_reserved.draw AREASTACK\n";
65+
echo "metaspace_reserved.info Other metaspace reserved but not committed, excluding class space\n";
66+
echo "heap_young.label Young heap\n";
67+
echo "heap_young.draw AREASTACK\n";
68+
echo "heap_young.info Young generation region of the heap\n";
69+
echo "heap_survivor.label Survivor heap\n";
70+
echo "heap_survivor.draw AREASTACK\n";
71+
echo "heap_survivor.info Survivor generation region of the heap\n";
72+
echo "heap_used.label Other used heap\n";
73+
echo "heap_used.draw AREASTACK\n";
74+
echo "heap_used.info Other used heap memory, excluding the young and survivor regions\n";
75+
echo "heap_free.label Free heap\n";
76+
echo "heap_free.draw AREASTACK\n";
77+
echo "heap_free.info Unused but allocated heap memory\n";
78+
die();
79+
}
80+
81+
// First, get the PID of the Minecraft server. It's stored in the file /home/minecraft/SID.running, where SID is the
82+
// server ID from the serverId environment variable.
83+
$pid = file_get_contents("/home/minecraft/" . $_ENV['serverId'] . ".running");
84+
$pid = trim($pid);
85+
86+
// Next, get the output of jcmd for the PID we just got.
87+
$jcmd = shell_exec("/opt/java/jdk-17.0.6+10/bin/jcmd $pid GC.heap_info");
88+
89+
// Parse out the contents of the output.
90+
preg_match("/class\s+space\s+used\s+(\d+)K,\s+committed\s+(\d+)K,\s+reserved\s+(\d+)K/", $jcmd, $matches);
91+
$metaspace_class_used = $matches[1] * 1024;
92+
$metaspace_class_committed = $matches[2] * 1024;
93+
$metaspace_class_reserved = $matches[3] * 1024;
94+
95+
preg_match("/Metaspace\s+used\s+(\d+)K,\s+committed\s+(\d+)K,\s+reserved\s+(\d+)K/", $jcmd, $matches);
96+
$metaspace_used = $matches[1] * 1024;
97+
$metaspace_committed = $matches[2] * 1024;
98+
$metaspace_reserved = $matches[3] * 1024;
99+
100+
preg_match("/region\s+size\s+(\d+)K,\s+(\d+)\s+young\s+\((\d+)K\),\s+(\d+)\s+survivors\s+\((\d+)K\)/", $jcmd, $matches);
101+
$region_size = $matches[1] * 1024;
102+
$heap_young = $matches[3] * 1024;
103+
$heap_survivor = $matches[5] * 1024;
104+
105+
preg_match("/garbage-first\s+heap\s+total\s+(\d+)K,\s+used\s+(\d+)K/", $jcmd, $matches);
106+
$heap_total = $matches[1] * 1024;
107+
$heap_used = $matches[2] * 1024;
108+
109+
// Some of these values need some additional processing. For example, class space is a subset of metaspace, so if we
110+
// want to accurately report total metaspace usage, we need to subtract class space values from their corresponding
111+
// metaspace values. Likewise, the total heap usage includes the young and survivor regions, so we need to subtract
112+
// those from the total heap usage to get the actual heap usage broken down accurately in the graph.
113+
$heap_free = $heap_total - $heap_used;
114+
$heap_used_other = $heap_used - $heap_young - $heap_survivor;
115+
116+
$metaspace_class_reserved -= $metaspace_class_committed;
117+
$metaspace_class_committed -= $metaspace_class_used;
118+
119+
$metaspace_reserved -= ($metaspace_committed + $metaspace_class_reserved);
120+
$metaspace_committed -= ($metaspace_used + $metaspace_class_committed);
121+
$metaspace_used -= $metaspace_class_used;
122+
123+
// Finally, output the data in the order we defined graph elements above.
124+
echo "metaspace_class_used.value $metaspace_class_used\n";
125+
echo "metaspace_used.value $metaspace_used\n";
126+
echo "metaspace_class_committed.value $metaspace_class_committed\n";
127+
echo "metaspace_committed.value $metaspace_committed\n";
128+
echo "metaspace_class_reserved.value $metaspace_class_reserved\n";
129+
echo "metaspace_reserved.value $metaspace_reserved\n";
130+
echo "heap_young.value $heap_young\n";
131+
echo "heap_survivor.value $heap_survivor\n";
132+
echo "heap_used.value $heap_used_other\n";
133+
echo "heap_free.value $heap_free\n";

0 commit comments

Comments
 (0)