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