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 memset (&n, '\0', sizeof (n));
75 name_copy = strdup (name);
76 if (name_copy == NULL)
77 {
78 ERROR ("uc_send_notification: strdup failed.");
79 return (-1);
80 }
82 status = parse_identifier (name_copy, &host,
83 &plugin, &plugin_instance,
84 &type, &type_instance);
85 if (status != 0)
86 {
87 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
88 return (-1);
89 }
91 n.severity = NOTIF_FAILURE;
92 strncpy (n.host, host, sizeof (n.host));
93 n.host[sizeof (n.host) - 1] = '\0';
95 sfree (name_copy);
96 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
98 pthread_mutex_lock (&cache_lock);
100 n.time = time (NULL);
102 status = c_avl_get (cache_tree, name, (void *) &ce);
103 if (status != 0)
104 {
105 pthread_mutex_unlock (&cache_lock);
106 sfree (name_copy);
107 return (-1);
108 }
110 /* Check if the entry has been updated in the meantime */
111 if ((n.time - ce->last_update) < (2 * ce->interval))
112 {
113 ce->state = STATE_OKAY;
114 pthread_mutex_unlock (&cache_lock);
115 sfree (name_copy);
116 return (-1);
117 }
119 snprintf (n.message, sizeof (n.message),
120 "%s has not been updated for %i seconds.", name,
121 (int) (n.time - ce->last_update));
123 pthread_mutex_unlock (&cache_lock);
125 n.message[sizeof (n.message) - 1] = '\0';
126 plugin_dispatch_notification (&n);
128 return (0);
129 } /* int uc_send_notification */
131 int uc_init (void)
132 {
133 if (cache_tree == NULL)
134 cache_tree = c_avl_create ((int (*) (const void *, const void *))
135 cache_compare);
137 return (0);
138 } /* int uc_init */
140 int uc_check_timeout (void)
141 {
142 time_t now;
143 cache_entry_t *ce;
145 char **keys = NULL;
146 int keys_len = 0;
148 char *key;
149 c_avl_iterator_t *iter;
150 int i;
152 pthread_mutex_lock (&cache_lock);
154 now = time (NULL);
156 /* Build a list of entries to be flushed */
157 iter = c_avl_get_iterator (cache_tree);
158 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
159 {
160 /* If entry has not been updated, add to `keys' array */
161 if ((now - ce->last_update) >= (2 * ce->interval))
162 {
163 char **tmp;
165 tmp = (char **) realloc ((void *) keys,
166 (keys_len + 1) * sizeof (char *));
167 if (tmp == NULL)
168 {
169 ERROR ("uc_purge: realloc failed.");
170 c_avl_iterator_destroy (iter);
171 return (-1);
172 }
174 keys = tmp;
175 keys[keys_len] = strdup (key);
176 if (keys[keys_len] == NULL)
177 {
178 ERROR ("uc_check_timeout: strdup failed.");
179 continue;
180 }
181 keys_len++;
182 }
183 } /* while (c_avl_iterator_next) */
185 for (i = 0; i < keys_len; i++)
186 {
187 int status;
189 status = ut_check_interesting (keys[i]);
191 if (status < 0)
192 {
193 ERROR ("uc_check_timeout: ut_check_interesting failed.");
194 sfree (keys[i]);
195 }
196 else if (status == 0) /* ``service'' is uninteresting */
197 {
198 ce = NULL;
199 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''", keys[i]);
200 status = c_avl_remove (cache_tree, keys[i], (void *) &key, (void *) &ce);
201 if (status != 0)
202 {
203 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
204 }
205 sfree (keys[i]);
206 sfree (ce);
207 }
208 else /* (status > 0); ``service'' is interesting */
209 {
210 /*
211 * `keys[i]' is not freed and set to NULL, so that the for-loop below
212 * will send out notifications. There's nothing else to do here.
213 */
214 DEBUG ("uc_check_timeout: %s is missing and ``interesting''", keys[i]);
215 ce->state = STATE_ERROR;
216 }
217 } /* for (keys[i]) */
219 c_avl_iterator_destroy (iter);
221 pthread_mutex_unlock (&cache_lock);
223 for (i = 0; i < keys_len; i++)
224 {
225 if (keys[i] == NULL)
226 continue;
228 uc_send_notification (keys[i]);
229 sfree (keys[i]);
230 }
232 sfree (keys);
234 return (0);
235 } /* int uc_check_timeout */
237 int uc_update (const data_set_t *ds, const value_list_t *vl)
238 {
239 char name[6 * DATA_MAX_NAME_LEN];
240 cache_entry_t *ce = NULL;
241 int send_okay_notification = 0;
243 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
244 {
245 ERROR ("uc_insert: FORMAT_VL failed.");
246 return (-1);
247 }
249 pthread_mutex_lock (&cache_lock);
251 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
252 {
253 int i;
255 assert (ce != NULL);
256 assert (ce->values_num == ds->ds_num);
258 if (ce->last_time >= vl->time)
259 {
260 pthread_mutex_unlock (&cache_lock);
261 NOTICE ("uc_insert: Value too old: name = %s; value time = %u; "
262 "last cache update = %u;",
263 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
264 return (-1);
265 }
267 if ((ce->last_time + ce->interval) < vl->time)
268 {
269 send_okay_notification = vl->time - ce->last_time;
270 ce->state = STATE_OKAY;
271 }
273 for (i = 0; i < ds->ds_num; i++)
274 {
275 if (ds->ds[i].type == DS_TYPE_COUNTER)
276 {
277 counter_t diff;
279 /* check if the counter has wrapped around */
280 if (vl->values[i].counter < ce->values_counter[i])
281 {
282 if (ce->values_counter[i] <= 4294967295U)
283 diff = (4294967295U - ce->values_counter[i])
284 + vl->values[i].counter;
285 else
286 diff = (18446744073709551615ULL - ce->values_counter[i])
287 + vl->values[i].counter;
288 }
289 else /* counter has NOT wrapped around */
290 {
291 diff = vl->values[i].counter - ce->values_counter[i];
292 }
294 ce->values_gauge[i] = ((double) diff)
295 / ((double) (vl->time - ce->last_time));
296 ce->values_counter[i] = vl->values[i].counter;
297 }
298 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
299 {
300 ce->values_gauge[i] = vl->values[i].gauge;
301 }
302 DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
303 } /* for (i) */
305 ce->last_time = vl->time;
306 ce->last_update = time (NULL);
307 ce->interval = vl->interval;
308 }
309 else /* key is not found */
310 {
311 int i;
312 size_t ce_size = sizeof (cache_entry_t)
313 + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
314 char *key;
316 key = strdup (name);
317 if (key == NULL)
318 {
319 pthread_mutex_unlock (&cache_lock);
320 ERROR ("uc_insert: strdup failed.");
321 return (-1);
322 }
324 ce = (cache_entry_t *) malloc (ce_size);
325 if (ce == NULL)
326 {
327 pthread_mutex_unlock (&cache_lock);
328 ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
329 return (-1);
330 }
332 memset (ce, '\0', ce_size);
334 strncpy (ce->name, name, sizeof (ce->name));
335 ce->name[sizeof (ce->name) - 1] = '\0';
337 ce->values_num = ds->ds_num;
338 ce->values_gauge = (gauge_t *) (ce + 1);
339 ce->values_counter = (counter_t *) (ce->values_gauge + ce->values_num);
341 for (i = 0; i < ds->ds_num; i++)
342 {
343 if (ds->ds[i].type == DS_TYPE_COUNTER)
344 {
345 ce->values_gauge[i] = NAN;
346 ce->values_counter[i] = vl->values[i].counter;
347 }
348 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
349 {
350 ce->values_gauge[i] = vl->values[i].gauge;
351 }
352 } /* for (i) */
354 ce->last_time = vl->time;
355 ce->last_update = time (NULL);
356 ce->interval = vl->interval;
357 ce->state = STATE_OKAY;
359 if (c_avl_insert (cache_tree, key, ce) != 0)
360 {
361 pthread_mutex_unlock (&cache_lock);
362 ERROR ("uc_insert: c_avl_insert failed.");
363 return (-1);
364 }
366 DEBUG ("uc_insert: Added %s to the cache.", name);
367 } /* if (key is not found) */
369 pthread_mutex_unlock (&cache_lock);
371 /* Do not send okay notifications for uninteresting values, i. e. values for
372 * which no threshold is configured. */
373 if (send_okay_notification > 0)
374 {
375 int status;
377 status = ut_check_interesting (name);
378 if (status <= 0)
379 send_okay_notification = 0;
380 }
382 if (send_okay_notification > 0)
383 {
384 notification_t n;
385 memset (&n, '\0', sizeof (n));
387 n.severity = NOTIF_OKAY;
388 strncpy (n.host, vl->host, sizeof (n.host));
389 n.host[sizeof (n.host) - 1] = '\0';
390 n.time = vl->time;
392 snprintf (n.message, sizeof (n.message),
393 "Received a value for %s. It was missing for %i seconds.",
394 name, send_okay_notification);
395 n.message[sizeof (n.message) - 1] = '\0';
397 plugin_dispatch_notification (&n);
398 }
400 return (0);
401 } /* int uc_insert */
403 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
404 {
405 char name[6 * DATA_MAX_NAME_LEN];
406 gauge_t *ret = NULL;
407 cache_entry_t *ce = NULL;
409 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
410 {
411 ERROR ("uc_insert: FORMAT_VL failed.");
412 return (NULL);
413 }
415 pthread_mutex_lock (&cache_lock);
417 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
418 {
419 assert (ce != NULL);
420 assert (ce->values_num == ds->ds_num);
422 ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
423 if (ret == NULL)
424 {
425 ERROR ("uc_get_rate: malloc failed.");
426 }
427 else
428 {
429 memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
430 }
431 }
433 pthread_mutex_unlock (&cache_lock);
435 return (ret);
436 } /* gauge_t *uc_get_rate */
438 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
439 {
440 char name[6 * DATA_MAX_NAME_LEN];
441 cache_entry_t *ce = NULL;
442 int ret = STATE_ERROR;
444 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
445 {
446 ERROR ("uc_get_state: FORMAT_VL failed.");
447 return (STATE_ERROR);
448 }
450 pthread_mutex_lock (&cache_lock);
452 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
453 {
454 assert (ce != NULL);
455 ret = ce->state;
456 }
458 pthread_mutex_unlock (&cache_lock);
460 return (ret);
461 } /* int uc_get_state */
463 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
464 {
465 char name[6 * DATA_MAX_NAME_LEN];
466 cache_entry_t *ce = NULL;
467 int ret = -1;
469 if (state < STATE_OKAY)
470 state = STATE_OKAY;
471 if (state > STATE_ERROR)
472 state = STATE_ERROR;
474 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
475 {
476 ERROR ("uc_get_state: FORMAT_VL failed.");
477 return (STATE_ERROR);
478 }
480 pthread_mutex_lock (&cache_lock);
482 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
483 {
484 assert (ce != NULL);
485 ret = ce->state;
486 ce->state = state;
487 }
489 pthread_mutex_unlock (&cache_lock);
491 return (ret);
492 } /* int uc_set_state */
493 /* vim: set sw=2 ts=8 sts=2 tw=78 : */