+static int handle_request_fetch (HANDLER_PROTO) /* {{{ */
+{
+ char *file, file_tmp[PATH_MAX];
+ char *cf;
+
+ char *start_str;
+ char *end_str;
+ time_t start_tm;
+ time_t end_tm;
+
+ unsigned long step;
+ unsigned long ds_cnt;
+ char **ds_namv;
+ rrd_value_t *data;
+
+ int status;
+ unsigned long i;
+ time_t t;
+ rrd_value_t *data_ptr;
+
+ file = NULL;
+ cf = NULL;
+ start_str = NULL;
+ end_str = NULL;
+
+ /* Read the arguments */
+ do /* while (0) */
+ {
+ status = buffer_get_field (&buffer, &buffer_size, &file);
+ if (status != 0)
+ break;
+
+ status = buffer_get_field (&buffer, &buffer_size, &cf);
+ if (status != 0)
+ break;
+
+ status = buffer_get_field (&buffer, &buffer_size, &start_str);
+ if (status != 0)
+ {
+ start_str = NULL;
+ status = 0;
+ break;
+ }
+
+ status = buffer_get_field (&buffer, &buffer_size, &end_str);
+ if (status != 0)
+ {
+ end_str = NULL;
+ status = 0;
+ break;
+ }
+ } while (0);
+
+ if (status != 0)
+ return (syntax_error(sock,cmd));
+
+ get_abs_path(&file, file_tmp);
+ if (!check_file_access(file, sock)) return 0;
+
+ status = flush_file (file);
+ if ((status != 0) && (status != ENOENT))
+ return (send_response (sock, RESP_ERR,
+ "flush_file (%s) failed with status %i.\n", file, status));
+
+ t = time (NULL); /* "now" */
+
+ /* Parse start time */
+ if (start_str != NULL)
+ {
+ char *endptr;
+ long value;
+
+ endptr = NULL;
+ errno = 0;
+ value = strtol (start_str, &endptr, /* base = */ 0);
+ if ((endptr == start_str) || (errno != 0))
+ return (send_response(sock, RESP_ERR,
+ "Cannot parse start time `%s': Only simple integers are allowed.\n",
+ start_str));
+
+ if (value > 0)
+ start_tm = (time_t) value;
+ else
+ start_tm = (time_t) (t + value);
+ }
+ else
+ {
+ start_tm = t - 86400;
+ }
+
+ /* Parse end time */
+ if (end_str != NULL)
+ {
+ char *endptr;
+ long value;
+
+ endptr = NULL;
+ errno = 0;
+ value = strtol (end_str, &endptr, /* base = */ 0);
+ if ((endptr == end_str) || (errno != 0))
+ return (send_response(sock, RESP_ERR,
+ "Cannot parse end time `%s': Only simple integers are allowed.\n",
+ end_str));
+
+ if (value > 0)
+ end_tm = (time_t) value;
+ else
+ end_tm = (time_t) (t + value);
+ }
+ else
+ {
+ end_tm = t;
+ }
+
+ step = -1;
+ ds_cnt = 0;
+ ds_namv = NULL;
+ data = NULL;
+
+ status = rrd_fetch_r (file, cf, &start_tm, &end_tm, &step,
+ &ds_cnt, &ds_namv, &data);
+ if (status != 0)
+ return (send_response(sock, RESP_ERR,
+ "rrd_fetch_r failed: %s\n", rrd_get_error ()));
+
+ add_response_info (sock, "FlushVersion: %lu\n", 1);
+ add_response_info (sock, "Start: %lu\n", (unsigned long) start_tm);
+ add_response_info (sock, "End: %lu\n", (unsigned long) end_tm);
+ add_response_info (sock, "Step: %lu\n", step);
+ add_response_info (sock, "DSCount: %lu\n", ds_cnt);
+
+#define SSTRCAT(buffer,str,buffer_fill) do { \
+ size_t str_len = strlen (str); \
+ if ((buffer_fill + str_len) > sizeof (buffer)) \
+ str_len = sizeof (buffer) - buffer_fill; \
+ if (str_len > 0) { \
+ strncpy (buffer + buffer_fill, str, str_len); \
+ buffer_fill += str_len; \
+ assert (buffer_fill <= sizeof (buffer)); \
+ if (buffer_fill == sizeof (buffer)) \
+ buffer[buffer_fill - 1] = 0; \
+ else \
+ buffer[buffer_fill] = 0; \
+ } \
+ } while (0)
+
+ { /* Add list of DS names */
+ char linebuf[1024];
+ size_t linebuf_fill;
+
+ memset (linebuf, 0, sizeof (linebuf));
+ linebuf_fill = 0;
+ for (i = 0; i < ds_cnt; i++)
+ {
+ if (i > 0)
+ SSTRCAT (linebuf, " ", linebuf_fill);
+ SSTRCAT (linebuf, ds_namv[i], linebuf_fill);
+ rrd_freemem(ds_namv[i]);
+ }
+ rrd_freemem(ds_namv);
+ add_response_info (sock, "DSName: %s\n", linebuf);
+ }
+
+ /* Add the actual data */
+ assert (step > 0);
+ data_ptr = data;
+ for (t = start_tm + step; t <= end_tm; t += step)
+ {
+ char linebuf[1024];
+ size_t linebuf_fill;
+ char tmp[128];
+
+ memset (linebuf, 0, sizeof (linebuf));
+ linebuf_fill = 0;
+ for (i = 0; i < ds_cnt; i++)
+ {
+ snprintf (tmp, sizeof (tmp), " %0.10e", *data_ptr);
+ tmp[sizeof (tmp) - 1] = 0;
+ SSTRCAT (linebuf, tmp, linebuf_fill);
+
+ data_ptr++;
+ }
+
+ add_response_info (sock, "%10lu:%s\n", (unsigned long) t, linebuf);
+ } /* for (t) */
+ rrd_freemem(data);
+
+ return (send_response (sock, RESP_OK, "Success\n"));
+#undef SSTRCAT
+} /* }}} int handle_request_fetch */
+