Merge branch 'collectd-4.2' into collectd-4.3
[collectd.git] / src / utils_cache.c
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  **/
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 int uc_send_notification (const char *name)
60 {
61   cache_entry_t *ce = NULL;
62   int status;
63
64   char *name_copy;
65   char *host;
66   char *plugin;
67   char *plugin_instance;
68   char *type;
69   char *type_instance;
70
71   notification_t n;
72
73   name_copy = strdup (name);
74   if (name_copy == NULL)
75   {
76     ERROR ("uc_send_notification: strdup failed.");
77     return (-1);
78   }
79
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   }
88
89   /* Copy the associative members */
90   notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
91       host, plugin, plugin_instance, type, type_instance);
92
93   sfree (name_copy);
94   name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
95
96   pthread_mutex_lock (&cache_lock);
97
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);
104
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   }
112     
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   }
121
122   snprintf (n.message, sizeof (n.message),
123       "%s has not been updated for %i seconds.", name,
124       (int) (n.time - ce->last_update));
125
126   pthread_mutex_unlock (&cache_lock);
127
128   n.message[sizeof (n.message) - 1] = '\0';
129   plugin_dispatch_notification (&n);
130
131   return (0);
132 } /* int uc_send_notification */
133
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);
139
140   return (0);
141 } /* int uc_init */
142
143 int uc_check_timeout (void)
144 {
145   time_t now;
146   cache_entry_t *ce;
147
148   char **keys = NULL;
149   int keys_len = 0;
150
151   char *key;
152   c_avl_iterator_t *iter;
153   int i;
154   
155   pthread_mutex_lock (&cache_lock);
156
157   now = time (NULL);
158
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;
167
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       }
176
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) */
187
188   for (i = 0; i < keys_len; i++)
189   {
190     int status;
191
192     status = ut_check_interesting (keys[i]);
193
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]) */
221
222   c_avl_iterator_destroy (iter);
223
224   pthread_mutex_unlock (&cache_lock);
225
226   for (i = 0; i < keys_len; i++)
227   {
228     if (keys[i] == NULL)
229       continue;
230
231     uc_send_notification (keys[i]);
232     sfree (keys[i]);
233   }
234
235   sfree (keys);
236
237   return (0);
238 } /* int uc_check_timeout */
239
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;
245
246   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
247   {
248     ERROR ("uc_insert: FORMAT_VL failed.");
249     return (-1);
250   }
251
252   pthread_mutex_lock (&cache_lock);
253
254   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
255   {
256     int i;
257
258     assert (ce != NULL);
259     assert (ce->values_num == ds->ds_num);
260
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     }
269
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     }
275
276     for (i = 0; i < ds->ds_num; i++)
277     {
278       if (ds->ds[i].type == DS_TYPE_COUNTER)
279       {
280         counter_t diff;
281
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         }
296
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) */
307
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;
318     
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     }
326
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     }
334
335     memset (ce, '\0', ce_size);
336
337     strncpy (ce->name, name, sizeof (ce->name));
338     ce->name[sizeof (ce->name) - 1] = '\0';
339
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);
343
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) */
356
357     ce->last_time = vl->time;
358     ce->last_update = time (NULL);
359     ce->interval = vl->interval;
360     ce->state = STATE_OKAY;
361
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     }
368
369     DEBUG ("uc_insert: Added %s to the cache.", name);
370   } /* if (key is not found) */
371
372   pthread_mutex_unlock (&cache_lock);
373
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;
379
380     status = ut_check_interesting (name);
381     if (status <= 0)
382       send_okay_notification = 0;
383   }
384
385   if (send_okay_notification > 0)
386   {
387     notification_t n;
388     memset (&n, '\0', sizeof (n));
389
390     /* Copy the associative members */
391     NOTIFICATION_INIT_VL (&n, vl, ds);
392
393     n.severity = NOTIF_OKAY;
394     n.time = vl->time;
395
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';
400
401     plugin_dispatch_notification (&n);
402   }
403
404   return (0);
405 } /* int uc_insert */
406
407 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
408 {
409   char name[6 * DATA_MAX_NAME_LEN];
410   gauge_t *ret = NULL;
411   cache_entry_t *ce = NULL;
412
413   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
414   {
415     ERROR ("uc_insert: FORMAT_VL failed.");
416     return (NULL);
417   }
418
419   pthread_mutex_lock (&cache_lock);
420
421   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
422   {
423     assert (ce != NULL);
424     assert (ce->values_num == ds->ds_num);
425
426     ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
427     if (ret == NULL)
428     {
429       ERROR ("uc_get_rate: malloc failed.");
430     }
431     else
432     {
433       memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
434     }
435   }
436
437   pthread_mutex_unlock (&cache_lock);
438
439   return (ret);
440 } /* gauge_t *uc_get_rate */
441
442 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
443 {
444   char name[6 * DATA_MAX_NAME_LEN];
445   cache_entry_t *ce = NULL;
446   int ret = STATE_ERROR;
447
448   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
449   {
450     ERROR ("uc_get_state: FORMAT_VL failed.");
451     return (STATE_ERROR);
452   }
453
454   pthread_mutex_lock (&cache_lock);
455
456   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
457   {
458     assert (ce != NULL);
459     ret = ce->state;
460   }
461
462   pthread_mutex_unlock (&cache_lock);
463
464   return (ret);
465 } /* int uc_get_state */
466
467 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
468 {
469   char name[6 * DATA_MAX_NAME_LEN];
470   cache_entry_t *ce = NULL;
471   int ret = -1;
472
473   if (state < STATE_OKAY)
474     state = STATE_OKAY;
475   if (state > STATE_ERROR)
476     state = STATE_ERROR;
477
478   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
479   {
480     ERROR ("uc_get_state: FORMAT_VL failed.");
481     return (STATE_ERROR);
482   }
483
484   pthread_mutex_lock (&cache_lock);
485
486   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
487   {
488     assert (ce != NULL);
489     ret = ce->state;
490     ce->state = state;
491   }
492
493   pthread_mutex_unlock (&cache_lock);
494
495   return (ret);
496 } /* int uc_set_state */
497 /* vim: set sw=2 ts=8 sts=2 tw=78 : */