Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / bindings / java / org / collectd / java / JMXMemory.java
1 /**
2  * collectd - bindings/java/org/collectd/java/JMXMemory.java
3  * Copyright (C) 2009       Florian octo Forster
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  */
26
27 package org.collectd.java;
28
29 import java.util.List;
30 import java.util.Date;
31
32 import java.lang.management.ManagementFactory;
33 import java.lang.management.MemoryUsage;
34 import java.lang.management.MemoryMXBean;
35
36 import javax.management.MBeanServerConnection;
37 import javax.management.remote.JMXConnector;
38 import javax.management.remote.JMXConnectorFactory;
39 import javax.management.remote.JMXServiceURL;
40
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;
46
47 import org.collectd.api.CollectdConfigInterface;
48 import org.collectd.api.CollectdInitInterface;
49 import org.collectd.api.CollectdReadInterface;
50 import org.collectd.api.CollectdShutdownInterface;
51
52 import org.collectd.api.OConfigValue;
53 import org.collectd.api.OConfigItem;
54
55 public class JMXMemory implements CollectdConfigInterface,
56        CollectdInitInterface,
57        CollectdReadInterface,
58        CollectdShutdownInterface
59 {
60   private String _jmx_service_url = null;
61   private MemoryMXBean _mbean = null;
62
63   public JMXMemory ()
64   {
65     Collectd.registerConfig   ("JMXMemory", this);
66     Collectd.registerInit     ("JMXMemory", this);
67     Collectd.registerRead     ("JMXMemory", this);
68     Collectd.registerShutdown ("JMXMemory", this);
69   }
70
71   private void submit (String plugin_instance, MemoryUsage usage) /* {{{ */
72   {
73     ValueList vl;
74
75     long mem_init;
76     long mem_used;
77     long mem_committed;
78     long mem_max;
79
80     mem_init = usage.getInit ();
81     mem_used = usage.getUsed ();
82     mem_committed = usage.getCommitted ();
83     mem_max = usage.getMax ();
84
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 + ";");
90
91     vl = new ValueList ();
92
93     vl.setHost ("localhost");
94     vl.setPlugin ("JMXMemory");
95     vl.setPluginInstance (plugin_instance);
96     vl.setType ("memory");
97
98     if (mem_init >= 0)
99     {
100       vl.addValue (mem_init);
101       vl.setTypeInstance ("init");
102       Collectd.dispatchValues (vl);
103       vl.clearValues ();
104     }
105
106     if (mem_used >= 0)
107     {
108       vl.addValue (mem_used);
109       vl.setTypeInstance ("used");
110       Collectd.dispatchValues (vl);
111       vl.clearValues ();
112     }
113
114     if (mem_committed >= 0)
115     {
116       vl.addValue (mem_committed);
117       vl.setTypeInstance ("committed");
118       Collectd.dispatchValues (vl);
119       vl.clearValues ();
120     }
121
122     if (mem_max >= 0)
123     {
124       vl.addValue (mem_max);
125       vl.setTypeInstance ("max");
126       Collectd.dispatchValues (vl);
127       vl.clearValues ();
128     }
129   } /* }}} void submit */
130
131   private int configServiceURL (OConfigItem ci) /* {{{ */
132   {
133     List<OConfigValue> values;
134     OConfigValue cv;
135
136     values = ci.getValues ();
137     if (values.size () != 1)
138     {
139       Collectd.logError ("JMXMemory plugin: The JMXServiceURL option needs "
140           + "exactly one string argument.");
141       return (-1);
142     }
143
144     cv = values.get (0);
145     if (cv.getType () != OConfigValue.OCONFIG_TYPE_STRING)
146     {
147       Collectd.logError ("JMXMemory plugin: The JMXServiceURL option needs "
148           + "exactly one string argument.");
149       return (-1);
150     }
151
152     _jmx_service_url = cv.getString ();
153     return (0);
154   } /* }}} int configServiceURL */
155
156   public int config (OConfigItem ci) /* {{{ */
157   {
158     List<OConfigItem> children;
159     int i;
160
161     Collectd.logDebug ("JMXMemory plugin: config: ci = " + ci + ";");
162
163     children = ci.getChildren ();
164     for (i = 0; i < children.size (); i++)
165     {
166       OConfigItem child;
167       String key;
168
169       child = children.get (i);
170       key = child.getKey ();
171       if (key.equalsIgnoreCase ("JMXServiceURL"))
172       {
173         configServiceURL (child);
174       }
175       else
176       {
177         Collectd.logError ("JMXMemory plugin: Unknown config option: " + key);
178       }
179     }
180
181     return (0);
182   } /* }}} int config */
183
184   public int init () /* {{{ */
185   {
186     JMXServiceURL service_url;
187     JMXConnector connector;
188     MBeanServerConnection connection;
189
190     if (_jmx_service_url == null)
191     {
192       Collectd.logError ("JMXMemory: _jmx_service_url == null");
193       return (-1);
194     }
195
196     try
197     {
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,
203           MemoryMXBean.class);
204     }
205     catch (Exception e)
206     {
207       Collectd.logError ("JMXMemory: Creating MBean failed: " + e);
208       return (-1);
209     }
210
211     return (0);
212   } /* }}} int init */
213
214   public int read () /* {{{ */
215   {
216     if (_mbean == null)
217     {
218       Collectd.logError ("JMXMemory: _mbean == null");
219       return (-1);
220     }
221
222     submit ("heap", _mbean.getHeapMemoryUsage ());
223     submit ("non_heap", _mbean.getNonHeapMemoryUsage ());
224
225     return (0);
226   } /* }}} int read */
227
228   public int shutdown () /* {{{ */
229   {
230     System.out.print ("org.collectd.java.JMXMemory.Shutdown ();\n");
231     _jmx_service_url = null;
232     _mbean = null;
233     return (0);
234   } /* }}} int shutdown */
235 } /* class JMXMemory */
236
237 /* vim: set sw=2 sts=2 et fdm=marker : */