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_threshold.h"
31 typedef struct cache_entry_s
33 char name[6 * DATA_MAX_NAME_LEN];
35 gauge_t *values_gauge;
36 counter_t *values_counter;
37 /* Time contained in the package
38 * (for calculating rates) */
40 /* Time according to the local clock
41 * (for purging old entries) */
43 /* Interval in which the data is collected
44 * (for purding old entries) */
48 static avl_tree_t *cache_tree = NULL;
49 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
51 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
53 assert ((a != NULL) && (b != NULL));
54 return (strcmp (a->name, b->name));
55 } /* int cache_compare */
57 static int uc_send_notification (const char *name)
59 cache_entry_t *ce = NULL;
65 char *plugin_instance;
71 memset (&n, '\0', sizeof (n));
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 n.severity = NOTIF_FAILURE;
90 strncpy (n.host, host, sizeof (n.host));
91 n.host[sizeof (n.host) - 1] = '\0';
94 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
96 pthread_mutex_lock (&cache_lock);
100 status = avl_get (cache_tree, name, (void *) &ce);
103 pthread_mutex_unlock (&cache_lock);
108 /* Check if the entry has been updated in the meantime */
109 if ((n.time - ce->last_update) < (2 * ce->interval))
111 pthread_mutex_unlock (&cache_lock);
116 snprintf (n.message, sizeof (n.message),
117 "%s has not been updated for %i seconds.", name,
118 (int) (n.time - ce->last_update));
120 pthread_mutex_unlock (&cache_lock);
122 n.message[sizeof (n.message) - 1] = '\0';
123 plugin_dispatch_notification (&n);
126 } /* int uc_send_notification */
130 if (cache_tree == NULL)
131 cache_tree = avl_create ((int (*) (const void *, const void *))
137 int uc_check_timeout (void)
146 avl_iterator_t *iter;
149 pthread_mutex_lock (&cache_lock);
153 /* Build a list of entries to be flushed */
154 iter = avl_get_iterator (cache_tree);
155 while (avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
157 /* If entry has not been updated, add to `keys' array */
158 if ((now - ce->last_update) >= (2 * ce->interval))
162 tmp = (char **) realloc ((void *) keys,
163 (keys_len + 1) * sizeof (char *));
166 ERROR ("uc_purge: realloc failed.");
167 avl_iterator_destroy (iter);
172 keys[keys_len] = strdup (key);
173 if (keys[keys_len] == NULL)
175 ERROR ("uc_check_timeout: strdup failed.");
180 } /* while (avl_iterator_next) */
182 for (i = 0; i < keys_len; i++)
186 status = ut_check_interesting (keys[i]);
190 ERROR ("uc_check_timeout: ut_check_interesting failed.");
193 else if (status == 0) /* ``service'' is uninteresting */
196 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''", keys[i]);
197 status = avl_remove (cache_tree, keys[i], (void *) &key, (void *) &ce);
200 ERROR ("uc_check_timeout: avl_remove (%s) failed.", keys[i]);
205 else /* (status > 0); ``service'' is interesting */
208 * `keys[i]' is not freed and set to NULL, so that the for-loop below
209 * will send out notifications. There's nothing else to do here.
211 DEBUG ("uc_check_timeout: %s is missing and ``interesting''", keys[i]);
213 } /* for (keys[i]) */
215 avl_iterator_destroy (iter);
217 pthread_mutex_unlock (&cache_lock);
219 for (i = 0; i < keys_len; i++)
224 uc_send_notification (keys[i]);
231 } /* int uc_check_timeout */
233 int uc_update (const data_set_t *ds, const value_list_t *vl)
235 char name[6 * DATA_MAX_NAME_LEN];
236 cache_entry_t *ce = NULL;
238 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
240 ERROR ("uc_insert: FORMAT_VL failed.");
244 pthread_mutex_lock (&cache_lock);
246 if (avl_get (cache_tree, name, (void *) &ce) == 0)
251 assert (ce->values_num == ds->ds_num);
253 if (ce->last_time >= vl->time)
255 pthread_mutex_unlock (&cache_lock);
256 NOTICE ("uc_insert: Value too old: name = %s; value time = %u; "
257 "last cache update = %u;",
258 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
262 if ((ce->last_time + ce->interval) < vl->time)
264 /* TODO: Implement a `real' okay notification. Watch out for locking
266 NOTICE ("uc_insert: Okay notification for %s", name);
269 for (i = 0; i < ds->ds_num; i++)
271 if (ds->ds[i].type == DS_TYPE_COUNTER)
275 /* check if the counter has wrapped around */
276 if (vl->values[i].counter < ce->values_counter[i])
278 if (ce->values_counter[i] <= 4294967295U)
279 diff = (4294967295U - ce->values_counter[i])
280 + vl->values[i].counter;
282 diff = (18446744073709551615ULL - ce->values_counter[i])
283 + vl->values[i].counter;
285 else /* counter has NOT wrapped around */
287 diff = vl->values[i].counter - ce->values_counter[i];
290 ce->values_gauge[i] = ((double) diff)
291 / ((double) (vl->time - ce->last_time));
292 ce->values_counter[i] = vl->values[i].counter;
294 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
296 ce->values_gauge[i] = vl->values[i].gauge;
298 DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
301 ce->last_time = vl->time;
302 ce->last_update = time (NULL);
303 ce->interval = vl->interval;
305 else /* key is not found */
308 size_t ce_size = sizeof (cache_entry_t)
309 + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
315 pthread_mutex_unlock (&cache_lock);
316 ERROR ("uc_insert: strdup failed.");
320 ce = (cache_entry_t *) malloc (ce_size);
323 pthread_mutex_unlock (&cache_lock);
324 ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
328 memset (ce, '\0', ce_size);
330 strncpy (ce->name, name, sizeof (ce->name));
331 ce->name[sizeof (ce->name) - 1] = '\0';
333 ce->values_num = ds->ds_num;
334 ce->values_gauge = (gauge_t *) (ce + 1);
335 ce->values_counter = (counter_t *) (ce->values_gauge + ce->values_num);
337 for (i = 0; i < ds->ds_num; i++)
339 if (ds->ds[i].type == DS_TYPE_COUNTER)
341 ce->values_gauge[i] = NAN;
342 ce->values_counter[i] = vl->values[i].counter;
344 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
346 ce->values_gauge[i] = vl->values[i].gauge;
350 ce->last_time = vl->time;
351 ce->last_update = time (NULL);
352 ce->interval = vl->interval;
354 if (avl_insert (cache_tree, key, ce) != 0)
356 pthread_mutex_unlock (&cache_lock);
357 ERROR ("uc_insert: avl_insert failed.");
361 DEBUG ("uc_insert: Added %s to the cache.", name);
362 } /* if (key is not found) */
364 pthread_mutex_unlock (&cache_lock);
367 } /* int uc_insert */
369 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
371 char name[6 * DATA_MAX_NAME_LEN];
373 cache_entry_t *ce = NULL;
375 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
377 ERROR ("uc_insert: FORMAT_VL failed.");
381 pthread_mutex_lock (&cache_lock);
383 if (avl_get (cache_tree, name, (void *) &ce) == 0)
386 assert (ce->values_num == ds->ds_num);
388 ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
391 ERROR ("uc_get_rate: malloc failed.");
395 memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
399 pthread_mutex_unlock (&cache_lock);
402 } /* gauge_t *uc_get_rate */
404 /* vim: set sw=2 ts=8 sts=2 tw=78 : */