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