=head1 NAME collectd-java - Documentation of collectd's "java plugin" =head1 SYNOPSIS LoadPlugin "java" JVMArg "-verbose:jni" JVMArg "-Djava.class.path=/opt/collectd/lib/collectd/bindings/java" LoadPlugin "org.collectd.java.Foobar" # To be parsed by the plugin =head1 DESCRIPTION The I plugin embeds a I (JVM) into I and provides a Java interface to part of collectd's API. This makes it possible to write additions to the daemon in Java. This plugin is similar in nature to, but shares no code with, the I plugin by Sebastian Harl, see L for details. =head1 CONFIGURATION A short outline of this plugin's configuration can be seen in L<"SYNOPSIS"> above. For a complete list of all configuration options and their semantics please read L>. =head1 OVERVIEW When writing additions for collectd in Java, the underlying C base is mostly hidden from you. All complex data types are converted to their Java counterparts before they're passed to your functions. These Java classes reside in the I namespace. The available classes are: =over 4 =item B Corresponds to C, defined in F. =item B Corresponds to C, defined in F. =item B Corresponds to C, defined in F. =item B Corresponds to C, defined in F. =item B Corresponds to C, defined in F. =back In the remainder of this document, we'll use the short form of these names, for example B. In order to be able to use these abbreviated names, you need to B the classes. The API functions that are available from Java are implemented as I functions of the B class. See L<"CALLING API FUNCTIONS"> below for details. =head1 REGISTERING CALLBACKS When starting up, collectd creates an object of each configured class. The constructor of this class should then register "callbacks" with the daemon, using the appropriate static functions in B, see L<"CALLING API FUNCTIONS"> below. To register a callback, the object being passed to one of the register functions must implement an appropriate interface, which are all in the B namespace. A constructor may register any number of these callbacks, even none. An object without callback methods is never actively called by collectd, but may still call the exported API functions. One could, for example, start a new thread in the constructor and dispatch (submit to the daemon) values asynchronously, whenever one is available. Each callback method is now explained in more detail: =head2 config callback Interface: B Signature: I B (I ci) This method is passed a B object, if both, method and configuration, are available. B is the root of a tree representing the configuration for this plugin. The root itself is the representation of the BPluginE/E> block, so in next to all cases the children of the root are the first interesting objects. To signal success, this method has to return zero. Anything else will be considered an error condition and the plugin will be disabled entirely. =head2 init callback Interface: B Signature: I B () This method is called after the configuration has been handled. It is supposed to set up the plugin. e.Eg. start threads, open connections, or check if can do anything useful at all. To signal success, this method has to return zero. Anything else will be considered an error condition and the plugin will be disabled entirely. =head2 read callback Interface: B Signature: I B () This method is called periodically and is supposed to gather statistics in whatever fashion. These statistics are represented as a B object and sent to the daemon using B, see L<"CALLING API FUNCTIONS"> below. To signal success, this method has to return zero. Anything else will be considered an error condition and cause an appropriate message to be logged. Currently, returning non-zero does not have any other effects. In particular, Java "read"-methods are not suspended for increasing intervals like C "read"-functions. =head2 write callback Interface: B Signature: I B (I vl) This method is called whenever a value is dispatched to the daemon. The corresponding C "write"-functions are passed a C, so they can decide which values are absolute values (gauge) and which are counter values. To get the corresponding CDataSourceE>, call the B method of the B object. To signal success, this method has to return zero. Anything else will be considered an error condition and cause an appropriate message to be logged. =head2 flush callback Interface: B Signature: I B (I timeout, I identifier) This method is called when the daemon received a flush command. This can either be done using the C signal (see L) or using the I plugin (see L). If I is greater than zero, only values older than this number of seconds should be flushed. To signal that all values should be flushed regardless of age, this argument is set to a negative number. The I specifies which value should be flushed. If it is not possible to flush one specific value, flush all values. To signal that all values should be flushed, this argument is set to I. To signal success, this method has to return zero. Anything else will be considered an error condition and cause an appropriate message to be logged. =head2 shutdown callback Interface: B Signature: I B () This method is called when the daemon is shutting down. You should not rely on the destructor to clean up behind the object but use this function instead. To signal success, this method has to return zero. Anything else will be considered an error condition and cause an appropriate message to be logged. =head2 log callback Interface: B Signature: I B (I severity, I message) This callback can be used to receive log messages from the daemon. The argument I is one of: =over 4 =item * org.collectd.api.Collectd.LOG_ERR =item * org.collectd.api.Collectd.LOG_WARNING =item * org.collectd.api.Collectd.LOG_NOTICE =item * org.collectd.api.Collectd.LOG_INFO =item * org.collectd.api.Collectd.LOG_DEBUG =back The function does not return any value. =head2 Example This short example demonstrates how to register a read callback with the daemon: import org.collectd.api.Collectd; import org.collectd.api.ValueList; import org.collectd.api.CollectdReadInterface; public class Foobar implements CollectdReadInterface { public Foobar () { Collectd.registerRead ("Foobar", this); } public int read () { ValueList vl; /* Do something... */ Collectd.dispatchValues (vl); } } =head1 CALLING API FUNCTIONS All collectd API functions that are available to Java plugins are implemented as Istatic> functions of the B class. This makes calling these functions pretty straight forward. The following are the currently exported functions. For information on the interfaces used, please see L<"REGISTERING CALLBACKS"> above. =head2 registerConfig Signature: I B (I name, I object); Registers the B function of I with the daemon. Returns zero upon success and non-zero when an error occurred. =head2 registerInit Signature: I B (I name, I object); Registers the B function of I with the daemon. Returns zero upon success and non-zero when an error occurred. =head2 registerRead Signature: I B (I name, I object) Registers the B function of I with the daemon. Returns zero upon success and non-zero when an error occurred. =head2 registerWrite Signature: I B (I name, I object) Registers the B function of I with the daemon. Returns zero upon success and non-zero when an error occurred. =head2 registerFlush Signature: I B (I name, I object) Registers the B function of I with the daemon. Returns zero upon success and non-zero when an error occurred. =head2 registerShutdown Signature: I B (I name, I object); Registers the B function of I with the daemon. Returns zero upon success and non-zero when an error occurred. =head2 registerLog Signature: I B (I name, I object); Registers the B function of I with the daemon. Returns zero upon success and non-zero when an error occurred. =head2 dispatchValues Signature: I B (I) Passes the values represented by the B object to the C function of the daemon. The "data set" (or list of "data sources") associated with the object are ignored, because C will automatically lookup the required data set. It is therefore absolutely okay to leave this blank. Returns zero upon success or non-zero upon failure. =head2 getDS Signature: I B (I) Returns the approrpate I or B if the type is not defined. =head2 logError Signature: I B (I) Sends a log message with severity B to the daemon. =head2 logWarning Signature: I B (I) Sends a log message with severity B to the daemon. =head2 logNotice Signature: I B (I) Sends a log message with severity B to the daemon. =head2 logInfo Signature: I B (I) Sends a log message with severity B to the daemon. =head2 logDebug Signature: I B (I) Sends a log message with severity B to the daemon. =head1 SEE ALSO L, L, L, L =head1 AUTHOR Florian Forster EoctoEatEverplant.orgE