rrdtool plugin: Implemented the settings `StepSize', `HeartBeat', `RRARows', and...
[collectd.git] / src / rrdtool.c
index 1c7d662..80fc767 100644 (file)
@@ -59,30 +59,35 @@ static int rra_timespans[] =
        86400,
        604800,
        2678400,
-       31622400,
-       0
+       31622400
 };
-static int rra_timespans_num = 5;
+static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
 
 static char *rra_types[] =
 {
        "AVERAGE",
        "MIN",
-       "MAX",
-       NULL
+       "MAX"
 };
-static int rra_types_num = 3;
+static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
 
 static const char *config_keys[] =
 {
        "CacheTimeout",
        "CacheFlush",
        "DataDir",
-       NULL
+       "StepSize",
+       "HeartBeat",
+       "RRARows",
+       "XFF"
 };
-static int config_keys_num = 3;
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
-static char *datadir = NULL;
+static char   *datadir   = NULL;
+static int     stepsize  = 0;
+static int     heartbeat = 0;
+static int     rrarows   = 1200;
+static double  xff       = 0.1;
 
 static int         cache_timeout = 0;
 static int         cache_flush_timeout = 0;
@@ -99,8 +104,6 @@ static int rra_get (char ***ret)
 
        int rra_max = rra_timespans_num * rra_types_num;
 
-       int step;
-       int rows;
        int span;
 
        int cdp_num;
@@ -119,10 +122,7 @@ static int rra_get (char ***ret)
                return (-1);
        memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
 
-       step = atoi (COLLECTD_STEP);
-       rows = atoi (COLLECTD_ROWS);
-
-       if ((step <= 0) || (rows <= 0))
+       if ((stepsize <= 0) || (rrarows <= 0))
        {
                *ret = NULL;
                return (-1);
@@ -133,23 +133,25 @@ static int rra_get (char ***ret)
        {
                span = rra_timespans[i];
 
-               if ((span / step) < rows)
+               if ((span / stepsize) < rrarows)
                        continue;
 
                if (cdp_len == 0)
                        cdp_len = 1;
                else
-                       cdp_len = (int) floor (((double) span) / ((double) (rows * step)));
+                       cdp_len = (int) floor (((double) span)
+                                       / ((double) (rrarows * stepsize)));
 
-               cdp_num = (int) ceil (((double) span) / ((double) (cdp_len * step)));
+               cdp_num = (int) ceil (((double) span)
+                               / ((double) (cdp_len * stepsize)));
 
                for (j = 0; j < rra_types_num; j++)
                {
                        if (rra_num >= rra_max)
                                break;
 
-                       if (snprintf (buffer, sizeof(buffer), "RRA:%s:%3.1f:%u:%u",
-                                               rra_types[j], COLLECTD_XFF,
+                       if (snprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
+                                               rra_types[j], xff,
                                                cdp_len, cdp_num) >= sizeof (buffer))
                        {
                                syslog (LOG_ERR, "rra_get: Buffer would have been truncated.");
@@ -240,8 +242,8 @@ static int ds_get (char ***ret, const data_set_t *ds)
                }
 
                status = snprintf (buffer, sizeof (buffer),
-                               "DS:%s:%s:%s:%s:%s",
-                               d->name, type, COLLECTD_HEARTBEAT,
+                               "DS:%s:%s:%i:%s:%s",
+                               d->name, type, heartbeat,
                                min, max);
                if ((status < 1) || (status >= sizeof (buffer)))
                        break;
@@ -277,6 +279,7 @@ static int rrd_create_file (char *filename, const data_set_t *ds)
        char **ds_def;
        int ds_num;
        int i, j;
+       char stepsize_str[16];
        int status = 0;
 
        if (check_create_dir (filename))
@@ -302,10 +305,18 @@ static int rrd_create_file (char *filename, const data_set_t *ds)
                return (-1);
        }
 
+       status = snprintf (stepsize_str, sizeof (stepsize_str),
+                       "%i", stepsize);
+       if ((status < 1) || (status >= sizeof (stepsize_str)))
+       {
+               syslog (LOG_ERR, "rrdtool plugin: snprintf failed.");
+               return (-1);
+       }
+
        argv[0] = "create";
        argv[1] = filename;
        argv[2] = "-s";
-       argv[3] = COLLECTD_STEP;
+       argv[3] = stepsize_str;
 
        j = 4;
        for (i = 0; i < ds_num; i++)
@@ -412,6 +423,7 @@ static rrd_cache_t *rrd_cache_insert (const char *filename,
                const char *value)
 {
        rrd_cache_t *rc = NULL;
+       int new_rc = 0;
 
        if (cache != NULL)
                avl_get (cache, filename, (void *) &rc);
@@ -424,6 +436,7 @@ static rrd_cache_t *rrd_cache_insert (const char *filename,
                rc->values_num = 0;
                rc->values = NULL;
                rc->first_value = 0;
+               new_rc = 1;
        }
 
        rc->values = (char **) realloc ((void *) rc->values,
@@ -450,7 +463,7 @@ static rrd_cache_t *rrd_cache_insert (const char *filename,
                rc->first_value = time (NULL);
 
        /* Insert if this is the first value */
-       if ((cache != NULL) && (rc->values_num == 1))
+       if ((cache != NULL) && (new_rc == 1))
        {
                void *cache_key = strdup (filename);
 
@@ -458,6 +471,7 @@ static rrd_cache_t *rrd_cache_insert (const char *filename,
                {
                        syslog (LOG_ERR, "rrdtool plugin: strdup failed: %s",
                                        strerror (errno));
+                       sfree (rc->values[0]);
                        sfree (rc->values);
                        sfree (rc);
                        return (NULL);
@@ -479,6 +493,8 @@ static int rrd_write_cache_entry (const char *filename, rrd_cache_t *rc)
        char *fn;
        int status;
 
+       int i;
+
        argc = rc->values_num + 2;
        argv = (char **) malloc ((argc + 1) * sizeof (char *));
        if (argv == NULL)
@@ -505,6 +521,9 @@ static int rrd_write_cache_entry (const char *filename, rrd_cache_t *rc)
        free (argv);
        free (fn);
 
+       /* Free the value list of `rc' */
+       for (i = 0; i < rc->values_num; i++)
+               free (rc->values[i]);
        free (rc->values);
        rc->values = NULL;
        rc->values_num = 0;
@@ -517,7 +536,7 @@ static int rrd_write_cache_entry (const char *filename, rrd_cache_t *rc)
        }
 
        return (0);
-} /* int rrd_update_file */
+} /* int rrd_write_cache_entry */
 
 static void rrd_cache_flush (int timeout)
 {
@@ -564,16 +583,17 @@ static void rrd_cache_flush (int timeout)
        
        for (i = 0; i < keys_num; i++)
        {
-               if (avl_remove (cache, keys[i], NULL, (void *) &rc) != 0)
+               if (avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
                {
                        DBG ("avl_remove (%s) failed.", keys[i]);
                        continue;
                }
 
                rrd_write_cache_entry (keys[i], rc);
-               sfree (keys[i]); keys[i] = NULL;
-               sfree (rc->values);
+               /* rc's value-list is free's by `rrd_write_cache_entry' */
                sfree (rc);
+               sfree (key);
+               keys[i] = NULL;
        } /* for (i = 0..keys_num) */
 
        free (keys);
@@ -624,8 +644,8 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl)
        if (cache == NULL)
        {
                rrd_write_cache_entry (filename, rc);
-               free (rc->values);
-               free (rc);
+               /* rc's value-list is free's by `rrd_write_cache_entry' */
+               sfree (rc);
                return (0);
        }
 
@@ -633,22 +653,22 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl)
 
        DBG ("age (%s) = %i", filename, now - rc->first_value);
 
+       /* `rc' is not free'd here, because we'll likely reuse it. If not, then
+        * the next flush will remove this entry.  */
        if ((now - rc->first_value) >= cache_timeout)
                rrd_write_cache_entry (filename, rc);
 
        if ((now - cache_flush_last) >= cache_flush_timeout)
-       {
                rrd_cache_flush (cache_flush_timeout);
-       }
 
        return (0);
 } /* int rrd_write */
 
-static int rrd_config (const char *key, const char *val)
+static int rrd_config (const char *key, const char *value)
 {
        if (strcasecmp ("CacheTimeout", key) == 0)
        {
-               int tmp = atoi (val);
+               int tmp = atoi (value);
                if (tmp < 0)
                {
                        fprintf (stderr, "rrdtool: `CacheTimeout' must "
@@ -659,7 +679,7 @@ static int rrd_config (const char *key, const char *val)
        }
        else if (strcasecmp ("CacheFlush", key) == 0)
        {
-               int tmp = atoi (val);
+               int tmp = atoi (value);
                if (tmp < 0)
                {
                        fprintf (stderr, "rrdtool: `CacheFlush' must "
@@ -672,7 +692,7 @@ static int rrd_config (const char *key, const char *val)
        {
                if (datadir != NULL)
                        free (datadir);
-               datadir = strdup (val);
+               datadir = strdup (value);
                if (datadir != NULL)
                {
                        int len = strlen (datadir);
@@ -688,6 +708,50 @@ static int rrd_config (const char *key, const char *val)
                        }
                }
        }
+       else if (strcasecmp ("StepSize", key) == 0)
+       {
+               int tmp = atoi (value);
+               if (tmp <= 0)
+               {
+                       fprintf (stderr, "rrdtool: `StepSize' must "
+                                       "be greater than 0.\n");
+                       return (1);
+               }
+               stepsize = tmp;
+       }
+       else if (strcasecmp ("HeartBeat", key) == 0)
+       {
+               int tmp = atoi (value);
+               if (tmp <= 0)
+               {
+                       fprintf (stderr, "rrdtool: `HeartBeat' must "
+                                       "be greater than 0.\n");
+                       return (1);
+               }
+               heartbeat = tmp;
+       }
+       else if (strcasecmp ("RRARows", key) == 0)
+       {
+               int tmp = atoi (value);
+               if (tmp <= 0)
+               {
+                       fprintf (stderr, "rrdtool: `RRARows' must "
+                                       "be greater than 0.\n");
+                       return (1);
+               }
+               rrarows = tmp;
+       }
+       else if (strcasecmp ("XFF", key) == 0)
+       {
+               double tmp = atof (value);
+               if ((tmp < 0.0) || (tmp >= 1.0))
+               {
+                       fprintf (stderr, "rrdtool: `XFF' must "
+                                       "be in the range 0 to 1 (exclusive).");
+                       return (1);
+               }
+               xff = tmp;
+       }
        else
        {
                return (-1);
@@ -698,12 +762,29 @@ static int rrd_config (const char *key, const char *val)
 static int rrd_shutdown (void)
 {
        rrd_cache_flush (-1);
+       if (cache != NULL)
+               avl_destroy (cache);
+       cache = NULL;
 
        return (0);
 } /* int rrd_shutdown */
 
 static int rrd_init (void)
 {
+       if (stepsize <= 0)
+               stepsize = interval_g;
+       if (heartbeat <= 0)
+               heartbeat = 2 * interval_g;
+
+       if (heartbeat < interval_g)
+               syslog (LOG_WARNING, "rrdtool plugin: Your `heartbeat' is "
+                               "smaller than your `interval'. This will "
+                               "likely cause problems.");
+       else if (stepsize < interval_g)
+               syslog (LOG_WARNING, "rrdtool plugin: Your `stepsize' is "
+                               "smaller than your `interval'. This will "
+                               "create needlessly big RRD-files.");
+
        if (cache_timeout < 2)
        {
                cache_timeout = 0;
@@ -718,6 +799,11 @@ static int rrd_init (void)
                cache_flush_last = time (NULL);
                plugin_register_shutdown ("rrdtool", rrd_shutdown);
        }
+
+       DBG ("datadir = %s; stepsize = %i; heartbeat = %i; rrarows = %i; xff = %lf;",
+                       (datadir == NULL) ? "(null)" : datadir,
+                       stepsize, heartbeat, rrarows, xff);
+
        return (0);
 } /* int rrd_init */