-/* * * * * * * * * *
- * WARNING: Magic *
- * * * * * * * * * */
-
-static void rra_free (int rra_num, char **rra_def)
-{
- int i;
-
- for (i = 0; i < rra_num; i++)
- {
- sfree (rra_def[i]);
- }
- sfree (rra_def);
-} /* void rra_free */
-
-static int rra_get (char ***ret, const value_list_t *vl)
-{
- char **rra_def;
- int rra_num;
-
- int *rts;
- int rts_num;
-
- int rra_max;
-
- int span;
-
- int cdp_num;
- int cdp_len;
- int i, j;
-
- char buffer[64];
-
- /* The stepsize we use here: If it is user-set, use it. If not, use the
- * interval of the value-list. */
- int ss;
-
- if (rrarows <= 0)
- {
- *ret = NULL;
- return (-1);
- }
-
- ss = (stepsize > 0) ? stepsize : vl->interval;
- if (ss <= 0)
- {
- *ret = NULL;
- return (-1);
- }
-
- /* 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 *));
- rra_num = 0;
-
- cdp_len = 0;
- for (i = 0; i < rts_num; i++)
- {
- span = rts[i];
-
- if ((span / ss) < rrarows)
- span = ss * rrarows;
-
- if (cdp_len == 0)
- cdp_len = 1;
- else
- cdp_len = (int) floor (((double) span)
- / ((double) (rrarows * ss)));
-
- cdp_num = (int) ceil (((double) span)
- / ((double) (cdp_len * ss)));
-
- for (j = 0; j < rra_types_num; j++)
- {
- if (rra_num >= rra_max)
- break;
-
- if (ssnprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
- rra_types[j], xff,
- cdp_len, cdp_num) >= sizeof (buffer))
- {
- ERROR ("rra_get: Buffer would have been truncated.");
- continue;
- }
-
- rra_def[rra_num++] = sstrdup (buffer);
- }
- }
-
-#if COLLECT_DEBUG
- DEBUG ("rra_num = %i", rra_num);
- for (i = 0; i < rra_num; i++)
- DEBUG (" %s", rra_def[i]);
-#endif
-
- *ret = rra_def;
- return (rra_num);
-} /* int rra_get */
-
-static void ds_free (int ds_num, char **ds_def)
-{
- int i;
-
- for (i = 0; i < ds_num; i++)
- if (ds_def[i] != NULL)
- free (ds_def[i]);
- free (ds_def);
-}
-
-static int ds_get (char ***ret, const data_set_t *ds, const value_list_t *vl)
-{
- char **ds_def;
- int ds_num;
-
- char min[32];
- char max[32];
- char buffer[128];
-
- DEBUG ("ds->ds_num = %i", ds->ds_num);
-
- ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
- if (ds_def == NULL)
- {
- char errbuf[1024];
- ERROR ("rrdtool plugin: malloc failed: %s",
- sstrerror (errno, errbuf, sizeof (errbuf)));
- return (-1);
- }
- memset (ds_def, '\0', ds->ds_num * sizeof (char *));
-
- for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
- {
- data_source_t *d = ds->ds + ds_num;
- char *type;
- int status;
-
- ds_def[ds_num] = NULL;
-
- if (d->type == DS_TYPE_COUNTER)
- type = "COUNTER";
- else if (d->type == DS_TYPE_GAUGE)
- type = "GAUGE";
- else
- {
- ERROR ("rrdtool plugin: Unknown DS type: %i",
- d->type);
- break;
- }
-
- if (isnan (d->min))
- {
- sstrncpy (min, "U", sizeof (min));
- }
- else
- ssnprintf (min, sizeof (min), "%lf", d->min);
-
- if (isnan (d->max))
- {
- sstrncpy (max, "U", sizeof (max));
- }
- else
- ssnprintf (max, sizeof (max), "%lf", d->max);
-
- status = ssnprintf (buffer, sizeof (buffer),
- "DS:%s:%s:%i:%s:%s",
- d->name, type,
- (heartbeat > 0) ? heartbeat : (2 * vl->interval),
- min, max);
- if ((status < 1) || (status >= sizeof (buffer)))
- break;
-
- ds_def[ds_num] = sstrdup (buffer);
- } /* for ds_num = 0 .. ds->ds_num */
-
-#if COLLECT_DEBUG
-{
- int i;
- DEBUG ("ds_num = %i", ds_num);
- for (i = 0; i < ds_num; i++)
- DEBUG (" %s", ds_def[i]);
-}
-#endif
-
- if (ds_num != ds->ds_num)
- {
- ds_free (ds_num, ds_def);
- return (-1);
- }
-
- *ret = ds_def;
- return (ds_num);
-}
-