2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007,2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
27 #include "utils_threshold.h"
32 typedef struct cache_entry_s
34 char name[6 * DATA_MAX_NAME_LEN];
36 gauge_t *values_gauge;
38 /* Time contained in the package
39 * (for calculating rates) */
41 /* Time according to the local clock
42 * (for purging old entries) */
44 /* Interval in which the data is collected
45 * (for purding old entries) */
50 static c_avl_tree_t *cache_tree = NULL;
51 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
53 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
55 assert ((a != NULL) && (b != NULL));
56 return (strcmp (a->name, b->name));
57 } /* int cache_compare */
59 static cache_entry_t *cache_alloc (int values_num)
63 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
66 ERROR ("utils_cache: cache_alloc: malloc failed.");
69 memset (ce, '\0', sizeof (cache_entry_t));
70 ce->values_num = values_num;
72 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
73 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
74 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
76 sfree (ce->values_gauge);
77 sfree (ce->values_raw);
79 ERROR ("utils_cache: cache_alloc: calloc failed.");
84 } /* cache_entry_t *cache_alloc */
86 static void cache_free (cache_entry_t *ce)
91 sfree (ce->values_gauge);
92 sfree (ce->values_raw);
94 } /* void cache_free */
96 static int uc_send_notification (const char *name)
98 cache_entry_t *ce = NULL;
104 char *plugin_instance;
110 name_copy = strdup (name);
111 if (name_copy == NULL)
113 ERROR ("uc_send_notification: strdup failed.");
117 status = parse_identifier (name_copy, &host,
118 &plugin, &plugin_instance,
119 &type, &type_instance);
122 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
126 /* Copy the associative members */
127 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
128 host, plugin, plugin_instance, type, type_instance);
131 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
133 pthread_mutex_lock (&cache_lock);
136 * Set the time _after_ getting the lock because we don't know how long
137 * acquiring the lock takes and we will use this time later to decide
138 * whether or not the state is OKAY.
140 n.time = time (NULL);
142 status = c_avl_get (cache_tree, name, (void *) &ce);
145 pthread_mutex_unlock (&cache_lock);
150 /* Check if the entry has been updated in the meantime */
151 if ((n.time - ce->last_update) < (2 * ce->interval))
153 ce->state = STATE_OKAY;
154 pthread_mutex_unlock (&cache_lock);
159 ssnprintf (n.message, sizeof (n.message),
160 "%s has not been updated for %i seconds.", name,
161 (int) (n.time - ce->last_update));
163 pthread_mutex_unlock (&cache_lock);
165 plugin_dispatch_notification (&n);
168 } /* int uc_send_notification */
170 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
174 for (i = 0; i < ds->ds_num; i++)
176 if (isnan (ce->values_gauge[i]))
178 else if (ce->values_gauge[i] < ds->ds[i].min)
179 ce->values_gauge[i] = NAN;
180 else if (ce->values_gauge[i] > ds->ds[i].max)
181 ce->values_gauge[i] = NAN;
183 } /* void uc_check_range */
185 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
192 /* `cache_lock' has been locked by `uc_update' */
194 key_copy = strdup (key);
195 if (key_copy == NULL)
197 ERROR ("uc_insert: strdup failed.");
201 ce = cache_alloc (ds->ds_num);
205 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
209 sstrncpy (ce->name, key, sizeof (ce->name));
211 for (i = 0; i < ds->ds_num; i++)
213 switch (ds->ds[i].type)
215 case DS_TYPE_COUNTER:
216 ce->values_gauge[i] = NAN;
217 ce->values_raw[i].counter = vl->values[i].counter;
221 ce->values_gauge[i] = vl->values[i].gauge;
222 ce->values_raw[i].gauge = vl->values[i].gauge;
226 ce->values_gauge[i] = NAN;
227 ce->values_raw[i].derive = vl->values[i].derive;
230 case DS_TYPE_ABSOLUTE:
231 ce->values_gauge[i] = NAN;
232 if (vl->interval > 0)
233 ce->values_gauge[i] = ((double) vl->values[i].absolute)
234 / ((double) vl->interval);
235 ce->values_raw[i].absolute = vl->values[i].absolute;
239 /* This shouldn't happen. */
240 ERROR ("uc_insert: Don't know how to handle data source type %i.",
243 } /* switch (ds->ds[i].type) */
246 /* Prune invalid gauge data */
247 uc_check_range (ds, ce);
249 ce->last_time = vl->time;
250 ce->last_update = time (NULL);
251 ce->interval = vl->interval;
252 ce->state = STATE_OKAY;
254 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
257 ERROR ("uc_insert: c_avl_insert failed.");
261 DEBUG ("uc_insert: Added %s to the cache.", key);
263 } /* int uc_insert */
267 if (cache_tree == NULL)
268 cache_tree = c_avl_create ((int (*) (const void *, const void *))
274 int uc_check_timeout (void)
283 c_avl_iterator_t *iter;
286 pthread_mutex_lock (&cache_lock);
290 /* Build a list of entries to be flushed */
291 iter = c_avl_get_iterator (cache_tree);
292 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
294 /* If entry has not been updated, add to `keys' array */
295 if ((now - ce->last_update) >= (2 * ce->interval))
299 tmp = (char **) realloc ((void *) keys,
300 (keys_len + 1) * sizeof (char *));
303 ERROR ("uc_check_timeout: realloc failed.");
304 c_avl_iterator_destroy (iter);
306 pthread_mutex_unlock (&cache_lock);
311 keys[keys_len] = strdup (key);
312 if (keys[keys_len] == NULL)
314 ERROR ("uc_check_timeout: strdup failed.");
319 } /* while (c_avl_iterator_next) */
323 for (i = 0; i < keys_len; i++)
327 status = ut_check_interesting (keys[i]);
331 ERROR ("uc_check_timeout: ut_check_interesting failed.");
335 else if (status == 0) /* ``service'' is uninteresting */
337 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
339 status = c_avl_remove (cache_tree, keys[i],
340 (void *) &key, (void *) &ce);
343 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
350 /* If we get here, the value is ``interesting''. Query the record from the
351 * cache and update the state field. */
352 if (c_avl_get (cache_tree, keys[i], (void *) &ce) != 0)
354 ERROR ("uc_check_timeout: cannot get data for %s from cache", keys[i]);
355 /* Do not free `keys[i]' so a notification is sent further down. */
360 if (status == 2) /* persist */
362 DEBUG ("uc_check_timeout: %s is missing, sending notification.",
364 ce->state = STATE_MISSING;
365 /* Do not free `keys[i]' so a notification is sent further down. */
367 else if (status == 1) /* do not persist */
369 if (ce->state == STATE_MISSING)
371 DEBUG ("uc_check_timeout: %s is missing but "
372 "notification has already been sent.",
374 /* Set `keys[i]' to NULL to no notification is sent. */
377 else /* (ce->state != STATE_MISSING) */
379 DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
381 ce->state = STATE_MISSING;
382 /* Do not free `keys[i]' so a notification is sent further down. */
387 WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
388 "invalid status %i.",
392 } /* for (keys[i]) */
394 c_avl_iterator_destroy (iter);
396 pthread_mutex_unlock (&cache_lock);
398 for (i = 0; i < keys_len; i++)
403 uc_send_notification (keys[i]);
410 } /* int uc_check_timeout */
412 int uc_update (const data_set_t *ds, const value_list_t *vl)
414 char name[6 * DATA_MAX_NAME_LEN];
415 cache_entry_t *ce = NULL;
416 int send_okay_notification = 0;
417 time_t update_delay = 0;
422 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
424 ERROR ("uc_update: FORMAT_VL failed.");
428 pthread_mutex_lock (&cache_lock);
430 status = c_avl_get (cache_tree, name, (void *) &ce);
431 if (status != 0) /* entry does not yet exist */
433 status = uc_insert (ds, vl, name);
434 pthread_mutex_unlock (&cache_lock);
439 assert (ce->values_num == ds->ds_num);
441 if (ce->last_time >= vl->time)
443 pthread_mutex_unlock (&cache_lock);
444 NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
445 "last cache update = %u;",
446 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
450 /* Send a notification (after the lock has been released) if we switch the
451 * state from something else to `okay'. */
452 if (ce->state == STATE_MISSING)
454 send_okay_notification = 1;
455 ce->state = STATE_OKAY;
456 update_delay = time (NULL) - ce->last_update;
459 for (i = 0; i < ds->ds_num; i++)
461 switch (ds->ds[i].type)
463 case DS_TYPE_COUNTER:
467 /* check if the counter has wrapped around */
468 if (vl->values[i].counter < ce->values_raw[i].counter)
470 if (ce->values_raw[i].counter <= 4294967295U)
471 diff = (4294967295U - ce->values_raw[i].counter)
472 + vl->values[i].counter;
474 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
475 + vl->values[i].counter;
477 else /* counter has NOT wrapped around */
479 diff = vl->values[i].counter - ce->values_raw[i].counter;
482 ce->values_gauge[i] = ((double) diff)
483 / ((double) (vl->time - ce->last_time));
484 ce->values_raw[i].counter = vl->values[i].counter;
489 ce->values_raw[i].gauge = vl->values[i].gauge;
490 ce->values_gauge[i] = vl->values[i].gauge;
497 diff = vl->values[i].derive - ce->values_raw[i].derive;
499 ce->values_gauge[i] = ((double) diff)
500 / ((double) (vl->time - ce->last_time));
501 ce->values_raw[i].derive = vl->values[i].derive;
505 case DS_TYPE_ABSOLUTE:
506 ce->values_gauge[i] = ((double) vl->values[i].absolute)
507 / ((double) (vl->time - ce->last_time));
508 ce->values_raw[i].absolute = vl->values[i].absolute;
512 /* This shouldn't happen. */
513 pthread_mutex_unlock (&cache_lock);
514 ERROR ("uc_update: Don't know how to handle data source type %i.",
517 } /* switch (ds->ds[i].type) */
519 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
522 /* Prune invalid gauge data */
523 uc_check_range (ds, ce);
525 ce->last_time = vl->time;
526 ce->last_update = time (NULL);
527 ce->interval = vl->interval;
529 pthread_mutex_unlock (&cache_lock);
531 if (send_okay_notification == 0)
534 /* Do not send okay notifications for uninteresting values, i. e. values for
535 * which no threshold is configured. */
536 status = ut_check_interesting (name);
540 /* Initialize the notification */
541 memset (&n, '\0', sizeof (n));
542 NOTIFICATION_INIT_VL (&n, vl, ds);
544 n.severity = NOTIF_OKAY;
547 ssnprintf (n.message, sizeof (n.message),
548 "Received a value for %s. It was missing for %u seconds.",
549 name, (unsigned int) update_delay);
551 plugin_dispatch_notification (&n);
554 } /* int uc_update */
556 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
560 cache_entry_t *ce = NULL;
563 pthread_mutex_lock (&cache_lock);
565 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
569 ret_num = ce->values_num;
570 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
573 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
578 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
583 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
587 pthread_mutex_unlock (&cache_lock);
592 *ret_values_num = ret_num;
596 } /* gauge_t *uc_get_rate_by_name */
598 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
600 char name[6 * DATA_MAX_NAME_LEN];
605 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
607 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
611 status = uc_get_rate_by_name (name, &ret, &ret_num);
615 /* This is important - the caller has no other way of knowing how many
616 * values are returned. */
617 if (ret_num != (size_t) ds->ds_num)
619 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
620 "but uc_get_rate_by_name returned %zu.",
621 ds->type, ds->ds_num, ret_num);
627 } /* gauge_t *uc_get_rate */
629 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
631 c_avl_iterator_t *iter;
633 cache_entry_t *value;
636 time_t *times = NULL;
641 if ((ret_names == NULL) || (ret_number == NULL))
644 pthread_mutex_lock (&cache_lock);
646 iter = c_avl_get_iterator (cache_tree);
647 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
651 if (ret_times != NULL)
655 tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
656 if (tmp_times == NULL)
662 times[number] = value->last_time;
665 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
672 names[number] = strdup (key);
673 if (names[number] == NULL)
679 } /* while (c_avl_iterator_next) */
681 c_avl_iterator_destroy (iter);
682 pthread_mutex_unlock (&cache_lock);
688 for (i = 0; i < number; i++)
698 if (ret_times != NULL)
700 *ret_number = number;
703 } /* int uc_get_names */
705 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
707 char name[6 * DATA_MAX_NAME_LEN];
708 cache_entry_t *ce = NULL;
709 int ret = STATE_ERROR;
711 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
713 ERROR ("uc_get_state: FORMAT_VL failed.");
714 return (STATE_ERROR);
717 pthread_mutex_lock (&cache_lock);
719 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
725 pthread_mutex_unlock (&cache_lock);
728 } /* int uc_get_state */
730 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
732 char name[6 * DATA_MAX_NAME_LEN];
733 cache_entry_t *ce = NULL;
736 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
738 ERROR ("uc_get_state: FORMAT_VL failed.");
739 return (STATE_ERROR);
742 pthread_mutex_lock (&cache_lock);
744 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
751 pthread_mutex_unlock (&cache_lock);
754 } /* int uc_set_state */
755 /* vim: set sw=2 ts=8 sts=2 tw=78 : */