6779bba86c829ddd25bb9e3b0802bf7d7f0a43e9
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 counter_t *values_counter;
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 = (gauge_t *) calloc (values_num, sizeof (gauge_t));
73 ce->values_counter = (counter_t *) calloc (values_num, sizeof (counter_t));
74 if ((ce->values_gauge == NULL) || (ce->values_counter == NULL))
75 {
76 sfree (ce->values_gauge);
77 sfree (ce->values_counter);
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_counter);
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 snprintf (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 n.message[sizeof (n.message) - 1] = '\0';
166 plugin_dispatch_notification (&n);
168 return (0);
169 } /* int uc_send_notification */
171 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
172 const char *key)
173 {
174 int i;
175 char *key_copy;
176 cache_entry_t *ce;
178 /* `cache_lock' has been locked by `uc_update' */
180 key_copy = strdup (key);
181 if (key_copy == NULL)
182 {
183 ERROR ("uc_insert: strdup failed.");
184 return (-1);
185 }
187 ce = cache_alloc (ds->ds_num);
188 if (ce == NULL)
189 {
190 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
191 return (-1);
192 }
194 sstrncpy (ce->name, key, sizeof (ce->name));
196 for (i = 0; i < ds->ds_num; i++)
197 {
198 if (ds->ds[i].type == DS_TYPE_COUNTER)
199 {
200 ce->values_gauge[i] = NAN;
201 ce->values_counter[i] = vl->values[i].counter;
202 }
203 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
204 {
205 ce->values_gauge[i] = vl->values[i].gauge;
206 }
207 } /* for (i) */
209 ce->last_time = vl->time;
210 ce->last_update = time (NULL);
211 ce->interval = vl->interval;
212 ce->state = STATE_OKAY;
214 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
215 {
216 sfree (key_copy);
217 ERROR ("uc_insert: c_avl_insert failed.");
218 return (-1);
219 }
221 DEBUG ("uc_insert: Added %s to the cache.", key);
222 return (0);
223 } /* int uc_insert */
225 int uc_init (void)
226 {
227 if (cache_tree == NULL)
228 cache_tree = c_avl_create ((int (*) (const void *, const void *))
229 cache_compare);
231 return (0);
232 } /* int uc_init */
234 int uc_check_timeout (void)
235 {
236 time_t now;
237 cache_entry_t *ce;
239 char **keys = NULL;
240 int keys_len = 0;
242 char *key;
243 c_avl_iterator_t *iter;
244 int i;
246 pthread_mutex_lock (&cache_lock);
248 now = time (NULL);
250 /* Build a list of entries to be flushed */
251 iter = c_avl_get_iterator (cache_tree);
252 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
253 {
254 /* If entry has not been updated, add to `keys' array */
255 if ((now - ce->last_update) >= (2 * ce->interval))
256 {
257 char **tmp;
259 tmp = (char **) realloc ((void *) keys,
260 (keys_len + 1) * sizeof (char *));
261 if (tmp == NULL)
262 {
263 ERROR ("uc_purge: realloc failed.");
264 c_avl_iterator_destroy (iter);
265 pthread_mutex_unlock (&cache_lock);
266 return (-1);
267 }
269 keys = tmp;
270 keys[keys_len] = strdup (key);
271 if (keys[keys_len] == NULL)
272 {
273 ERROR ("uc_check_timeout: strdup failed.");
274 continue;
275 }
276 keys_len++;
277 }
278 } /* while (c_avl_iterator_next) */
280 for (i = 0; i < keys_len; i++)
281 {
282 int status;
284 status = ut_check_interesting (keys[i]);
286 if (status < 0)
287 {
288 ERROR ("uc_check_timeout: ut_check_interesting failed.");
289 sfree (keys[i]);
290 }
291 else if (status == 0) /* ``service'' is uninteresting */
292 {
293 ce = NULL;
294 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
295 keys[i]);
296 status = c_avl_remove (cache_tree, keys[i],
297 (void *) &key, (void *) &ce);
298 if (status != 0)
299 {
300 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
301 }
302 sfree (keys[i]);
303 sfree (key);
304 cache_free (ce);
305 }
306 else if (status == 1) /* persist */
307 {
308 DEBUG ("uc_check_timeout: %s is missing, sending notification.",
309 keys[i]);
310 ce->state = STATE_MISSING;
311 }
312 else if (status == 2) /* do not persist */
313 {
314 if (ce->state == STATE_MISSING)
315 {
316 DEBUG ("uc_check_timeout: %s is missing but "
317 "notification has already been sent.",
318 keys[i]);
319 sfree (keys[i]);
320 }
321 else /* (ce->state != STATE_MISSING) */
322 {
323 DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
324 keys[i]);
325 ce->state = STATE_MISSING;
326 }
327 }
328 else
329 {
330 WARNING ("uc_check_timeout: ut_check_interesting (%s) returned ",
331 "invalid status %i.",
332 keys[i], status);
333 }
334 } /* for (keys[i]) */
336 c_avl_iterator_destroy (iter);
338 pthread_mutex_unlock (&cache_lock);
340 for (i = 0; i < keys_len; i++)
341 {
342 if (keys[i] == NULL)
343 continue;
345 uc_send_notification (keys[i]);
346 sfree (keys[i]);
347 }
349 sfree (keys);
351 return (0);
352 } /* int uc_check_timeout */
354 int uc_update (const data_set_t *ds, const value_list_t *vl)
355 {
356 char name[6 * DATA_MAX_NAME_LEN];
357 cache_entry_t *ce = NULL;
358 int send_okay_notification = 0;
359 time_t update_delay = 0;
360 notification_t n;
361 int status;
362 int i;
364 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
365 {
366 ERROR ("uc_update: FORMAT_VL failed.");
367 return (-1);
368 }
370 pthread_mutex_lock (&cache_lock);
372 status = c_avl_get (cache_tree, name, (void *) &ce);
373 if (status != 0) /* entry does not yet exist */
374 {
375 status = uc_insert (ds, vl, name);
376 pthread_mutex_unlock (&cache_lock);
377 return (status);
378 }
380 assert (ce != NULL);
381 assert (ce->values_num == ds->ds_num);
383 if (ce->last_time >= vl->time)
384 {
385 pthread_mutex_unlock (&cache_lock);
386 NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
387 "last cache update = %u;",
388 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
389 return (-1);
390 }
392 /* Send a notification (after the lock has been released) if we switch the
393 * state from something else to `okay'. */
394 if (ce->state == STATE_MISSING)
395 {
396 send_okay_notification = 1;
397 ce->state = STATE_OKAY;
398 update_delay = time (NULL) - ce->last_update;
399 }
401 for (i = 0; i < ds->ds_num; i++)
402 {
403 if (ds->ds[i].type == DS_TYPE_COUNTER)
404 {
405 counter_t diff;
407 /* check if the counter has wrapped around */
408 if (vl->values[i].counter < ce->values_counter[i])
409 {
410 if (ce->values_counter[i] <= 4294967295U)
411 diff = (4294967295U - ce->values_counter[i])
412 + vl->values[i].counter;
413 else
414 diff = (18446744073709551615ULL - ce->values_counter[i])
415 + vl->values[i].counter;
416 }
417 else /* counter has NOT wrapped around */
418 {
419 diff = vl->values[i].counter - ce->values_counter[i];
420 }
422 ce->values_gauge[i] = ((double) diff)
423 / ((double) (vl->time - ce->last_time));
424 ce->values_counter[i] = vl->values[i].counter;
425 }
426 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
427 {
428 ce->values_gauge[i] = vl->values[i].gauge;
429 }
430 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
431 } /* for (i) */
433 ce->last_time = vl->time;
434 ce->last_update = time (NULL);
435 ce->interval = vl->interval;
437 pthread_mutex_unlock (&cache_lock);
439 if (send_okay_notification == 0)
440 return (0);
442 /* Do not send okay notifications for uninteresting values, i. e. values for
443 * which no threshold is configured. */
444 status = ut_check_interesting (name);
445 if (status <= 0)
446 return (0);
448 /* Initialize the notification */
449 memset (&n, '\0', sizeof (n));
450 NOTIFICATION_INIT_VL (&n, vl, ds);
452 n.severity = NOTIF_OKAY;
453 n.time = vl->time;
455 snprintf (n.message, sizeof (n.message),
456 "Received a value for %s. It was missing for %u seconds.",
457 name, (unsigned int) update_delay);
458 n.message[sizeof (n.message) - 1] = '\0';
460 plugin_dispatch_notification (&n);
462 return (0);
463 } /* int uc_update */
465 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
466 {
467 gauge_t *ret = NULL;
468 size_t ret_num = 0;
469 cache_entry_t *ce = NULL;
470 int status = 0;
472 pthread_mutex_lock (&cache_lock);
474 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
475 {
476 assert (ce != NULL);
478 ret_num = ce->values_num;
479 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
480 if (ret == NULL)
481 {
482 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
483 status = -1;
484 }
485 else
486 {
487 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
488 }
489 }
490 else
491 {
492 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
493 status = -1;
494 }
496 pthread_mutex_unlock (&cache_lock);
498 if (status == 0)
499 {
500 *ret_values = ret;
501 *ret_values_num = ret_num;
502 }
504 return (status);
505 } /* gauge_t *uc_get_rate_by_name */
507 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
508 {
509 char name[6 * DATA_MAX_NAME_LEN];
510 gauge_t *ret = NULL;
511 size_t ret_num = 0;
512 int status;
514 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
515 {
516 ERROR ("uc_insert: FORMAT_VL failed.");
517 return (NULL);
518 }
520 status = uc_get_rate_by_name (name, &ret, &ret_num);
521 if (status != 0)
522 return (NULL);
524 /* This is important - the caller has no other way of knowing how many
525 * values are returned. */
526 if (ret_num != ds->ds_num)
527 {
528 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
529 "but uc_get_rate_by_name returned %i.",
530 ds->type, ds->ds_num, ret_num);
531 sfree (ret);
532 return (NULL);
533 }
535 return (ret);
536 } /* gauge_t *uc_get_rate */
538 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
539 {
540 c_avl_iterator_t *iter;
541 char *key;
542 cache_entry_t *value;
544 char **names = NULL;
545 time_t *times = NULL;
546 size_t number = 0;
548 int status = 0;
550 if ((ret_names == NULL) || (ret_number == NULL))
551 return (-1);
553 pthread_mutex_lock (&cache_lock);
555 iter = c_avl_get_iterator (cache_tree);
556 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
557 {
558 char **temp;
560 if (ret_times != NULL)
561 {
562 time_t *tmp_times;
564 tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
565 if (tmp_times == NULL)
566 {
567 status = -1;
568 break;
569 }
570 times = tmp_times;
571 times[number] = value->last_time;
572 }
574 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
575 if (temp == NULL)
576 {
577 status = -1;
578 break;
579 }
580 names = temp;
581 names[number] = strdup (key);
582 if (names[number] == NULL)
583 {
584 status = -1;
585 break;
586 }
587 number++;
588 } /* while (c_avl_iterator_next) */
590 c_avl_iterator_destroy (iter);
591 pthread_mutex_unlock (&cache_lock);
593 if (status != 0)
594 {
595 size_t i;
597 for (i = 0; i < number; i++)
598 {
599 sfree (names[i]);
600 }
601 sfree (names);
603 return (-1);
604 }
606 *ret_names = names;
607 if (ret_times != NULL)
608 *ret_times = times;
609 *ret_number = number;
611 return (0);
612 } /* int uc_get_names */
614 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
615 {
616 char name[6 * DATA_MAX_NAME_LEN];
617 cache_entry_t *ce = NULL;
618 int ret = STATE_ERROR;
620 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
621 {
622 ERROR ("uc_get_state: FORMAT_VL failed.");
623 return (STATE_ERROR);
624 }
626 pthread_mutex_lock (&cache_lock);
628 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
629 {
630 assert (ce != NULL);
631 ret = ce->state;
632 }
634 pthread_mutex_unlock (&cache_lock);
636 return (ret);
637 } /* int uc_get_state */
639 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
640 {
641 char name[6 * DATA_MAX_NAME_LEN];
642 cache_entry_t *ce = NULL;
643 int ret = -1;
645 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
646 {
647 ERROR ("uc_get_state: FORMAT_VL failed.");
648 return (STATE_ERROR);
649 }
651 pthread_mutex_lock (&cache_lock);
653 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
654 {
655 assert (ce != NULL);
656 ret = ce->state;
657 ce->state = state;
658 }
660 pthread_mutex_unlock (&cache_lock);
662 return (ret);
663 } /* int uc_set_state */
664 /* vim: set sw=2 ts=8 sts=2 tw=78 : */