Merge branch 'collectd-4.3'
[collectd.git] / src / utils_cache.c
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  **/
21
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"
28
29 #include <assert.h>
30 #include <pthread.h>
31
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;
49
50 static c_avl_tree_t   *cache_tree = NULL;
51 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
52
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 */
58
59 static cache_entry_t *cache_alloc (int values_num)
60 {
61   cache_entry_t *ce;
62
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;
71
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   }
82
83   return (ce);
84 } /* cache_entry_t *cache_alloc */
85
86 static void cache_free (cache_entry_t *ce)
87 {
88   if (ce == NULL)
89     return;
90
91   sfree (ce->values_gauge);
92   sfree (ce->values_counter);
93   sfree (ce);
94 } /* void cache_free */
95
96 static int uc_send_notification (const char *name)
97 {
98   cache_entry_t *ce = NULL;
99   int status;
100
101   char *name_copy;
102   char *host;
103   char *plugin;
104   char *plugin_instance;
105   char *type;
106   char *type_instance;
107
108   notification_t n;
109
110   name_copy = strdup (name);
111   if (name_copy == NULL)
112   {
113     ERROR ("uc_send_notification: strdup failed.");
114     return (-1);
115   }
116
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   }
125
126   /* Copy the associative members */
127   notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
128       host, plugin, plugin_instance, type, type_instance);
129
130   sfree (name_copy);
131   name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
132
133   pthread_mutex_lock (&cache_lock);
134
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);
141
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   }
149     
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   }
158
159   ssnprintf (n.message, sizeof (n.message),
160       "%s has not been updated for %i seconds.", name,
161       (int) (n.time - ce->last_update));
162
163   pthread_mutex_unlock (&cache_lock);
164
165   plugin_dispatch_notification (&n);
166
167   return (0);
168 } /* int uc_send_notification */
169
170 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
171     const char *key)
172 {
173   int i;
174   char *key_copy;
175   cache_entry_t *ce;
176
177   /* `cache_lock' has been locked by `uc_update' */
178
179   key_copy = strdup (key);
180   if (key_copy == NULL)
181   {
182     ERROR ("uc_insert: strdup failed.");
183     return (-1);
184   }
185
186   ce = cache_alloc (ds->ds_num);
187   if (ce == NULL)
188   {
189     ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
190     return (-1);
191   }
192
193   sstrncpy (ce->name, key, sizeof (ce->name));
194
195   for (i = 0; i < ds->ds_num; i++)
196   {
197     if (ds->ds[i].type == DS_TYPE_COUNTER)
198     {
199       ce->values_gauge[i] = NAN;
200       ce->values_counter[i] = vl->values[i].counter;
201     }
202     else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
203     {
204       ce->values_gauge[i] = vl->values[i].gauge;
205     }
206   } /* for (i) */
207
208   ce->last_time = vl->time;
209   ce->last_update = time (NULL);
210   ce->interval = vl->interval;
211   ce->state = STATE_OKAY;
212
213   if (c_avl_insert (cache_tree, key_copy, ce) != 0)
214   {
215     sfree (key_copy);
216     ERROR ("uc_insert: c_avl_insert failed.");
217     return (-1);
218   }
219
220   DEBUG ("uc_insert: Added %s to the cache.", key);
221   return (0);
222 } /* int uc_insert */
223
224 int uc_init (void)
225 {
226   if (cache_tree == NULL)
227     cache_tree = c_avl_create ((int (*) (const void *, const void *))
228         cache_compare);
229
230   return (0);
231 } /* int uc_init */
232
233 int uc_check_timeout (void)
234 {
235   time_t now;
236   cache_entry_t *ce;
237
238   char **keys = NULL;
239   int keys_len = 0;
240
241   char *key;
242   c_avl_iterator_t *iter;
243   int i;
244   
245   pthread_mutex_lock (&cache_lock);
246
247   now = time (NULL);
248
249   /* Build a list of entries to be flushed */
250   iter = c_avl_get_iterator (cache_tree);
251   while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
252   {
253     /* If entry has not been updated, add to `keys' array */
254     if ((now - ce->last_update) >= (2 * ce->interval))
255     {
256       char **tmp;
257
258       tmp = (char **) realloc ((void *) keys,
259           (keys_len + 1) * sizeof (char *));
260       if (tmp == NULL)
261       {
262         ERROR ("uc_purge: realloc failed.");
263         c_avl_iterator_destroy (iter);
264         return (-1);
265       }
266
267       keys = tmp;
268       keys[keys_len] = strdup (key);
269       if (keys[keys_len] == NULL)
270       {
271         ERROR ("uc_check_timeout: strdup failed.");
272         continue;
273       }
274       keys_len++;
275     }
276   } /* while (c_avl_iterator_next) */
277
278   for (i = 0; i < keys_len; i++)
279   {
280     int status;
281
282     status = ut_check_interesting (keys[i]);
283
284     if (status < 0)
285     {
286       ERROR ("uc_check_timeout: ut_check_interesting failed.");
287       sfree (keys[i]);
288     }
289     else if (status == 0) /* ``service'' is uninteresting */
290     {
291       ce = NULL;
292       DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
293           keys[i]);
294       status = c_avl_remove (cache_tree, keys[i],
295           (void *) &key, (void *) &ce);
296       if (status != 0)
297       {
298         ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
299       }
300       sfree (keys[i]);
301       cache_free (ce);
302     }
303     else if (status == 1) /* persist */
304     {
305       DEBUG ("uc_check_timeout: %s is missing, sending notification.",
306           keys[i]);
307       ce->state = STATE_MISSING;
308     }
309     else if (status == 2) /* do not persist */
310     {
311       if (ce->state == STATE_MISSING)
312       {
313         DEBUG ("uc_check_timeout: %s is missing but "
314             "notification has already been sent.",
315             keys[i]);
316         sfree (keys[i]);
317       }
318       else /* (ce->state != STATE_MISSING) */
319       {
320         DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
321             keys[i]);
322         ce->state = STATE_MISSING;
323       }
324     }
325     else
326     {
327       WARNING ("uc_check_timeout: ut_check_interesting (%s) returned ",
328           "invalid status %i.",
329           keys[i], status);
330     }
331   } /* for (keys[i]) */
332
333   c_avl_iterator_destroy (iter);
334
335   pthread_mutex_unlock (&cache_lock);
336
337   for (i = 0; i < keys_len; i++)
338   {
339     if (keys[i] == NULL)
340       continue;
341
342     uc_send_notification (keys[i]);
343     sfree (keys[i]);
344   }
345
346   sfree (keys);
347
348   return (0);
349 } /* int uc_check_timeout */
350
351 int uc_update (const data_set_t *ds, const value_list_t *vl)
352 {
353   char name[6 * DATA_MAX_NAME_LEN];
354   cache_entry_t *ce = NULL;
355   int send_okay_notification = 0;
356   time_t update_delay = 0;
357   notification_t n;
358   int status;
359   int i;
360
361   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
362   {
363     ERROR ("uc_update: FORMAT_VL failed.");
364     return (-1);
365   }
366
367   pthread_mutex_lock (&cache_lock);
368
369   status = c_avl_get (cache_tree, name, (void *) &ce);
370   if (status != 0) /* entry does not yet exist */
371   {
372     status = uc_insert (ds, vl, name);
373     pthread_mutex_unlock (&cache_lock);
374     return (status);
375   }
376
377   assert (ce != NULL);
378   assert (ce->values_num == ds->ds_num);
379
380   if (ce->last_time >= vl->time)
381   {
382     pthread_mutex_unlock (&cache_lock);
383     NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
384         "last cache update = %u;",
385         name, (unsigned int) vl->time, (unsigned int) ce->last_time);
386     return (-1);
387   }
388
389   /* Send a notification (after the lock has been released) if we switch the
390    * state from something else to `okay'. */
391   if (ce->state == STATE_MISSING)
392   {
393     send_okay_notification = 1;
394     ce->state = STATE_OKAY;
395     update_delay = time (NULL) - ce->last_update;
396   }
397
398   for (i = 0; i < ds->ds_num; i++)
399   {
400     if (ds->ds[i].type == DS_TYPE_COUNTER)
401     {
402       counter_t diff;
403
404       /* check if the counter has wrapped around */
405       if (vl->values[i].counter < ce->values_counter[i])
406       {
407         if (ce->values_counter[i] <= 4294967295U)
408           diff = (4294967295U - ce->values_counter[i])
409             + vl->values[i].counter;
410         else
411           diff = (18446744073709551615ULL - ce->values_counter[i])
412             + vl->values[i].counter;
413       }
414       else /* counter has NOT wrapped around */
415       {
416         diff = vl->values[i].counter - ce->values_counter[i];
417       }
418
419       ce->values_gauge[i] = ((double) diff)
420         / ((double) (vl->time - ce->last_time));
421       ce->values_counter[i] = vl->values[i].counter;
422     }
423     else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
424     {
425       ce->values_gauge[i] = vl->values[i].gauge;
426     }
427     DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
428   } /* for (i) */
429
430   ce->last_time = vl->time;
431   ce->last_update = time (NULL);
432   ce->interval = vl->interval;
433
434   pthread_mutex_unlock (&cache_lock);
435
436   if (send_okay_notification == 0)
437     return (0);
438
439   /* Do not send okay notifications for uninteresting values, i. e. values for
440    * which no threshold is configured. */
441   status = ut_check_interesting (name);
442   if (status <= 0)
443     return (0);
444
445   /* Initialize the notification */
446   memset (&n, '\0', sizeof (n));
447   NOTIFICATION_INIT_VL (&n, vl, ds);
448
449   n.severity = NOTIF_OKAY;
450   n.time = vl->time;
451
452   ssnprintf (n.message, sizeof (n.message),
453       "Received a value for %s. It was missing for %u seconds.",
454       name, (unsigned int) update_delay);
455
456   plugin_dispatch_notification (&n);
457
458   return (0);
459 } /* int uc_update */
460
461 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
462 {
463   gauge_t *ret = NULL;
464   size_t ret_num = 0;
465   cache_entry_t *ce = NULL;
466   int status = 0;
467
468   pthread_mutex_lock (&cache_lock);
469
470   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
471   {
472     assert (ce != NULL);
473
474     ret_num = ce->values_num;
475     ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
476     if (ret == NULL)
477     {
478       ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
479       status = -1;
480     }
481     else
482     {
483       memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
484     }
485   }
486   else
487   {
488     DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
489     status = -1;
490   }
491
492   pthread_mutex_unlock (&cache_lock);
493
494   if (status == 0)
495   {
496     *ret_values = ret;
497     *ret_values_num = ret_num;
498   }
499
500   return (status);
501 } /* gauge_t *uc_get_rate_by_name */
502
503 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
504 {
505   char name[6 * DATA_MAX_NAME_LEN];
506   gauge_t *ret = NULL;
507   size_t ret_num = 0;
508   int status;
509
510   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
511   {
512     ERROR ("uc_insert: FORMAT_VL failed.");
513     return (NULL);
514   }
515
516   status = uc_get_rate_by_name (name, &ret, &ret_num);
517   if (status != 0)
518     return (NULL);
519
520   /* This is important - the caller has no other way of knowing how many
521    * values are returned. */
522   if (ret_num != ds->ds_num)
523   {
524     ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
525         "but uc_get_rate_by_name returned %i.",
526         ds->type, ds->ds_num, ret_num);
527     sfree (ret);
528     return (NULL);
529   }
530
531   return (ret);
532 } /* gauge_t *uc_get_rate */
533
534 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
535 {
536   c_avl_iterator_t *iter;
537   char *key;
538   cache_entry_t *value;
539
540   char **names = NULL;
541   time_t *times = NULL;
542   size_t number = 0;
543
544   int status = 0;
545
546   if ((ret_names == NULL) || (ret_number == NULL))
547     return (-1);
548
549   pthread_mutex_lock (&cache_lock);
550
551   iter = c_avl_get_iterator (cache_tree);
552   while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
553   {
554     char **temp;
555
556     if (ret_times != NULL)
557     {
558       time_t *tmp_times;
559
560       tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
561       if (tmp_times == NULL)
562       {
563         status = -1;
564         break;
565       }
566       times = tmp_times;
567       times[number] = value->last_time;
568     }
569
570     temp = (char **) realloc (names, sizeof (char *) * (number + 1));
571     if (temp == NULL)
572     {
573       status = -1;
574       break;
575     }
576     names = temp;
577     names[number] = strdup (key);
578     if (names[number] == NULL)
579     {
580       status = -1;
581       break;
582     }
583     number++;
584   } /* while (c_avl_iterator_next) */
585
586   c_avl_iterator_destroy (iter);
587   pthread_mutex_unlock (&cache_lock);
588
589   if (status != 0)
590   {
591     size_t i;
592     
593     for (i = 0; i < number; i++)
594     {
595       sfree (names[i]);
596     }
597     sfree (names);
598
599     return (-1);
600   }
601
602   *ret_names = names;
603   if (ret_times != NULL)
604     *ret_times = times;
605   *ret_number = number;
606
607   return (0);
608 } /* int uc_get_names */
609
610 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
611 {
612   char name[6 * DATA_MAX_NAME_LEN];
613   cache_entry_t *ce = NULL;
614   int ret = STATE_ERROR;
615
616   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
617   {
618     ERROR ("uc_get_state: FORMAT_VL failed.");
619     return (STATE_ERROR);
620   }
621
622   pthread_mutex_lock (&cache_lock);
623
624   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
625   {
626     assert (ce != NULL);
627     ret = ce->state;
628   }
629
630   pthread_mutex_unlock (&cache_lock);
631
632   return (ret);
633 } /* int uc_get_state */
634
635 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
636 {
637   char name[6 * DATA_MAX_NAME_LEN];
638   cache_entry_t *ce = NULL;
639   int ret = -1;
640
641   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
642   {
643     ERROR ("uc_get_state: FORMAT_VL failed.");
644     return (STATE_ERROR);
645   }
646
647   pthread_mutex_lock (&cache_lock);
648
649   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
650   {
651     assert (ce != NULL);
652     ret = ce->state;
653     ce->state = state;
654   }
655
656   pthread_mutex_unlock (&cache_lock);
657
658   return (ret);
659 } /* int uc_set_state */
660 /* vim: set sw=2 ts=8 sts=2 tw=78 : */