3 collectd-java - Documentation of collectd's "java plugin"
10 JVMArg "-Djava.class.path=/opt/collectd/lib/collectd/bindings/java"
12 LoadPlugin "org.collectd.java.Foobar"
13 <Plugin "org.collectd.java.Foobar">
14 # To be parsed by the plugin
20 The I<Java> plugin embeds a I<Java Virtual Machine> (JVM) into I<collectd> and
21 provides a Java interface to part of collectd's API. This makes it possible to
22 write additions to the daemon in Java.
24 This plugin is similar in nature to, but shares no code with, the I<Perl>
25 plugin by Sebastian Harl, see L<collectd-perl(5)> for details.
29 A short outline of this plugin's configuration can be seen in L<"SYNOPSIS">
30 above. For a complete list of all configuration options and their semantics
31 please read L<collectd.conf(5)/Plugin C<java>>.
35 When writing additions for collectd in Java, the underlying C base is mostly
36 hidden from you. All complex data types are converted to their Java counterparts
37 before they're passed to your functions. These Java classes reside in the
38 I<org.collectd.api> namespace.
40 The I<Java> plugin will create one object of each class configured with the
41 B<LoadPlugin> option. The constructor of this class can then register "callback
42 methods", i.E<nbsp>e. methods that will be called by the daemon when
45 The available classes are:
49 =item B<org.collectd.api.Collectd>
51 All API functions exported to Java are implemented as static functions of this
52 class. See L<"EXPORTED API FUNCTIONS"> below.
54 =item B<org.collectd.api.OConfigValue>
56 Corresponds to C<oconfig_value_t>, defined in F<src/liboconfig/oconfig.h>.
58 =item B<org.collectd.api.OConfigItem>
60 Corresponds to C<oconfig_item_t>, defined in F<src/liboconfig/oconfig.h>.
62 =item B<org.collectd.api.DataSource>
64 Corresponds to C<data_source_t>, defined in F<src/plugin.h>.
66 =item B<org.collectd.api.DataSet>
68 Corresponds to C<data_set_t>, defined in F<src/plugin.h>.
70 =item B<org.collectd.api.ValueList>
72 Corresponds to C<value_list_t>, defined in F<src/plugin.h>.
74 =item B<org.collectd.api.Notification>
76 Corresponds to C<notification_t>, defined in F<src/plugin.h>.
80 In the remainder of this document, we'll use the short form of these names, for
81 example B<ValueList>. In order to be able to use these abbreviated names, you
82 need to B<import> the classes.
84 =head1 EXPORTED API FUNCTIONS
86 All collectd API functions that are available to Java plugins are implemented
87 as I<publicE<nbsp>static> functions of the B<Collectd> class. This makes
88 calling these functions pretty straight forward. For example, to send an error
89 message to the daemon, you'd do something like this:
91 Collectd.logError ("That wasn't chicken!");
93 The following are the currently exported functions.
97 Signature: I<int> B<registerConfig> (I<String> name,
98 I<CollectdConfigInterface> object);
100 Registers the B<config> function of I<object> with the daemon.
102 Returns zero upon success and non-zero when an error occurred.
104 See L<"config callback"> below.
108 Signature: I<int> B<registerInit> (I<String> name,
109 I<CollectdInitInterface> object);
111 Registers the B<init> function of I<object> with the daemon.
113 Returns zero upon success and non-zero when an error occurred.
115 See L<"init callback"> below.
119 Signature: I<int> B<registerRead> (I<String> name,
120 I<CollectdReadInterface> object)
122 Registers the B<read> function of I<object> with the daemon.
124 Returns zero upon success and non-zero when an error occurred.
126 See L<"read callback"> below.
130 Signature: I<int> B<registerWrite> (I<String> name,
131 I<CollectdWriteInterface> object)
133 Registers the B<write> function of I<object> with the daemon.
135 Returns zero upon success and non-zero when an error occurred.
137 See L<"write callback"> below.
141 Signature: I<int> B<registerFlush> (I<String> name,
142 I<CollectdFlushInterface> object)
144 Registers the B<flush> function of I<object> with the daemon.
146 Returns zero upon success and non-zero when an error occurred.
148 See L<"flush callback"> below.
150 =head2 registerShutdown
152 Signature: I<int> B<registerShutdown> (I<String> name,
153 I<CollectdShutdownInterface> object);
155 Registers the B<shutdown> function of I<object> with the daemon.
157 Returns zero upon success and non-zero when an error occurred.
159 See L<"shutdown callback"> below.
163 Signature: I<int> B<registerLog> (I<String> name,
164 I<CollectdLogInterface> object);
166 Registers the B<log> function of I<object> with the daemon.
168 Returns zero upon success and non-zero when an error occurred.
170 See L<"log callback"> below.
172 =head2 registerNotification
174 Signature: I<int> B<registerNotification> (I<String> name,
175 I<CollectdNotificationInterface> object);
177 Registers the B<notification> function of I<object> with the daemon.
179 Returns zero upon success and non-zero when an error occurred.
181 See L<"notification callback"> below.
185 Signature: I<int> B<registerMatch> (I<String> name,
186 I<CollectdMatchFactoryInterface> object);
188 Registers the B<createMatch> function of I<object> with the daemon.
190 Returns zero upon success and non-zero when an error occurred.
192 See L<"match callback"> below.
194 =head2 registerTarget
196 Signature: I<int> B<registerTarget> (I<String> name,
197 I<CollectdTargetFactoryInterface> object);
199 Registers the B<createTarget> function of I<object> with the daemon.
201 Returns zero upon success and non-zero when an error occurred.
203 See L<"target callback"> below.
205 =head2 dispatchValues
207 Signature: I<int> B<dispatchValues> (I<ValueList>)
209 Passes the values represented by the B<ValueList> object to the
210 C<plugin_dispatch_values> function of the daemon. The "data set" (or list of
211 "data sources") associated with the object are ignored, because
212 C<plugin_dispatch_values> will automatically lookup the required data set. It
213 is therefore absolutely okay to leave this blank.
215 Returns zero upon success or non-zero upon failure.
219 Signature: I<DataSet> B<getDS> (I<String>)
221 Returns the appropriate I<type> or B<null> if the type is not defined.
225 Signature: I<void> B<logError> (I<String>)
227 Sends a log message with severity B<ERROR> to the daemon.
231 Signature: I<void> B<logWarning> (I<String>)
233 Sends a log message with severity B<WARNING> to the daemon.
237 Signature: I<void> B<logNotice> (I<String>)
239 Sends a log message with severity B<NOTICE> to the daemon.
243 Signature: I<void> B<logInfo> (I<String>)
245 Sends a log message with severity B<INFO> to the daemon.
249 Signature: I<void> B<logDebug> (I<String>)
251 Sends a log message with severity B<DEBUG> to the daemon.
253 =head1 REGISTERING CALLBACKS
255 When starting up, collectd creates an object of each configured class. The
256 constructor of this class should then register "callbacks" with the daemon,
257 using the appropriate static functions in B<Collectd>,
258 see L<"EXPORTED API FUNCTIONS"> above. To register a callback, the object being
259 passed to one of the register functions must implement an appropriate
260 interface, which are all in the B<org.collectd.api> namespace.
262 A constructor may register any number of these callbacks, even none. An object
263 without callback methods is never actively called by collectd, but may still
264 call the exported API functions. One could, for example, start a new thread in
265 the constructor and dispatch (submit to the daemon) values asynchronously,
266 whenever one is available.
268 Each callback method is now explained in more detail:
270 =head2 config callback
272 Interface: B<org.collectd.api.CollectdConfigInterface>
274 Signature: I<int> B<config> (I<OConfigItem> ci)
276 This method is passed a B<OConfigItem> object, if both, method and
277 configuration, are available. B<OConfigItem> is the root of a tree representing
278 the configuration for this plugin. The root itself is the representation of the
279 B<E<lt>PluginE<nbsp>/E<gt>> block, so in next to all cases the children of the
280 root are the first interesting objects.
282 To signal success, this method has to return zero. Anything else will be
283 considered an error condition and the plugin will be disabled entirely.
285 See L<"registerConfig"> above.
289 Interface: B<org.collectd.api.CollectdInitInterface>
291 Signature: I<int> B<init> ()
293 This method is called after the configuration has been handled. It is
294 supposed to set up the plugin. e.E<nbsp>g. start threads, open connections, or
295 check if can do anything useful at all.
297 To signal success, this method has to return zero. Anything else will be
298 considered an error condition and the plugin will be disabled entirely.
300 See L<"registerInit"> above.
304 Interface: B<org.collectd.api.CollectdReadInterface>
306 Signature: I<int> B<read> ()
308 This method is called periodically and is supposed to gather statistics in
309 whatever fashion. These statistics are represented as a B<ValueList> object and
310 sent to the daemon using L<dispatchValues|"dispatchValues">.
312 To signal success, this method has to return zero. Anything else will be
313 considered an error condition and cause an appropriate message to be logged.
314 Currently, returning non-zero does not have any other effects. In particular,
315 Java "read"-methods are not suspended for increasing intervals like C
318 See L<"registerRead"> above.
320 =head2 write callback
322 Interface: B<org.collectd.api.CollectdWriteInterface>
324 Signature: I<int> B<write> (I<ValueList> vl)
326 This method is called whenever a value is dispatched to the daemon. The
327 corresponding C "write"-functions are passed a C<data_set_t>, so they can
328 decide which values are absolute values (gauge) and which are counter values.
329 To get the corresponding C<ListE<lt>DataSourceE<gt>>, call the B<getDataSource>
330 method of the B<ValueList> object.
332 To signal success, this method has to return zero. Anything else will be
333 considered an error condition and cause an appropriate message to be logged.
335 See L<"registerWrite"> above.
337 =head2 flush callback
339 Interface: B<org.collectd.api.CollectdFlushInterface>
341 Signature: I<int> B<flush> (I<int> timeout, I<String> identifier)
343 This method is called when the daemon received a flush command. This can either
344 be done using the C<USR1> signal (see L<collectd(1)>) or using the I<unixsock>
345 plugin (see L<collectd-unixsock(5)>).
347 If I<timeout> is greater than zero, only values older than this number of
348 seconds should be flushed. To signal that all values should be flushed
349 regardless of age, this argument is set to a negative number.
351 The I<identifier> specifies which value should be flushed. If it is not
352 possible to flush one specific value, flush all values. To signal that all
353 values should be flushed, this argument is set to I<null>.
355 To signal success, this method has to return zero. Anything else will be
356 considered an error condition and cause an appropriate message to be logged.
358 See L<"registerFlush"> above.
360 =head2 shutdown callback
362 Interface: B<org.collectd.api.CollectdShutdownInterface>
364 Signature: I<int> B<shutdown> ()
366 This method is called when the daemon is shutting down. You should not rely on
367 the destructor to clean up behind the object but use this function instead.
369 To signal success, this method has to return zero. Anything else will be
370 considered an error condition and cause an appropriate message to be logged.
372 See L<"registerShutdown"> above.
376 Interface: B<org.collectd.api.CollectdLogInterface>
378 Signature: I<void> B<log> (I<int> severity, I<String> message)
380 This callback can be used to receive log messages from the daemon.
382 The argument I<severity> is one of:
388 org.collectd.api.Collectd.LOG_ERR
392 org.collectd.api.Collectd.LOG_WARNING
396 org.collectd.api.Collectd.LOG_NOTICE
400 org.collectd.api.Collectd.LOG_INFO
404 org.collectd.api.Collectd.LOG_DEBUG
408 The function does not return any value.
410 See L<"registerLog"> above.
412 =head2 notification callback
414 Interface: B<org.collectd.api.CollectdNotificationInterface>
416 Signature: I<int> B<notification> (I<Notification> n)
418 This callback can be used to receive notifications from the daemon.
420 To signal success, this method has to return zero. Anything else will be
421 considered an error condition and cause an appropriate message to be logged.
423 See L<"registerNotification"> above.
425 =head2 match callback
427 The match (and target, see L<"target callback"> below) callbacks work a bit
428 different from the other callbacks above: You don't register a match callback
429 with the daemon directly, but you register a function which, when called,
430 creates an appropriate object. The object creating the "match" objects is
431 called "match factory".
433 See L<"registerMatch"> above.
435 =head3 Factory object
437 Interface: B<org.collectd.api.CollectdMatchFactoryInterface>
439 Signature: I<CollectdMatchInterface> B<createMatch>
442 Called by the daemon to create "match" objects.
444 Returns: A new object which implements the B<CollectdMatchInterface> interface.
448 Interface: B<org.collectd.api.CollectdMatchInterface>
450 Signature: I<int> B<match> (I<DataSet> ds, I<ValueList> vl);
452 Called when processing a chain to determine whether or not a I<ValueList>
453 matches. How values are matches is up to the implementing class.
455 Has to return one of:
461 B<Collectd.FC_MATCH_NO_MATCH>
465 B<Collectd.FC_MATCH_MATCHES>
469 =head2 target callback
471 The target (and match, see L<"match callback"> above) callbacks work a bit
472 different from the other callbacks above: You don't register a target callback
473 with the daemon directly, but you register a function which, when called,
474 creates an appropriate object. The object creating the "target" objects is
475 called "target factory".
477 See L<"registerTarget"> above.
479 =head3 Factory object
481 Interface: B<org.collectd.api.CollectdTargetFactoryInterface>
483 Signature: I<CollectdTargetInterface> B<createTarget>
486 Called by the daemon to create "target" objects.
488 Returns: A new object which implements the B<CollectdTargetInterface>
493 Interface: B<org.collectd.api.CollectdTargetInterface>
495 Signature: I<int> B<invoke> (I<DataSet> ds, I<ValueList> vl);
497 Called when processing a chain to perform some action. The action performed is
498 up to the implementing class.
500 Has to return one of:
506 B<Collectd.FC_TARGET_CONTINUE>
510 B<Collectd.FC_TARGET_STOP>
514 B<Collectd.FC_TARGET_RETURN>
520 This short example demonstrates how to register a read callback with the
523 import org.collectd.api.Collectd;
524 import org.collectd.api.ValueList;
526 import org.collectd.api.CollectdReadInterface;
528 public class Foobar implements CollectdReadInterface
532 Collectd.registerRead ("Foobar", this);
539 /* Do something... */
541 Collectd.dispatchValues (vl);
554 Florian Forster E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt>