Do not use *printf() to report errors / debugging messages.
[collectd.git] / src / rrdtool.c
index e356bf1..ac81d55 100644 (file)
@@ -105,7 +105,7 @@ static double  xff       = 0.1;
 static int         cache_timeout = 0;
 static int         cache_flush_timeout = 0;
 static time_t      cache_flush_last;
-static avl_tree_t *cache = NULL;
+static c_avl_tree_t *cache = NULL;
 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
 
 static rrd_queue_t    *queue_head = NULL;
@@ -195,7 +195,7 @@ static int rra_get (char ***ret, const value_list_t *vl)
                span = rts[i];
 
                if ((span / ss) < rrarows)
-                       continue;
+                       span = ss * rrarows;
 
                if (cdp_len == 0)
                        cdp_len = 1;
@@ -285,7 +285,7 @@ static int ds_get (char ***ret, const data_set_t *ds, const value_list_t *vl)
 
                if (isnan (d->min))
                {
-                       strcpy (min, "U");
+                       sstrncpy (min, "U", sizeof (min));
                }
                else
                {
@@ -295,7 +295,7 @@ static int ds_get (char ***ret, const data_set_t *ds, const value_list_t *vl)
 
                if (isnan (d->max))
                {
-                       strcpy (max, "U");
+                       sstrncpy (max, "U", sizeof (max));
                }
                else
                {
@@ -342,7 +342,7 @@ static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
        optind = 0; /* bug in librrd? */
        rrd_clear_error ();
 
-       status = rrd_create_r (filename, pdp_step, last_up, argc, argv);
+       status = rrd_create_r (filename, pdp_step, last_up, argc, (void *) argv);
 
        if (status != 0)
        {
@@ -360,7 +360,7 @@ static int srrd_update (char *filename, char *template, int argc, char **argv)
        optind = 0; /* bug in librrd? */
        rrd_clear_error ();
 
-       status = rrd_update_r (filename, template, argc, argv);
+       status = rrd_update_r (filename, template, argc, (void *) argv);
 
        if (status != 0)
        {
@@ -641,7 +641,7 @@ static void *rrd_queue_thread (void *data)
                 * we make a copy of it's values */
                pthread_mutex_lock (&cache_lock);
 
-               avl_get (cache, queue_entry->filename, (void *) &cache_entry);
+               c_avl_get (cache, queue_entry->filename, (void *) &cache_entry);
 
                values = cache_entry->values;
                values_num = cache_entry->values_num;
@@ -667,7 +667,7 @@ static void *rrd_queue_thread (void *data)
        } /* while (42) */
 
        pthread_mutex_lock (&cache_lock);
-       avl_destroy (cache);
+       c_avl_destroy (cache);
        cache = NULL;
        pthread_mutex_unlock (&cache_lock);
 
@@ -715,7 +715,7 @@ static void rrd_cache_flush (int timeout)
        int    keys_num = 0;
 
        char *key;
-       avl_iterator_t *iter;
+       c_avl_iterator_t *iter;
        int i;
 
        DEBUG ("rrdtool plugin: Flushing cache, timeout = %i", timeout);
@@ -723,8 +723,8 @@ static void rrd_cache_flush (int timeout)
        now = time (NULL);
 
        /* Build a list of entries to be flushed */
-       iter = avl_get_iterator (cache);
-       while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
+       iter = c_avl_get_iterator (cache);
+       while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
        {
                if (rc->flags == FLAG_QUEUED)
                        continue;
@@ -737,29 +737,31 @@ static void rrd_cache_flush (int timeout)
                }
                else /* ancient and no values -> waste of memory */
                {
-                       keys = (char **) realloc ((void *) keys,
+                       char **tmp = (char **) realloc ((void *) keys,
                                        (keys_num + 1) * sizeof (char *));
-                       if (keys == NULL)
+                       if (tmp == NULL)
                        {
                                char errbuf[1024];
                                ERROR ("rrdtool plugin: "
                                                "realloc failed: %s",
                                                sstrerror (errno, errbuf,
                                                        sizeof (errbuf)));
-                               avl_iterator_destroy (iter);
+                               c_avl_iterator_destroy (iter);
+                               sfree (keys);
                                return;
                        }
+                       keys = tmp;
                        keys[keys_num] = key;
                        keys_num++;
                }
-       } /* while (avl_iterator_next) */
-       avl_iterator_destroy (iter);
+       } /* while (c_avl_iterator_next) */
+       c_avl_iterator_destroy (iter);
        
        for (i = 0; i < keys_num; i++)
        {
-               if (avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
+               if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
                {
-                       DEBUG ("rrdtool plugin: avl_remove (%s) failed.", keys[i]);
+                       DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
                        continue;
                }
 
@@ -771,7 +773,7 @@ static void rrd_cache_flush (int timeout)
                keys[i] = NULL;
        } /* for (i = 0..keys_num) */
 
-       free (keys);
+       sfree (keys);
 
        cache_flush_last = now;
 } /* void rrd_cache_flush */
@@ -785,7 +787,7 @@ static int rrd_cache_insert (const char *filename,
 
        pthread_mutex_lock (&cache_lock);
 
-       avl_get (cache, filename, (void *) &rc);
+       c_avl_get (cache, filename, (void *) &rc);
 
        if (rc == NULL)
        {
@@ -818,7 +820,7 @@ static int rrd_cache_insert (const char *filename,
 
                sstrerror (errno, errbuf, sizeof (errbuf));
 
-               avl_remove (cache, filename, &cache_key, NULL);
+               c_avl_remove (cache, filename, &cache_key, NULL);
                pthread_mutex_unlock (&cache_lock);
 
                ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
@@ -858,7 +860,7 @@ static int rrd_cache_insert (const char *filename,
                        return (-1);
                }
 
-               avl_insert (cache, cache_key, rc);
+               c_avl_insert (cache, cache_key, rc);
        }
 
        DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
@@ -891,6 +893,19 @@ static int rrd_cache_insert (const char *filename,
        return (0);
 } /* int rrd_cache_insert */
 
+static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
+{
+       int a = *((int *) a_ptr);
+       int b = *((int *) b_ptr);
+
+       if (a < b)
+               return (-1);
+       else if (a > b)
+               return (1);
+       else
+               return (0);
+} /* int rrd_compare_numeric */
+
 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
 {
        struct stat  statbuf;
@@ -932,6 +947,20 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl)
        return (status);
 } /* int rrd_write */
 
+static int rrd_flush (const int timeout)
+{
+       pthread_mutex_lock (&cache_lock);
+
+       if (cache == NULL) {
+               pthread_mutex_unlock (&cache_lock);
+               return (0);
+       }
+
+       rrd_cache_flush (timeout);
+       pthread_mutex_unlock (&cache_lock);
+       return (0);
+} /* int rrd_flush */
+
 static int rrd_config (const char *key, const char *value)
 {
        if (strcasecmp ("CacheTimeout", key) == 0)
@@ -941,6 +970,8 @@ static int rrd_config (const char *key, const char *value)
                {
                        fprintf (stderr, "rrdtool: `CacheTimeout' must "
                                        "be greater than 0.\n");
+                       ERROR ("rrdtool: `CacheTimeout' must "
+                                       "be greater than 0.\n");
                        return (1);
                }
                cache_timeout = tmp;
@@ -952,6 +983,8 @@ static int rrd_config (const char *key, const char *value)
                {
                        fprintf (stderr, "rrdtool: `CacheFlush' must "
                                        "be greater than 0.\n");
+                       ERROR ("rrdtool: `CacheFlush' must "
+                                       "be greater than 0.\n");
                        return (1);
                }
                cache_flush_timeout = tmp;
@@ -995,6 +1028,8 @@ static int rrd_config (const char *key, const char *value)
                {
                        fprintf (stderr, "rrdtool: `RRARows' must "
                                        "be greater than 0.\n");
+                       ERROR ("rrdtool: `RRARows' must "
+                                       "be greater than 0.\n");
                        return (1);
                }
                rrarows = tmp;
@@ -1021,6 +1056,7 @@ static int rrd_config (const char *key, const char *value)
                        if (tmp_alloc == NULL)
                        {
                                fprintf (stderr, "rrdtool: realloc failed.\n");
+                               ERROR ("rrdtool: realloc failed.\n");
                                free (value_copy);
                                return (1);
                        }
@@ -1029,6 +1065,12 @@ static int rrd_config (const char *key, const char *value)
                        if (rra_timespans_custom[rra_timespans_custom_num] != 0)
                                rra_timespans_custom_num++;
                } /* while (strtok_r) */
+
+               qsort (/* base = */ rra_timespans_custom,
+                               /* nmemb  = */ rra_timespans_custom_num,
+                               /* size   = */ sizeof (rra_timespans_custom[0]),
+                               /* compar = */ rrd_compare_numeric);
+
                free (value_copy);
        }
        else if (strcasecmp ("XFF", key) == 0)
@@ -1038,6 +1080,8 @@ static int rrd_config (const char *key, const char *value)
                {
                        fprintf (stderr, "rrdtool: `XFF' must "
                                        "be in the range 0 to 1 (exclusive).");
+                       ERROR ("rrdtool: `XFF' must "
+                                       "be in the range 0 to 1 (exclusive).");
                        return (1);
                }
                xff = tmp;
@@ -1078,12 +1122,7 @@ static int rrd_init (void)
        if (stepsize < 0)
                stepsize = 0;
        if (heartbeat <= 0)
-       {
-               if (stepsize > 0)
-                       heartbeat = 2 * stepsize;
-               else
-                       heartbeat = 0;
-       }
+               heartbeat = 2 * stepsize;
 
        if ((heartbeat > 0) && (heartbeat < interval_g))
                WARNING ("rrdtool plugin: Your `heartbeat' is "
@@ -1097,10 +1136,10 @@ static int rrd_init (void)
        /* Set the cache up */
        pthread_mutex_lock (&cache_lock);
 
-       cache = avl_create ((int (*) (const void *, const void *)) strcmp);
+       cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
        if (cache == NULL)
        {
-               ERROR ("rrdtool plugin: avl_create failed.");
+               ERROR ("rrdtool plugin: c_avl_create failed.");
                return (-1);
        }
 
@@ -1136,5 +1175,6 @@ void module_register (void)
                        config_keys, config_keys_num);
        plugin_register_init ("rrdtool", rrd_init);
        plugin_register_write ("rrdtool", rrd_write);
+       plugin_register_flush ("rrdtool", rrd_flush);
        plugin_register_shutdown ("rrdtool", rrd_shutdown);
 }