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