1 package org.collectd.java;
6 import java.lang.management.ManagementFactory;
7 import java.lang.management.MemoryUsage;
8 import java.lang.management.MemoryMXBean;
10 import javax.management.MBeanServerConnection;
11 import javax.management.remote.JMXConnector;
12 import javax.management.remote.JMXConnectorFactory;
13 import javax.management.remote.JMXServiceURL;
15 import org.collectd.api.Collectd;
16 import org.collectd.api.DataSet;
17 import org.collectd.api.ValueList;
18 import org.collectd.api.Notification;
19 import org.collectd.api.OConfigItem;
21 import org.collectd.api.CollectdConfigInterface;
22 import org.collectd.api.CollectdInitInterface;
23 import org.collectd.api.CollectdReadInterface;
24 import org.collectd.api.CollectdShutdownInterface;
26 import org.collectd.api.OConfigValue;
27 import org.collectd.api.OConfigItem;
29 public class JMXMemory implements CollectdConfigInterface, /* {{{ */
30 CollectdInitInterface,
31 CollectdReadInterface,
32 CollectdShutdownInterface
34 private String _jmx_service_url = null;
35 private MemoryMXBean _mbean = null;
39 Collectd.registerConfig ("JMXMemory", this);
40 Collectd.registerInit ("JMXMemory", this);
41 Collectd.registerRead ("JMXMemory", this);
42 Collectd.registerShutdown ("JMXMemory", this);
45 private void submit (String plugin_instance, MemoryUsage usage) /* {{{ */
54 mem_init = usage.getInit ();
55 mem_used = usage.getUsed ();
56 mem_committed = usage.getCommitted ();
57 mem_max = usage.getMax ();
59 Collectd.logDebug ("JMXMemory plugin: plugin_instance = " + plugin_instance + "; "
60 + "mem_init = " + mem_init + "; "
61 + "mem_used = " + mem_used + "; "
62 + "mem_committed = " + mem_committed + "; "
63 + "mem_max = " + mem_max + ";");
65 vl = new ValueList ();
67 vl.setHost ("localhost");
68 vl.setPlugin ("JMXMemory");
69 vl.setPluginInstance (plugin_instance);
70 vl.setType ("memory");
74 vl.addValue (mem_init);
75 vl.setTypeInstance ("init");
76 Collectd.dispatchValues (vl);
82 vl.addValue (mem_used);
83 vl.setTypeInstance ("used");
84 Collectd.dispatchValues (vl);
88 if (mem_committed >= 0)
90 vl.addValue (mem_committed);
91 vl.setTypeInstance ("committed");
92 Collectd.dispatchValues (vl);
98 vl.addValue (mem_max);
99 vl.setTypeInstance ("max");
100 Collectd.dispatchValues (vl);
103 } /* }}} void submit */
105 private int configServiceURL (OConfigItem ci) /* {{{ */
107 List<OConfigValue> values;
110 values = ci.getValues ();
111 if (values.size () != 1)
113 Collectd.logError ("JMXMemory plugin: The JMXServiceURL option needs "
114 + "exactly one string argument.");
119 if (cv.getType () != OConfigValue.OCONFIG_TYPE_STRING)
121 Collectd.logError ("JMXMemory plugin: The JMXServiceURL option needs "
122 + "exactly one string argument.");
126 _jmx_service_url = cv.getString ();
128 } /* }}} int configServiceURL */
130 public int config (OConfigItem ci) /* {{{ */
132 List<OConfigItem> children;
135 Collectd.logDebug ("JMXMemory plugin: config: ci = " + ci + ";");
137 children = ci.getChildren ();
138 for (i = 0; i < children.size (); i++)
143 child = children.get (i);
144 key = child.getKey ();
145 if (key.equalsIgnoreCase ("JMXServiceURL"))
147 configServiceURL (child);
151 Collectd.logError ("JMXMemory plugin: Unknown config option: " + key);
156 } /* }}} int config */
158 public int init () /* {{{ */
160 JMXServiceURL service_url;
161 JMXConnector connector;
162 MBeanServerConnection connection;
164 if (_jmx_service_url == null)
166 Collectd.logError ("JMXMemory: _jmx_service_url == null");
172 service_url = new JMXServiceURL (_jmx_service_url);
173 connector = JMXConnectorFactory.connect (service_url);
174 connection = connector.getMBeanServerConnection ();
175 _mbean = ManagementFactory.newPlatformMXBeanProxy (connection,
176 ManagementFactory.MEMORY_MXBEAN_NAME,
181 Collectd.logError ("JMXMemory: Creating MBean failed: " + e);
188 public int read () /* {{{ */
192 Collectd.logError ("JMXMemory: _mbean == null");
196 submit ("heap", _mbean.getHeapMemoryUsage ());
197 submit ("non_heap", _mbean.getNonHeapMemoryUsage ());
202 public int shutdown () /* {{{ */
204 System.out.print ("org.collectd.java.JMXMemory.Shutdown ();\n");
205 _jmx_service_url = null;
208 } /* }}} int shutdown */
209 } /* }}} class JMXMemory */
211 /* vim: set sw=2 sts=2 et fdm=marker : */