From: Florian Forster Date: Mon, 13 Jan 2014 14:57:34 +0000 (+0100) Subject: src/plugin.[ch]: Implement plugin_dispatch_multivalue(). X-Git-Tag: collectd-5.5.0~316^2~9^2~4 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=8ea02ba4a8c5c4c402c26392bb3b30c39d53ecc6;p=collectd.git src/plugin.[ch]: Implement plugin_dispatch_multivalue(). --- diff --git a/src/plugin.c b/src/plugin.c index 6c7aa057..5f1ca559 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -1,6 +1,6 @@ /** * collectd - src/plugin.c - * Copyright (C) 2005-2013 Florian octo Forster + * Copyright (C) 2005-2014 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -2089,6 +2089,65 @@ int plugin_dispatch_values (value_list_t const *vl) return (0); } +__attribute__((sentinel)) +int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */ + _Bool store_percentage, ...) +{ + value_list_t *vl; + int failed = 0; + gauge_t sum = 0.0; + va_list ap; + + assert (template->values_len == 1); + + va_start (ap, store_percentage); + while (42) + { + char const *name; + gauge_t value; + + name = va_arg (ap, char const *); + if (name == NULL) + break; + + value = va_arg (ap, gauge_t); + if (!isnan (value)) + sum += value; + } + va_end (ap); + + vl = plugin_value_list_clone (template); + /* plugin_value_list_clone makes sure vl->time is set to non-zero. */ + if (store_percentage) + sstrncpy (vl->type, "percent", sizeof (vl->type)); + + va_start (ap, store_percentage); + while (42) + { + char const *name; + int status; + + /* Set the type instance. */ + name = va_arg (ap, char const *); + if (name == NULL) + break; + sstrncpy (vl->type_instance, name, sizeof (vl->type_instance)); + + /* Set the value. */ + vl->values[0].gauge = va_arg (ap, gauge_t); + if (store_percentage) + vl->values[0].gauge *= 100.0 / sum; + + status = plugin_write_enqueue (vl); + if (status != 0) + failed++; + } + va_end (ap); + + plugin_value_list_free (vl); + return (failed); +} /* }}} int plugin_dispatch_multivalue */ + int plugin_dispatch_notification (const notification_t *notif) { llentry_t *le; diff --git a/src/plugin.h b/src/plugin.h index 8f0c6d86..924c842d 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -2,7 +2,7 @@ #define PLUGIN_H /** * collectd - src/plugin.h - * Copyright (C) 2005-2011 Florian octo Forster + * Copyright (C) 2005-2014 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -328,6 +328,35 @@ int plugin_unregister_notification (const char *name); * function. */ int plugin_dispatch_values (value_list_t const *vl); + +/* + * NAME + * plugin_dispatch_multivalue + * + * SYNOPSIS + * plugin_dispatch_multivalue (vl, 1, + * "free", 42.0, + * "used", 58.0, + * NULL); + * + * DESCRIPTION + * Takes a list of type instances and values and dispatches that in a batch, + * making sure that all values have the same time stamp. If "store_percentage" + * is set to true, the "type" is set to "percent" and a percentage is + * calculated and dispatched, rather than the absolute values. Values that are + * NaN are dispatched as NaN and will not influence the total. + * + * The variadic arguments is a list of type_instance / gauge pairs, that are + * interpreted as type "char const *" and "gauge_t". The last argument must be + * a NULL pointer to signal end-of-list. + * + * RETURNS + * The number of values it failed to dispatch (zero on success). + */ +__attribute__((sentinel)) +int plugin_dispatch_multivalue (value_list_t const *vl, + _Bool store_percentage, ...); + int plugin_dispatch_missing (const value_list_t *vl); int plugin_dispatch_notification (const notification_t *notif);