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