5 collectd-java - Documentation of collectd's "java plugin"
12 JVMArg "-Djava.class.path=/opt/collectd/lib/collectd/bindings/java"
14 LoadPlugin "org.collectd.java.Foobar"
15 <Plugin "org.collectd.java.Foobar">
16 # To be parsed by the plugin
22 The I<Java> plugin embeds a I<Java Virtual Machine> (JVM) into I<collectd> and
23 provides a Java interface to part of collectd's API. This makes it possible to
24 write additions to the daemon in Java.
26 This plugin is similar in nature to, but shares no code with, the I<Perl>
27 plugin by Sebastian Harl, see L<collectd-perl(5)> for details.
31 A short outline of this plugin's configuration can be seen in L<"SYNOPSIS">
32 above. For a complete list of all configuration options and their semantics
33 please read L<collectd.conf(5)/Plugin C<java>>.
37 When writing additions for collectd in Java, the underlying C base is mostly
38 hidden from you. All complex data types are converted to their Java counterparts
39 before they're passed to your functions. These Java classes reside in the
40 I<org.collectd.api> namespace.
42 The I<Java> plugin will create one object of each class configured with the
43 B<LoadPlugin> option. The constructor of this class can then register "callback
44 methods", i.E<nbsp>e. methods that will be called by the daemon when
47 The available classes are:
51 =item B<org.collectd.api.Collectd>
53 All API functions exported to Java are implemented as static functions of this
54 class. See L<"EXPORTED API FUNCTIONS"> below.
56 =item B<org.collectd.api.OConfigValue>
58 Corresponds to C<oconfig_value_t>, defined in F<src/liboconfig/oconfig.h>.
60 =item B<org.collectd.api.OConfigItem>
62 Corresponds to C<oconfig_item_t>, defined in F<src/liboconfig/oconfig.h>.
64 =item B<org.collectd.api.DataSource>
66 Corresponds to C<data_source_t>, defined in F<src/plugin.h>.
68 =item B<org.collectd.api.DataSet>
70 Corresponds to C<data_set_t>, defined in F<src/plugin.h>.
72 =item B<org.collectd.api.ValueList>
74 Corresponds to C<value_list_t>, defined in F<src/plugin.h>.
76 =item B<org.collectd.api.Notification>
78 Corresponds to C<notification_t>, defined in F<src/plugin.h>.
82 In the remainder of this document, we'll use the short form of these names, for
83 example B<ValueList>. In order to be able to use these abbreviated names, you
84 need to B<import> the classes.
86 =head1 EXPORTED API FUNCTIONS
88 All collectd API functions that are available to Java plugins are implemented
89 as I<publicE<nbsp>static> functions of the B<Collectd> class. This makes
90 calling these functions pretty straight forward. For example, to send an error
91 message to the daemon, you'd do something like this:
93 Collectd.logError ("That wasn't chicken!");
95 The following are the currently exported functions.
99 Signature: I<int> B<registerConfig> (I<String> name,
100 I<CollectdConfigInterface> object);
102 Registers the B<config> function of I<object> with the daemon.
104 Returns zero upon success and non-zero when an error occurred.
106 See L<"config callback"> below.
110 Signature: I<int> B<registerInit> (I<String> name,
111 I<CollectdInitInterface> object);
113 Registers the B<init> function of I<object> with the daemon.
115 Returns zero upon success and non-zero when an error occurred.
117 See L<"init callback"> below.
121 Signature: I<int> B<registerRead> (I<String> name,
122 I<CollectdReadInterface> object)
124 Registers the B<read> function of I<object> with the daemon.
126 Returns zero upon success and non-zero when an error occurred.
128 See L<"read callback"> below.
132 Signature: I<int> B<registerWrite> (I<String> name,
133 I<CollectdWriteInterface> object)
135 Registers the B<write> function of I<object> with the daemon.
137 Returns zero upon success and non-zero when an error occurred.
139 See L<"write callback"> below.
143 Signature: I<int> B<registerFlush> (I<String> name,
144 I<CollectdFlushInterface> object)
146 Registers the B<flush> function of I<object> with the daemon.
148 Returns zero upon success and non-zero when an error occurred.
150 See L<"flush callback"> below.
152 =head2 registerShutdown
154 Signature: I<int> B<registerShutdown> (I<String> name,
155 I<CollectdShutdownInterface> object);
157 Registers the B<shutdown> function of I<object> with the daemon.
159 Returns zero upon success and non-zero when an error occurred.
161 See L<"shutdown callback"> below.
165 Signature: I<int> B<registerLog> (I<String> name,
166 I<CollectdLogInterface> object);
168 Registers the B<log> function of I<object> with the daemon.
170 Returns zero upon success and non-zero when an error occurred.
172 See L<"log callback"> below.
174 =head2 registerNotification
176 Signature: I<int> B<registerNotification> (I<String> name,
177 I<CollectdNotificationInterface> object);
179 Registers the B<notification> function of I<object> with the daemon.
181 Returns zero upon success and non-zero when an error occurred.
183 See L<"notification callback"> below.
187 Signature: I<int> B<registerMatch> (I<String> name,
188 I<CollectdMatchFactoryInterface> object);
190 Registers the B<createMatch> function of I<object> with the daemon.
192 Returns zero upon success and non-zero when an error occurred.
194 See L<"match callback"> below.
196 =head2 registerTarget
198 Signature: I<int> B<registerTarget> (I<String> name,
199 I<CollectdTargetFactoryInterface> object);
201 Registers the B<createTarget> function of I<object> with the daemon.
203 Returns zero upon success and non-zero when an error occurred.
205 See L<"target callback"> below.
207 =head2 dispatchValues
209 Signature: I<int> B<dispatchValues> (I<ValueList>)
211 Passes the values represented by the B<ValueList> object to the
212 C<plugin_dispatch_values> function of the daemon. The "data set" (or list of
213 "data sources") associated with the object are ignored, because
214 C<plugin_dispatch_values> will automatically lookup the required data set. It
215 is therefore absolutely okay to leave this blank.
217 Returns zero upon success or non-zero upon failure.
221 Signature: I<DataSet> B<getDS> (I<String>)
223 Returns the appropriate I<type> or B<null> if the type is not defined.
227 Signature: I<void> B<logError> (I<String>)
229 Sends a log message with severity B<ERROR> to the daemon.
233 Signature: I<void> B<logWarning> (I<String>)
235 Sends a log message with severity B<WARNING> to the daemon.
239 Signature: I<void> B<logNotice> (I<String>)
241 Sends a log message with severity B<NOTICE> to the daemon.
245 Signature: I<void> B<logInfo> (I<String>)
247 Sends a log message with severity B<INFO> to the daemon.
251 Signature: I<void> B<logDebug> (I<String>)
253 Sends a log message with severity B<DEBUG> to the daemon.
255 =head1 REGISTERING CALLBACKS
257 When starting up, collectd creates an object of each configured class. The
258 constructor of this class should then register "callbacks" with the daemon,
259 using the appropriate static functions in B<Collectd>,
260 see L<"EXPORTED API FUNCTIONS"> above. To register a callback, the object being
261 passed to one of the register functions must implement an appropriate
262 interface, which are all in the B<org.collectd.api> namespace.
264 A constructor may register any number of these callbacks, even none. An object
265 without callback methods is never actively called by collectd, but may still
266 call the exported API functions. One could, for example, start a new thread in
267 the constructor and dispatch (submit to the daemon) values asynchronously,
268 whenever one is available.
270 Each callback method is now explained in more detail:
272 =head2 config callback
274 Interface: B<org.collectd.api.CollectdConfigInterface>
276 Signature: I<int> B<config> (I<OConfigItem> ci)
278 This method is passed a B<OConfigItem> object, if both, method and
279 configuration, are available. B<OConfigItem> is the root of a tree representing
280 the configuration for this plugin. The root itself is the representation of the
281 B<E<lt>PluginE<nbsp>/E<gt>> block, so in next to all cases the children of the
282 root are the first interesting objects.
284 To signal success, this method has to return zero. Anything else will be
285 considered an error condition and the plugin will be disabled entirely.
287 See L<"registerConfig"> above.
291 Interface: B<org.collectd.api.CollectdInitInterface>
293 Signature: I<int> B<init> ()
295 This method is called after the configuration has been handled. It is
296 supposed to set up the plugin. e.E<nbsp>g. start threads, open connections, or
297 check if can do anything useful at all.
299 To signal success, this method has to return zero. Anything else will be
300 considered an error condition and the plugin will be disabled entirely.
302 See L<"registerInit"> above.
306 Interface: B<org.collectd.api.CollectdReadInterface>
308 Signature: I<int> B<read> ()
310 This method is called periodically and is supposed to gather statistics in
311 whatever fashion. These statistics are represented as a B<ValueList> object and
312 sent to the daemon using L<dispatchValues|"dispatchValues">.
314 To signal success, this method has to return zero. Anything else will be
315 considered an error condition and cause an appropriate message to be logged.
316 Currently, returning non-zero does not have any other effects. In particular,
317 Java "read"-methods are not suspended for increasing intervals like C
320 See L<"registerRead"> above.
322 =head2 write callback
324 Interface: B<org.collectd.api.CollectdWriteInterface>
326 Signature: I<int> B<write> (I<ValueList> vl)
328 This method is called whenever a value is dispatched to the daemon. The
329 corresponding C "write"-functions are passed a C<data_set_t>, so they can
330 decide which values are absolute values (gauge) and which are counter values.
331 To get the corresponding C<ListE<lt>DataSourceE<gt>>, call the B<getDataSource>
332 method of the B<ValueList> object.
334 To signal success, this method has to return zero. Anything else will be
335 considered an error condition and cause an appropriate message to be logged.
337 See L<"registerWrite"> above.
339 =head2 flush callback
341 Interface: B<org.collectd.api.CollectdFlushInterface>
343 Signature: I<int> B<flush> (I<int> timeout, I<String> identifier)
345 This method is called when the daemon received a flush command. This can either
346 be done using the C<USR1> signal (see L<collectd(1)>) or using the I<unixsock>
347 plugin (see L<collectd-unixsock(5)>).
349 If I<timeout> is greater than zero, only values older than this number of
350 seconds should be flushed. To signal that all values should be flushed
351 regardless of age, this argument is set to a negative number.
353 The I<identifier> specifies which value should be flushed. If it is not
354 possible to flush one specific value, flush all values. To signal that all
355 values should be flushed, this argument is set to I<null>.
357 To signal success, this method has to return zero. Anything else will be
358 considered an error condition and cause an appropriate message to be logged.
360 See L<"registerFlush"> above.
362 =head2 shutdown callback
364 Interface: B<org.collectd.api.CollectdShutdownInterface>
366 Signature: I<int> B<shutdown> ()
368 This method is called when the daemon is shutting down. You should not rely on
369 the destructor to clean up behind the object but use this function instead.
371 To signal success, this method has to return zero. Anything else will be
372 considered an error condition and cause an appropriate message to be logged.
374 See L<"registerShutdown"> above.
378 Interface: B<org.collectd.api.CollectdLogInterface>
380 Signature: I<void> B<log> (I<int> severity, I<String> message)
382 This callback can be used to receive log messages from the daemon.
384 The argument I<severity> is one of:
390 org.collectd.api.Collectd.LOG_ERR
394 org.collectd.api.Collectd.LOG_WARNING
398 org.collectd.api.Collectd.LOG_NOTICE
402 org.collectd.api.Collectd.LOG_INFO
406 org.collectd.api.Collectd.LOG_DEBUG
410 The function does not return any value.
412 See L<"registerLog"> above.
414 =head2 notification callback
416 Interface: B<org.collectd.api.CollectdNotificationInterface>
418 Signature: I<int> B<notification> (I<Notification> n)
420 This callback can be used to receive notifications from the daemon.
422 To signal success, this method has to return zero. Anything else will be
423 considered an error condition and cause an appropriate message to be logged.
425 See L<"registerNotification"> above.
427 =head2 match callback
429 The match (and target, see L<"target callback"> below) callbacks work a bit
430 different from the other callbacks above: You don't register a match callback
431 with the daemon directly, but you register a function which, when called,
432 creates an appropriate object. The object creating the "match" objects is
433 called "match factory".
435 See L<"registerMatch"> above.
437 =head3 Factory object
439 Interface: B<org.collectd.api.CollectdMatchFactoryInterface>
441 Signature: I<CollectdMatchInterface> B<createMatch>
444 Called by the daemon to create "match" objects.
446 Returns: A new object which implements the B<CollectdMatchInterface> interface.
450 Interface: B<org.collectd.api.CollectdMatchInterface>
452 Signature: I<int> B<match> (I<DataSet> ds, I<ValueList> vl);
454 Called when processing a chain to determine whether or not a I<ValueList>
455 matches. How values are matches is up to the implementing class.
457 Has to return one of:
463 B<Collectd.FC_MATCH_NO_MATCH>
467 B<Collectd.FC_MATCH_MATCHES>
471 =head2 target callback
473 The target (and match, see L<"match callback"> above) callbacks work a bit
474 different from the other callbacks above: You don't register a target callback
475 with the daemon directly, but you register a function which, when called,
476 creates an appropriate object. The object creating the "target" objects is
477 called "target factory".
479 See L<"registerTarget"> above.
481 =head3 Factory object
483 Interface: B<org.collectd.api.CollectdTargetFactoryInterface>
485 Signature: I<CollectdTargetInterface> B<createTarget>
488 Called by the daemon to create "target" objects.
490 Returns: A new object which implements the B<CollectdTargetInterface>
495 Interface: B<org.collectd.api.CollectdTargetInterface>
497 Signature: I<int> B<invoke> (I<DataSet> ds, I<ValueList> vl);
499 Called when processing a chain to perform some action. The action performed is
500 up to the implementing class.
502 Has to return one of:
508 B<Collectd.FC_TARGET_CONTINUE>
512 B<Collectd.FC_TARGET_STOP>
516 B<Collectd.FC_TARGET_RETURN>
522 This short example demonstrates how to register a read callback with the
525 import org.collectd.api.Collectd;
526 import org.collectd.api.ValueList;
528 import org.collectd.api.CollectdReadInterface;
530 public class Foobar implements CollectdReadInterface
534 Collectd.registerRead ("Foobar", this);
541 /* Do something... */
543 Collectd.dispatchValues (vl);
549 The following plugins are implemented in I<Java>. Both, the B<LoadPlugin>
550 option and the B<Plugin> block must be inside the
551 B<E<lt>PluginE<nbsp>javaE<gt>> block (see above).
553 =head2 GenericJMX plugin
555 The GenericJMX plugin reads I<Managed Beans> (MBeans) from an I<MBeanServer>
556 using JMX. JMX is a generic framework to provide and query various management
557 information. The interface is used by Java processes to provide internal
558 statistics as well as by the I<Java Virtual Machine> (JVM) to provide
559 information about the memory used, threads and so on.
561 The configuration of the I<GenericJMX plugin> consists of two blocks: I<MBean>
562 blocks that define a mapping of MBean attributes to the “types” used by
563 I<collectd>, and I<Connection> blocks which define the parameters needed to
564 connect to an I<MBeanServer> and what data to collect. The configuration of the
565 I<SNMP plugin> is similar in nature, in case you know it.
569 I<MBean> blocks specify what data is retrieved from I<MBeans> and how that data
570 is mapped on the I<collectd> data types. The block requires one string
571 argument, a name. This name is used in the I<Connection> blocks (see below) to
572 refer to a specific I<MBean> block. Therefore, the names must be unique.
574 The following options are recognized within I<MBean> blocks:
578 =item B<ObjectName> I<pattern>
580 Sets the pattern which is used to retrieve I<MBeans> from the I<MBeanServer>.
581 If more than one MBean is returned you should use the B<InstanceFrom> option
582 (see below) to make the identifiers unique.
585 L<http://java.sun.com/javase/6/docs/api/javax/management/ObjectName.html>
587 =item B<InstancePrefix> I<prefix>
589 Prefixes the generated I<plugin instance> with I<prefix>. I<(optional)>
591 =item B<InstanceFrom> I<property>
593 The I<object names> used by JMX to identify I<MBeans> include so called
594 I<“properties”> which are basically key-value-pairs. If the given object name
595 is not unique and multiple MBeans are returned, the values of those properties
596 usually differ. You can use this option to build the I<plugin instance> from
597 the appropriate property values. This option is optional and may be repeated to
598 generate the I<plugin instance> from multiple property values.
600 =item B<E<lt>value /E<gt>> blocks
602 The I<value> blocks map one or more attributes of an I<MBean> to a value list
603 in I<collectd>. There must be at least one Value block within each I<MBean>
610 Sets the data set used within I<collectd> to handle the values of the I<MBean>
613 =item B<InstancePrefix> I<prefix>
615 Works like the option of the same name directly beneath the I<MBean> block, but
616 sets the type instance instead. I<(optional)>
618 =item B<InstanceFrom> I<prefix>
620 Works like the option of the same name directly beneath the I<MBean> block, but
621 sets the type instance instead. I<(optional)>
623 =item B<PluginName> I<name>
625 When set, overrides the default setting for the I<plugin> field
628 =item B<Table> B<true>|B<false>
630 Set this to true if the returned attribute is a I<composite type>. If set to
631 true, the keys within the I<composite type> is appended to the
634 =item B<Attribute> I<path>
636 Sets the name of the attribute from which to read the value. You can access the
637 keys of composite types by using a dot to concatenate the key name to the
638 attribute name. For example: “attrib0.key42”. If B<Table> is set to B<true>
639 I<path> must point to a I<composite type>, otherwise it must point to a numeric
646 =head3 Connection blocks
648 Connection blocks specify I<how> to connect to an I<MBeanServer> and what data
649 to retrieve. The following configuration options are available:
653 =item B<Host> I<name>
655 Host name used when dispatching the values to I<collectd>. The option sets this
656 field only, it is I<not> used to connect to anything and doesn't need to be a
657 real, resolvable name.
659 =item B<ServiceURL> I<URL>
661 Specifies how the I<MBeanServer> can be reached. Any string accepted by the
662 I<JMXServiceURL> is valid.
665 L<http://java.sun.com/javase/6/docs/api/javax/management/remote/JMXServiceURL.html>
667 =item B<User> I<name>
669 Use I<name> to authenticate to the server. If not configured, “monitorRole”
672 =item B<Password> I<password>
674 Use I<password> to authenticate to the server. If not given, unauthenticated
677 =item B<InstancePrefix> I<prefix>
679 Prefixes the generated I<plugin instance> with I<prefix>. If a second
680 I<InstancePrefix> is specified in a referenced I<MBean> block, the prefix
681 specified in the I<Connection> block will appear at the beginning of the
682 I<plugin instance>, the prefix specified in the I<MBean> block will be appended
685 =item B<Collect> I<mbean_block_name>
687 Configures which of the I<MBean> blocks to use with this connection. May be
688 repeated to collect multiple I<MBeans> from this server.
701 Florian Forster E<lt>octoE<nbsp>atE<nbsp>collectd.orgE<gt>