1 /**
2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007 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 int uc_init (void)
172 {
173 if (cache_tree == NULL)
174 cache_tree = c_avl_create ((int (*) (const void *, const void *))
175 cache_compare);
177 return (0);
178 } /* int uc_init */
180 int uc_check_timeout (void)
181 {
182 time_t now;
183 cache_entry_t *ce;
185 char **keys = NULL;
186 int keys_len = 0;
188 char *key;
189 c_avl_iterator_t *iter;
190 int i;
192 pthread_mutex_lock (&cache_lock);
194 now = time (NULL);
196 /* Build a list of entries to be flushed */
197 iter = c_avl_get_iterator (cache_tree);
198 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
199 {
200 /* If entry has not been updated, add to `keys' array */
201 if ((now - ce->last_update) >= (2 * ce->interval))
202 {
203 char **tmp;
205 tmp = (char **) realloc ((void *) keys,
206 (keys_len + 1) * sizeof (char *));
207 if (tmp == NULL)
208 {
209 ERROR ("uc_purge: realloc failed.");
210 c_avl_iterator_destroy (iter);
211 return (-1);
212 }
214 keys = tmp;
215 keys[keys_len] = strdup (key);
216 if (keys[keys_len] == NULL)
217 {
218 ERROR ("uc_check_timeout: strdup failed.");
219 continue;
220 }
221 keys_len++;
222 }
223 } /* while (c_avl_iterator_next) */
225 for (i = 0; i < keys_len; i++)
226 {
227 int status;
229 status = ut_check_interesting (keys[i]);
231 if (status < 0)
232 {
233 ERROR ("uc_check_timeout: ut_check_interesting failed.");
234 sfree (keys[i]);
235 }
236 else if (status == 0) /* ``service'' is uninteresting */
237 {
238 ce = NULL;
239 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''", keys[i]);
240 status = c_avl_remove (cache_tree, keys[i], (void *) &key, (void *) &ce);
241 if (status != 0)
242 {
243 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
244 }
245 sfree (keys[i]);
246 cache_free (ce);
247 }
248 else /* (status > 0); ``service'' is interesting */
249 {
250 /*
251 * `keys[i]' is not freed and set to NULL, so that the for-loop below
252 * will send out notifications. There's nothing else to do here.
253 */
254 DEBUG ("uc_check_timeout: %s is missing and ``interesting''", keys[i]);
255 ce->state = STATE_ERROR;
256 }
257 } /* for (keys[i]) */
259 c_avl_iterator_destroy (iter);
261 pthread_mutex_unlock (&cache_lock);
263 for (i = 0; i < keys_len; i++)
264 {
265 if (keys[i] == NULL)
266 continue;
268 uc_send_notification (keys[i]);
269 sfree (keys[i]);
270 }
272 sfree (keys);
274 return (0);
275 } /* int uc_check_timeout */
277 int uc_update (const data_set_t *ds, const value_list_t *vl)
278 {
279 char name[6 * DATA_MAX_NAME_LEN];
280 cache_entry_t *ce = NULL;
281 int send_okay_notification = 0;
283 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
284 {
285 ERROR ("uc_insert: FORMAT_VL failed.");
286 return (-1);
287 }
289 pthread_mutex_lock (&cache_lock);
291 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
292 {
293 int i;
295 assert (ce != NULL);
296 assert (ce->values_num == ds->ds_num);
298 if (ce->last_time >= vl->time)
299 {
300 pthread_mutex_unlock (&cache_lock);
301 NOTICE ("uc_insert: Value too old: name = %s; value time = %u; "
302 "last cache update = %u;",
303 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
304 return (-1);
305 }
307 if ((ce->last_time + ce->interval) < vl->time)
308 {
309 send_okay_notification = vl->time - ce->last_time;
310 ce->state = STATE_OKAY;
311 }
313 for (i = 0; i < ds->ds_num; i++)
314 {
315 if (ds->ds[i].type == DS_TYPE_COUNTER)
316 {
317 counter_t diff;
319 /* check if the counter has wrapped around */
320 if (vl->values[i].counter < ce->values_counter[i])
321 {
322 if (ce->values_counter[i] <= 4294967295U)
323 diff = (4294967295U - ce->values_counter[i])
324 + vl->values[i].counter;
325 else
326 diff = (18446744073709551615ULL - ce->values_counter[i])
327 + vl->values[i].counter;
328 }
329 else /* counter has NOT wrapped around */
330 {
331 diff = vl->values[i].counter - ce->values_counter[i];
332 }
334 ce->values_gauge[i] = ((double) diff)
335 / ((double) (vl->time - ce->last_time));
336 ce->values_counter[i] = vl->values[i].counter;
337 }
338 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
339 {
340 ce->values_gauge[i] = vl->values[i].gauge;
341 }
342 DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
343 } /* for (i) */
345 ce->last_time = vl->time;
346 ce->last_update = time (NULL);
347 ce->interval = vl->interval;
348 }
349 else /* key is not found */
350 {
351 int i;
352 char *key;
354 key = strdup (name);
355 if (key == NULL)
356 {
357 pthread_mutex_unlock (&cache_lock);
358 ERROR ("uc_insert: strdup failed.");
359 return (-1);
360 }
362 ce = cache_alloc (ds->ds_num);
363 if (ce == NULL)
364 {
365 pthread_mutex_unlock (&cache_lock);
366 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
367 return (-1);
368 }
370 sstrncpy (ce->name, name, sizeof (ce->name));
372 for (i = 0; i < ds->ds_num; i++)
373 {
374 if (ds->ds[i].type == DS_TYPE_COUNTER)
375 {
376 ce->values_gauge[i] = NAN;
377 ce->values_counter[i] = vl->values[i].counter;
378 }
379 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
380 {
381 ce->values_gauge[i] = vl->values[i].gauge;
382 }
383 } /* for (i) */
385 ce->last_time = vl->time;
386 ce->last_update = time (NULL);
387 ce->interval = vl->interval;
388 ce->state = STATE_OKAY;
390 if (c_avl_insert (cache_tree, key, ce) != 0)
391 {
392 pthread_mutex_unlock (&cache_lock);
393 ERROR ("uc_insert: c_avl_insert failed.");
394 return (-1);
395 }
397 DEBUG ("uc_insert: Added %s to the cache.", name);
398 } /* if (key is not found) */
400 pthread_mutex_unlock (&cache_lock);
402 /* Do not send okay notifications for uninteresting values, i. e. values for
403 * which no threshold is configured. */
404 if (send_okay_notification > 0)
405 {
406 int status;
408 status = ut_check_interesting (name);
409 if (status <= 0)
410 send_okay_notification = 0;
411 }
413 if (send_okay_notification > 0)
414 {
415 notification_t n;
416 memset (&n, '\0', sizeof (n));
418 /* Copy the associative members */
419 NOTIFICATION_INIT_VL (&n, vl, ds);
421 n.severity = NOTIF_OKAY;
422 n.time = vl->time;
424 snprintf (n.message, sizeof (n.message),
425 "Received a value for %s. It was missing for %i seconds.",
426 name, send_okay_notification);
427 n.message[sizeof (n.message) - 1] = '\0';
429 plugin_dispatch_notification (&n);
430 }
432 return (0);
433 } /* int uc_insert */
435 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
436 {
437 gauge_t *ret = NULL;
438 size_t ret_num = 0;
439 cache_entry_t *ce = NULL;
440 int status = 0;
442 pthread_mutex_lock (&cache_lock);
444 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
445 {
446 assert (ce != NULL);
448 ret_num = ce->values_num;
449 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
450 if (ret == NULL)
451 {
452 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
453 status = -1;
454 }
455 else
456 {
457 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
458 }
459 }
460 else
461 {
462 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
463 status = -1;
464 }
466 pthread_mutex_unlock (&cache_lock);
468 if (status == 0)
469 {
470 *ret_values = ret;
471 *ret_values_num = ret_num;
472 }
474 return (status);
475 } /* gauge_t *uc_get_rate_by_name */
477 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
478 {
479 char name[6 * DATA_MAX_NAME_LEN];
480 gauge_t *ret = NULL;
481 size_t ret_num = 0;
482 int status;
484 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
485 {
486 ERROR ("uc_insert: FORMAT_VL failed.");
487 return (NULL);
488 }
490 status = uc_get_rate_by_name (name, &ret, &ret_num);
491 if (status != 0)
492 return (NULL);
494 /* This is important - the caller has no other way of knowing how many
495 * values are returned. */
496 if (ret_num != ds->ds_num)
497 {
498 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
499 "but uc_get_rate_by_name returned %i.",
500 ds->type, ds->ds_num, ret_num);
501 sfree (ret);
502 return (NULL);
503 }
505 return (ret);
506 } /* gauge_t *uc_get_rate */
508 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
509 {
510 char name[6 * DATA_MAX_NAME_LEN];
511 cache_entry_t *ce = NULL;
512 int ret = STATE_ERROR;
514 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
515 {
516 ERROR ("uc_get_state: FORMAT_VL failed.");
517 return (STATE_ERROR);
518 }
520 pthread_mutex_lock (&cache_lock);
522 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
523 {
524 assert (ce != NULL);
525 ret = ce->state;
526 }
528 pthread_mutex_unlock (&cache_lock);
530 return (ret);
531 } /* int uc_get_state */
533 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
534 {
535 char name[6 * DATA_MAX_NAME_LEN];
536 cache_entry_t *ce = NULL;
537 int ret = -1;
539 if (state < STATE_OKAY)
540 state = STATE_OKAY;
541 if (state > STATE_ERROR)
542 state = STATE_ERROR;
544 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
545 {
546 ERROR ("uc_get_state: FORMAT_VL failed.");
547 return (STATE_ERROR);
548 }
550 pthread_mutex_lock (&cache_lock);
552 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
553 {
554 assert (ce != NULL);
555 ret = ce->state;
556 ce->state = state;
557 }
559 pthread_mutex_unlock (&cache_lock);
561 return (ret);
562 } /* int uc_set_state */
563 /* vim: set sw=2 ts=8 sts=2 tw=78 : */