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