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