src/utils_cache.c: Added the creation of an `OKAY' notification.
[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   int send_okay_notification = 0;
242
243   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
244   {
245     ERROR ("uc_insert: FORMAT_VL failed.");
246     return (-1);
247   }
248
249   pthread_mutex_lock (&cache_lock);
250
251   if (avl_get (cache_tree, name, (void *) &ce) == 0)
252   {
253     int i;
254
255     assert (ce != NULL);
256     assert (ce->values_num == ds->ds_num);
257
258     if (ce->last_time >= vl->time)
259     {
260       pthread_mutex_unlock (&cache_lock);
261       NOTICE ("uc_insert: Value too old: name = %s; value time = %u; "
262           "last cache update = %u;",
263           name, (unsigned int) vl->time, (unsigned int) ce->last_time);
264       return (-1);
265     }
266
267     if ((ce->last_time + ce->interval) < vl->time)
268     {
269       send_okay_notification = vl->time - ce->last_time;
270       ce->state = STATE_OKAY;
271     }
272
273     for (i = 0; i < ds->ds_num; i++)
274     {
275       if (ds->ds[i].type == DS_TYPE_COUNTER)
276       {
277         counter_t diff;
278
279         /* check if the counter has wrapped around */
280         if (vl->values[i].counter < ce->values_counter[i])
281         {
282           if (ce->values_counter[i] <= 4294967295U)
283             diff = (4294967295U - ce->values_counter[i])
284               + vl->values[i].counter;
285           else
286             diff = (18446744073709551615ULL - ce->values_counter[i])
287               + vl->values[i].counter;
288         }
289         else /* counter has NOT wrapped around */
290         {
291           diff = vl->values[i].counter - ce->values_counter[i];
292         }
293
294         ce->values_gauge[i] = ((double) diff)
295           / ((double) (vl->time - ce->last_time));
296         ce->values_counter[i] = vl->values[i].counter;
297       }
298       else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
299       {
300         ce->values_gauge[i] = vl->values[i].gauge;
301       }
302       DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
303     } /* for (i) */
304
305     ce->last_time = vl->time;
306     ce->last_update = time (NULL);
307     ce->interval = vl->interval;
308   }
309   else /* key is not found */
310   {
311     int i;
312     size_t ce_size = sizeof (cache_entry_t)
313       + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
314     char *key;
315     
316     key = strdup (name);
317     if (key == NULL)
318     {
319       pthread_mutex_unlock (&cache_lock);
320       ERROR ("uc_insert: strdup failed.");
321       return (-1);
322     }
323
324     ce = (cache_entry_t *) malloc (ce_size);
325     if (ce == NULL)
326     {
327       pthread_mutex_unlock (&cache_lock);
328       ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
329       return (-1);
330     }
331
332     memset (ce, '\0', ce_size);
333
334     strncpy (ce->name, name, sizeof (ce->name));
335     ce->name[sizeof (ce->name) - 1] = '\0';
336
337     ce->values_num = ds->ds_num;
338     ce->values_gauge = (gauge_t *) (ce + 1);
339     ce->values_counter = (counter_t *) (ce->values_gauge + ce->values_num);
340
341     for (i = 0; i < ds->ds_num; i++)
342     {
343       if (ds->ds[i].type == DS_TYPE_COUNTER)
344       {
345         ce->values_gauge[i] = NAN;
346         ce->values_counter[i] = vl->values[i].counter;
347       }
348       else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
349       {
350         ce->values_gauge[i] = vl->values[i].gauge;
351       }
352     } /* for (i) */
353
354     ce->last_time = vl->time;
355     ce->last_update = time (NULL);
356     ce->interval = vl->interval;
357     ce->state = STATE_OKAY;
358
359     if (avl_insert (cache_tree, key, ce) != 0)
360     {
361       pthread_mutex_unlock (&cache_lock);
362       ERROR ("uc_insert: avl_insert failed.");
363       return (-1);
364     }
365
366     DEBUG ("uc_insert: Added %s to the cache.", name);
367   } /* if (key is not found) */
368
369   pthread_mutex_unlock (&cache_lock);
370
371   if (send_okay_notification > 0)
372   {
373     notification_t n;
374     memset (&n, '\0', sizeof (n));
375
376     n.severity = NOTIF_OKAY;
377     strncpy (n.host, vl->host, sizeof (n.host));
378     n.host[sizeof (n.host) - 1] = '\0';
379     n.time = vl->time;
380
381     snprintf (n.message, sizeof (n.message),
382         "Received a value for %s. It was missing for %i seconds.",
383         name, send_okay_notification);
384     n.message[sizeof (n.message) - 1] = '\0';
385
386     plugin_dispatch_notification (&n);
387   }
388
389   return (0);
390 } /* int uc_insert */
391
392 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
393 {
394   char name[6 * DATA_MAX_NAME_LEN];
395   gauge_t *ret = NULL;
396   cache_entry_t *ce = NULL;
397
398   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
399   {
400     ERROR ("uc_insert: FORMAT_VL failed.");
401     return (NULL);
402   }
403
404   pthread_mutex_lock (&cache_lock);
405
406   if (avl_get (cache_tree, name, (void *) &ce) == 0)
407   {
408     assert (ce != NULL);
409     assert (ce->values_num == ds->ds_num);
410
411     ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
412     if (ret == NULL)
413     {
414       ERROR ("uc_get_rate: malloc failed.");
415     }
416     else
417     {
418       memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
419     }
420   }
421
422   pthread_mutex_unlock (&cache_lock);
423
424   return (ret);
425 } /* gauge_t *uc_get_rate */
426
427 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
428 {
429   char name[6 * DATA_MAX_NAME_LEN];
430   cache_entry_t *ce = NULL;
431   int ret = STATE_ERROR;
432
433   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
434   {
435     ERROR ("uc_get_state: FORMAT_VL failed.");
436     return (STATE_ERROR);
437   }
438
439   pthread_mutex_lock (&cache_lock);
440
441   if (avl_get (cache_tree, name, (void *) &ce) == 0)
442   {
443     assert (ce != NULL);
444     ret = ce->state;
445   }
446
447   pthread_mutex_unlock (&cache_lock);
448
449   return (ret);
450 } /* int uc_get_state */
451
452 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
453 {
454   char name[6 * DATA_MAX_NAME_LEN];
455   cache_entry_t *ce = NULL;
456   int ret = -1;
457
458   if (state < STATE_OKAY)
459     state = STATE_OKAY;
460   if (state > STATE_ERROR)
461     state = STATE_ERROR;
462
463   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
464   {
465     ERROR ("uc_get_state: FORMAT_VL failed.");
466     return (STATE_ERROR);
467   }
468
469   pthread_mutex_lock (&cache_lock);
470
471   if (avl_get (cache_tree, name, (void *) &ce) == 0)
472   {
473     assert (ce != NULL);
474     ret = ce->state;
475     ce->state = state;
476   }
477
478   pthread_mutex_unlock (&cache_lock);
479
480   return (ret);
481 } /* int uc_set_state */
482 /* vim: set sw=2 ts=8 sts=2 tw=78 : */