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