Code

apple_sensors plugin: Fix the `module_register' function.
[collectd.git] / src / rrdtool.c
index b221341ca4925e002b099381cf960f1bf5d4eb58..52557c0918dc4bb88e229b40992c863004f7d772 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/rrdtool.c
- * Copyright (C) 2006  Florian octo Forster
+ * Copyright (C) 2006,2007  Florian octo Forster
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -52,6 +52,9 @@ static int rra_timespans[] =
 };
 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
 
+static int *rra_timespans_custom = NULL;
+static int rra_timespans_custom_num = 0;
+
 static char *rra_types[] =
 {
        "AVERAGE",
@@ -68,6 +71,7 @@ static const char *config_keys[] =
        "StepSize",
        "HeartBeat",
        "RRARows",
+       "RRATimespan",
        "XFF"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
@@ -92,7 +96,10 @@ static int rra_get (char ***ret)
        static char **rra_def = NULL;
        static int rra_num = 0;
 
-       int rra_max = rra_timespans_num * rra_types_num;
+       int *rts;
+       int  rts_num;
+
+       int rra_max;
 
        int span;
 
@@ -108,6 +115,20 @@ static int rra_get (char ***ret)
                return (rra_num);
        }
 
+       /* Use the configured timespans or fall back to the built-in defaults */
+       if (rra_timespans_custom_num != 0)
+       {
+               rts = rra_timespans_custom;
+               rts_num = rra_timespans_custom_num;
+       }
+       else
+       {
+               rts = rra_timespans;
+               rts_num = rra_timespans_num;
+       }
+
+       rra_max = rts_num * rra_types_num;
+
        if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
                return (-1);
        memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
@@ -119,9 +140,9 @@ static int rra_get (char ***ret)
        }
 
        cdp_len = 0;
-       for (i = 0; i < rra_timespans_num; i++)
+       for (i = 0; i < rts_num; i++)
        {
-               span = rra_timespans[i];
+               span = rts[i];
 
                if ((span / stepsize) < rrarows)
                        continue;
@@ -746,6 +767,38 @@ static int rrd_config (const char *key, const char *value)
                }
                rrarows = tmp;
        }
+       else if (strcasecmp ("RRATimespan", key) == 0)
+       {
+               char *saveptr = NULL;
+               char *dummy;
+               char *ptr;
+               char *value_copy;
+               int *tmp_alloc;
+
+               value_copy = strdup (value);
+               if (value_copy == NULL)
+                       return (1);
+
+               dummy = value_copy;
+               while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
+               {
+                       dummy = NULL;
+                       
+                       tmp_alloc = realloc (rra_timespans_custom,
+                                       sizeof (int) * (rra_timespans_custom_num + 1));
+                       if (tmp_alloc == NULL)
+                       {
+                               fprintf (stderr, "rrdtool: realloc failed.\n");
+                               free (value_copy);
+                               return (1);
+                       }
+                       rra_timespans_custom = tmp_alloc;
+                       rra_timespans_custom[rra_timespans_custom_num] = atoi (ptr);
+                       if (rra_timespans_custom[rra_timespans_custom_num] != 0)
+                               rra_timespans_custom_num++;
+               } /* while (strtok_r) */
+               free (value_copy);
+       }
        else if (strcasecmp ("XFF", key) == 0)
        {
                double tmp = atof (value);
@@ -817,7 +870,7 @@ static int rrd_init (void)
        return (0);
 } /* int rrd_init */
 
-void module_register (modreg_e load)
+void module_register (void)
 {
        plugin_register_config ("rrdtool", rrd_config,
                        config_keys, config_keys_num);