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