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