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 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 memset (&n, '\0', sizeof (n));
75 name_copy = strdup (name);
76 if (name_copy == NULL)
78 ERROR ("uc_send_notification: strdup failed.");
82 status = parse_identifier (name_copy, &host,
83 &plugin, &plugin_instance,
84 &type, &type_instance);
87 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
91 n.severity = NOTIF_FAILURE;
92 strncpy (n.host, host, sizeof (n.host));
93 n.host[sizeof (n.host) - 1] = '\0';
96 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
98 pthread_mutex_lock (&cache_lock);
100 n.time = time (NULL);
102 status = avl_get (cache_tree, name, (void *) &ce);
105 pthread_mutex_unlock (&cache_lock);
110 /* Check if the entry has been updated in the meantime */
111 if ((n.time - ce->last_update) < (2 * ce->interval))
113 ce->state = STATE_OKAY;
114 pthread_mutex_unlock (&cache_lock);
119 snprintf (n.message, sizeof (n.message),
120 "%s has not been updated for %i seconds.", name,
121 (int) (n.time - ce->last_update));
123 pthread_mutex_unlock (&cache_lock);
125 n.message[sizeof (n.message) - 1] = '\0';
126 plugin_dispatch_notification (&n);
129 } /* int uc_send_notification */
133 if (cache_tree == NULL)
134 cache_tree = avl_create ((int (*) (const void *, const void *))
140 int uc_check_timeout (void)
149 avl_iterator_t *iter;
152 pthread_mutex_lock (&cache_lock);
156 /* Build a list of entries to be flushed */
157 iter = avl_get_iterator (cache_tree);
158 while (avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
160 /* If entry has not been updated, add to `keys' array */
161 if ((now - ce->last_update) >= (2 * ce->interval))
165 tmp = (char **) realloc ((void *) keys,
166 (keys_len + 1) * sizeof (char *));
169 ERROR ("uc_purge: realloc failed.");
170 avl_iterator_destroy (iter);
175 keys[keys_len] = strdup (key);
176 if (keys[keys_len] == NULL)
178 ERROR ("uc_check_timeout: strdup failed.");
183 } /* while (avl_iterator_next) */
185 for (i = 0; i < keys_len; i++)
189 status = ut_check_interesting (keys[i]);
193 ERROR ("uc_check_timeout: ut_check_interesting failed.");
196 else if (status == 0) /* ``service'' is uninteresting */
199 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''", keys[i]);
200 status = avl_remove (cache_tree, keys[i], (void *) &key, (void *) &ce);
203 ERROR ("uc_check_timeout: avl_remove (%s) failed.", keys[i]);
208 else /* (status > 0); ``service'' is interesting */
211 * `keys[i]' is not freed and set to NULL, so that the for-loop below
212 * will send out notifications. There's nothing else to do here.
214 DEBUG ("uc_check_timeout: %s is missing and ``interesting''", keys[i]);
215 ce->state = STATE_ERROR;
217 } /* for (keys[i]) */
219 avl_iterator_destroy (iter);
221 pthread_mutex_unlock (&cache_lock);
223 for (i = 0; i < keys_len; i++)
228 uc_send_notification (keys[i]);
235 } /* int uc_check_timeout */
237 int uc_update (const data_set_t *ds, const value_list_t *vl)
239 char name[6 * DATA_MAX_NAME_LEN];
240 cache_entry_t *ce = NULL;
242 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
244 ERROR ("uc_insert: FORMAT_VL failed.");
248 pthread_mutex_lock (&cache_lock);
250 if (avl_get (cache_tree, name, (void *) &ce) == 0)
255 assert (ce->values_num == ds->ds_num);
257 if (ce->last_time >= vl->time)
259 pthread_mutex_unlock (&cache_lock);
260 NOTICE ("uc_insert: Value too old: name = %s; value time = %u; "
261 "last cache update = %u;",
262 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
266 if ((ce->last_time + ce->interval) < vl->time)
268 /* TODO: Implement a `real' okay notification. Watch out for locking
270 NOTICE ("uc_insert: Okay notification for %s", name);
271 ce->state = STATE_OKAY;
274 for (i = 0; i < ds->ds_num; i++)
276 if (ds->ds[i].type == DS_TYPE_COUNTER)
280 /* check if the counter has wrapped around */
281 if (vl->values[i].counter < ce->values_counter[i])
283 if (ce->values_counter[i] <= 4294967295U)
284 diff = (4294967295U - ce->values_counter[i])
285 + vl->values[i].counter;
287 diff = (18446744073709551615ULL - ce->values_counter[i])
288 + vl->values[i].counter;
290 else /* counter has NOT wrapped around */
292 diff = vl->values[i].counter - ce->values_counter[i];
295 ce->values_gauge[i] = ((double) diff)
296 / ((double) (vl->time - ce->last_time));
297 ce->values_counter[i] = vl->values[i].counter;
299 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
301 ce->values_gauge[i] = vl->values[i].gauge;
303 DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
306 ce->last_time = vl->time;
307 ce->last_update = time (NULL);
308 ce->interval = vl->interval;
310 else /* key is not found */
313 size_t ce_size = sizeof (cache_entry_t)
314 + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
320 pthread_mutex_unlock (&cache_lock);
321 ERROR ("uc_insert: strdup failed.");
325 ce = (cache_entry_t *) malloc (ce_size);
328 pthread_mutex_unlock (&cache_lock);
329 ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
333 memset (ce, '\0', ce_size);
335 strncpy (ce->name, name, sizeof (ce->name));
336 ce->name[sizeof (ce->name) - 1] = '\0';
338 ce->values_num = ds->ds_num;
339 ce->values_gauge = (gauge_t *) (ce + 1);
340 ce->values_counter = (counter_t *) (ce->values_gauge + ce->values_num);
342 for (i = 0; i < ds->ds_num; i++)
344 if (ds->ds[i].type == DS_TYPE_COUNTER)
346 ce->values_gauge[i] = NAN;
347 ce->values_counter[i] = vl->values[i].counter;
349 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
351 ce->values_gauge[i] = vl->values[i].gauge;
355 ce->last_time = vl->time;
356 ce->last_update = time (NULL);
357 ce->interval = vl->interval;
358 ce->state = STATE_OKAY;
360 if (avl_insert (cache_tree, key, ce) != 0)
362 pthread_mutex_unlock (&cache_lock);
363 ERROR ("uc_insert: avl_insert failed.");
367 DEBUG ("uc_insert: Added %s to the cache.", name);
368 } /* if (key is not found) */
370 pthread_mutex_unlock (&cache_lock);
373 } /* int uc_insert */
375 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
377 char name[6 * DATA_MAX_NAME_LEN];
379 cache_entry_t *ce = NULL;
381 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
383 ERROR ("uc_insert: FORMAT_VL failed.");
387 pthread_mutex_lock (&cache_lock);
389 if (avl_get (cache_tree, name, (void *) &ce) == 0)
392 assert (ce->values_num == ds->ds_num);
394 ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
397 ERROR ("uc_get_rate: malloc failed.");
401 memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
405 pthread_mutex_unlock (&cache_lock);
408 } /* gauge_t *uc_get_rate */
410 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
412 char name[6 * DATA_MAX_NAME_LEN];
413 cache_entry_t *ce = NULL;
414 int ret = STATE_ERROR;
416 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
418 ERROR ("uc_get_state: FORMAT_VL failed.");
419 return (STATE_ERROR);
422 pthread_mutex_lock (&cache_lock);
424 if (avl_get (cache_tree, name, (void *) &ce) == 0)
430 pthread_mutex_unlock (&cache_lock);
433 } /* int uc_get_state */
435 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
437 char name[6 * DATA_MAX_NAME_LEN];
438 cache_entry_t *ce = NULL;
441 if (state < STATE_OKAY)
443 if (state > STATE_ERROR)
446 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
448 ERROR ("uc_get_state: FORMAT_VL failed.");
449 return (STATE_ERROR);
452 pthread_mutex_lock (&cache_lock);
454 if (avl_get (cache_tree, name, (void *) &ce) == 0)
461 pthread_mutex_unlock (&cache_lock);
464 } /* int uc_set_state */
465 /* vim: set sw=2 ts=8 sts=2 tw=78 : */