f68cc1ac85c090792efa1bfe9433272327b378fa
[collectd.git] / contrib / examples / myplugin.c
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  */
12
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  */
20
21 #if ! HAVE_CONFIG_H
22
23 #include <stdlib.h>
24
25 #include <string.h>
26
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 */
36
37 #include <time.h>
38
39 #endif /* ! HAVE_CONFIG */
40
41 #include <collectd/collectd.h>
42 #include <collectd/common.h>
43 #include <collectd/plugin.h>
44
45 /*
46  * data source definition:
47  * - name of the data source
48  * - type of the data source (DS_TYPE_GAUGE, DS_TYPE_COUNTER)
49  * - minimum allowed value
50  * - maximum allowed value
51  */
52 static data_source_t dsrc[1] =
53 {
54         { "my_ds", DS_TYPE_GAUGE, 0, NAN }
55 };
56
57 /*
58  * data set definition:
59  * - name of the data set
60  * - number of data sources
61  * - list of data sources
62  *
63  * NOTE: If you're defining a custom data-set, you have to make that known to
64  * any servers as well. Else, the server is not able to store values using the
65  * type defined by that data-set.
66  * It is strongly recommended to use one of the types and data-sets
67  * pre-defined in the types.db file.
68  */
69 static data_set_t ds =
70 {
71         "myplugin", STATIC_ARRAY_SIZE (dsrc), dsrc
72 };
73
74 /*
75  * This function is called once upon startup to initialize the plugin.
76  */
77 static int my_init (void)
78 {
79         /* open sockets, initialize data structures, ... */
80
81         /* A return value != 0 indicates an error and causes the plugin to be
82            disabled. */
83     return 0;
84 } /* static int my_init (void) */
85
86 /*
87  * This function is called in regular intervalls to collect the data.
88  */
89 static int my_read (void)
90 {
91         value_t values[1]; /* the size of this list should equal the number of
92                                                   data sources */
93         value_list_t vl = VALUE_LIST_INIT;
94
95         /* do the magic to read the data */
96         values[0].gauge = random ();
97
98         vl.values     = values;
99         vl.values_len = 1;
100         vl.time       = time (NULL);
101         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
102         sstrncpy (vl.plugin, "myplugin", sizeof (vl.plugin));
103         /* optionally set vl.plugin_instance and vl.type_instance to reasonable
104          * values (default: "") */
105
106         /* dispatch the values to collectd which passes them on to all registered
107          * write functions - the first argument is used to lookup the data set
108          * definition (it is strongly recommended to use a type defined in the
109          * types.db file) */
110         plugin_dispatch_values ("myplugin", &vl);
111
112         /* A return value != 0 indicates an error and the plugin will be skipped
113          * for an increasing amount of time. */
114     return 0;
115 } /* static int my_read (void) */
116
117 /*
118  * This function is called after values have been dispatched to collectd.
119  */
120 static int my_write (const data_set_t *ds, const value_list_t *vl)
121 {
122         char name[1024] = "";
123         int i = 0;
124
125         if (ds->ds_num != vl->values_len) {
126                 plugin_log (LOG_WARNING, "DS number does not match values length");
127                 return -1;
128         }
129
130         /* get the default base filename for the output file - depending on the
131          * provided values this will be something like
132          * <host>/<plugin>[-<plugin_type>]/<instance>[-<instance_type>] */
133         if (0 != format_name (name, 1024, vl->host, vl->plugin,
134                         vl->plugin_instance, ds->type, vl->type_instance))
135                 return -1;
136
137         for (i = 0; i < ds->ds_num; ++i) {
138                 /* do the magic to output the data */
139                 printf ("%s (%s) at %i: ", name,
140                                 (ds->ds->type == DS_TYPE_GAUGE) ? "GAUGE" : "COUNTER",
141                                 (int)vl->time);
142
143                 if (ds->ds->type == DS_TYPE_GAUGE)
144                         printf ("%f\n", vl->values[i].gauge);
145                 else
146                         printf ("%lld\n", vl->values[i].counter);
147         }
148         return 0;
149 } /* static int my_write (data_set_t *, value_list_t *) */
150
151 /*
152  * This function is called when plugin_log () has been used.
153  */
154 static void my_log (int severity, const char *msg)
155 {
156         printf ("LOG: %i - %s\n", severity, msg);
157         return;
158 } /* static void my_log (int, const char *) */
159
160 /*
161  * This function is called when plugin_dispatch_notification () has been used.
162  */
163 static int my_notify (const notification_t *notif)
164 {
165         char time_str[32] = "";
166         struct tm *tm = NULL;
167
168         int n = 0;
169
170         if (NULL == (tm = localtime (&notif->time)))
171                 time_str[0] = '\0';
172
173         n = strftime (time_str, 32, "%F %T", tm);
174         if (n >= 32) n = 31;
175         time_str[n] = '\0';
176
177         printf ("NOTIF (%s): %i - ", time_str, notif->severity);
178
179         if ('\0' != *notif->host)
180                 printf ("%s: ", notif->host);
181
182         if ('\0' != *notif->plugin)
183                 printf ("%s: ", notif->plugin);
184
185         if ('\0' != *notif->plugin_instance)
186                 printf ("%s: ", notif->plugin_instance);
187
188         if ('\0' != *notif->type)
189                 printf ("%s: ", notif->type);
190
191         if ('\0' != *notif->type_instance)
192                 printf ("%s: ", notif->type_instance);
193
194         printf ("%s\n", notif->message);
195         return 0;
196 } /* static int my_notify (notification_t *) */
197
198 /*
199  * This function is called before shutting down collectd.
200  */
201 static int my_shutdown (void)
202 {
203         /* close sockets, free data structures, ... */
204         return 0;
205 } /* static int my_shutdown (void) */
206
207 /*
208  * This function is called after loading the plugin to register it with
209  * collectd.
210  */
211 void module_register (void)
212 {
213         plugin_register_log ("myplugin", my_log);
214         plugin_register_notification ("myplugin", my_notify);
215         plugin_register_data_set (&ds);
216         plugin_register_read ("myplugin", my_read);
217         plugin_register_init ("myplugin", my_init);
218         plugin_register_write ("myplugin", my_write);
219         plugin_register_shutdown ("myplugin", my_shutdown);
220     return;
221 } /* void module_register (void) */
222