+rrd_info_t * rrdc_info (const char *filename) /* {{{ */
+{
+ char buffer[RRD_CMD_MAX];
+ char *buffer_ptr;
+ size_t buffer_free;
+ size_t buffer_size;
+ rrdc_response_t *res;
+ int status;
+ char file_path[PATH_MAX];
+ rrd_info_t *data = NULL, *cd;
+ rrd_infoval_t info;
+ unsigned int l;
+ rrd_info_type_t itype;
+ char *k, *s;
+
+ if (filename == NULL) {
+ rrd_set_error ("rrdc_info: no filename");
+ return (NULL);
+ }
+
+ memset (buffer, 0, sizeof (buffer));
+ buffer_ptr = &buffer[0];
+ buffer_free = sizeof (buffer);
+
+ status = buffer_add_string ("info", &buffer_ptr, &buffer_free);
+ if (status != 0) {
+ rrd_set_error ("rrdc_info: out of memory");
+ return (NULL);
+ }
+
+ pthread_mutex_lock (&lock);
+ filename = get_path (filename, file_path);
+ if (filename == NULL)
+ {
+ pthread_mutex_unlock (&lock);
+ return (NULL);
+ }
+
+ status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
+ if (status != 0)
+ {
+ pthread_mutex_unlock (&lock);
+ rrd_set_error ("rrdc_info: out of memory");
+ return (NULL);
+ }
+
+ assert (buffer_free < sizeof (buffer));
+ buffer_size = sizeof (buffer) - buffer_free;
+ assert (buffer[buffer_size - 1] == ' ');
+ buffer[buffer_size - 1] = '\n';
+
+ res = NULL;
+ status = request (buffer, buffer_size, &res);
+ pthread_mutex_unlock (&lock);
+
+ if (status != 0) {
+ rrd_set_error ("rrdcached: %s", res->message);
+ return (NULL);
+ }
+ data = cd = NULL;
+ for( l=0 ; l < res->lines_num ; l++ ) {
+ /* first extract the keyword */
+ for(k = s = res->lines[l];s && *s;s++) {
+ if(*s == ' ') { *s = 0; s++; break; }
+ }
+ if(!s || !*s) break;
+ itype = atoi(s); /* extract type code */
+ for(;*s;s++) { if(*s == ' ') { *s = 0; s++; break; } }
+ if(!*s) break;
+ /* finally, we're pointing to the value */
+ switch(itype) {
+ case RD_I_VAL:
+ if(*s == 'N') { info.u_val = DNAN; } else { info.u_val = atof(s); }
+ break;
+ case RD_I_CNT:
+ info.u_cnt = atol(s);
+ break;
+ case RD_I_INT:
+ info.u_int = atoi(s);
+ break;
+ case RD_I_STR:
+ chomp(s);
+ info.u_str = (char*)malloc(sizeof(char) * (strlen(s) + 1));
+ strcpy(info.u_str,s);
+ break;
+ case RD_I_BLO:
+ rrd_set_error ("rrdc_info: BLOB objects are not supported");
+ return (NULL);
+ default:
+ rrd_set_error ("rrdc_info: Unsupported info type %d",itype);
+ return (NULL);
+ }
+
+ cd = rrd_info_push(cd, sprintf_alloc("%s",k), itype, info);
+ if(!data) data = cd;
+ }
+ response_free (res);
+
+ return (data);
+} /* }}} int rrdc_info */
+
+time_t rrdc_last (const char *filename) /* {{{ */
+{
+ char buffer[RRD_CMD_MAX];
+ char *buffer_ptr;
+ size_t buffer_free;
+ size_t buffer_size;
+ rrdc_response_t *res;
+ int status;
+ char file_path[PATH_MAX];
+ time_t lastup;
+
+ if (filename == NULL) {
+ rrd_set_error ("rrdc_last: no filename");
+ return (-1);
+ }
+
+ memset (buffer, 0, sizeof (buffer));
+ buffer_ptr = &buffer[0];
+ buffer_free = sizeof (buffer);
+
+ status = buffer_add_string ("last", &buffer_ptr, &buffer_free);
+ if (status != 0) {
+ rrd_set_error ("rrdc_last: out of memory");
+ return (-1);
+ }
+
+ pthread_mutex_lock (&lock);
+ filename = get_path (filename, file_path);
+ if (filename == NULL)
+ {
+ pthread_mutex_unlock (&lock);
+ return (-1);
+ }
+
+ status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
+ if (status != 0)
+ {
+ pthread_mutex_unlock (&lock);
+ rrd_set_error ("rrdc_last: out of memory");
+ return (-1);
+ }
+
+ assert (buffer_free < sizeof (buffer));
+ buffer_size = sizeof (buffer) - buffer_free;
+ assert (buffer[buffer_size - 1] == ' ');
+ buffer[buffer_size - 1] = '\n';
+
+ res = NULL;
+ status = request (buffer, buffer_size, &res);
+ pthread_mutex_unlock (&lock);
+
+ if (status != 0) {
+ rrd_set_error ("rrdcached: %s", res->message);
+ return (-1);
+ }
+ lastup = atol(res->message);
+ response_free (res);
+
+ return (lastup);
+} /* }}} int rrdc_last */
+
+time_t rrdc_first (const char *filename, int rraindex) /* {{{ */
+{
+ char buffer[RRD_CMD_MAX];
+ char *buffer_ptr;
+ size_t buffer_free;
+ size_t buffer_size;
+ rrdc_response_t *res;
+ int status;
+ char file_path[PATH_MAX];
+ time_t firstup;
+
+ if (filename == NULL) {
+ rrd_set_error ("rrdc_first: no filename specified");
+ return (-1);
+ }
+
+ memset (buffer, 0, sizeof (buffer));
+ buffer_ptr = &buffer[0];
+ buffer_free = sizeof (buffer);
+
+ status = buffer_add_string ("first", &buffer_ptr, &buffer_free);
+ if (status != 0) {
+ rrd_set_error ("rrdc_first: out of memory");
+ return (-1);
+ }
+
+ pthread_mutex_lock (&lock);
+ filename = get_path (filename, file_path);
+ if (filename == NULL)
+ {
+ pthread_mutex_unlock (&lock);
+ return (-1);
+ }
+
+ status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
+ if (status != 0)
+ {
+ pthread_mutex_unlock (&lock);
+ rrd_set_error ("rrdc_first: out of memory");
+ return (-1);
+ }
+ status = buffer_add_ulong (rraindex, &buffer_ptr, &buffer_free);
+ if (status != 0)
+ {
+ pthread_mutex_unlock (&lock);
+ rrd_set_error ("rrdc_first: out of memory");
+ return (-1);
+ }
+
+ assert (buffer_free < sizeof (buffer));
+ buffer_size = sizeof (buffer) - buffer_free;
+ assert (buffer[buffer_size - 1] == ' ');
+ buffer[buffer_size - 1] = '\n';
+
+ res = NULL;
+ status = request (buffer, buffer_size, &res);
+ pthread_mutex_unlock (&lock);
+
+ if (status != 0) {
+ rrd_set_error ("rrdcached: %s", res->message);
+ return (-1);
+ }
+ firstup = atol(res->message);
+ response_free (res);
+
+ return (firstup);
+} /* }}} int rrdc_first */
+
+int rrdc_create (const char *filename, /* {{{ */
+ unsigned long pdp_step,
+ time_t last_up,
+ int no_overwrite,
+ int argc,
+ const char **argv)
+{
+ char buffer[RRD_CMD_MAX];
+ char *buffer_ptr;
+ size_t buffer_free;
+ size_t buffer_size;
+ rrdc_response_t *res;
+ int status;
+ char file_path[PATH_MAX];
+ int i;
+
+ if (filename == NULL) {
+ rrd_set_error ("rrdc_create: no filename specified");
+ return (-1);
+ }
+
+ memset (buffer, 0, sizeof (buffer));
+ buffer_ptr = &buffer[0];
+ buffer_free = sizeof (buffer);
+
+ status = buffer_add_string ("create", &buffer_ptr, &buffer_free);
+ if (status != 0) {
+ rrd_set_error ("rrdc_create: out of memory");
+ return (-1);
+ }
+
+ pthread_mutex_lock (&lock);
+ filename = get_path (filename, file_path);
+ if (filename == NULL)
+ {
+ pthread_mutex_unlock (&lock);
+ return (-1);
+ }
+
+ status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
+ status = buffer_add_string ("-b", &buffer_ptr, &buffer_free);
+ status = buffer_add_ulong (last_up, &buffer_ptr, &buffer_free);
+ status = buffer_add_string ("-s", &buffer_ptr, &buffer_free);
+ status = buffer_add_ulong (pdp_step, &buffer_ptr, &buffer_free);
+ if(no_overwrite) {
+ status = buffer_add_string ("-O", &buffer_ptr, &buffer_free);
+ }
+ if (status != 0)
+ {
+ pthread_mutex_unlock (&lock);
+ rrd_set_error ("rrdc_create: out of memory");
+ return (-1);
+ }
+
+ for( i=0; i<argc; i++ ) {
+ if( argv[i] ) {
+ status = buffer_add_string (argv[i], &buffer_ptr, &buffer_free);
+ if (status != 0)
+ {
+ pthread_mutex_unlock (&lock);
+ rrd_set_error ("rrdc_create: out of memory");
+ return (-1);
+ }
+ }
+ }
+
+ /* buffer ready to send? */
+ assert (buffer_free < sizeof (buffer));
+ buffer_size = sizeof (buffer) - buffer_free;
+ assert (buffer[buffer_size - 1] == ' ');
+ buffer[buffer_size - 1] = '\n';
+
+ res = NULL;
+ status = request (buffer, buffer_size, &res);
+ pthread_mutex_unlock (&lock);
+
+ if (status != 0) {
+ rrd_set_error ("rrdcached: %s", res->message);
+ return (-1);
+ }
+ response_free (res);
+ return(0);
+} /* }}} int rrdc_create */
+
+int rrdc_fetch (const char *filename, /* {{{ */
+ const char *cf,
+ time_t *ret_start, time_t *ret_end,
+ unsigned long *ret_step,
+ unsigned long *ret_ds_num,
+ char ***ret_ds_names,
+ rrd_value_t **ret_data)
+{
+ char buffer[RRD_CMD_MAX];
+ char *buffer_ptr;
+ size_t buffer_free;
+ size_t buffer_size;
+ rrdc_response_t *res;
+ char path_buffer[PATH_MAX];
+ const char *path_ptr;
+
+ char *str_tmp;
+ unsigned long flush_version;
+
+ time_t start;
+ time_t end;
+ unsigned long step;
+ unsigned long ds_num;
+ char **ds_names;
+
+ rrd_value_t *data;
+ size_t data_size;
+ size_t data_fill;
+
+ int status;
+ size_t current_line;
+ time_t t;
+
+ if ((filename == NULL) || (cf == NULL))
+ return (-1);
+
+ /* Send request {{{ */
+ memset (buffer, 0, sizeof (buffer));
+ buffer_ptr = &buffer[0];
+ buffer_free = sizeof (buffer);
+
+ status = buffer_add_string ("FETCH", &buffer_ptr, &buffer_free);
+ if (status != 0)
+ return (ENOBUFS);
+
+ /* change to path for rrdcached */
+ path_ptr = get_path (filename, path_buffer);
+ if (path_ptr == NULL)
+ return (EINVAL);
+
+ status = buffer_add_string (path_ptr, &buffer_ptr, &buffer_free);
+ if (status != 0)
+ return (ENOBUFS);
+
+ status = buffer_add_string (cf, &buffer_ptr, &buffer_free);
+ if (status != 0)
+ return (ENOBUFS);
+
+ if ((ret_start != NULL) && (*ret_start > 0))
+ {
+ char tmp[64];
+ snprintf (tmp, sizeof (tmp), "%lu", (unsigned long) *ret_start);
+ tmp[sizeof (tmp) - 1] = 0;
+ status = buffer_add_string (tmp, &buffer_ptr, &buffer_free);
+ if (status != 0)
+ return (ENOBUFS);
+
+ if ((ret_end != NULL) && (*ret_end > 0))
+ {
+ snprintf (tmp, sizeof (tmp), "%lu", (unsigned long) *ret_end);
+ tmp[sizeof (tmp) - 1] = 0;
+ status = buffer_add_string (tmp, &buffer_ptr, &buffer_free);
+ if (status != 0)
+ return (ENOBUFS);
+ }
+ }
+
+ assert (buffer_free < sizeof (buffer));
+ buffer_size = sizeof (buffer) - buffer_free;
+ assert (buffer[buffer_size - 1] == ' ');
+ buffer[buffer_size - 1] = '\n';
+
+ res = NULL;
+ status = request (buffer, buffer_size, &res);
+ if (status != 0)
+ return (status);
+
+ status = res->status;
+ if (status < 0)
+ {
+ rrd_set_error ("rrdcached: %s", res->message);
+ response_free (res);
+ return (status);
+ }
+ /* }}} Send request */
+
+ ds_names = NULL;
+ ds_num = 0;
+ data = NULL;
+ current_line = 0;
+
+ /* Macros to make error handling a little easier (i. e. less to type and
+ * read. `BAIL_OUT' sets the error message, frees all dynamically allocated
+ * variables and returns the provided status code. */
+#define BAIL_OUT(status, ...) do { \
+ rrd_set_error ("rrdc_fetch: " __VA_ARGS__); \
+ free (data); \
+ if (ds_names != 0) { size_t k; for (k = 0; k < ds_num; k++) free (ds_names[k]); } \
+ free (ds_names); \
+ response_free (res); \
+ return (status); \
+ } while (0)
+
+#define READ_NUMERIC_FIELD(name,type,var) do { \
+ char *key; \
+ unsigned long value; \
+ assert (current_line < res->lines_num); \
+ status = parse_ulong_header (res->lines[current_line], &key, &value); \
+ if (status != 0) \
+ BAIL_OUT (-1, "Unable to parse header `%s'", name); \
+ if (strcasecmp (key, name) != 0) \
+ BAIL_OUT (-1, "Unexpected header line: Expected `%s', got `%s'", name, key); \
+ var = (type) value; \
+ current_line++; \
+ } while (0)
+
+ if (res->lines_num < 1)
+ BAIL_OUT (-1, "Premature end of response packet");
+
+ /* We're making some very strong assumptions about the fields below. We
+ * therefore check the version of the `flush' command first, so that later
+ * versions can change the order of fields and it's easier to implement
+ * backwards compatibility. */
+ READ_NUMERIC_FIELD ("FlushVersion", unsigned long, flush_version);
+ if (flush_version != 1)
+ BAIL_OUT (-1, "Don't know how to handle flush format version %lu.",
+ flush_version);
+
+ if (res->lines_num < 5)
+ BAIL_OUT (-1, "Premature end of response packet");
+
+ READ_NUMERIC_FIELD ("Start", time_t, start);
+ READ_NUMERIC_FIELD ("End", time_t, end);
+ if (start >= end)
+ BAIL_OUT (-1, "Malformed start and end times: start = %lu; end = %lu;",
+ (unsigned long) start,
+ (unsigned long) end);
+
+ READ_NUMERIC_FIELD ("Step", unsigned long, step);
+ if (step < 1)
+ BAIL_OUT (-1, "Invalid number for Step: %lu", step);
+
+ READ_NUMERIC_FIELD ("DSCount", unsigned long, ds_num);
+ if (ds_num < 1)
+ BAIL_OUT (-1, "Invalid number for DSCount: %lu", ds_num);
+
+ /* It's time to allocate some memory */
+ ds_names = calloc ((size_t) ds_num, sizeof (*ds_names));
+ if (ds_names == NULL)
+ BAIL_OUT (-1, "Out of memory");
+
+ status = parse_char_array_header (res->lines[current_line],
+ &str_tmp, ds_names, (size_t) ds_num, /* alloc = */ 1);
+ if (status != 0)
+ BAIL_OUT (-1, "Unable to parse header `DSName'");
+ if (strcasecmp ("DSName", str_tmp) != 0)
+ BAIL_OUT (-1, "Unexpected header line: Expected `DSName', got `%s'", str_tmp);
+ current_line++;
+
+ data_size = ds_num * (end - start) / step;
+ if (data_size < 1)
+ BAIL_OUT (-1, "No data returned or headers invalid.");
+
+ if (res->lines_num != (6 + (data_size / ds_num)))
+ BAIL_OUT (-1, "Got %zu lines, expected %zu",
+ res->lines_num, (6 + (data_size / ds_num)));
+
+ data = calloc (data_size, sizeof (*data));
+ if (data == NULL)
+ BAIL_OUT (-1, "Out of memory");
+
+
+ data_fill = 0;
+ for (t = start + step; t <= end; t += step, current_line++)
+ {
+ time_t tmp;
+
+ assert (current_line < res->lines_num);
+
+ status = parse_value_array_header (res->lines[current_line],
+ &tmp, data + data_fill, (size_t) ds_num);
+ if (status != 0)
+ BAIL_OUT (-1, "Cannot parse value line");
+
+ data_fill += (size_t) ds_num;
+ }
+
+ *ret_start = start;
+ *ret_end = end;
+ *ret_step = step;
+ *ret_ds_num = ds_num;
+ *ret_ds_names = ds_names;
+ *ret_data = data;
+
+ response_free (res);
+ return (0);
+#undef READ_NUMERIC_FIELD
+#undef BAIL_OUT
+} /* }}} int rrdc_flush */