1 /**
2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007,2008 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Author:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
27 #include "utils_threshold.h"
29 #include <assert.h>
30 #include <pthread.h>
32 typedef struct cache_entry_s
33 {
34 char name[6 * DATA_MAX_NAME_LEN];
35 int values_num;
36 gauge_t *values_gauge;
37 value_t *values_raw;
38 /* Time contained in the package
39 * (for calculating rates) */
40 time_t last_time;
41 /* Time according to the local clock
42 * (for purging old entries) */
43 time_t last_update;
44 /* Interval in which the data is collected
45 * (for purding old entries) */
46 int interval;
47 int state;
48 } cache_entry_t;
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)
54 {
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)
60 {
61 cache_entry_t *ce;
63 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
64 if (ce == NULL)
65 {
66 ERROR ("utils_cache: cache_alloc: malloc failed.");
67 return (NULL);
68 }
69 memset (ce, '\0', sizeof (cache_entry_t));
70 ce->values_num = values_num;
72 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
73 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
74 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
75 {
76 sfree (ce->values_gauge);
77 sfree (ce->values_raw);
78 sfree (ce);
79 ERROR ("utils_cache: cache_alloc: calloc failed.");
80 return (NULL);
81 }
83 return (ce);
84 } /* cache_entry_t *cache_alloc */
86 static void cache_free (cache_entry_t *ce)
87 {
88 if (ce == NULL)
89 return;
91 sfree (ce->values_gauge);
92 sfree (ce->values_raw);
93 sfree (ce);
94 } /* void cache_free */
96 static int uc_send_notification (const char *name)
97 {
98 cache_entry_t *ce = NULL;
99 int status;
101 char *name_copy;
102 char *host;
103 char *plugin;
104 char *plugin_instance;
105 char *type;
106 char *type_instance;
108 notification_t n;
110 name_copy = strdup (name);
111 if (name_copy == NULL)
112 {
113 ERROR ("uc_send_notification: strdup failed.");
114 return (-1);
115 }
117 status = parse_identifier (name_copy, &host,
118 &plugin, &plugin_instance,
119 &type, &type_instance);
120 if (status != 0)
121 {
122 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
123 return (-1);
124 }
126 /* Copy the associative members */
127 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
128 host, plugin, plugin_instance, type, type_instance);
130 sfree (name_copy);
131 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
133 pthread_mutex_lock (&cache_lock);
135 /*
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.
139 */
140 n.time = time (NULL);
142 status = c_avl_get (cache_tree, name, (void *) &ce);
143 if (status != 0)
144 {
145 pthread_mutex_unlock (&cache_lock);
146 sfree (name_copy);
147 return (-1);
148 }
150 /* Check if the entry has been updated in the meantime */
151 if ((n.time - ce->last_update) < (2 * ce->interval))
152 {
153 ce->state = STATE_OKAY;
154 pthread_mutex_unlock (&cache_lock);
155 sfree (name_copy);
156 return (-1);
157 }
159 ssnprintf (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 plugin_dispatch_notification (&n);
167 return (0);
168 } /* int uc_send_notification */
170 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
171 {
172 int i;
174 for (i = 0; i < ds->ds_num; i++)
175 {
176 if (isnan (ce->values_gauge[i]))
177 continue;
178 else if (ce->values_gauge[i] < ds->ds[i].min)
179 ce->values_gauge[i] = NAN;
180 else if (ce->values_gauge[i] > ds->ds[i].max)
181 ce->values_gauge[i] = NAN;
182 }
183 } /* void uc_check_range */
185 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
186 const char *key)
187 {
188 int i;
189 char *key_copy;
190 cache_entry_t *ce;
192 /* `cache_lock' has been locked by `uc_update' */
194 key_copy = strdup (key);
195 if (key_copy == NULL)
196 {
197 ERROR ("uc_insert: strdup failed.");
198 return (-1);
199 }
201 ce = cache_alloc (ds->ds_num);
202 if (ce == NULL)
203 {
204 sfree (key_copy);
205 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
206 return (-1);
207 }
209 sstrncpy (ce->name, key, sizeof (ce->name));
211 for (i = 0; i < ds->ds_num; i++)
212 {
213 switch (ds->ds[i].type)
214 {
215 case DS_TYPE_COUNTER:
216 ce->values_gauge[i] = NAN;
217 ce->values_raw[i].counter = vl->values[i].counter;
218 break;
220 case DS_TYPE_GAUGE:
221 ce->values_gauge[i] = vl->values[i].gauge;
222 ce->values_raw[i].gauge = vl->values[i].gauge;
223 break;
225 case DS_TYPE_DERIVE:
226 ce->values_gauge[i] = NAN;
227 ce->values_raw[i].derive = vl->values[i].derive;
228 break;
230 case DS_TYPE_ABSOLUTE:
231 ce->values_gauge[i] = NAN;
232 if (vl->interval > 0)
233 ce->values_gauge[i] = ((double) vl->values[i].absolute)
234 / ((double) vl->interval);
235 ce->values_raw[i].absolute = vl->values[i].absolute;
236 break;
238 default:
239 /* This shouldn't happen. */
240 ERROR ("uc_insert: Don't know how to handle data source type %i.",
241 ds->ds[i].type);
242 return (-1);
243 } /* switch (ds->ds[i].type) */
244 } /* for (i) */
246 /* Prune invalid gauge data */
247 uc_check_range (ds, ce);
249 ce->last_time = vl->time;
250 ce->last_update = time (NULL);
251 ce->interval = vl->interval;
252 ce->state = STATE_OKAY;
254 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
255 {
256 sfree (key_copy);
257 ERROR ("uc_insert: c_avl_insert failed.");
258 return (-1);
259 }
261 DEBUG ("uc_insert: Added %s to the cache.", key);
262 return (0);
263 } /* int uc_insert */
265 int uc_init (void)
266 {
267 if (cache_tree == NULL)
268 cache_tree = c_avl_create ((int (*) (const void *, const void *))
269 cache_compare);
271 return (0);
272 } /* int uc_init */
274 int uc_check_timeout (void)
275 {
276 time_t now;
277 cache_entry_t *ce;
279 char **keys = NULL;
280 int keys_len = 0;
282 char *key;
283 c_avl_iterator_t *iter;
284 int i;
286 pthread_mutex_lock (&cache_lock);
288 now = time (NULL);
290 /* Build a list of entries to be flushed */
291 iter = c_avl_get_iterator (cache_tree);
292 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
293 {
294 /* If entry has not been updated, add to `keys' array */
295 if ((now - ce->last_update) >= (2 * ce->interval))
296 {
297 char **tmp;
299 tmp = (char **) realloc ((void *) keys,
300 (keys_len + 1) * sizeof (char *));
301 if (tmp == NULL)
302 {
303 ERROR ("uc_purge: realloc failed.");
304 c_avl_iterator_destroy (iter);
305 pthread_mutex_unlock (&cache_lock);
306 return (-1);
307 }
309 keys = tmp;
310 keys[keys_len] = strdup (key);
311 if (keys[keys_len] == NULL)
312 {
313 ERROR ("uc_check_timeout: strdup failed.");
314 continue;
315 }
316 keys_len++;
317 }
318 } /* while (c_avl_iterator_next) */
320 for (i = 0; i < keys_len; i++)
321 {
322 int status;
324 status = ut_check_interesting (keys[i]);
326 if (status < 0)
327 {
328 ERROR ("uc_check_timeout: ut_check_interesting failed.");
329 sfree (keys[i]);
330 }
331 else if (status == 0) /* ``service'' is uninteresting */
332 {
333 ce = NULL;
334 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
335 keys[i]);
336 status = c_avl_remove (cache_tree, keys[i],
337 (void *) &key, (void *) &ce);
338 if (status != 0)
339 {
340 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
341 }
342 sfree (keys[i]);
343 sfree (key);
344 cache_free (ce);
345 }
346 else if (status == 2) /* persist */
347 {
348 DEBUG ("uc_check_timeout: %s is missing, sending notification.",
349 keys[i]);
350 ce->state = STATE_MISSING;
351 }
352 else if (status == 1) /* do not persist */
353 {
354 if (ce->state == STATE_MISSING)
355 {
356 DEBUG ("uc_check_timeout: %s is missing but "
357 "notification has already been sent.",
358 keys[i]);
359 sfree (keys[i]);
360 }
361 else /* (ce->state != STATE_MISSING) */
362 {
363 DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
364 keys[i]);
365 ce->state = STATE_MISSING;
366 }
367 }
368 else
369 {
370 WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
371 "invalid status %i.",
372 keys[i], status);
373 }
374 } /* for (keys[i]) */
376 c_avl_iterator_destroy (iter);
378 pthread_mutex_unlock (&cache_lock);
380 for (i = 0; i < keys_len; i++)
381 {
382 if (keys[i] == NULL)
383 continue;
385 uc_send_notification (keys[i]);
386 sfree (keys[i]);
387 }
389 sfree (keys);
391 return (0);
392 } /* int uc_check_timeout */
394 int uc_update (const data_set_t *ds, const value_list_t *vl)
395 {
396 char name[6 * DATA_MAX_NAME_LEN];
397 cache_entry_t *ce = NULL;
398 int send_okay_notification = 0;
399 time_t update_delay = 0;
400 notification_t n;
401 int status;
402 int i;
404 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
405 {
406 ERROR ("uc_update: FORMAT_VL failed.");
407 return (-1);
408 }
410 pthread_mutex_lock (&cache_lock);
412 status = c_avl_get (cache_tree, name, (void *) &ce);
413 if (status != 0) /* entry does not yet exist */
414 {
415 status = uc_insert (ds, vl, name);
416 pthread_mutex_unlock (&cache_lock);
417 return (status);
418 }
420 assert (ce != NULL);
421 assert (ce->values_num == ds->ds_num);
423 if (ce->last_time >= vl->time)
424 {
425 pthread_mutex_unlock (&cache_lock);
426 NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
427 "last cache update = %u;",
428 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
429 return (-1);
430 }
432 /* Send a notification (after the lock has been released) if we switch the
433 * state from something else to `okay'. */
434 if (ce->state == STATE_MISSING)
435 {
436 send_okay_notification = 1;
437 ce->state = STATE_OKAY;
438 update_delay = time (NULL) - ce->last_update;
439 }
441 for (i = 0; i < ds->ds_num; i++)
442 {
443 switch (ds->ds[i].type)
444 {
445 case DS_TYPE_COUNTER:
446 {
447 counter_t diff;
449 /* check if the counter has wrapped around */
450 if (vl->values[i].counter < ce->values_raw[i].counter)
451 {
452 if (ce->values_raw[i].counter <= 4294967295U)
453 diff = (4294967295U - ce->values_raw[i].counter)
454 + vl->values[i].counter;
455 else
456 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
457 + vl->values[i].counter;
458 }
459 else /* counter has NOT wrapped around */
460 {
461 diff = vl->values[i].counter - ce->values_raw[i].counter;
462 }
464 ce->values_gauge[i] = ((double) diff)
465 / ((double) (vl->time - ce->last_time));
466 ce->values_raw[i].counter = vl->values[i].counter;
467 }
468 break;
470 case DS_TYPE_GAUGE:
471 ce->values_raw[i].gauge = vl->values[i].gauge;
472 ce->values_gauge[i] = vl->values[i].gauge;
473 break;
475 case DS_TYPE_DERIVE:
476 {
477 derive_t diff;
479 diff = vl->values[i].derive - ce->values_raw[i].derive;
481 ce->values_gauge[i] = ((double) diff)
482 / ((double) (vl->time - ce->last_time));
483 ce->values_raw[i].derive = vl->values[i].derive;
484 }
485 break;
487 case DS_TYPE_ABSOLUTE:
488 ce->values_gauge[i] = ((double) vl->values[i].absolute)
489 / ((double) (vl->time - ce->last_time));
490 ce->values_raw[i].absolute = vl->values[i].absolute;
491 break;
493 default:
494 /* This shouldn't happen. */
495 pthread_mutex_unlock (&cache_lock);
496 ERROR ("uc_update: Don't know how to handle data source type %i.",
497 ds->ds[i].type);
498 return (-1);
499 } /* switch (ds->ds[i].type) */
501 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
502 } /* for (i) */
504 /* Prune invalid gauge data */
505 uc_check_range (ds, ce);
507 ce->last_time = vl->time;
508 ce->last_update = time (NULL);
509 ce->interval = vl->interval;
511 pthread_mutex_unlock (&cache_lock);
513 if (send_okay_notification == 0)
514 return (0);
516 /* Do not send okay notifications for uninteresting values, i. e. values for
517 * which no threshold is configured. */
518 status = ut_check_interesting (name);
519 if (status <= 0)
520 return (0);
522 /* Initialize the notification */
523 memset (&n, '\0', sizeof (n));
524 NOTIFICATION_INIT_VL (&n, vl, ds);
526 n.severity = NOTIF_OKAY;
527 n.time = vl->time;
529 ssnprintf (n.message, sizeof (n.message),
530 "Received a value for %s. It was missing for %u seconds.",
531 name, (unsigned int) update_delay);
533 plugin_dispatch_notification (&n);
535 return (0);
536 } /* int uc_update */
538 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
539 {
540 gauge_t *ret = NULL;
541 size_t ret_num = 0;
542 cache_entry_t *ce = NULL;
543 int status = 0;
545 pthread_mutex_lock (&cache_lock);
547 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
548 {
549 assert (ce != NULL);
551 ret_num = ce->values_num;
552 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
553 if (ret == NULL)
554 {
555 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
556 status = -1;
557 }
558 else
559 {
560 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
561 }
562 }
563 else
564 {
565 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
566 status = -1;
567 }
569 pthread_mutex_unlock (&cache_lock);
571 if (status == 0)
572 {
573 *ret_values = ret;
574 *ret_values_num = ret_num;
575 }
577 return (status);
578 } /* gauge_t *uc_get_rate_by_name */
580 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
581 {
582 char name[6 * DATA_MAX_NAME_LEN];
583 gauge_t *ret = NULL;
584 size_t ret_num = 0;
585 int status;
587 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
588 {
589 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
590 return (NULL);
591 }
593 status = uc_get_rate_by_name (name, &ret, &ret_num);
594 if (status != 0)
595 return (NULL);
597 /* This is important - the caller has no other way of knowing how many
598 * values are returned. */
599 if (ret_num != (size_t) ds->ds_num)
600 {
601 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
602 "but uc_get_rate_by_name returned %zu.",
603 ds->type, ds->ds_num, ret_num);
604 sfree (ret);
605 return (NULL);
606 }
608 return (ret);
609 } /* gauge_t *uc_get_rate */
611 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
612 {
613 c_avl_iterator_t *iter;
614 char *key;
615 cache_entry_t *value;
617 char **names = NULL;
618 time_t *times = NULL;
619 size_t number = 0;
621 int status = 0;
623 if ((ret_names == NULL) || (ret_number == NULL))
624 return (-1);
626 pthread_mutex_lock (&cache_lock);
628 iter = c_avl_get_iterator (cache_tree);
629 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
630 {
631 char **temp;
633 if (ret_times != NULL)
634 {
635 time_t *tmp_times;
637 tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
638 if (tmp_times == NULL)
639 {
640 status = -1;
641 break;
642 }
643 times = tmp_times;
644 times[number] = value->last_time;
645 }
647 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
648 if (temp == NULL)
649 {
650 status = -1;
651 break;
652 }
653 names = temp;
654 names[number] = strdup (key);
655 if (names[number] == NULL)
656 {
657 status = -1;
658 break;
659 }
660 number++;
661 } /* while (c_avl_iterator_next) */
663 c_avl_iterator_destroy (iter);
664 pthread_mutex_unlock (&cache_lock);
666 if (status != 0)
667 {
668 size_t i;
670 for (i = 0; i < number; i++)
671 {
672 sfree (names[i]);
673 }
674 sfree (names);
676 return (-1);
677 }
679 *ret_names = names;
680 if (ret_times != NULL)
681 *ret_times = times;
682 *ret_number = number;
684 return (0);
685 } /* int uc_get_names */
687 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
688 {
689 char name[6 * DATA_MAX_NAME_LEN];
690 cache_entry_t *ce = NULL;
691 int ret = STATE_ERROR;
693 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
694 {
695 ERROR ("uc_get_state: FORMAT_VL failed.");
696 return (STATE_ERROR);
697 }
699 pthread_mutex_lock (&cache_lock);
701 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
702 {
703 assert (ce != NULL);
704 ret = ce->state;
705 }
707 pthread_mutex_unlock (&cache_lock);
709 return (ret);
710 } /* int uc_get_state */
712 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
713 {
714 char name[6 * DATA_MAX_NAME_LEN];
715 cache_entry_t *ce = NULL;
716 int ret = -1;
718 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
719 {
720 ERROR ("uc_get_state: FORMAT_VL failed.");
721 return (STATE_ERROR);
722 }
724 pthread_mutex_lock (&cache_lock);
726 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
727 {
728 assert (ce != NULL);
729 ret = ce->state;
730 ce->state = state;
731 }
733 pthread_mutex_unlock (&cache_lock);
735 return (ret);
736 } /* int uc_set_state */
737 /* vim: set sw=2 ts=8 sts=2 tw=78 : */