Fix compile time issues
[collectd.git] / src / daemon / plugin.h
1 /**
2  * collectd - src/daemon/plugin.h
3  * Copyright (C) 2005-2014  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  *   Sebastian Harl <sh at tokkee.org>
26  **/
27
28 #ifndef PLUGIN_H
29 #define PLUGIN_H
30
31 #include "collectd.h"
32
33 #include "configfile.h"
34 #include "utils/metadata/meta_data.h"
35 #include "utils_time.h"
36
37 #include <inttypes.h>
38 #include <pthread.h>
39
40 #define DS_TYPE_COUNTER 0
41 #define DS_TYPE_GAUGE 1
42 #define DS_TYPE_DERIVE 2
43 #define DS_TYPE_ABSOLUTE 3
44
45 #define DS_TYPE_TO_STRING(t)                                                   \
46   (t == DS_TYPE_COUNTER)                                                       \
47       ? "counter"                                                              \
48       : (t == DS_TYPE_GAUGE)                                                   \
49             ? "gauge"                                                          \
50             : (t == DS_TYPE_DERIVE)                                            \
51                   ? "derive"                                                   \
52                   : (t == DS_TYPE_ABSOLUTE) ? "absolute" : "unknown"
53
54 #ifndef LOG_ERR
55 #define LOG_ERR 3
56 #endif
57 #ifndef LOG_WARNING
58 #define LOG_WARNING 4
59 #endif
60 #ifndef LOG_NOTICE
61 #define LOG_NOTICE 5
62 #endif
63 #ifndef LOG_INFO
64 #define LOG_INFO 6
65 #endif
66 #ifndef LOG_DEBUG
67 #define LOG_DEBUG 7
68 #endif
69
70 #define NOTIF_MAX_MSG_LEN 256
71
72 #define NOTIF_FAILURE 1
73 #define NOTIF_WARNING 2
74 #define NOTIF_OKAY 4
75
76 #define plugin_interval (plugin_get_ctx().interval)
77
78 /*
79  * Public data types
80  */
81 struct identifier_s {
82   char *host;
83   char *plugin;
84   char *plugin_instance;
85   char *type;
86   char *type_instance;
87 };
88 typedef struct identifier_s identifier_t;
89
90 typedef unsigned long long counter_t;
91 typedef double gauge_t;
92 typedef int64_t derive_t;
93 typedef uint64_t absolute_t;
94
95 union value_u {
96   counter_t counter;
97   gauge_t gauge;
98   derive_t derive;
99   absolute_t absolute;
100 };
101 typedef union value_u value_t;
102
103 struct value_list_s {
104   value_t *values;
105   size_t values_len;
106   cdtime_t time;
107   cdtime_t interval;
108   char host[DATA_MAX_NAME_LEN];
109   char plugin[DATA_MAX_NAME_LEN];
110   char plugin_instance[DATA_MAX_NAME_LEN];
111   char type[DATA_MAX_NAME_LEN];
112   char type_instance[DATA_MAX_NAME_LEN];
113   meta_data_t *meta;
114 };
115 typedef struct value_list_s value_list_t;
116
117 #define VALUE_LIST_INIT                                                        \
118   { .values = NULL, .meta = NULL }
119
120 struct data_source_s {
121   char name[DATA_MAX_NAME_LEN];
122   int type;
123   double min;
124   double max;
125 };
126 typedef struct data_source_s data_source_t;
127
128 struct data_set_s {
129   char type[DATA_MAX_NAME_LEN];
130   size_t ds_num;
131   data_source_t *ds;
132 };
133 typedef struct data_set_s data_set_t;
134
135 enum notification_meta_type_e {
136   NM_TYPE_STRING,
137   NM_TYPE_SIGNED_INT,
138   NM_TYPE_UNSIGNED_INT,
139   NM_TYPE_DOUBLE,
140   NM_TYPE_BOOLEAN
141 };
142
143 typedef struct notification_meta_s {
144   char name[DATA_MAX_NAME_LEN];
145   enum notification_meta_type_e type;
146   union {
147     const char *nm_string;
148     int64_t nm_signed_int;
149     uint64_t nm_unsigned_int;
150     double nm_double;
151     bool nm_boolean;
152   } nm_value;
153   struct notification_meta_s *next;
154 } notification_meta_t;
155
156 typedef struct notification_s {
157   int severity;
158   cdtime_t time;
159   char message[NOTIF_MAX_MSG_LEN];
160   char host[DATA_MAX_NAME_LEN];
161   char plugin[DATA_MAX_NAME_LEN];
162   char plugin_instance[DATA_MAX_NAME_LEN];
163   char type[DATA_MAX_NAME_LEN];
164   char type_instance[DATA_MAX_NAME_LEN];
165   notification_meta_t *meta;
166 } notification_t;
167
168 struct user_data_s {
169   void *data;
170   void (*free_func)(void *);
171 };
172 typedef struct user_data_s user_data_t;
173
174 struct plugin_ctx_s {
175   char *name;
176   cdtime_t interval;
177   cdtime_t flush_interval;
178   cdtime_t flush_timeout;
179 };
180 typedef struct plugin_ctx_s plugin_ctx_t;
181
182 /*
183  * Callback types
184  */
185 typedef int (*plugin_init_cb)(void);
186 typedef int (*plugin_read_cb)(user_data_t *);
187 typedef int (*plugin_write_cb)(const data_set_t *, const value_list_t *,
188                                user_data_t *);
189 typedef int (*plugin_flush_cb)(cdtime_t timeout, const char *identifier,
190                                user_data_t *);
191 /* "missing" callback. Returns less than zero on failure, zero if other
192  * callbacks should be called, greater than zero if no more callbacks should be
193  * called. */
194 typedef int (*plugin_missing_cb)(const value_list_t *, user_data_t *);
195 typedef void (*plugin_log_cb)(int severity, const char *message, user_data_t *);
196 typedef int (*plugin_shutdown_cb)(void);
197 typedef int (*plugin_notification_cb)(const notification_t *, user_data_t *);
198 /*
199  * NAME
200  *  plugin_set_dir
201  *
202  * DESCRIPTION
203  *  Sets the current `plugindir'
204  *
205  * ARGUMENTS
206  *  `dir'       Path to the plugin directory
207  *
208  * NOTES
209  *  If `dir' is NULL the compiled in default `PLUGINDIR' is used.
210  */
211 void plugin_set_dir(const char *dir);
212
213 /*
214  * NAME
215  *  plugin_load
216  *
217  * DESCRIPTION
218  *  Searches the current `plugindir' (see `plugin_set_dir') for the plugin
219  *  named $type and loads it. Afterwards the plugin's `module_register'
220  *  function is called, which then calls `plugin_register' to register callback
221  *  functions.
222  *
223  * ARGUMENTS
224  *  `name'      Name of the plugin to load.
225  *  `global'    Make this plugins symbols available for other shared libraries.
226  *
227  * RETURN VALUE
228  *  Returns zero upon success, a value greater than zero if no plugin was found
229  *  and a value below zero if an error occurs.
230  *
231  * NOTES
232  *  Re-loading an already loaded module is detected and zero is returned in
233  *  this case.
234  */
235 int plugin_load(const char *name, bool global);
236
237 int plugin_init_all(void);
238 void plugin_read_all(void);
239 int plugin_read_all_once(void);
240 int plugin_shutdown_all(void);
241
242 /*
243  * NAME
244  *  plugin_write
245  *
246  * DESCRIPTION
247  *  Calls the write function of the given plugin with the provided data set and
248  *  value list. It differs from `plugin_dispatch_values' in that it does not
249  *  update the cache, does not do threshold checking, call the chain subsystem
250  *  and so on. It looks up the requested plugin and invokes the function, end
251  *  of story.
252  *
253  * ARGUMENTS
254  *  plugin     Name of the plugin. If NULL, the value is sent to all registered
255  *             write functions.
256  *  ds         Pointer to the data_set_t structure. If NULL, the data set is
257  *             looked up according to the `type' member in the `vl' argument.
258  *  vl         The actual value to be processed. Must not be NULL.
259  *
260  * RETURN VALUE
261  *  Returns zero upon success or non-zero if an error occurred. If `plugin' is
262  *  NULL and more than one plugin is called, an error is only returned if *all*
263  *  plugins fail.
264  *
265  * NOTES
266  *  This is the function used by the `write' built-in target. May be used by
267  *  other target plugins.
268  */
269 int plugin_write(const char *plugin, const data_set_t *ds,
270                  const value_list_t *vl);
271
272 int plugin_flush(const char *plugin, cdtime_t timeout, const char *identifier);
273
274 /*
275  * The `plugin_register_*' functions are used to make `config', `init',
276  * `read', `write' and `shutdown' functions known to the plugin
277  * infrastructure. Also, the data-formats are made public like this.
278  */
279 int plugin_register_config(const char *name,
280                            int (*callback)(const char *key, const char *val),
281                            const char **keys, int keys_num);
282 int plugin_register_complex_config(const char *type,
283                                    int (*callback)(oconfig_item_t *));
284 int plugin_register_init(const char *name, plugin_init_cb callback);
285 int plugin_register_read(const char *name, int (*callback)(void));
286 /* "user_data" will be freed automatically, unless
287  * "plugin_register_complex_read" returns an error (non-zero). */
288 int plugin_register_complex_read(const char *group, const char *name,
289                                  plugin_read_cb callback, cdtime_t interval,
290                                  user_data_t const *user_data);
291 int plugin_register_write(const char *name, plugin_write_cb callback,
292                           user_data_t const *user_data);
293 int plugin_register_flush(const char *name, plugin_flush_cb callback,
294                           user_data_t const *user_data);
295 int plugin_register_missing(const char *name, plugin_missing_cb callback,
296                             user_data_t const *user_data);
297 int plugin_register_shutdown(const char *name, plugin_shutdown_cb callback);
298 int plugin_register_data_set(const data_set_t *ds);
299 int plugin_register_log(const char *name, plugin_log_cb callback,
300                         user_data_t const *user_data);
301 int plugin_register_notification(const char *name,
302                                  plugin_notification_cb callback,
303                                  user_data_t const *user_data);
304
305 int plugin_unregister_config(const char *name);
306 int plugin_unregister_complex_config(const char *name);
307 int plugin_unregister_init(const char *name);
308 int plugin_unregister_read(const char *name);
309 int plugin_unregister_read_group(const char *group);
310 int plugin_unregister_write(const char *name);
311 int plugin_unregister_flush(const char *name);
312 int plugin_unregister_missing(const char *name);
313 int plugin_unregister_shutdown(const char *name);
314 int plugin_unregister_data_set(const char *name);
315 int plugin_unregister_log(const char *name);
316 int plugin_unregister_notification(const char *name);
317
318 /*
319  * NAME
320  *  plugin_log_available_writers
321  *
322  * DESCRIPTION
323  *  This function can be called to output a list of _all_ registered
324  *  writers to the logfacility.
325  *  Since some writers dynamically build their name it can be hard for
326  *  the configuring person to know it. This function will fill this gap.
327  */
328 void plugin_log_available_writers(void);
329
330 /*
331  * NAME
332  *  plugin_dispatch_values
333  *
334  * DESCRIPTION
335  *  This function is called by reading processes with the values they've
336  *  aquired. The function fetches the data-set definition (that has been
337  *  registered using `plugin_register_data_set') and calls _all_ registered
338  *  write-functions.
339  *
340  * ARGUMENTS
341  *  `vl'        Value list of the values that have been read by a `read'
342  *              function.
343  */
344 int plugin_dispatch_values(value_list_t const *vl);
345
346 /*
347  * NAME
348  *  plugin_dispatch_multivalue
349  *
350  * SYNOPSIS
351  *  plugin_dispatch_multivalue (vl, true, DS_TYPE_GAUGE,
352  *                              "free", 42.0,
353  *                              "used", 58.0,
354  *                              NULL);
355  *
356  * DESCRIPTION
357  *  Takes a list of type instances and values and dispatches that in a batch,
358  *  making sure that all values have the same time stamp. If "store_percentage"
359  *  is set to true, the "type" is set to "percent" and a percentage is
360  *  calculated and dispatched, rather than the absolute values. Values that are
361  *  NaN are dispatched as NaN and will not influence the total.
362  *
363  *  The variadic arguments is a list of type_instance / type pairs, that are
364  *  interpreted as type "char const *" and type, encoded by their corresponding
365  *  "store_type":
366  *
367  *     - "gauge_t"    when "DS_TYPE_GAUGE"
368  *     - "absolute_t" when "DS_TYPE_ABSOLUTE"
369  *     - "derive_t"   when "DS_TYPE_DERIVE"
370  *     - "counter_t"  when "DS_TYPE_COUNTER"
371  *
372  *  The last argument must be
373  *  a NULL pointer to signal end-of-list.
374  *
375  * RETURNS
376  *  The number of values it failed to dispatch (zero on success).
377  */
378 __attribute__((sentinel)) int plugin_dispatch_multivalue(value_list_t const *vl,
379                                                          bool store_percentage,
380                                                          int store_type, ...);
381
382 int plugin_dispatch_missing(const value_list_t *vl);
383
384 int plugin_dispatch_notification(const notification_t *notif);
385
386 void plugin_log(int level, const char *format, ...)
387     __attribute__((format(printf, 2, 3)));
388
389 /* These functions return the parsed severity or less than zero on failure. */
390 int parse_log_severity(const char *severity);
391 int parse_notif_severity(const char *severity);
392
393 #define ERROR(...) plugin_log(LOG_ERR, __VA_ARGS__)
394 #define WARNING(...) plugin_log(LOG_WARNING, __VA_ARGS__)
395 #define NOTICE(...) plugin_log(LOG_NOTICE, __VA_ARGS__)
396 #define INFO(...) plugin_log(LOG_INFO, __VA_ARGS__)
397 #if COLLECT_DEBUG
398 #define DEBUG(...) plugin_log(LOG_DEBUG, __VA_ARGS__)
399 #else              /* COLLECT_DEBUG */
400 #define DEBUG(...) /* noop */
401 #endif             /* ! COLLECT_DEBUG */
402
403 /* This will log messages, prefixed by plugin name */
404 void daemon_log(int level, const char *format, ...)
405     __attribute__((format(printf, 2, 3)));
406
407 #define P_ERROR(...) daemon_log(LOG_ERR, __VA_ARGS__)
408 #define P_WARNING(...) daemon_log(LOG_WARNING, __VA_ARGS__)
409 #define P_NOTICE(...) daemon_log(LOG_NOTICE, __VA_ARGS__)
410 #define P_INFO(...) daemon_log(LOG_INFO, __VA_ARGS__)
411
412 const data_set_t *plugin_get_ds(const char *name);
413
414 int plugin_notification_meta_add_string(notification_t *n, const char *name,
415                                         const char *value);
416 int plugin_notification_meta_add_signed_int(notification_t *n, const char *name,
417                                             int64_t value);
418 int plugin_notification_meta_add_unsigned_int(notification_t *n,
419                                               const char *name, uint64_t value);
420 int plugin_notification_meta_add_double(notification_t *n, const char *name,
421                                         double value);
422 int plugin_notification_meta_add_boolean(notification_t *n, const char *name,
423                                          bool value);
424
425 int plugin_notification_meta_copy(notification_t *dst,
426                                   const notification_t *src);
427
428 int plugin_notification_meta_free(notification_meta_t *n);
429
430 /*
431  * Plugin context management.
432  */
433
434 void plugin_init_ctx(void);
435
436 plugin_ctx_t plugin_get_ctx(void);
437 plugin_ctx_t plugin_set_ctx(plugin_ctx_t ctx);
438
439 /*
440  * NAME
441  *  plugin_get_interval
442  *
443  * DESCRIPTION
444  *  This function returns the current value of the plugin's interval. The
445  *  return value will be strictly greater than zero in all cases. If
446  *  everything else fails, it will fall back to 10 seconds.
447  */
448 cdtime_t plugin_get_interval(void);
449
450 /*
451  * Context-aware thread management.
452  */
453
454 int plugin_thread_create(pthread_t *thread, const pthread_attr_t *attr,
455                          void *(*start_routine)(void *), void *arg,
456                          char const *name);
457
458 /*
459  * Plugins need to implement this
460  */
461
462 void module_register(void);
463
464 #endif /* PLUGIN_H */