2 * collectd - src/daemon/plugin.h
3 * Copyright (C) 2005-2014 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian Harl <sh at tokkee.org>
33 #include "configfile.h"
34 #include "utils/metadata/meta_data.h"
35 #include "utils_time.h"
40 #define DS_TYPE_COUNTER 0
41 #define DS_TYPE_GAUGE 1
42 #define DS_TYPE_DERIVE 2
43 #define DS_TYPE_ABSOLUTE 3
45 #define DS_TYPE_TO_STRING(t) \
46 (t == DS_TYPE_COUNTER) \
48 : (t == DS_TYPE_GAUGE) \
50 : (t == DS_TYPE_DERIVE) \
52 : (t == DS_TYPE_ABSOLUTE) ? "absolute" : "unknown"
70 #define NOTIF_MAX_MSG_LEN 256
72 #define NOTIF_FAILURE 1
73 #define NOTIF_WARNING 2
76 #define plugin_interval (plugin_get_ctx().interval)
84 char *plugin_instance;
88 typedef struct identifier_s identifier_t;
90 typedef unsigned long long counter_t;
91 typedef double gauge_t;
92 typedef int64_t derive_t;
93 typedef uint64_t absolute_t;
101 typedef union value_u value_t;
103 struct value_list_s {
108 char host[DATA_MAX_NAME_LEN];
109 char plugin[DATA_MAX_NAME_LEN];
110 char plugin_instance[DATA_MAX_NAME_LEN];
111 char type[DATA_MAX_NAME_LEN];
112 char type_instance[DATA_MAX_NAME_LEN];
115 typedef struct value_list_s value_list_t;
117 #define VALUE_LIST_INIT \
118 { .values = NULL, .meta = NULL }
120 struct data_source_s {
121 char name[DATA_MAX_NAME_LEN];
126 typedef struct data_source_s data_source_t;
129 char type[DATA_MAX_NAME_LEN];
133 typedef struct data_set_s data_set_t;
135 enum notification_meta_type_e {
138 NM_TYPE_UNSIGNED_INT,
143 typedef struct notification_meta_s {
144 char name[DATA_MAX_NAME_LEN];
145 enum notification_meta_type_e type;
147 const char *nm_string;
148 int64_t nm_signed_int;
149 uint64_t nm_unsigned_int;
153 struct notification_meta_s *next;
154 } notification_meta_t;
156 typedef struct notification_s {
159 char message[NOTIF_MAX_MSG_LEN];
160 char host[DATA_MAX_NAME_LEN];
161 char plugin[DATA_MAX_NAME_LEN];
162 char plugin_instance[DATA_MAX_NAME_LEN];
163 char type[DATA_MAX_NAME_LEN];
164 char type_instance[DATA_MAX_NAME_LEN];
165 notification_meta_t *meta;
170 void (*free_func)(void *);
172 typedef struct user_data_s user_data_t;
174 enum cache_event_type_e { CE_VALUE_NEW, CE_VALUE_UPDATE, CE_VALUE_EXPIRED };
176 typedef struct cache_event_s {
177 enum cache_event_type_e type;
178 const value_list_t *value_list;
179 const char *value_list_name;
183 struct plugin_ctx_s {
186 cdtime_t flush_interval;
187 cdtime_t flush_timeout;
189 typedef struct plugin_ctx_s plugin_ctx_t;
194 typedef int (*plugin_init_cb)(void);
195 typedef int (*plugin_read_cb)(user_data_t *);
196 typedef int (*plugin_write_cb)(const data_set_t *, const value_list_t *,
198 typedef int (*plugin_flush_cb)(cdtime_t timeout, const char *identifier,
200 /* "missing" callback. Returns less than zero on failure, zero if other
201 * callbacks should be called, greater than zero if no more callbacks should be
203 typedef int (*plugin_missing_cb)(const value_list_t *, user_data_t *);
204 /* "cache event" callback. CE_VALUE_NEW events are sent to all registered
205 * callbacks. Callback should check if it interested in further CE_VALUE_UPDATE
206 * and CE_VALUE_EXPIRED events for metric and set event->ret = 1 if so.
208 typedef int (*plugin_cache_event_cb)(cache_event_t *, user_data_t *);
209 typedef void (*plugin_log_cb)(int severity, const char *message, user_data_t *);
210 typedef int (*plugin_shutdown_cb)(void);
211 typedef int (*plugin_notification_cb)(const notification_t *, user_data_t *);
217 * Sets the current `plugindir'
220 * `dir' Path to the plugin directory
223 * If `dir' is NULL the compiled in default `PLUGINDIR' is used.
225 void plugin_set_dir(const char *dir);
232 * Searches the current `plugindir' (see `plugin_set_dir') for the plugin
233 * named $type and loads it. Afterwards the plugin's `module_register'
234 * function is called, which then calls `plugin_register' to register callback
238 * `name' Name of the plugin to load.
239 * `global' Make this plugins symbols available for other shared libraries.
242 * Returns zero upon success, a value greater than zero if no plugin was found
243 * and a value below zero if an error occurs.
246 * Re-loading an already loaded module is detected and zero is returned in
249 int plugin_load(const char *name, bool global);
251 int plugin_init_all(void);
252 void plugin_read_all(void);
253 int plugin_read_all_once(void);
254 int plugin_shutdown_all(void);
261 * Calls the write function of the given plugin with the provided data set and
262 * value list. It differs from `plugin_dispatch_values' in that it does not
263 * update the cache, does not do threshold checking, call the chain subsystem
264 * and so on. It looks up the requested plugin and invokes the function, end
268 * plugin Name of the plugin. If NULL, the value is sent to all registered
270 * ds Pointer to the data_set_t structure. If NULL, the data set is
271 * looked up according to the `type' member in the `vl' argument.
272 * vl The actual value to be processed. Must not be NULL.
275 * Returns zero upon success or non-zero if an error occurred. If `plugin' is
276 * NULL and more than one plugin is called, an error is only returned if *all*
280 * This is the function used by the `write' built-in target. May be used by
281 * other target plugins.
283 int plugin_write(const char *plugin, const data_set_t *ds,
284 const value_list_t *vl);
286 int plugin_flush(const char *plugin, cdtime_t timeout, const char *identifier);
289 * The `plugin_register_*' functions are used to make `config', `init',
290 * `read', `write' and `shutdown' functions known to the plugin
291 * infrastructure. Also, the data-formats are made public like this.
293 int plugin_register_config(const char *name,
294 int (*callback)(const char *key, const char *val),
295 const char **keys, int keys_num);
296 int plugin_register_complex_config(const char *type,
297 int (*callback)(oconfig_item_t *));
298 int plugin_register_init(const char *name, plugin_init_cb callback);
299 int plugin_register_read(const char *name, int (*callback)(void));
300 /* "user_data" will be freed automatically, unless
301 * "plugin_register_complex_read" returns an error (non-zero). */
302 int plugin_register_complex_read(const char *group, const char *name,
303 plugin_read_cb callback, cdtime_t interval,
304 user_data_t const *user_data);
305 int plugin_register_write(const char *name, plugin_write_cb callback,
306 user_data_t const *user_data);
307 int plugin_register_flush(const char *name, plugin_flush_cb callback,
308 user_data_t const *user_data);
309 int plugin_register_missing(const char *name, plugin_missing_cb callback,
310 user_data_t const *user_data);
311 int plugin_register_cache_event(const char *name,
312 plugin_cache_event_cb callback,
313 user_data_t const *ud);
314 int plugin_register_shutdown(const char *name, plugin_shutdown_cb callback);
315 int plugin_register_data_set(const data_set_t *ds);
316 int plugin_register_log(const char *name, plugin_log_cb callback,
317 user_data_t const *user_data);
318 int plugin_register_notification(const char *name,
319 plugin_notification_cb callback,
320 user_data_t const *user_data);
322 int plugin_unregister_config(const char *name);
323 int plugin_unregister_complex_config(const char *name);
324 int plugin_unregister_init(const char *name);
325 int plugin_unregister_read(const char *name);
326 int plugin_unregister_read_group(const char *group);
327 int plugin_unregister_write(const char *name);
328 int plugin_unregister_flush(const char *name);
329 int plugin_unregister_missing(const char *name);
330 int plugin_unregister_cache_event(const char *name);
331 int plugin_unregister_shutdown(const char *name);
332 int plugin_unregister_data_set(const char *name);
333 int plugin_unregister_log(const char *name);
334 int plugin_unregister_notification(const char *name);
338 * plugin_log_available_writers
341 * This function can be called to output a list of _all_ registered
342 * writers to the logfacility.
343 * Since some writers dynamically build their name it can be hard for
344 * the configuring person to know it. This function will fill this gap.
346 void plugin_log_available_writers(void);
350 * plugin_dispatch_values
353 * This function is called by reading processes with the values they've
354 * aquired. The function fetches the data-set definition (that has been
355 * registered using `plugin_register_data_set') and calls _all_ registered
359 * `vl' Value list of the values that have been read by a `read'
362 int plugin_dispatch_values(value_list_t const *vl);
366 * plugin_dispatch_multivalue
369 * plugin_dispatch_multivalue (vl, true, DS_TYPE_GAUGE,
375 * Takes a list of type instances and values and dispatches that in a batch,
376 * making sure that all values have the same time stamp. If "store_percentage"
377 * is set to true, the "type" is set to "percent" and a percentage is
378 * calculated and dispatched, rather than the absolute values. Values that are
379 * NaN are dispatched as NaN and will not influence the total.
381 * The variadic arguments is a list of type_instance / type pairs, that are
382 * interpreted as type "char const *" and type, encoded by their corresponding
385 * - "gauge_t" when "DS_TYPE_GAUGE"
386 * - "absolute_t" when "DS_TYPE_ABSOLUTE"
387 * - "derive_t" when "DS_TYPE_DERIVE"
388 * - "counter_t" when "DS_TYPE_COUNTER"
390 * The last argument must be
391 * a NULL pointer to signal end-of-list.
394 * The number of values it failed to dispatch (zero on success).
396 __attribute__((sentinel)) int plugin_dispatch_multivalue(value_list_t const *vl,
397 bool store_percentage,
398 int store_type, ...);
400 int plugin_dispatch_missing(const value_list_t *vl);
401 void plugin_dispatch_cache_event(enum cache_event_type_e event_type,
402 unsigned long callbacks_mask, const char *name,
403 const value_list_t *vl);
405 int plugin_dispatch_notification(const notification_t *notif);
407 void plugin_log(int level, const char *format, ...)
408 __attribute__((format(printf, 2, 3)));
410 /* These functions return the parsed severity or less than zero on failure. */
411 int parse_log_severity(const char *severity);
412 int parse_notif_severity(const char *severity);
414 #define ERROR(...) plugin_log(LOG_ERR, __VA_ARGS__)
415 #define WARNING(...) plugin_log(LOG_WARNING, __VA_ARGS__)
416 #define NOTICE(...) plugin_log(LOG_NOTICE, __VA_ARGS__)
417 #define INFO(...) plugin_log(LOG_INFO, __VA_ARGS__)
419 #define DEBUG(...) plugin_log(LOG_DEBUG, __VA_ARGS__)
420 #else /* COLLECT_DEBUG */
421 #define DEBUG(...) /* noop */
422 #endif /* ! COLLECT_DEBUG */
424 /* This will log messages, prefixed by plugin name */
425 void daemon_log(int level, const char *format, ...)
426 __attribute__((format(printf, 2, 3)));
428 #define P_ERROR(...) daemon_log(LOG_ERR, __VA_ARGS__)
429 #define P_WARNING(...) daemon_log(LOG_WARNING, __VA_ARGS__)
430 #define P_NOTICE(...) daemon_log(LOG_NOTICE, __VA_ARGS__)
431 #define P_INFO(...) daemon_log(LOG_INFO, __VA_ARGS__)
433 const data_set_t *plugin_get_ds(const char *name);
435 int plugin_notification_meta_add_string(notification_t *n, const char *name,
437 int plugin_notification_meta_add_signed_int(notification_t *n, const char *name,
439 int plugin_notification_meta_add_unsigned_int(notification_t *n,
440 const char *name, uint64_t value);
441 int plugin_notification_meta_add_double(notification_t *n, const char *name,
443 int plugin_notification_meta_add_boolean(notification_t *n, const char *name,
446 int plugin_notification_meta_copy(notification_t *dst,
447 const notification_t *src);
449 int plugin_notification_meta_free(notification_meta_t *n);
452 * Plugin context management.
455 void plugin_init_ctx(void);
457 plugin_ctx_t plugin_get_ctx(void);
458 plugin_ctx_t plugin_set_ctx(plugin_ctx_t ctx);
462 * plugin_get_interval
465 * This function returns the current value of the plugin's interval. The
466 * return value will be strictly greater than zero in all cases. If
467 * everything else fails, it will fall back to 10 seconds.
469 cdtime_t plugin_get_interval(void);
472 * Context-aware thread management.
475 int plugin_thread_create(pthread_t *thread, const pthread_attr_t *attr,
476 void *(*start_routine)(void *), void *arg,
480 * Plugins need to implement this
483 void module_register(void);
485 #endif /* PLUGIN_H */