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 int uc_send_notification (const char *name)
61 cache_entry_t *ce = NULL;
67 char *plugin_instance;
73 name_copy = strdup (name);
74 if (name_copy == NULL)
76 ERROR ("uc_send_notification: strdup failed.");
80 status = parse_identifier (name_copy, &host,
81 &plugin, &plugin_instance,
82 &type, &type_instance);
85 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
89 /* Copy the associative members */
90 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
91 host, plugin, plugin_instance, type, type_instance);
94 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
96 pthread_mutex_lock (&cache_lock);
99 * Set the time _after_ getting the lock because we don't know how long
100 * acquiring the lock takes and we will use this time later to decide
101 * whether or not the state is OKAY.
103 n.time = time (NULL);
105 status = c_avl_get (cache_tree, name, (void *) &ce);
108 pthread_mutex_unlock (&cache_lock);
113 /* Check if the entry has been updated in the meantime */
114 if ((n.time - ce->last_update) < (2 * ce->interval))
116 ce->state = STATE_OKAY;
117 pthread_mutex_unlock (&cache_lock);
122 snprintf (n.message, sizeof (n.message),
123 "%s has not been updated for %i seconds.", name,
124 (int) (n.time - ce->last_update));
126 pthread_mutex_unlock (&cache_lock);
128 n.message[sizeof (n.message) - 1] = '\0';
129 plugin_dispatch_notification (&n);
132 } /* int uc_send_notification */
136 if (cache_tree == NULL)
137 cache_tree = c_avl_create ((int (*) (const void *, const void *))
143 int uc_check_timeout (void)
152 c_avl_iterator_t *iter;
155 pthread_mutex_lock (&cache_lock);
159 /* Build a list of entries to be flushed */
160 iter = c_avl_get_iterator (cache_tree);
161 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
163 /* If entry has not been updated, add to `keys' array */
164 if ((now - ce->last_update) >= (2 * ce->interval))
168 tmp = (char **) realloc ((void *) keys,
169 (keys_len + 1) * sizeof (char *));
172 ERROR ("uc_purge: realloc failed.");
173 c_avl_iterator_destroy (iter);
178 keys[keys_len] = strdup (key);
179 if (keys[keys_len] == NULL)
181 ERROR ("uc_check_timeout: strdup failed.");
186 } /* while (c_avl_iterator_next) */
188 for (i = 0; i < keys_len; i++)
192 status = ut_check_interesting (keys[i]);
196 ERROR ("uc_check_timeout: ut_check_interesting failed.");
199 else if (status == 0) /* ``service'' is uninteresting */
202 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''", keys[i]);
203 status = c_avl_remove (cache_tree, keys[i], (void *) &key, (void *) &ce);
206 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
211 else /* (status > 0); ``service'' is interesting */
214 * `keys[i]' is not freed and set to NULL, so that the for-loop below
215 * will send out notifications. There's nothing else to do here.
217 DEBUG ("uc_check_timeout: %s is missing and ``interesting''", keys[i]);
218 ce->state = STATE_ERROR;
220 } /* for (keys[i]) */
222 c_avl_iterator_destroy (iter);
224 pthread_mutex_unlock (&cache_lock);
226 for (i = 0; i < keys_len; i++)
231 uc_send_notification (keys[i]);
238 } /* int uc_check_timeout */
240 int uc_update (const data_set_t *ds, const value_list_t *vl)
242 char name[6 * DATA_MAX_NAME_LEN];
243 cache_entry_t *ce = NULL;
244 int send_okay_notification = 0;
246 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
248 ERROR ("uc_insert: FORMAT_VL failed.");
252 pthread_mutex_lock (&cache_lock);
254 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
259 assert (ce->values_num == ds->ds_num);
261 if (ce->last_time >= vl->time)
263 pthread_mutex_unlock (&cache_lock);
264 NOTICE ("uc_insert: Value too old: name = %s; value time = %u; "
265 "last cache update = %u;",
266 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
270 if ((ce->last_time + ce->interval) < vl->time)
272 send_okay_notification = vl->time - ce->last_time;
273 ce->state = STATE_OKAY;
276 for (i = 0; i < ds->ds_num; i++)
278 if (ds->ds[i].type == DS_TYPE_COUNTER)
282 /* check if the counter has wrapped around */
283 if (vl->values[i].counter < ce->values_counter[i])
285 if (ce->values_counter[i] <= 4294967295U)
286 diff = (4294967295U - ce->values_counter[i])
287 + vl->values[i].counter;
289 diff = (18446744073709551615ULL - ce->values_counter[i])
290 + vl->values[i].counter;
292 else /* counter has NOT wrapped around */
294 diff = vl->values[i].counter - ce->values_counter[i];
297 ce->values_gauge[i] = ((double) diff)
298 / ((double) (vl->time - ce->last_time));
299 ce->values_counter[i] = vl->values[i].counter;
301 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
303 ce->values_gauge[i] = vl->values[i].gauge;
305 DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
308 ce->last_time = vl->time;
309 ce->last_update = time (NULL);
310 ce->interval = vl->interval;
312 else /* key is not found */
315 size_t ce_size = sizeof (cache_entry_t)
316 + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
322 pthread_mutex_unlock (&cache_lock);
323 ERROR ("uc_insert: strdup failed.");
327 ce = (cache_entry_t *) malloc (ce_size);
330 pthread_mutex_unlock (&cache_lock);
331 ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
335 memset (ce, '\0', ce_size);
337 strncpy (ce->name, name, sizeof (ce->name));
338 ce->name[sizeof (ce->name) - 1] = '\0';
340 ce->values_num = ds->ds_num;
341 ce->values_gauge = (gauge_t *) (ce + 1);
342 ce->values_counter = (counter_t *) (ce->values_gauge + ce->values_num);
344 for (i = 0; i < ds->ds_num; i++)
346 if (ds->ds[i].type == DS_TYPE_COUNTER)
348 ce->values_gauge[i] = NAN;
349 ce->values_counter[i] = vl->values[i].counter;
351 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
353 ce->values_gauge[i] = vl->values[i].gauge;
357 ce->last_time = vl->time;
358 ce->last_update = time (NULL);
359 ce->interval = vl->interval;
360 ce->state = STATE_OKAY;
362 if (c_avl_insert (cache_tree, key, ce) != 0)
364 pthread_mutex_unlock (&cache_lock);
365 ERROR ("uc_insert: c_avl_insert failed.");
369 DEBUG ("uc_insert: Added %s to the cache.", name);
370 } /* if (key is not found) */
372 pthread_mutex_unlock (&cache_lock);
374 /* Do not send okay notifications for uninteresting values, i. e. values for
375 * which no threshold is configured. */
376 if (send_okay_notification > 0)
380 status = ut_check_interesting (name);
382 send_okay_notification = 0;
385 if (send_okay_notification > 0)
388 memset (&n, '\0', sizeof (n));
390 /* Copy the associative members */
391 NOTIFICATION_INIT_VL (&n, vl, ds);
393 n.severity = NOTIF_OKAY;
396 snprintf (n.message, sizeof (n.message),
397 "Received a value for %s. It was missing for %i seconds.",
398 name, send_okay_notification);
399 n.message[sizeof (n.message) - 1] = '\0';
401 plugin_dispatch_notification (&n);
405 } /* int uc_insert */
407 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
411 cache_entry_t *ce = NULL;
414 pthread_mutex_lock (&cache_lock);
416 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
420 ret_num = ce->values_num;
421 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
424 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
429 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
434 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
438 pthread_mutex_unlock (&cache_lock);
443 *ret_values_num = ret_num;
447 } /* gauge_t *uc_get_rate_by_name */
449 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
451 char name[6 * DATA_MAX_NAME_LEN];
456 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
458 ERROR ("uc_insert: FORMAT_VL failed.");
462 status = uc_get_rate_by_name (name, &ret, &ret_num);
466 /* This is important - the caller has no other way of knowing how many
467 * values are returned. */
468 if (ret_num != ds->ds_num)
470 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
471 "but uc_get_rate_by_name returned %i.",
472 ds->type, ds->ds_num, ret_num);
478 } /* gauge_t *uc_get_rate */
480 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
482 char name[6 * DATA_MAX_NAME_LEN];
483 cache_entry_t *ce = NULL;
484 int ret = STATE_ERROR;
486 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
488 ERROR ("uc_get_state: FORMAT_VL failed.");
489 return (STATE_ERROR);
492 pthread_mutex_lock (&cache_lock);
494 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
500 pthread_mutex_unlock (&cache_lock);
503 } /* int uc_get_state */
505 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
507 char name[6 * DATA_MAX_NAME_LEN];
508 cache_entry_t *ce = NULL;
511 if (state < STATE_OKAY)
513 if (state > STATE_ERROR)
516 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
518 ERROR ("uc_get_state: FORMAT_VL failed.");
519 return (STATE_ERROR);
522 pthread_mutex_lock (&cache_lock);
524 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
531 pthread_mutex_unlock (&cache_lock);
534 } /* int uc_set_state */
535 /* vim: set sw=2 ts=8 sts=2 tw=78 : */