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;
int rra_max = rra_timespans_num * rra_types_num;
- int step;
- int rows;
int span;
int cdp_num;
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);
{
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.");
}
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;
char **ds_def;
int ds_num;
int i, j;
+ char stepsize_str[16];
int status = 0;
if (check_create_dir (filename))
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++)
const char *value)
{
rrd_cache_t *rc = NULL;
+ int new_rc = 0;
if (cache != NULL)
avl_get (cache, filename, (void *) &rc);
rc->values_num = 0;
rc->values = NULL;
rc->first_value = 0;
+ new_rc = 1;
}
rc->values = (char **) realloc ((void *) rc->values,
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);
{
syslog (LOG_ERR, "rrdtool plugin: strdup failed: %s",
strerror (errno));
+ sfree (rc->values[0]);
sfree (rc->values);
sfree (rc);
return (NULL);
char *fn;
int status;
+ int i;
+
argc = rc->values_num + 2;
argv = (char **) malloc ((argc + 1) * sizeof (char *));
if (argv == NULL)
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;
}
return (0);
-} /* int rrd_update_file */
+} /* int rrd_write_cache_entry */
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);
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);
}
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 "
}
else if (strcasecmp ("CacheFlush", key) == 0)
{
- int tmp = atoi (val);
+ int tmp = atoi (value);
if (tmp < 0)
{
fprintf (stderr, "rrdtool: `CacheFlush' must "
{
if (datadir != NULL)
free (datadir);
- datadir = strdup (val);
+ datadir = strdup (value);
if (datadir != NULL)
{
int len = strlen (datadir);
}
}
}
+ 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);
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;
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 */