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