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