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 /* Calcualte the rate */
96 if (prev_counter > curr_counter) /* => counter overflow */
98 if (prev_counter <= 4294967295UL) /* 32 bit overflow */
99 difference = (4294967295UL - prev_counter) + curr_counter;
100 else /* 64 bit overflow */
101 difference = (18446744073709551615ULL - prev_counter) + curr_counter;
103 else /* no overflow */
105 difference = curr_counter - prev_counter;
107 rate = ((double) difference) / CDTIME_T_TO_DOUBLE (vl->interval);
109 /* Modify the rate. */
110 if (!isnan (data->factor))
111 rate *= data->factor;
112 if (!isnan (data->offset))
113 rate += data->offset;
115 /* Calculate the internal counter. */
116 int_fraction += (rate * CDTIME_T_TO_DOUBLE (vl->interval));
117 difference = (uint64_t) int_fraction;
118 int_fraction -= ((double) difference);
119 int_counter += difference;
121 assert (int_fraction >= 0.0);
122 assert (int_fraction < 1.0);
124 DEBUG ("Target `scale': ts_invoke_counter: %"PRIu64" -> %g -> %"PRIu64
126 curr_counter, rate, int_counter, int_fraction);
128 else /* (failure != 0) */
134 vl->values[dsrc_index].counter = (counter_t) int_counter;
136 /* Update to the new counter value */
137 uc_meta_data_add_unsigned_int (vl, key_prev_counter, curr_counter);
138 uc_meta_data_add_unsigned_int (vl, key_int_counter, int_counter);
139 uc_meta_data_add_double (vl, key_int_fraction, int_fraction);
143 } /* }}} int ts_invoke_counter */
145 static int ts_invoke_gauge (const data_set_t *ds, value_list_t *vl, /* {{{ */
146 ts_data_t *data, int dsrc_index)
148 if (!isnan (data->factor))
149 vl->values[dsrc_index].gauge *= data->factor;
150 if (!isnan (data->offset))
151 vl->values[dsrc_index].gauge += data->offset;
154 } /* }}} int ts_invoke_gauge */
156 static int ts_invoke_derive (const data_set_t *ds, value_list_t *vl, /* {{{ */
157 ts_data_t *data, int dsrc_index)
163 /* Required meta data */
165 char key_prev_derive[128];
167 char key_int_derive[128];
169 char key_int_fraction[128];
171 curr_derive = (int64_t) vl->values[dsrc_index].derive;
173 ssnprintf (key_prev_derive, sizeof (key_prev_derive),
174 "target_scale[%p,%i]:prev_derive",
175 (void *) data, dsrc_index);
176 ssnprintf (key_int_derive, sizeof (key_int_derive),
177 "target_scale[%p,%i]:int_derive",
178 (void *) data, dsrc_index);
179 ssnprintf (key_int_fraction, sizeof (key_int_fraction),
180 "target_scale[%p,%i]:int_fraction",
181 (void *) data, dsrc_index);
183 prev_derive = curr_derive;
187 /* Query the meta data */
190 status = uc_meta_data_get_signed_int (vl, key_prev_derive,
195 status = uc_meta_data_get_signed_int (vl, key_int_derive, &int_derive);
199 status = uc_meta_data_get_double (vl, key_int_fraction, &int_fraction);
208 /* Calcualte the rate */
209 difference = curr_derive - prev_derive;
210 rate = ((double) difference) / CDTIME_T_TO_DOUBLE (vl->interval);
212 /* Modify the rate. */
213 if (!isnan (data->factor))
214 rate *= data->factor;
215 if (!isnan (data->offset))
216 rate += data->offset;
218 /* Calculate the internal derive. */
219 int_fraction += (rate * CDTIME_T_TO_DOUBLE (vl->interval));
220 if (int_fraction < 0.0) /* handle negative integer rounding correctly */
221 difference = ((int64_t) int_fraction) - 1;
223 difference = (int64_t) int_fraction;
224 int_fraction -= ((double) difference);
225 int_derive += difference;
227 assert (int_fraction >= 0.0);
228 assert (int_fraction < 1.0);
230 DEBUG ("Target `scale': ts_invoke_derive: %"PRIu64" -> %g -> %"PRIu64
232 curr_derive, rate, int_derive, int_fraction);
234 else /* (failure != 0) */
240 vl->values[dsrc_index].derive = (derive_t) int_derive;
242 /* Update to the new derive value */
243 uc_meta_data_add_signed_int (vl, key_prev_derive, curr_derive);
244 uc_meta_data_add_signed_int (vl, key_int_derive, int_derive);
245 uc_meta_data_add_double (vl, key_int_fraction, int_fraction);
248 } /* }}} int ts_invoke_derive */
250 static int ts_invoke_absolute (const data_set_t *ds, value_list_t *vl, /* {{{ */
251 ts_data_t *data, int dsrc_index)
253 uint64_t curr_absolute;
257 /* Required meta data */
259 char key_int_fraction[128];
261 curr_absolute = (uint64_t) vl->values[dsrc_index].absolute;
263 ssnprintf (key_int_fraction, sizeof (key_int_fraction),
264 "target_scale[%p,%i]:int_fraction",
265 (void *) data, dsrc_index);
269 /* Query the meta data */
270 status = uc_meta_data_get_double (vl, key_int_fraction, &int_fraction);
274 rate = ((double) curr_absolute) / CDTIME_T_TO_DOUBLE (vl->interval);
276 /* Modify the rate. */
277 if (!isnan (data->factor))
278 rate *= data->factor;
279 if (!isnan (data->offset))
280 rate += data->offset;
282 /* Calculate the new absolute. */
283 int_fraction += (rate * CDTIME_T_TO_DOUBLE (vl->interval));
284 curr_absolute = (uint64_t) int_fraction;
285 int_fraction -= ((double) curr_absolute);
287 vl->values[dsrc_index].absolute = (absolute_t) curr_absolute;
289 /* Update to the new absolute value */
290 uc_meta_data_add_double (vl, key_int_fraction, int_fraction);
293 } /* }}} int ts_invoke_absolute */
295 static int ts_config_set_double (double *ret, oconfig_item_t *ci) /* {{{ */
297 if ((ci->values_num != 1)
298 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
300 WARNING ("scale target: The `%s' config option needs "
301 "exactly one numeric argument.", ci->key);
305 *ret = ci->values[0].value.number;
306 DEBUG ("ts_config_set_double: *ret = %g", *ret);
309 } /* }}} int ts_config_set_double */
311 static int ts_config_add_data_source(ts_data_t *data, /* {{{ */
314 size_t new_data_sources_num;
318 /* Check number of arbuments. */
319 if (ci->values_num < 1)
321 ERROR ("`value' match: `%s' needs at least one argument.",
326 /* Check type of arguments */
327 for (i = 0; i < ci->values_num; i++)
329 if (ci->values[i].type == OCONFIG_TYPE_STRING)
332 ERROR ("`value' match: `%s' accepts only string arguments "
333 "(argument %i is a %s).",
335 (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
336 ? "truth value" : "number");
340 /* Allocate space for the char pointers */
341 new_data_sources_num = data->data_sources_num + ((size_t) ci->values_num);
342 temp = (char **) realloc (data->data_sources,
343 new_data_sources_num * sizeof (char *));
346 ERROR ("`value' match: realloc failed.");
349 data->data_sources = temp;
351 /* Copy the strings, allocating memory as needed. */
352 for (i = 0; i < ci->values_num; i++)
356 /* If we get here, there better be memory for us to write to. */
357 assert (data->data_sources_num < new_data_sources_num);
359 j = data->data_sources_num;
360 data->data_sources[j] = sstrdup (ci->values[i].value.string);
361 if (data->data_sources[j] == NULL)
363 ERROR ("`value' match: sstrdup failed.");
366 data->data_sources_num++;
370 } /* }}} int ts_config_add_data_source */
372 static int ts_destroy (void **user_data) /* {{{ */
376 if (user_data == NULL)
379 data = (ts_data_t *) *user_data;
381 if ((data != NULL) && (data->data_sources != NULL))
384 for (i = 0; i < data->data_sources_num; i++)
385 sfree (data->data_sources[i]);
386 sfree (data->data_sources);
393 } /* }}} int ts_destroy */
395 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
401 data = (ts_data_t *) malloc (sizeof (*data));
404 ERROR ("ts_create: malloc failed.");
407 memset (data, 0, sizeof (*data));
413 for (i = 0; i < ci->children_num; i++)
415 oconfig_item_t *child = ci->children + i;
417 if (strcasecmp ("Factor", child->key) == 0)
418 status = ts_config_set_double (&data->factor, child);
419 else if (strcasecmp ("Offset", child->key) == 0)
420 status = ts_config_set_double (&data->offset, child);
421 else if (strcasecmp ("DataSource", child->key) == 0)
422 status = ts_config_add_data_source(data, child);
425 ERROR ("Target `scale': The `%s' configuration option is not understood "
426 "and will be ignored.", child->key);
434 /* Additional sanity-checking */
437 if (isnan (data->factor) && isnan (data->offset))
439 ERROR ("Target `scale': You need to at least set either the `Factor' "
440 "or `Offset' option!");
449 ts_destroy ((void *) &data);
455 } /* }}} int ts_create */
457 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
458 notification_meta_t __attribute__((unused)) **meta, void **user_data)
463 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
469 ERROR ("Target `scale': Invoke: `data' is NULL.");
473 for (i = 0; i < ds->ds_num; i++)
475 /* If we've got a list of data sources, is it in the list? */
476 if (data->data_sources) {
478 for (j = 0; j < data->data_sources_num; j++)
479 if (strcasecmp(ds->ds[i].name, data->data_sources[j]) == 0)
482 /* No match, ignore */
483 if (j >= data->data_sources_num)
487 if (ds->ds[i].type == DS_TYPE_COUNTER)
488 ts_invoke_counter (ds, vl, data, i);
489 else if (ds->ds[i].type == DS_TYPE_GAUGE)
490 ts_invoke_gauge (ds, vl, data, i);
491 else if (ds->ds[i].type == DS_TYPE_DERIVE)
492 ts_invoke_derive (ds, vl, data, i);
493 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
494 ts_invoke_absolute (ds, vl, data, i);
496 ERROR ("Target `scale': Ignoring unknown data source type %i",
500 return (FC_TARGET_CONTINUE);
501 } /* }}} int ts_invoke */
503 void module_register (void)
507 memset (&tproc, 0, sizeof (tproc));
508 tproc.create = ts_create;
509 tproc.destroy = ts_destroy;
510 tproc.invoke = ts_invoke;
511 fc_register_target ("scale", tproc);
512 } /* module_register */
514 /* vim: set sw=2 ts=2 tw=78 fdm=marker : */