rrd_info_t *rrd_info(
    int argc,
    char **argv)
{
    struct optparse_long longopts[] = {
        {"daemon", 'd', OPTPARSE_REQUIRED},
        {"noflush", 'F', OPTPARSE_NONE},
        {0},
    };
    struct    optparse options;
    int       opt;
    rrd_info_t *info;
    char *opt_daemon = NULL;
    int status;
    int flushfirst = 1;

    optparse_init(&options, argc, argv);
    while ((opt = optparse_long(&options, longopts, NULL)) != -1) {
        switch (opt) {
        case 'd':
            if (opt_daemon != NULL)
                free (opt_daemon);
            opt_daemon = strdup(options.optarg);
            if (opt_daemon == NULL)
            {
                rrd_set_error ("strdup failed.");
                return NULL;
            }
            break;

        case 'F':
            flushfirst = 0;
            break;

        case '?':
            rrd_set_error("%s", options.errmsg);
            return NULL;
        }
    } /* while (opt != -1) */

    if (options.argc - options.optind != 1) {
        rrd_set_error ("Usage: rrdtool %s [--daemon |-d <addr> [--noflush|-F]] <file>",
                options.argv[0]);
        return NULL;
    }

    if (flushfirst) {
        status = rrdc_flush_if_daemon(opt_daemon, options.argv[options.optind]);
        if (status) return (NULL);
    }

    rrdc_connect (opt_daemon);
    if (rrdc_is_connected (opt_daemon))
        info = rrdc_info(options.argv[options.optind]);
    else
        info = rrd_info_r(options.argv[options.optind]);

    if (opt_daemon) free(opt_daemon);
    return (info);
} /* rrd_info_t *rrd_info */
Exemple #2
0
SEXP smartImportRRD(SEXP filenameIn){

    time_t first;
    time_t last;
    time_t start;
    time_t end;

    time_t *startAr;


    unsigned long curStep;
    unsigned long ds_cnt;
    unsigned long step;

    int rraCnt;
    int status;
    int size;
    int i;
    int ds;
    int j;
    int timeStamp;

    char **ds_namv;
    const char *filename;

    rrd_value_t *data;

    rraInfo* rraInfoList;
    rraInfo* rraInfoTmp;

    rrd_info_t *rrdInfo;


    SEXP out;
    SEXP vec;
    SEXP rraSexpList; 
    SEXP rraNames;
    SEXP nam;
    SEXP rowNam;
    SEXP cls;


    filename  = CHAR(asChar(filenameIn));
    if (access(filename, F_OK) == -1) {
	printf("file does not exist\n");
	exit(0);
    }


    printf("calling rrd_last\n");
    last = rrd_last_r(filename);


    printf("calling rrd_info\n");
    rrdInfo = rrd_info_r(filename);

    if (rrdInfo == NULL) {
	printf("getting rrd info failed");
	exit(0);
    }


    printf("calling getrrainfo\n");
    rraInfoList = getRraInfo(rrdInfo, &rraCnt, &step);

    if (rraInfoList == NULL) {
	printf("getting rra info failed\n");
	free(rrdInfo);
	exit(0);

    }
    
    printf("rraCnt %d step %d last %d rraInfoList %p\n", rraCnt, step, last, rraInfoList);
    printRraInfo(rraInfoList);



    startAr = malloc(rraCnt * sizeof(time_t));

    if (startAr == NULL) {
	printf("memory allocation error");
	free(rrdInfo);
	freeRraInfo(rraInfoList);
	exit(0);
    }



    for (i = 0; i < rraCnt; i++) {
	startAr[i] = rrd_first_r(filename, i);
    }
    

    rraInfoTmp = rraInfoList;
    PROTECT(rraNames = allocVector(STRSXP, rraCnt));

    PROTECT(cls = allocVector(STRSXP, 1)); // class attribute
    SET_STRING_ELT(cls, 0, mkChar("data.frame"));


    out = PROTECT(allocVector(VECSXP, rraCnt));

    i = 0;

    printf("entering loop\n");
    while (rraInfoTmp) {

	start = startAr[i];
	end = last;
	curStep = step * rraInfoTmp->perRow;


	status = rrd_fetch_r(filename, rraInfoTmp->cf, &start, &end, &curStep, &ds_cnt, &ds_namv, &data);

	if (status != 0 || data == NULL) {
	    printf("error running rrd_fetch_r\n");
	    free(rrdInfo);
	    freeRraInfo(rraInfoList);
	    free(startAr);
	    if (data)
		free(data);
	    if (ds_namv) {
		for (int k = 0; k < sizeof(ds_namv)/sizeof(char*); k++) {
		    free(ds_namv[k]);
		}
		free(ds_namv);
	    }
	    //TODO unprotect how many times?
	    exit(0);
	}


	printf("size of data %d start %d end %d step %d ds_cnt %d\n", sizeof(data)/sizeof(rrd_value_t), start, end, curStep, ds_cnt);
	fflush(stdout);

	//rrd_fetch does not include start
	size = (end - start)/curStep - 1;
	printf("size %d\n", size);

	rraSexpList = PROTECT(allocVector(VECSXP, ds_cnt + 1));

	vec = PROTECT(allocVector(INTSXP, size));
	PROTECT(rowNam = allocVector(STRSXP, size));
	//rrd_fetch does not include start
	timeStamp = start + curStep;




	for (int j = 0; j < size; j++) {
	    INTEGER(vec)[j] = timeStamp;
	    timeStamp += curStep;

	}

	printf("setting row names\n");
	SET_VECTOR_ELT(rraSexpList, 0, vec);
	setAttrib(rraSexpList, R_RowNamesSymbol, vec);

	PROTECT(nam = allocVector(STRSXP, ds_cnt + 1));
	SET_STRING_ELT(nam, 0, mkChar("timestamp"));


	//TODO stick to row/columns convention
	for (ds = 0; ds < ds_cnt; ds++){
	    SET_STRING_ELT(nam, ds + 1, mkChar(ds_namv[ds]));
	    vec = PROTECT(allocVector(REALSXP, size));

	    for (j = 0; j < size; j++){
		REAL(vec)[j] = data[ds + j*ds_cnt];
	    }



	    printf("adding ds vector to data frame\n");
	    SET_VECTOR_ELT(rraSexpList, ds + 1, vec);
	}

	classgets(rraSexpList, cls);
	namesgets(rraSexpList, nam);



	printf("adding data frame to out\n");
	SET_VECTOR_ELT(out, i, rraSexpList);

	char rraNameString[80];
	char stepString[40];

	sprintf(stepString, "%d", curStep);
	strcpy(rraNameString, rraInfoTmp->cf);
	strcat(rraNameString, stepString);
	SET_STRING_ELT(rraNames, i, mkChar(rraNameString));


	rraInfoTmp = rraInfoTmp->next;

	i++;
	free(data);
    }

    setAttrib(out, R_NamesSymbol, rraNames);


    freeRraInfo(rraInfoList);
    free(startAr);
    free(rrdInfo);
    for (int k = 0; k < sizeof(ds_namv)/sizeof(char*); k++) {
	free(ds_namv[k]);
    }
    free(ds_namv);

    UNPROTECT((ds_cnt + 2)*rraCnt + 3);


    return out;

}
Exemple #3
0
static sdb_timeseries_info_t *
sdb_rrd_describe(const char *id, sdb_object_t *user_data)
{
	rrd_info_t *info, *iter;
	char filename[strlen(id) + 1];
	sdb_timeseries_info_t *ts_info;

	strncpy(filename, id, sizeof(filename));

	if (user_data) {
		/* -> use RRDCacheD */
		char *addr = SDB_OBJ_WRAPPER(user_data)->data;

		if (! rrdcached_connect(addr))
			return NULL;

#ifdef HAVE_RRD_CLIENT_H
		/* TODO: detect and use rrdc_info if possible */
		sdb_log(SDB_LOG_ERR, "DESCRIBE not yet supported via RRDCacheD");
		return NULL;
#endif
	}
	else {
		rrd_clear_error();
		info = rrd_info_r(filename);
	}
	if (! info) {
		sdb_log(SDB_LOG_ERR, "Failed to extract header information from '%s': %s",
				filename, rrd_get_error());
		return NULL;
	}

	ts_info = calloc(1, sizeof(*ts_info));
	if (! ts_info) {
		sdb_log(SDB_LOG_ERR, "Failed to allocate memory");
		rrd_info_free(info);
		return NULL;
	}

	for (iter = info; iter != NULL; iter = iter->next) {
		size_t len, n, m;
		char *ds_name;
		char **tmp;

		/* Parse the DS name. The raw value is not exposed via the rrd_info
		 * interface. */
		n = strlen("ds[");
		if (strncmp(iter->key, "ds[", n))
			continue;

		len = strlen(iter->key);
		m = strlen("].index");
		if ((len < m) || strcmp(iter->key + len - m, "].index"))
			continue;

		ds_name = iter->key + n;
		len -= n;
		ds_name[len - m] = '\0';

		/* Append the new datum. */
		tmp = realloc(ts_info->data_names,
				(ts_info->data_names_len + 1) * sizeof(*ts_info->data_names));
		if (! tmp) {
			sdb_log(SDB_LOG_ERR, "Failed to allocate memory");
			sdb_timeseries_info_destroy(ts_info);
			rrd_info_free(info);
			return NULL;
		}

		ts_info->data_names = tmp;
		ts_info->data_names[ts_info->data_names_len] = strdup(ds_name);
		if (! ts_info->data_names[ts_info->data_names_len]) {
			sdb_log(SDB_LOG_ERR, "Failed to allocate memory");
			sdb_timeseries_info_destroy(ts_info);
			rrd_info_free(info);
			return NULL;
		}
		ts_info->data_names_len++;
	}
	rrd_info_free(info);

	return ts_info;
} /* sdb_rrd_describe */
Exemple #4
0
rrd_info_t *rrd_info(
    int argc,
    char **argv)
{
    rrd_info_t *info;
    char *opt_daemon = NULL;
    int status;
    int flushfirst = 1;

    optind = 0;
    opterr = 0;         /* initialize getopt */

    while (42) {
        int       opt;
        int       option_index = 0;
        static struct option long_options[] = {
            {"daemon", required_argument, 0, 'd'},
            {"noflush", no_argument, 0, 'F'},
            {0, 0, 0, 0}
        };

        opt = getopt_long(argc, argv, "d:F", long_options, &option_index);

        if (opt == EOF)
            break;

        switch (opt) {
        case 'd':
            if (opt_daemon != NULL)
                    free (opt_daemon);
            opt_daemon = strdup (optarg);
            if (opt_daemon == NULL)
            {
                rrd_set_error ("strdup failed.");
                return (NULL);
            }
            break;

        case 'F':
            flushfirst = 0;
            break;

        default:
            rrd_set_error ("Usage: rrdtool %s [--daemon <addr> [--noflush]] <file>",
                    argv[0]);
            return (NULL);
            break;
        }
    }                   /* while (42) */

    if ((argc - optind) != 1) {
        rrd_set_error ("Usage: rrdtool %s [--daemon <addr> [--noflush]] <file>",
                argv[0]);
        return (NULL);
    }

    if( flushfirst ) {
    status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
    if (status) return (NULL);
    }

    rrdc_connect (opt_daemon);
    if (rrdc_is_connected (opt_daemon))
        info = rrdc_info (argv[optind]);
    else
    info = rrd_info_r(argv[optind]);

    if (opt_daemon) free(opt_daemon);
    return (info);
} /* rrd_info_t *rrd_info */