2 * collectd - src/target_scale.c
3 * Copyright (C) 2008-2009 Florian 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 Forster <octo at collectd.org>
29 #include "filter_chain.h"
31 #include "utils_cache.h"
39 size_t data_sources_num;
41 typedef struct ts_data_s ts_data_t;
43 static int ts_invoke_counter (const data_set_t *ds, value_list_t *vl, /* {{{ */
44 ts_data_t *data, int dsrc_index)
46 uint64_t curr_counter;
50 /* Required meta data */
51 uint64_t prev_counter;
52 char key_prev_counter[128];
54 char key_int_counter[128];
56 char key_int_fraction[128];
58 curr_counter = (uint64_t) vl->values[dsrc_index].counter;
60 ssnprintf (key_prev_counter, sizeof (key_prev_counter),
61 "target_scale[%p,%i]:prev_counter",
62 (void *) data, dsrc_index);
63 ssnprintf (key_int_counter, sizeof (key_int_counter),
64 "target_scale[%p,%i]:int_counter",
65 (void *) data, dsrc_index);
66 ssnprintf (key_int_fraction, sizeof (key_int_fraction),
67 "target_scale[%p,%i]:int_fraction",
68 (void *) data, dsrc_index);
70 prev_counter = curr_counter;
74 /* Query the meta data */
77 status = uc_meta_data_get_unsigned_int (vl, key_prev_counter,
82 status = uc_meta_data_get_unsigned_int (vl, key_int_counter, &int_counter);
86 status = uc_meta_data_get_double (vl, key_int_fraction, &int_fraction);
95 diff = (uint64_t) counter_diff (prev_counter, curr_counter);
96 rate = ((double) diff) / CDTIME_T_TO_DOUBLE (vl->interval);
98 /* Modify the rate. */
99 if (!isnan (data->factor))
100 rate *= data->factor;
101 if (!isnan (data->offset))
102 rate += data->offset;
104 /* Calculate the internal counter. */
105 int_fraction += (rate * CDTIME_T_TO_DOUBLE (vl->interval));
106 diff = (uint64_t) int_fraction;
107 int_fraction -= ((double) diff);
110 assert (int_fraction >= 0.0);
111 assert (int_fraction < 1.0);
113 DEBUG ("Target `scale': ts_invoke_counter: %"PRIu64" -> %g -> %"PRIu64
115 curr_counter, rate, int_counter, int_fraction);
117 else /* (failure != 0) */
123 vl->values[dsrc_index].counter = (counter_t) int_counter;
125 /* Update to the new counter value */
126 uc_meta_data_add_unsigned_int (vl, key_prev_counter, curr_counter);
127 uc_meta_data_add_unsigned_int (vl, key_int_counter, int_counter);
128 uc_meta_data_add_double (vl, key_int_fraction, int_fraction);
132 } /* }}} int ts_invoke_counter */
134 static int ts_invoke_gauge (const data_set_t *ds, value_list_t *vl, /* {{{ */
135 ts_data_t *data, int dsrc_index)
137 if (!isnan (data->factor))
138 vl->values[dsrc_index].gauge *= data->factor;
139 if (!isnan (data->offset))
140 vl->values[dsrc_index].gauge += data->offset;
143 } /* }}} int ts_invoke_gauge */
145 static int ts_invoke_derive (const data_set_t *ds, value_list_t *vl, /* {{{ */
146 ts_data_t *data, int dsrc_index)
152 /* Required meta data */
154 char key_prev_derive[128];
156 char key_int_derive[128];
158 char key_int_fraction[128];
160 curr_derive = (int64_t) vl->values[dsrc_index].derive;
162 ssnprintf (key_prev_derive, sizeof (key_prev_derive),
163 "target_scale[%p,%i]:prev_derive",
164 (void *) data, dsrc_index);
165 ssnprintf (key_int_derive, sizeof (key_int_derive),
166 "target_scale[%p,%i]:int_derive",
167 (void *) data, dsrc_index);
168 ssnprintf (key_int_fraction, sizeof (key_int_fraction),
169 "target_scale[%p,%i]:int_fraction",
170 (void *) data, dsrc_index);
172 prev_derive = curr_derive;
176 /* Query the meta data */
179 status = uc_meta_data_get_signed_int (vl, key_prev_derive,
184 status = uc_meta_data_get_signed_int (vl, key_int_derive, &int_derive);
188 status = uc_meta_data_get_double (vl, key_int_fraction, &int_fraction);
197 /* Calcualte the rate */
198 difference = curr_derive - prev_derive;
199 rate = ((double) difference) / CDTIME_T_TO_DOUBLE (vl->interval);
201 /* Modify the rate. */
202 if (!isnan (data->factor))
203 rate *= data->factor;
204 if (!isnan (data->offset))
205 rate += data->offset;
207 /* Calculate the internal derive. */
208 int_fraction += (rate * CDTIME_T_TO_DOUBLE (vl->interval));
209 if (int_fraction < 0.0) /* handle negative integer rounding correctly */
210 difference = ((int64_t) int_fraction) - 1;
212 difference = (int64_t) int_fraction;
213 int_fraction -= ((double) difference);
214 int_derive += difference;
216 assert (int_fraction >= 0.0);
217 assert (int_fraction < 1.0);
219 DEBUG ("Target `scale': ts_invoke_derive: %"PRIu64" -> %g -> %"PRIu64
221 curr_derive, rate, int_derive, int_fraction);
223 else /* (failure != 0) */
229 vl->values[dsrc_index].derive = (derive_t) int_derive;
231 /* Update to the new derive value */
232 uc_meta_data_add_signed_int (vl, key_prev_derive, curr_derive);
233 uc_meta_data_add_signed_int (vl, key_int_derive, int_derive);
234 uc_meta_data_add_double (vl, key_int_fraction, int_fraction);
237 } /* }}} int ts_invoke_derive */
239 static int ts_invoke_absolute (const data_set_t *ds, value_list_t *vl, /* {{{ */
240 ts_data_t *data, int dsrc_index)
242 uint64_t curr_absolute;
246 /* Required meta data */
248 char key_int_fraction[128];
250 curr_absolute = (uint64_t) vl->values[dsrc_index].absolute;
252 ssnprintf (key_int_fraction, sizeof (key_int_fraction),
253 "target_scale[%p,%i]:int_fraction",
254 (void *) data, dsrc_index);
258 /* Query the meta data */
259 status = uc_meta_data_get_double (vl, key_int_fraction, &int_fraction);
263 rate = ((double) curr_absolute) / CDTIME_T_TO_DOUBLE (vl->interval);
265 /* Modify the rate. */
266 if (!isnan (data->factor))
267 rate *= data->factor;
268 if (!isnan (data->offset))
269 rate += data->offset;
271 /* Calculate the new absolute. */
272 int_fraction += (rate * CDTIME_T_TO_DOUBLE (vl->interval));
273 curr_absolute = (uint64_t) int_fraction;
274 int_fraction -= ((double) curr_absolute);
276 vl->values[dsrc_index].absolute = (absolute_t) curr_absolute;
278 /* Update to the new absolute value */
279 uc_meta_data_add_double (vl, key_int_fraction, int_fraction);
282 } /* }}} int ts_invoke_absolute */
284 static int ts_config_set_double (double *ret, oconfig_item_t *ci) /* {{{ */
286 if ((ci->values_num != 1)
287 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
289 WARNING ("scale target: The `%s' config option needs "
290 "exactly one numeric argument.", ci->key);
294 *ret = ci->values[0].value.number;
295 DEBUG ("ts_config_set_double: *ret = %g", *ret);
298 } /* }}} int ts_config_set_double */
300 static int ts_config_add_data_source(ts_data_t *data, /* {{{ */
303 size_t new_data_sources_num;
307 /* Check number of arbuments. */
308 if (ci->values_num < 1)
310 ERROR ("`value' match: `%s' needs at least one argument.",
315 /* Check type of arguments */
316 for (i = 0; i < ci->values_num; i++)
318 if (ci->values[i].type == OCONFIG_TYPE_STRING)
321 ERROR ("`value' match: `%s' accepts only string arguments "
322 "(argument %i is a %s).",
324 (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
325 ? "truth value" : "number");
329 /* Allocate space for the char pointers */
330 new_data_sources_num = data->data_sources_num + ((size_t) ci->values_num);
331 temp = (char **) realloc (data->data_sources,
332 new_data_sources_num * sizeof (char *));
335 ERROR ("`value' match: realloc failed.");
338 data->data_sources = temp;
340 /* Copy the strings, allocating memory as needed. */
341 for (i = 0; i < ci->values_num; i++)
345 /* If we get here, there better be memory for us to write to. */
346 assert (data->data_sources_num < new_data_sources_num);
348 j = data->data_sources_num;
349 data->data_sources[j] = sstrdup (ci->values[i].value.string);
350 if (data->data_sources[j] == NULL)
352 ERROR ("`value' match: sstrdup failed.");
355 data->data_sources_num++;
359 } /* }}} int ts_config_add_data_source */
361 static int ts_destroy (void **user_data) /* {{{ */
365 if (user_data == NULL)
368 data = (ts_data_t *) *user_data;
370 if ((data != NULL) && (data->data_sources != NULL))
373 for (i = 0; i < data->data_sources_num; i++)
374 sfree (data->data_sources[i]);
375 sfree (data->data_sources);
382 } /* }}} int ts_destroy */
384 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
390 data = malloc (sizeof (*data));
393 ERROR ("ts_create: malloc failed.");
396 memset (data, 0, sizeof (*data));
402 for (i = 0; i < ci->children_num; i++)
404 oconfig_item_t *child = ci->children + i;
406 if (strcasecmp ("Factor", child->key) == 0)
407 status = ts_config_set_double (&data->factor, child);
408 else if (strcasecmp ("Offset", child->key) == 0)
409 status = ts_config_set_double (&data->offset, child);
410 else if (strcasecmp ("DataSource", child->key) == 0)
411 status = ts_config_add_data_source(data, child);
414 ERROR ("Target `scale': The `%s' configuration option is not understood "
415 "and will be ignored.", child->key);
423 /* Additional sanity-checking */
426 if (isnan (data->factor) && isnan (data->offset))
428 ERROR ("Target `scale': You need to at least set either the `Factor' "
429 "or `Offset' option!");
438 ts_destroy ((void *) &data);
444 } /* }}} int ts_create */
446 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
447 notification_meta_t __attribute__((unused)) **meta, void **user_data)
452 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
458 ERROR ("Target `scale': Invoke: `data' is NULL.");
462 for (i = 0; i < ds->ds_num; i++)
464 /* If we've got a list of data sources, is it in the list? */
465 if (data->data_sources) {
467 for (j = 0; j < data->data_sources_num; j++)
468 if (strcasecmp(ds->ds[i].name, data->data_sources[j]) == 0)
471 /* No match, ignore */
472 if (j >= data->data_sources_num)
476 if (ds->ds[i].type == DS_TYPE_COUNTER)
477 ts_invoke_counter (ds, vl, data, i);
478 else if (ds->ds[i].type == DS_TYPE_GAUGE)
479 ts_invoke_gauge (ds, vl, data, i);
480 else if (ds->ds[i].type == DS_TYPE_DERIVE)
481 ts_invoke_derive (ds, vl, data, i);
482 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
483 ts_invoke_absolute (ds, vl, data, i);
485 ERROR ("Target `scale': Ignoring unknown data source type %i",
489 return (FC_TARGET_CONTINUE);
490 } /* }}} int ts_invoke */
492 void module_register (void)
496 memset (&tproc, 0, sizeof (tproc));
497 tproc.create = ts_create;
498 tproc.destroy = ts_destroy;
499 tproc.invoke = ts_invoke;
500 fc_register_target ("scale", tproc);
501 } /* module_register */
503 /* vim: set sw=2 ts=2 tw=78 fdm=marker : */