2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007 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;
37 counter_t *values_counter;
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 = (gauge_t *) calloc (values_num, sizeof (gauge_t));
73 ce->values_counter = (counter_t *) calloc (values_num, sizeof (counter_t));
74 if ((ce->values_gauge == NULL) || (ce->values_counter == NULL))
76 sfree (ce->values_gauge);
77 sfree (ce->values_counter);
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_counter);
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 snprintf (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 n.message[sizeof (n.message) - 1] = '\0';
166 plugin_dispatch_notification (&n);
169 } /* int uc_send_notification */
171 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
178 /* `cache_lock' has been locked by `uc_update' */
180 key_copy = strdup (key);
181 if (key_copy == NULL)
183 ERROR ("uc_insert: strdup failed.");
187 ce = cache_alloc (ds->ds_num);
190 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
194 sstrncpy (ce->name, key, sizeof (ce->name));
196 for (i = 0; i < ds->ds_num; i++)
198 if (ds->ds[i].type == DS_TYPE_COUNTER)
200 ce->values_gauge[i] = NAN;
201 ce->values_counter[i] = vl->values[i].counter;
203 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
205 ce->values_gauge[i] = vl->values[i].gauge;
209 ce->last_time = vl->time;
210 ce->last_update = time (NULL);
211 ce->interval = vl->interval;
212 ce->state = STATE_OKAY;
214 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
217 ERROR ("uc_insert: c_avl_insert failed.");
221 DEBUG ("uc_insert: Added %s to the cache.", key);
223 } /* int uc_insert */
227 if (cache_tree == NULL)
228 cache_tree = c_avl_create ((int (*) (const void *, const void *))
234 int uc_check_timeout (void)
243 c_avl_iterator_t *iter;
246 pthread_mutex_lock (&cache_lock);
250 /* Build a list of entries to be flushed */
251 iter = c_avl_get_iterator (cache_tree);
252 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
254 /* If entry has not been updated, add to `keys' array */
255 if ((now - ce->last_update) >= (2 * ce->interval))
259 tmp = (char **) realloc ((void *) keys,
260 (keys_len + 1) * sizeof (char *));
263 ERROR ("uc_purge: realloc failed.");
264 c_avl_iterator_destroy (iter);
269 keys[keys_len] = strdup (key);
270 if (keys[keys_len] == NULL)
272 ERROR ("uc_check_timeout: strdup failed.");
277 } /* while (c_avl_iterator_next) */
279 for (i = 0; i < keys_len; i++)
283 status = ut_check_interesting (keys[i]);
287 ERROR ("uc_check_timeout: ut_check_interesting failed.");
290 else if (status == 0) /* ``service'' is uninteresting */
293 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
295 status = c_avl_remove (cache_tree, keys[i],
296 (void *) &key, (void *) &ce);
299 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
304 else if (status == 1) /* persist */
306 DEBUG ("uc_check_timeout: %s is missing, sending notification.",
308 ce->state = STATE_ERROR;
310 else if (status == 2) /* do not persist */
312 if (ce->state == STATE_ERROR)
314 DEBUG ("uc_check_timeout: %s is missing but "
315 "notification has already been sent.",
319 else /* (ce->state != STATE_ERROR) */
321 DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
323 ce->state = STATE_ERROR;
328 WARNING ("uc_check_timeout: ut_check_interesting (%s) returned ",
329 "invalid status %i.",
332 } /* for (keys[i]) */
334 c_avl_iterator_destroy (iter);
336 pthread_mutex_unlock (&cache_lock);
338 for (i = 0; i < keys_len; i++)
343 uc_send_notification (keys[i]);
350 } /* int uc_check_timeout */
352 int uc_update (const data_set_t *ds, const value_list_t *vl)
354 char name[6 * DATA_MAX_NAME_LEN];
355 cache_entry_t *ce = NULL;
356 int send_okay_notification = 0;
357 time_t update_delay = 0;
362 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
364 ERROR ("uc_update: FORMAT_VL failed.");
368 pthread_mutex_lock (&cache_lock);
370 status = c_avl_get (cache_tree, name, (void *) &ce);
371 if (status != 0) /* entry does not yet exist */
373 status = uc_insert (ds, vl, name);
374 pthread_mutex_unlock (&cache_lock);
379 assert (ce->values_num == ds->ds_num);
381 if (ce->last_time >= vl->time)
383 pthread_mutex_unlock (&cache_lock);
384 NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
385 "last cache update = %u;",
386 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
390 /* Send a notification (after the lock has been released) if we switch the
391 * state from something else to `okay'. */
392 if (ce->state != STATE_OKAY)
394 send_okay_notification = 1;
395 ce->state = STATE_OKAY;
396 update_delay = time (NULL) - ce->last_update;
399 for (i = 0; i < ds->ds_num; i++)
401 if (ds->ds[i].type == DS_TYPE_COUNTER)
405 /* check if the counter has wrapped around */
406 if (vl->values[i].counter < ce->values_counter[i])
408 if (ce->values_counter[i] <= 4294967295U)
409 diff = (4294967295U - ce->values_counter[i])
410 + vl->values[i].counter;
412 diff = (18446744073709551615ULL - ce->values_counter[i])
413 + vl->values[i].counter;
415 else /* counter has NOT wrapped around */
417 diff = vl->values[i].counter - ce->values_counter[i];
420 ce->values_gauge[i] = ((double) diff)
421 / ((double) (vl->time - ce->last_time));
422 ce->values_counter[i] = vl->values[i].counter;
424 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
426 ce->values_gauge[i] = vl->values[i].gauge;
428 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
431 ce->last_time = vl->time;
432 ce->last_update = time (NULL);
433 ce->interval = vl->interval;
435 pthread_mutex_unlock (&cache_lock);
437 if (send_okay_notification == 0)
440 /* Do not send okay notifications for uninteresting values, i. e. values for
441 * which no threshold is configured. */
442 status = ut_check_interesting (name);
446 /* Initialize the notification */
447 memset (&n, '\0', sizeof (n));
448 NOTIFICATION_INIT_VL (&n, vl, ds);
450 n.severity = NOTIF_OKAY;
453 snprintf (n.message, sizeof (n.message),
454 "Received a value for %s. It was missing for %u seconds.",
455 name, (unsigned int) update_delay);
456 n.message[sizeof (n.message) - 1] = '\0';
458 plugin_dispatch_notification (&n);
461 } /* int uc_update */
463 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
465 char name[6 * DATA_MAX_NAME_LEN];
467 cache_entry_t *ce = NULL;
469 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
471 ERROR ("uc_get_rate: FORMAT_VL failed.");
475 pthread_mutex_lock (&cache_lock);
477 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
480 assert (ce->values_num == ds->ds_num);
482 ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
485 ERROR ("uc_get_rate: malloc failed.");
489 memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
493 pthread_mutex_unlock (&cache_lock);
496 } /* gauge_t *uc_get_rate */
498 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
500 char name[6 * DATA_MAX_NAME_LEN];
501 cache_entry_t *ce = NULL;
502 int ret = STATE_ERROR;
504 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
506 ERROR ("uc_get_state: FORMAT_VL failed.");
507 return (STATE_ERROR);
510 pthread_mutex_lock (&cache_lock);
512 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
518 pthread_mutex_unlock (&cache_lock);
521 } /* int uc_get_state */
523 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
525 char name[6 * DATA_MAX_NAME_LEN];
526 cache_entry_t *ce = NULL;
529 if (state < STATE_OKAY)
531 if (state > STATE_ERROR)
534 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
536 ERROR ("uc_get_state: FORMAT_VL failed.");
537 return (STATE_ERROR);
540 pthread_mutex_lock (&cache_lock);
542 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
549 pthread_mutex_unlock (&cache_lock);
552 } /* int uc_set_state */
553 /* vim: set sw=2 ts=8 sts=2 tw=78 : */