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