src/utils_cache.[ch]: Implemented uc_[gs]et_state to receive and set the state of...
[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 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   memset (&n, '\0', sizeof (n));
74
75   name_copy = strdup (name);
76   if (name_copy == NULL)
77   {
78     ERROR ("uc_send_notification: strdup failed.");
79     return (-1);
80   }
81
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   }
90
91   n.severity = NOTIF_FAILURE;
92   strncpy (n.host, host, sizeof (n.host));
93   n.host[sizeof (n.host) - 1] = '\0';
94
95   sfree (name_copy);
96   name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
97
98   pthread_mutex_lock (&cache_lock);
99
100   n.time = time (NULL);
101
102   status = 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   }
109     
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   }
118
119   snprintf (n.message, sizeof (n.message),
120       "%s has not been updated for %i seconds.", name,
121       (int) (n.time - ce->last_update));
122
123   pthread_mutex_unlock (&cache_lock);
124
125   n.message[sizeof (n.message) - 1] = '\0';
126   plugin_dispatch_notification (&n);
127
128   return (0);
129 } /* int uc_send_notification */
130
131 int uc_init (void)
132 {
133   if (cache_tree == NULL)
134     cache_tree = avl_create ((int (*) (const void *, const void *))
135         cache_compare);
136
137   return (0);
138 } /* int uc_init */
139
140 int uc_check_timeout (void)
141 {
142   time_t now;
143   cache_entry_t *ce;
144
145   char **keys = NULL;
146   int keys_len = 0;
147
148   char *key;
149   avl_iterator_t *iter;
150   int i;
151   
152   pthread_mutex_lock (&cache_lock);
153
154   now = time (NULL);
155
156   /* Build a list of entries to be flushed */
157   iter = avl_get_iterator (cache_tree);
158   while (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;
164
165       tmp = (char **) realloc ((void *) keys,
166           (keys_len + 1) * sizeof (char *));
167       if (tmp == NULL)
168       {
169         ERROR ("uc_purge: realloc failed.");
170         avl_iterator_destroy (iter);
171         return (-1);
172       }
173
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 (avl_iterator_next) */
184
185   for (i = 0; i < keys_len; i++)
186   {
187     int status;
188
189     status = ut_check_interesting (keys[i]);
190
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 = avl_remove (cache_tree, keys[i], (void *) &key, (void *) &ce);
201       if (status != 0)
202       {
203         ERROR ("uc_check_timeout: 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]) */
218
219   avl_iterator_destroy (iter);
220
221   pthread_mutex_unlock (&cache_lock);
222
223   for (i = 0; i < keys_len; i++)
224   {
225     if (keys[i] == NULL)
226       continue;
227
228     uc_send_notification (keys[i]);
229     sfree (keys[i]);
230   }
231
232   sfree (keys);
233
234   return (0);
235 } /* int uc_check_timeout */
236
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
242   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
243   {
244     ERROR ("uc_insert: FORMAT_VL failed.");
245     return (-1);
246   }
247
248   pthread_mutex_lock (&cache_lock);
249
250   if (avl_get (cache_tree, name, (void *) &ce) == 0)
251   {
252     int i;
253
254     assert (ce != NULL);
255     assert (ce->values_num == ds->ds_num);
256
257     if (ce->last_time >= vl->time)
258     {
259       pthread_mutex_unlock (&cache_lock);
260       NOTICE ("uc_insert: Value too old: name = %s; value time = %u; "
261           "last cache update = %u;",
262           name, (unsigned int) vl->time, (unsigned int) ce->last_time);
263       return (-1);
264     }
265
266     if ((ce->last_time + ce->interval) < vl->time)
267     {
268       /* TODO: Implement a `real' okay notification. Watch out for locking
269        * issues, though! */
270       NOTICE ("uc_insert: Okay notification for %s", name);
271       ce->state = STATE_OKAY;
272     }
273
274     for (i = 0; i < ds->ds_num; i++)
275     {
276       if (ds->ds[i].type == DS_TYPE_COUNTER)
277       {
278         counter_t diff;
279
280         /* check if the counter has wrapped around */
281         if (vl->values[i].counter < ce->values_counter[i])
282         {
283           if (ce->values_counter[i] <= 4294967295U)
284             diff = (4294967295U - ce->values_counter[i])
285               + vl->values[i].counter;
286           else
287             diff = (18446744073709551615ULL - ce->values_counter[i])
288               + vl->values[i].counter;
289         }
290         else /* counter has NOT wrapped around */
291         {
292           diff = vl->values[i].counter - ce->values_counter[i];
293         }
294
295         ce->values_gauge[i] = ((double) diff)
296           / ((double) (vl->time - ce->last_time));
297         ce->values_counter[i] = vl->values[i].counter;
298       }
299       else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
300       {
301         ce->values_gauge[i] = vl->values[i].gauge;
302       }
303       DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
304     } /* for (i) */
305
306     ce->last_time = vl->time;
307     ce->last_update = time (NULL);
308     ce->interval = vl->interval;
309   }
310   else /* key is not found */
311   {
312     int i;
313     size_t ce_size = sizeof (cache_entry_t)
314       + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
315     char *key;
316     
317     key = strdup (name);
318     if (key == NULL)
319     {
320       pthread_mutex_unlock (&cache_lock);
321       ERROR ("uc_insert: strdup failed.");
322       return (-1);
323     }
324
325     ce = (cache_entry_t *) malloc (ce_size);
326     if (ce == NULL)
327     {
328       pthread_mutex_unlock (&cache_lock);
329       ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
330       return (-1);
331     }
332
333     memset (ce, '\0', ce_size);
334
335     strncpy (ce->name, name, sizeof (ce->name));
336     ce->name[sizeof (ce->name) - 1] = '\0';
337
338     ce->values_num = ds->ds_num;
339     ce->values_gauge = (gauge_t *) (ce + 1);
340     ce->values_counter = (counter_t *) (ce->values_gauge + ce->values_num);
341
342     for (i = 0; i < ds->ds_num; i++)
343     {
344       if (ds->ds[i].type == DS_TYPE_COUNTER)
345       {
346         ce->values_gauge[i] = NAN;
347         ce->values_counter[i] = vl->values[i].counter;
348       }
349       else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
350       {
351         ce->values_gauge[i] = vl->values[i].gauge;
352       }
353     } /* for (i) */
354
355     ce->last_time = vl->time;
356     ce->last_update = time (NULL);
357     ce->interval = vl->interval;
358     ce->state = STATE_OKAY;
359
360     if (avl_insert (cache_tree, key, ce) != 0)
361     {
362       pthread_mutex_unlock (&cache_lock);
363       ERROR ("uc_insert: avl_insert failed.");
364       return (-1);
365     }
366
367     DEBUG ("uc_insert: Added %s to the cache.", name);
368   } /* if (key is not found) */
369
370   pthread_mutex_unlock (&cache_lock);
371
372   return (0);
373 } /* int uc_insert */
374
375 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
376 {
377   char name[6 * DATA_MAX_NAME_LEN];
378   gauge_t *ret = NULL;
379   cache_entry_t *ce = NULL;
380
381   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
382   {
383     ERROR ("uc_insert: FORMAT_VL failed.");
384     return (NULL);
385   }
386
387   pthread_mutex_lock (&cache_lock);
388
389   if (avl_get (cache_tree, name, (void *) &ce) == 0)
390   {
391     assert (ce != NULL);
392     assert (ce->values_num == ds->ds_num);
393
394     ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
395     if (ret == NULL)
396     {
397       ERROR ("uc_get_rate: malloc failed.");
398     }
399     else
400     {
401       memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
402     }
403   }
404
405   pthread_mutex_unlock (&cache_lock);
406
407   return (ret);
408 } /* gauge_t *uc_get_rate */
409
410 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
411 {
412   char name[6 * DATA_MAX_NAME_LEN];
413   cache_entry_t *ce = NULL;
414   int ret = STATE_ERROR;
415
416   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
417   {
418     ERROR ("uc_get_state: FORMAT_VL failed.");
419     return (STATE_ERROR);
420   }
421
422   pthread_mutex_lock (&cache_lock);
423
424   if (avl_get (cache_tree, name, (void *) &ce) == 0)
425   {
426     assert (ce != NULL);
427     ret = ce->state;
428   }
429
430   pthread_mutex_unlock (&cache_lock);
431
432   return (ret);
433 } /* int uc_get_state */
434
435 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
436 {
437   char name[6 * DATA_MAX_NAME_LEN];
438   cache_entry_t *ce = NULL;
439   int ret = -1;
440
441   if (state < STATE_OKAY)
442     state = STATE_OKAY;
443   if (state > STATE_ERROR)
444     state = STATE_ERROR;
445
446   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
447   {
448     ERROR ("uc_get_state: FORMAT_VL failed.");
449     return (STATE_ERROR);
450   }
451
452   pthread_mutex_lock (&cache_lock);
453
454   if (avl_get (cache_tree, name, (void *) &ce) == 0)
455   {
456     assert (ce != NULL);
457     ret = ce->state;
458     ce->state = state;
459   }
460
461   pthread_mutex_unlock (&cache_lock);
462
463   return (ret);
464 } /* int uc_set_state */
465 /* vim: set sw=2 ts=8 sts=2 tw=78 : */