2 * collectd - bindings/java/org/collectd/java/JMXMemory.java
3 * Copyright (C) 2009 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
27 package org.collectd.java;
29 import java.util.List;
30 import java.util.Date;
32 import java.lang.management.ManagementFactory;
33 import java.lang.management.MemoryUsage;
34 import java.lang.management.MemoryMXBean;
36 import javax.management.MBeanServerConnection;
37 import javax.management.remote.JMXConnector;
38 import javax.management.remote.JMXConnectorFactory;
39 import javax.management.remote.JMXServiceURL;
41 import org.collectd.api.Collectd;
42 import org.collectd.api.DataSet;
43 import org.collectd.api.ValueList;
44 import org.collectd.api.Notification;
45 import org.collectd.api.OConfigItem;
47 import org.collectd.api.CollectdConfigInterface;
48 import org.collectd.api.CollectdInitInterface;
49 import org.collectd.api.CollectdReadInterface;
50 import org.collectd.api.CollectdShutdownInterface;
52 import org.collectd.api.OConfigValue;
53 import org.collectd.api.OConfigItem;
55 public class JMXMemory implements CollectdConfigInterface,
56 CollectdInitInterface,
57 CollectdReadInterface,
58 CollectdShutdownInterface
60 private String _jmx_service_url = null;
61 private MemoryMXBean _mbean = null;
65 Collectd.registerConfig ("JMXMemory", this);
66 Collectd.registerInit ("JMXMemory", this);
67 Collectd.registerRead ("JMXMemory", this);
68 Collectd.registerShutdown ("JMXMemory", this);
71 private void submit (String plugin_instance, MemoryUsage usage) /* {{{ */
80 mem_init = usage.getInit ();
81 mem_used = usage.getUsed ();
82 mem_committed = usage.getCommitted ();
83 mem_max = usage.getMax ();
85 Collectd.logDebug ("JMXMemory plugin: plugin_instance = " + plugin_instance + "; "
86 + "mem_init = " + mem_init + "; "
87 + "mem_used = " + mem_used + "; "
88 + "mem_committed = " + mem_committed + "; "
89 + "mem_max = " + mem_max + ";");
91 vl = new ValueList ();
93 vl.setHost ("localhost");
94 vl.setPlugin ("JMXMemory");
95 vl.setPluginInstance (plugin_instance);
96 vl.setType ("memory");
100 vl.addValue (mem_init);
101 vl.setTypeInstance ("init");
102 Collectd.dispatchValues (vl);
108 vl.addValue (mem_used);
109 vl.setTypeInstance ("used");
110 Collectd.dispatchValues (vl);
114 if (mem_committed >= 0)
116 vl.addValue (mem_committed);
117 vl.setTypeInstance ("committed");
118 Collectd.dispatchValues (vl);
124 vl.addValue (mem_max);
125 vl.setTypeInstance ("max");
126 Collectd.dispatchValues (vl);
129 } /* }}} void submit */
131 private int configServiceURL (OConfigItem ci) /* {{{ */
133 List<OConfigValue> values;
136 values = ci.getValues ();
137 if (values.size () != 1)
139 Collectd.logError ("JMXMemory plugin: The JMXServiceURL option needs "
140 + "exactly one string argument.");
145 if (cv.getType () != OConfigValue.OCONFIG_TYPE_STRING)
147 Collectd.logError ("JMXMemory plugin: The JMXServiceURL option needs "
148 + "exactly one string argument.");
152 _jmx_service_url = cv.getString ();
154 } /* }}} int configServiceURL */
156 public int config (OConfigItem ci) /* {{{ */
158 List<OConfigItem> children;
161 Collectd.logDebug ("JMXMemory plugin: config: ci = " + ci + ";");
163 children = ci.getChildren ();
164 for (i = 0; i < children.size (); i++)
169 child = children.get (i);
170 key = child.getKey ();
171 if (key.equalsIgnoreCase ("JMXServiceURL"))
173 configServiceURL (child);
177 Collectd.logError ("JMXMemory plugin: Unknown config option: " + key);
182 } /* }}} int config */
184 public int init () /* {{{ */
186 JMXServiceURL service_url;
187 JMXConnector connector;
188 MBeanServerConnection connection;
190 if (_jmx_service_url == null)
192 Collectd.logError ("JMXMemory: _jmx_service_url == null");
198 service_url = new JMXServiceURL (_jmx_service_url);
199 connector = JMXConnectorFactory.connect (service_url);
200 connection = connector.getMBeanServerConnection ();
201 _mbean = ManagementFactory.newPlatformMXBeanProxy (connection,
202 ManagementFactory.MEMORY_MXBEAN_NAME,
207 Collectd.logError ("JMXMemory: Creating MBean failed: " + e);
214 public int read () /* {{{ */
218 Collectd.logError ("JMXMemory: _mbean == null");
222 submit ("heap", _mbean.getHeapMemoryUsage ());
223 submit ("non_heap", _mbean.getNonHeapMemoryUsage ());
228 public int shutdown () /* {{{ */
230 System.out.print ("org.collectd.java.JMXMemory.Shutdown ();\n");
231 _jmx_service_url = null;
234 } /* }}} int shutdown */
235 } /* class JMXMemory */
237 /* vim: set sw=2 sts=2 et fdm=marker : */