1 /*
2 * /usr/share/doc/collectd/examples/myplugin.c
3 *
4 * A plugin template for collectd.
5 *
6 * Written by Sebastian Harl <sh@tokkee.org>
7 *
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.
11 */
13 /*
14 * Notes:
15 * - plugins are executed in parallel, thus, thread-safe
16 * functions need to be used
17 * - each of the functions below (except module_register)
18 * is optional
19 */
21 #if ! HAVE_CONFIG_H
23 #include <stdlib.h>
25 #include <string.h>
27 #ifndef __USE_ISOC99 /* required for NAN */
28 # define DISABLE_ISOC99 1
29 # define __USE_ISOC99 1
30 #endif /* !defined(__USE_ISOC99) */
31 #include <math.h>
32 #if DISABLE_ISOC99
33 # undef DISABLE_ISOC99
34 # undef __USE_ISOC99
35 #endif /* DISABLE_ISOC99 */
37 #include <time.h>
39 #endif /* ! HAVE_CONFIG */
41 #include <collectd/collectd.h>
43 #include <collectd/common.h>
44 #include <collectd/plugin.h>
46 /*
47 * data source definition:
48 * - name of the data source
49 * - type of the data source (DS_TYPE_GAUGE, DS_TYPE_COUNTER)
50 * - minimum allowed value
51 * - maximum allowed value
52 */
53 static data_source_t dsrc[1] =
54 {
55 { "my_ds", DS_TYPE_GAUGE, 0, NAN }
56 };
58 /*
59 * data set definition:
60 * - name of the data set
61 * - number of data sources
62 * - list of data sources
63 *
64 * NOTE: If you're defining a custom data-set, you have to make that known to
65 * any servers as well. Else, the server is not able to store values using the
66 * type defined by that data-set.
67 * It is strongly recommended to use one of the types and data-sets
68 * pre-defined in the types.db file.
69 */
70 static data_set_t ds =
71 {
72 "myplugin", STATIC_ARRAY_SIZE (dsrc), dsrc
73 };
75 /*
76 * This function is called once upon startup to initialize the plugin.
77 */
78 static int my_init (void)
79 {
80 /* open sockets, initialize data structures, ... */
82 /* A return value != 0 indicates an error and causes the plugin to be
83 disabled. */
84 return 0;
85 } /* static int my_init (void) */
87 /*
88 * This is a utility function used by the read callback to populate a
89 * value_list_t and pass it to plugin_dispatch_values.
90 */
91 static int my_submit (gauge_t value)
92 {
93 value_list_t vl = VALUE_LIST_INIT;
95 /* Convert the gauge_t to a value_t and add it to the value_list_t. */
96 vl.values = &(value_t) { .gauge = value };
97 vl.values_len = 1;
99 /* Only set vl.time yourself if you update multiple metrics (i.e. you
100 * have multiple calls to plugin_dispatch_values()) and they need to all
101 * have the same timestamp. */
102 /* vl.time = cdtime(); */
104 sstrncpy (vl.plugin, "myplugin", sizeof (vl.plugin));
106 /* it is strongly recommended to use a type defined in the types.db file
107 * instead of a custom type */
108 sstrncpy (vl.type, "myplugin", sizeof (vl.type));
109 /* optionally set vl.plugin_instance and vl.type_instance to reasonable
110 * values (default: "") */
112 /* dispatch the values to collectd which passes them on to all registered
113 * write functions */
114 return plugin_dispatch_values (&vl);
115 }
117 /*
118 * This function is called in regular intervalls to collect the data.
119 */
120 static int my_read (void)
121 {
122 /* do the magic to read the data */
123 gauge_t value = random ();
125 if (my_submit (value) != 0)
126 WARNING ("myplugin plugin: Dispatching a random value failed.");
128 /* A return value != 0 indicates an error and the plugin will be skipped
129 * for an increasing amount of time. */
130 return 0;
131 } /* static int my_read (void) */
133 /*
134 * This function is called after values have been dispatched to collectd.
135 */
136 static int my_write (const data_set_t *ds, const value_list_t *vl,
137 user_data_t *ud)
138 {
139 char name[1024] = "";
140 int i = 0;
142 if (ds->ds_num != vl->values_len) {
143 plugin_log (LOG_WARNING, "DS number does not match values length");
144 return -1;
145 }
147 /* get the default base filename for the output file - depending on the
148 * provided values this will be something like
149 * <host>/<plugin>[-<plugin_type>]/<instance>[-<instance_type>] */
150 if (0 != format_name (name, 1024, vl->host, vl->plugin,
151 vl->plugin_instance, ds->type, vl->type_instance))
152 return -1;
154 for (i = 0; i < ds->ds_num; ++i) {
155 /* do the magic to output the data */
156 printf ("%s (%s) at %i: ", name,
157 (ds->ds->type == DS_TYPE_GAUGE) ? "GAUGE" : "COUNTER",
158 (int)vl->time);
160 if (ds->ds->type == DS_TYPE_GAUGE)
161 printf ("%f\n", vl->values[i].gauge);
162 else
163 printf ("%lld\n", vl->values[i].counter);
164 }
165 return 0;
166 } /* static int my_write (data_set_t *, value_list_t *) */
168 /*
169 * This function is called when plugin_log () has been used.
170 */
171 static void my_log (int severity, const char *msg, user_data_t *ud)
172 {
173 printf ("LOG: %i - %s\n", severity, msg);
174 return;
175 } /* static void my_log (int, const char *) */
177 /*
178 * This function is called when plugin_dispatch_notification () has been used.
179 */
180 static int my_notify (const notification_t *notif, user_data_t *ud)
181 {
182 char time_str[32] = "";
183 struct tm *tm = NULL;
185 int n = 0;
187 if (NULL == (tm = localtime (¬if->time)))
188 time_str[0] = '\0';
190 n = strftime (time_str, 32, "%F %T", tm);
191 if (n >= 32) n = 31;
192 time_str[n] = '\0';
194 printf ("NOTIF (%s): %i - ", time_str, notif->severity);
196 if ('\0' != *notif->host)
197 printf ("%s: ", notif->host);
199 if ('\0' != *notif->plugin)
200 printf ("%s: ", notif->plugin);
202 if ('\0' != *notif->plugin_instance)
203 printf ("%s: ", notif->plugin_instance);
205 if ('\0' != *notif->type)
206 printf ("%s: ", notif->type);
208 if ('\0' != *notif->type_instance)
209 printf ("%s: ", notif->type_instance);
211 printf ("%s\n", notif->message);
212 return 0;
213 } /* static int my_notify (notification_t *) */
215 /*
216 * This function is called before shutting down collectd.
217 */
218 static int my_shutdown (void)
219 {
220 /* close sockets, free data structures, ... */
221 return 0;
222 } /* static int my_shutdown (void) */
224 /*
225 * This function is called after loading the plugin to register it with
226 * collectd.
227 */
228 void module_register (void)
229 {
230 plugin_register_log ("myplugin", my_log, /* user data */ NULL);
231 plugin_register_notification ("myplugin", my_notify,
232 /* user data */ NULL);
233 plugin_register_data_set (&ds);
234 plugin_register_read ("myplugin", my_read);
235 plugin_register_init ("myplugin", my_init);
236 plugin_register_write ("myplugin", my_write, /* user data */ NULL);
237 plugin_register_shutdown ("myplugin", my_shutdown);
238 return;
239 } /* void module_register (void) */