2 * /usr/share/doc/collectd/examples/sample_plugin.c
4 * A sample plugin for collectd.
6 * Written by Sebastian Harl <sh@tokkee.org>
8 * This is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <collectd/common.h> /* rrd_update_file */
15 #include <collectd/plugin.h> /* plugin_* */
20 /* Optional config file support */
21 /* #include <collectd/configfile.h> */
23 /* Optional debugging support
24 * (only available if collectd was compiled with debugging support) */
25 /* #include <collectd/utils_debug.h> */
27 #define MODULE_NAME "myplugin"
29 /* Name of the rrd file under DataDir (/var/lib/collectd by default)
31 * The name may contain slashes to create subdirectories. */
32 static char *my_rrd = "myplugin.rrd";
34 /* DS definitions for the rrd file
36 * See the rrdcreate(1) manpage for details. The heartbeat is configurable in
37 * collectd. It defaults to 25. */
38 static char *ds_def[] =
40 "DS:my_ds:GAUGE:25:0:U",
45 static int ds_num = 1;
47 /* Time at which the read function is called */
48 extern time_t curtime;
50 /* Initialize the plugin
52 * This function is called to set up a plugin before using it. */
53 static void my_init(void)
55 /* we have nothing to do here :-) */
61 * This function implements the magic used to get the desired values that
62 * should be stored in the rrd file. It uses plugin_submit to transfer the
63 * data to whatever place is configured in the config file. If there are more
64 * than one instances you should pass a uniq identifier as seconds argument to
65 * the plugin_submit function. */
67 static void my_read(void)
70 char buf[BUFSIZE] = "";
75 if (snprintf(buf, BUFSIZE, "%u:%li",
76 (unsigned int)curtime, data) >= BUFSIZE)
79 plugin_submit(MODULE_NAME, NULL, buf);
86 * This function saves the data to the appropriate location by calling
87 * rrd_update_file. It is used to "calculate" the filename and DS definition
88 * appropriate for the given instance. */
89 static void my_write(host, inst, val)
94 rrd_update_file(host, my_rrd, val, ds_def, ds_num);
98 /* Register the plugin
100 * This function registers the plugin with collectd. It has to be named
101 * "module_register". */
102 void module_register(void)
104 plugin_register(MODULE_NAME, my_init, my_read, my_write);