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