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 int uc_send_notification (const char *name)
60 {
61 cache_entry_t *ce = NULL;
62 int status;
64 char *name_copy;
65 char *host;
66 char *plugin;
67 char *plugin_instance;
68 char *type;
69 char *type_instance;
71 notification_t n;
73 name_copy = strdup (name);
74 if (name_copy == NULL)
75 {
76 ERROR ("uc_send_notification: strdup failed.");
77 return (-1);
78 }
80 status = parse_identifier (name_copy, &host,
81 &plugin, &plugin_instance,
82 &type, &type_instance);
83 if (status != 0)
84 {
85 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
86 return (-1);
87 }
89 /* Copy the associative members */
90 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
91 host, plugin, plugin_instance, type, type_instance);
93 sfree (name_copy);
94 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
96 pthread_mutex_lock (&cache_lock);
98 /*
99 * Set the time _after_ getting the lock because we don't know how long
100 * acquiring the lock takes and we will use this time later to decide
101 * whether or not the state is OKAY.
102 */
103 n.time = time (NULL);
105 status = c_avl_get (cache_tree, name, (void *) &ce);
106 if (status != 0)
107 {
108 pthread_mutex_unlock (&cache_lock);
109 sfree (name_copy);
110 return (-1);
111 }
113 /* Check if the entry has been updated in the meantime */
114 if ((n.time - ce->last_update) < (2 * ce->interval))
115 {
116 ce->state = STATE_OKAY;
117 pthread_mutex_unlock (&cache_lock);
118 sfree (name_copy);
119 return (-1);
120 }
122 snprintf (n.message, sizeof (n.message),
123 "%s has not been updated for %i seconds.", name,
124 (int) (n.time - ce->last_update));
126 pthread_mutex_unlock (&cache_lock);
128 n.message[sizeof (n.message) - 1] = '\0';
129 plugin_dispatch_notification (&n);
131 return (0);
132 } /* int uc_send_notification */
134 int uc_init (void)
135 {
136 if (cache_tree == NULL)
137 cache_tree = c_avl_create ((int (*) (const void *, const void *))
138 cache_compare);
140 return (0);
141 } /* int uc_init */
143 int uc_check_timeout (void)
144 {
145 time_t now;
146 cache_entry_t *ce;
148 char **keys = NULL;
149 int keys_len = 0;
151 char *key;
152 c_avl_iterator_t *iter;
153 int i;
155 pthread_mutex_lock (&cache_lock);
157 now = time (NULL);
159 /* Build a list of entries to be flushed */
160 iter = c_avl_get_iterator (cache_tree);
161 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
162 {
163 /* If entry has not been updated, add to `keys' array */
164 if ((now - ce->last_update) >= (2 * ce->interval))
165 {
166 char **tmp;
168 tmp = (char **) realloc ((void *) keys,
169 (keys_len + 1) * sizeof (char *));
170 if (tmp == NULL)
171 {
172 ERROR ("uc_purge: realloc failed.");
173 c_avl_iterator_destroy (iter);
174 return (-1);
175 }
177 keys = tmp;
178 keys[keys_len] = strdup (key);
179 if (keys[keys_len] == NULL)
180 {
181 ERROR ("uc_check_timeout: strdup failed.");
182 continue;
183 }
184 keys_len++;
185 }
186 } /* while (c_avl_iterator_next) */
188 for (i = 0; i < keys_len; i++)
189 {
190 int status;
192 status = ut_check_interesting (keys[i]);
194 if (status < 0)
195 {
196 ERROR ("uc_check_timeout: ut_check_interesting failed.");
197 sfree (keys[i]);
198 }
199 else if (status == 0) /* ``service'' is uninteresting */
200 {
201 ce = NULL;
202 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''", keys[i]);
203 status = c_avl_remove (cache_tree, keys[i], (void *) &key, (void *) &ce);
204 if (status != 0)
205 {
206 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
207 }
208 sfree (keys[i]);
209 sfree (ce);
210 }
211 else /* (status > 0); ``service'' is interesting */
212 {
213 /*
214 * `keys[i]' is not freed and set to NULL, so that the for-loop below
215 * will send out notifications. There's nothing else to do here.
216 */
217 DEBUG ("uc_check_timeout: %s is missing and ``interesting''", keys[i]);
218 ce->state = STATE_ERROR;
219 }
220 } /* for (keys[i]) */
222 c_avl_iterator_destroy (iter);
224 pthread_mutex_unlock (&cache_lock);
226 for (i = 0; i < keys_len; i++)
227 {
228 if (keys[i] == NULL)
229 continue;
231 uc_send_notification (keys[i]);
232 sfree (keys[i]);
233 }
235 sfree (keys);
237 return (0);
238 } /* int uc_check_timeout */
240 int uc_update (const data_set_t *ds, const value_list_t *vl)
241 {
242 char name[6 * DATA_MAX_NAME_LEN];
243 cache_entry_t *ce = NULL;
244 int send_okay_notification = 0;
246 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
247 {
248 ERROR ("uc_insert: FORMAT_VL failed.");
249 return (-1);
250 }
252 pthread_mutex_lock (&cache_lock);
254 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
255 {
256 int i;
258 assert (ce != NULL);
259 assert (ce->values_num == ds->ds_num);
261 if (ce->last_time >= vl->time)
262 {
263 pthread_mutex_unlock (&cache_lock);
264 NOTICE ("uc_insert: Value too old: name = %s; value time = %u; "
265 "last cache update = %u;",
266 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
267 return (-1);
268 }
270 if ((ce->last_time + ce->interval) < vl->time)
271 {
272 send_okay_notification = vl->time - ce->last_time;
273 ce->state = STATE_OKAY;
274 }
276 for (i = 0; i < ds->ds_num; i++)
277 {
278 if (ds->ds[i].type == DS_TYPE_COUNTER)
279 {
280 counter_t diff;
282 /* check if the counter has wrapped around */
283 if (vl->values[i].counter < ce->values_counter[i])
284 {
285 if (ce->values_counter[i] <= 4294967295U)
286 diff = (4294967295U - ce->values_counter[i])
287 + vl->values[i].counter;
288 else
289 diff = (18446744073709551615ULL - ce->values_counter[i])
290 + vl->values[i].counter;
291 }
292 else /* counter has NOT wrapped around */
293 {
294 diff = vl->values[i].counter - ce->values_counter[i];
295 }
297 ce->values_gauge[i] = ((double) diff)
298 / ((double) (vl->time - ce->last_time));
299 ce->values_counter[i] = vl->values[i].counter;
300 }
301 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
302 {
303 ce->values_gauge[i] = vl->values[i].gauge;
304 }
305 DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
306 } /* for (i) */
308 ce->last_time = vl->time;
309 ce->last_update = time (NULL);
310 ce->interval = vl->interval;
311 }
312 else /* key is not found */
313 {
314 int i;
315 size_t ce_size = sizeof (cache_entry_t)
316 + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
317 char *key;
319 key = strdup (name);
320 if (key == NULL)
321 {
322 pthread_mutex_unlock (&cache_lock);
323 ERROR ("uc_insert: strdup failed.");
324 return (-1);
325 }
327 ce = (cache_entry_t *) malloc (ce_size);
328 if (ce == NULL)
329 {
330 pthread_mutex_unlock (&cache_lock);
331 ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
332 return (-1);
333 }
335 memset (ce, '\0', ce_size);
337 strncpy (ce->name, name, sizeof (ce->name));
338 ce->name[sizeof (ce->name) - 1] = '\0';
340 ce->values_num = ds->ds_num;
341 ce->values_gauge = (gauge_t *) (ce + 1);
342 ce->values_counter = (counter_t *) (ce->values_gauge + ce->values_num);
344 for (i = 0; i < ds->ds_num; i++)
345 {
346 if (ds->ds[i].type == DS_TYPE_COUNTER)
347 {
348 ce->values_gauge[i] = NAN;
349 ce->values_counter[i] = vl->values[i].counter;
350 }
351 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
352 {
353 ce->values_gauge[i] = vl->values[i].gauge;
354 }
355 } /* for (i) */
357 ce->last_time = vl->time;
358 ce->last_update = time (NULL);
359 ce->interval = vl->interval;
360 ce->state = STATE_OKAY;
362 if (c_avl_insert (cache_tree, key, ce) != 0)
363 {
364 pthread_mutex_unlock (&cache_lock);
365 ERROR ("uc_insert: c_avl_insert failed.");
366 return (-1);
367 }
369 DEBUG ("uc_insert: Added %s to the cache.", name);
370 } /* if (key is not found) */
372 pthread_mutex_unlock (&cache_lock);
374 /* Do not send okay notifications for uninteresting values, i. e. values for
375 * which no threshold is configured. */
376 if (send_okay_notification > 0)
377 {
378 int status;
380 status = ut_check_interesting (name);
381 if (status <= 0)
382 send_okay_notification = 0;
383 }
385 if (send_okay_notification > 0)
386 {
387 notification_t n;
388 memset (&n, '\0', sizeof (n));
390 /* Copy the associative members */
391 NOTIFICATION_INIT_VL (&n, vl, ds);
393 n.severity = NOTIF_OKAY;
394 n.time = vl->time;
396 snprintf (n.message, sizeof (n.message),
397 "Received a value for %s. It was missing for %i seconds.",
398 name, send_okay_notification);
399 n.message[sizeof (n.message) - 1] = '\0';
401 plugin_dispatch_notification (&n);
402 }
404 return (0);
405 } /* int uc_insert */
407 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
408 {
409 gauge_t *ret = NULL;
410 size_t ret_num = 0;
411 cache_entry_t *ce = NULL;
412 int status = 0;
414 pthread_mutex_lock (&cache_lock);
416 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
417 {
418 assert (ce != NULL);
420 ret_num = ce->values_num;
421 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
422 if (ret == NULL)
423 {
424 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
425 status = -1;
426 }
427 else
428 {
429 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
430 }
431 }
432 else
433 {
434 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
435 status = -1;
436 }
438 pthread_mutex_unlock (&cache_lock);
440 if (status == 0)
441 {
442 *ret_values = ret;
443 *ret_values_num = ret_num;
444 }
446 return (status);
447 } /* gauge_t *uc_get_rate_by_name */
449 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
450 {
451 char name[6 * DATA_MAX_NAME_LEN];
452 gauge_t *ret = NULL;
453 size_t ret_num = 0;
454 int status;
456 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
457 {
458 ERROR ("uc_insert: FORMAT_VL failed.");
459 return (NULL);
460 }
462 status = uc_get_rate_by_name (name, &ret, &ret_num);
463 if (status != 0)
464 return (NULL);
466 /* This is important - the caller has no other way of knowing how many
467 * values are returned. */
468 if (ret_num != ds->ds_num)
469 {
470 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
471 "but uc_get_rate_by_name returned %i.",
472 ds->type, ds->ds_num, ret_num);
473 sfree (ret);
474 return (NULL);
475 }
477 return (ret);
478 } /* gauge_t *uc_get_rate */
480 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
481 {
482 char name[6 * DATA_MAX_NAME_LEN];
483 cache_entry_t *ce = NULL;
484 int ret = STATE_ERROR;
486 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
487 {
488 ERROR ("uc_get_state: FORMAT_VL failed.");
489 return (STATE_ERROR);
490 }
492 pthread_mutex_lock (&cache_lock);
494 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
495 {
496 assert (ce != NULL);
497 ret = ce->state;
498 }
500 pthread_mutex_unlock (&cache_lock);
502 return (ret);
503 } /* int uc_get_state */
505 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
506 {
507 char name[6 * DATA_MAX_NAME_LEN];
508 cache_entry_t *ce = NULL;
509 int ret = -1;
511 if (state < STATE_OKAY)
512 state = STATE_OKAY;
513 if (state > STATE_ERROR)
514 state = STATE_ERROR;
516 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
517 {
518 ERROR ("uc_get_state: FORMAT_VL failed.");
519 return (STATE_ERROR);
520 }
522 pthread_mutex_lock (&cache_lock);
524 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
525 {
526 assert (ce != NULL);
527 ret = ce->state;
528 ce->state = state;
529 }
531 pthread_mutex_unlock (&cache_lock);
533 return (ret);
534 } /* int uc_set_state */
535 /* vim: set sw=2 ts=8 sts=2 tw=78 : */