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 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 = c_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 = c_avl_create ((int (*) (const void *, const void *))
140 int uc_check_timeout (void)
149 c_avl_iterator_t *iter;
152 pthread_mutex_lock (&cache_lock);
156 /* Build a list of entries to be flushed */
157 iter = c_avl_get_iterator (cache_tree);
158 while (c_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 c_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 (c_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 = c_avl_remove (cache_tree, keys[i], (void *) &key, (void *) &ce);
203 ERROR ("uc_check_timeout: c_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 c_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;
241 int send_okay_notification = 0;
243 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
245 ERROR ("uc_insert: FORMAT_VL failed.");
249 pthread_mutex_lock (&cache_lock);
251 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
256 assert (ce->values_num == ds->ds_num);
258 if (ce->last_time >= vl->time)
260 pthread_mutex_unlock (&cache_lock);
261 NOTICE ("uc_insert: Value too old: name = %s; value time = %u; "
262 "last cache update = %u;",
263 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
267 if ((ce->last_time + ce->interval) < vl->time)
269 send_okay_notification = vl->time - ce->last_time;
270 ce->state = STATE_OKAY;
273 for (i = 0; i < ds->ds_num; i++)
275 if (ds->ds[i].type == DS_TYPE_COUNTER)
279 /* check if the counter has wrapped around */
280 if (vl->values[i].counter < ce->values_counter[i])
282 if (ce->values_counter[i] <= 4294967295U)
283 diff = (4294967295U - ce->values_counter[i])
284 + vl->values[i].counter;
286 diff = (18446744073709551615ULL - ce->values_counter[i])
287 + vl->values[i].counter;
289 else /* counter has NOT wrapped around */
291 diff = vl->values[i].counter - ce->values_counter[i];
294 ce->values_gauge[i] = ((double) diff)
295 / ((double) (vl->time - ce->last_time));
296 ce->values_counter[i] = vl->values[i].counter;
298 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
300 ce->values_gauge[i] = vl->values[i].gauge;
302 DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
305 ce->last_time = vl->time;
306 ce->last_update = time (NULL);
307 ce->interval = vl->interval;
309 else /* key is not found */
312 size_t ce_size = sizeof (cache_entry_t)
313 + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
319 pthread_mutex_unlock (&cache_lock);
320 ERROR ("uc_insert: strdup failed.");
324 ce = (cache_entry_t *) malloc (ce_size);
327 pthread_mutex_unlock (&cache_lock);
328 ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
332 memset (ce, '\0', ce_size);
334 strncpy (ce->name, name, sizeof (ce->name));
335 ce->name[sizeof (ce->name) - 1] = '\0';
337 ce->values_num = ds->ds_num;
338 ce->values_gauge = (gauge_t *) (ce + 1);
339 ce->values_counter = (counter_t *) (ce->values_gauge + ce->values_num);
341 for (i = 0; i < ds->ds_num; i++)
343 if (ds->ds[i].type == DS_TYPE_COUNTER)
345 ce->values_gauge[i] = NAN;
346 ce->values_counter[i] = vl->values[i].counter;
348 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
350 ce->values_gauge[i] = vl->values[i].gauge;
354 ce->last_time = vl->time;
355 ce->last_update = time (NULL);
356 ce->interval = vl->interval;
357 ce->state = STATE_OKAY;
359 if (c_avl_insert (cache_tree, key, ce) != 0)
361 pthread_mutex_unlock (&cache_lock);
362 ERROR ("uc_insert: c_avl_insert failed.");
366 DEBUG ("uc_insert: Added %s to the cache.", name);
367 } /* if (key is not found) */
369 pthread_mutex_unlock (&cache_lock);
371 /* Do not send okay notifications for uninteresting values, i. e. values for
372 * which no threshold is configured. */
373 if (send_okay_notification > 0)
377 status = ut_check_interesting (name);
379 send_okay_notification = 0;
382 if (send_okay_notification > 0)
385 memset (&n, '\0', sizeof (n));
387 n.severity = NOTIF_OKAY;
388 strncpy (n.host, vl->host, sizeof (n.host));
389 n.host[sizeof (n.host) - 1] = '\0';
392 snprintf (n.message, sizeof (n.message),
393 "Received a value for %s. It was missing for %i seconds.",
394 name, send_okay_notification);
395 n.message[sizeof (n.message) - 1] = '\0';
397 plugin_dispatch_notification (&n);
401 } /* int uc_insert */
403 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
405 char name[6 * DATA_MAX_NAME_LEN];
407 cache_entry_t *ce = NULL;
409 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
411 ERROR ("uc_insert: FORMAT_VL failed.");
415 pthread_mutex_lock (&cache_lock);
417 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
420 assert (ce->values_num == ds->ds_num);
422 ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
425 ERROR ("uc_get_rate: malloc failed.");
429 memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
433 pthread_mutex_unlock (&cache_lock);
436 } /* gauge_t *uc_get_rate */
438 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
440 char name[6 * DATA_MAX_NAME_LEN];
441 cache_entry_t *ce = NULL;
442 int ret = STATE_ERROR;
444 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
446 ERROR ("uc_get_state: FORMAT_VL failed.");
447 return (STATE_ERROR);
450 pthread_mutex_lock (&cache_lock);
452 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
458 pthread_mutex_unlock (&cache_lock);
461 } /* int uc_get_state */
463 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
465 char name[6 * DATA_MAX_NAME_LEN];
466 cache_entry_t *ce = NULL;
469 if (state < STATE_OKAY)
471 if (state > STATE_ERROR)
474 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
476 ERROR ("uc_get_state: FORMAT_VL failed.");
477 return (STATE_ERROR);
480 pthread_mutex_lock (&cache_lock);
482 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
489 pthread_mutex_unlock (&cache_lock);
492 } /* int uc_set_state */
493 /* vim: set sw=2 ts=8 sts=2 tw=78 : */