2 * collectd/java - org/collectd/java/JMXMemory.java
3 * Copyright (C) 2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
22 package org.collectd.java;
24 import java.util.List;
25 import java.util.Date;
27 import java.lang.management.ManagementFactory;
28 import java.lang.management.MemoryUsage;
29 import java.lang.management.MemoryMXBean;
31 import javax.management.MBeanServerConnection;
32 import javax.management.remote.JMXConnector;
33 import javax.management.remote.JMXConnectorFactory;
34 import javax.management.remote.JMXServiceURL;
36 import org.collectd.api.Collectd;
37 import org.collectd.api.DataSet;
38 import org.collectd.api.ValueList;
39 import org.collectd.api.Notification;
40 import org.collectd.api.OConfigItem;
42 import org.collectd.api.CollectdConfigInterface;
43 import org.collectd.api.CollectdInitInterface;
44 import org.collectd.api.CollectdReadInterface;
45 import org.collectd.api.CollectdShutdownInterface;
47 import org.collectd.api.OConfigValue;
48 import org.collectd.api.OConfigItem;
50 public class JMXMemory implements CollectdConfigInterface,
51 CollectdInitInterface,
52 CollectdReadInterface,
53 CollectdShutdownInterface
55 private String _jmx_service_url = null;
56 private MemoryMXBean _mbean = null;
60 Collectd.registerConfig ("JMXMemory", this);
61 Collectd.registerInit ("JMXMemory", this);
62 Collectd.registerRead ("JMXMemory", this);
63 Collectd.registerShutdown ("JMXMemory", this);
66 private void submit (String plugin_instance, MemoryUsage usage) /* {{{ */
75 mem_init = usage.getInit ();
76 mem_used = usage.getUsed ();
77 mem_committed = usage.getCommitted ();
78 mem_max = usage.getMax ();
80 Collectd.logDebug ("JMXMemory plugin: plugin_instance = " + plugin_instance + "; "
81 + "mem_init = " + mem_init + "; "
82 + "mem_used = " + mem_used + "; "
83 + "mem_committed = " + mem_committed + "; "
84 + "mem_max = " + mem_max + ";");
86 vl = new ValueList ();
88 vl.setHost ("localhost");
89 vl.setPlugin ("JMXMemory");
90 vl.setPluginInstance (plugin_instance);
91 vl.setType ("memory");
95 vl.addValue (mem_init);
96 vl.setTypeInstance ("init");
97 Collectd.dispatchValues (vl);
103 vl.addValue (mem_used);
104 vl.setTypeInstance ("used");
105 Collectd.dispatchValues (vl);
109 if (mem_committed >= 0)
111 vl.addValue (mem_committed);
112 vl.setTypeInstance ("committed");
113 Collectd.dispatchValues (vl);
119 vl.addValue (mem_max);
120 vl.setTypeInstance ("max");
121 Collectd.dispatchValues (vl);
124 } /* }}} void submit */
126 private int configServiceURL (OConfigItem ci) /* {{{ */
128 List<OConfigValue> values;
131 values = ci.getValues ();
132 if (values.size () != 1)
134 Collectd.logError ("JMXMemory plugin: The JMXServiceURL option needs "
135 + "exactly one string argument.");
140 if (cv.getType () != OConfigValue.OCONFIG_TYPE_STRING)
142 Collectd.logError ("JMXMemory plugin: The JMXServiceURL option needs "
143 + "exactly one string argument.");
147 _jmx_service_url = cv.getString ();
149 } /* }}} int configServiceURL */
151 public int config (OConfigItem ci) /* {{{ */
153 List<OConfigItem> children;
156 Collectd.logDebug ("JMXMemory plugin: config: ci = " + ci + ";");
158 children = ci.getChildren ();
159 for (i = 0; i < children.size (); i++)
164 child = children.get (i);
165 key = child.getKey ();
166 if (key.equalsIgnoreCase ("JMXServiceURL"))
168 configServiceURL (child);
172 Collectd.logError ("JMXMemory plugin: Unknown config option: " + key);
177 } /* }}} int config */
179 public int init () /* {{{ */
181 JMXServiceURL service_url;
182 JMXConnector connector;
183 MBeanServerConnection connection;
185 if (_jmx_service_url == null)
187 Collectd.logError ("JMXMemory: _jmx_service_url == null");
193 service_url = new JMXServiceURL (_jmx_service_url);
194 connector = JMXConnectorFactory.connect (service_url);
195 connection = connector.getMBeanServerConnection ();
196 _mbean = ManagementFactory.newPlatformMXBeanProxy (connection,
197 ManagementFactory.MEMORY_MXBEAN_NAME,
202 Collectd.logError ("JMXMemory: Creating MBean failed: " + e);
209 public int read () /* {{{ */
213 Collectd.logError ("JMXMemory: _mbean == null");
217 submit ("heap", _mbean.getHeapMemoryUsage ());
218 submit ("non_heap", _mbean.getNonHeapMemoryUsage ());
223 public int shutdown () /* {{{ */
225 System.out.print ("org.collectd.java.JMXMemory.Shutdown ();\n");
226 _jmx_service_url = null;
229 } /* }}} int shutdown */
230 } /* class JMXMemory */
232 /* vim: set sw=2 sts=2 et fdm=marker : */