2 * /usr/share/doc/collectd/examples/myplugin.c
4 * A plugin template 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; only version 2 of the License is applicable.
15 * - plugins are executed in parallel, thus, thread-safe
16 * functions need to be used
17 * - each of the functions below (except module_register)
26 #ifndef __USE_ISOC99 /* required for NAN */
27 # define DISABLE_ISOC99 1
28 # define __USE_ISOC99 1
29 #endif /* !defined(__USE_ISOC99) */
32 # undef DISABLE_ISOC99
34 #endif /* DISABLE_ISOC99 */
36 #include <collectd/collectd.h>
37 #include <collectd/common.h>
38 #include <collectd/plugin.h>
41 * data source definition:
42 * - name of the data source
43 * - type of the data source (DS_TYPE_GAUGE, DS_TYPE_COUNTER)
44 * - minimum allowed value
45 * - maximum allowed value
47 static data_source_t dsrc[1] =
49 { "my_ds", DS_TYPE_GAUGE, 0, NAN }
53 * data set definition:
54 * - name of the data set
55 * - number of data sources
56 * - list of data sources
58 static data_set_t ds =
60 "myplugin", STATIC_ARRAY_SIZE (dsrc), dsrc
64 * This function is called once upon startup to initialize the plugin.
66 static int my_init (void)
68 /* open sockets, initialize data structures, ... */
70 /* A return value != 0 indicates an error and causes the plugin to be
73 } /* static int my_init (void) */
76 * This function is called in regular intervalls to collect the data.
78 static int my_read (void)
80 value_t values[1]; /* the size of this list should equal the number of
82 value_list_t vl = VALUE_LIST_INIT;
84 /* do the magic to read the data */
85 values[0].gauge = random ();
89 vl.time = time (NULL);
90 strcpy (vl.host, hostname_g);
91 strcpy (vl.plugin, "myplugin");
92 /* optionally set vl.plugin_instance and vl.type_instance to reasonable
93 * values (default: "") */
95 /* dispatch the values to collectd which passes them on to all registered
96 * write functions - the first argument is used to lookup the data set
98 plugin_dispatch_values ("myplugin", &vl);
100 /* A return value != 0 indicates an error and the plugin will be skipped
101 * for an increasing amount of time. */
103 } /* static int my_read (void) */
106 * This function is called after values have been dispatched to collectd.
108 static int my_write (const data_set_t *ds, const value_list_t *vl)
110 char name[1024] = "";
113 if (ds->ds_num != vl->values_len) {
114 plugin_log (LOG_WARNING, "DS number does not match values length");
118 /* get the default base filename for the output file - depending on the
119 * provided values this will be something like
120 * <host>/<plugin>[-<plugin_type>]/<instance>[-<instance_type>] */
121 if (0 != format_name (name, 1024, vl->host, vl->plugin,
122 vl->plugin_instance, ds->type, vl->type_instance))
125 for (i = 0; i < ds->ds_num; ++i) {
126 /* do the magic to output the data */
127 printf ("%s (%s) at %i: ", name,
128 (ds->ds->type == DS_TYPE_GAUGE) ? "GAUGE" : "COUNTER",
131 if (ds->ds->type == DS_TYPE_GAUGE)
132 printf ("%f\n", vl->values[i].gauge);
134 printf ("%lld\n", vl->values[i].counter);
137 } /* static int my_write (data_set_t *, value_list_t *) */
140 * This function is called when plugin_log () has been used.
142 static void my_log (int severity, const char *msg)
144 printf ("LOG: %i - %s\n", severity, msg);
146 } /* static void my_log (int, const char *) */
149 * This function is called before shutting down collectd.
151 static int my_shutdown (void)
153 /* close sockets, free data structures, ... */
155 } /* static int my_shutdown (void) */
158 * This function is called after loading the plugin to register it with
161 void module_register (void)
163 plugin_register_log ("myplugin", my_log);
164 plugin_register_data_set (&ds);
165 plugin_register_read ("myplugin", my_read);
166 plugin_register_init ("myplugin", my_init);
167 plugin_register_write ("myplugin", my_write);
168 plugin_register_shutdown ("myplugin", my_shutdown);
170 } /* void module_register (void) */