static int ntop_rrd_fetch(lua_State* L) { int argc = lua_gettop(L) + 1; char **argv = make_argv("fetch", L); unsigned long i, j, step, ds_cnt; rrd_value_t *data, *p; char **names; time_t t, start, end; ntop->rrdLock(__FILE__, __LINE__); reset_rrd_state(); rrd_fetch(argc, argv, &start, &end, &step, &ds_cnt, &names, &data); free(argv); if (rrd_test_error()) luaL_error(L, rrd_get_error()); lua_pushnumber(L, (lua_Number) start); lua_pushnumber(L, (lua_Number) step); /* fprintf(stderr, "%lu, %lu, %lu, %lu\n", start, end, step, num_points); */ /* create the ds names array */ lua_newtable(L); for (i=0; i<ds_cnt; i++) { lua_pushstring(L, names[i]); lua_rawseti(L, -2, i+1); rrd_freemem(names[i]); } rrd_freemem(names); /* create the data points array */ lua_newtable(L); p = data; for (t=start, i=0; t<end; t+=step, i++) { lua_newtable(L); for (j=0; j<ds_cnt; j++) { /*fprintf(stderr, "Point #%lu\n", j+1); */ lua_pushnumber(L, (lua_Number) *p++); lua_rawseti(L, -2, j+1); } lua_rawseti(L, -2, i+1); } rrd_freemem(data); ntop->rrdUnlock(__FILE__, __LINE__); /* return the end as the last value */ lua_pushnumber(L, (lua_Number) end); return 5; }
VALUE rb_rrd_fetch( VALUE self, VALUE args) { string_arr a; unsigned long i, j, k, step, ds_cnt; rrd_value_t *raw_data; char **raw_names; VALUE data, names, result; time_t start, end; a = string_arr_new(args); reset_rrd_state(); rrd_fetch(a.len, a.strings, &start, &end, &step, &ds_cnt, &raw_names, &raw_data); string_arr_delete(a); RRD_CHECK_ERROR names = rb_ary_new(); for (i = 0; i < ds_cnt; i++) { rb_ary_push(names, rb_str_new2(raw_names[i])); rrd_freemem(raw_names[i]); } rrd_freemem(raw_names); k = 0; data = rb_ary_new(); for (i = start; i <= end; i += step) { VALUE line = rb_ary_new2(ds_cnt); for (j = 0; j < ds_cnt; j++) { rb_ary_store(line, j, rb_float_new(raw_data[k])); k++; } rb_ary_push(data, line); } rrd_freemem(raw_data); result = rb_ary_new2(5); rb_ary_store(result, 0, INT2NUM(start)); rb_ary_store(result, 1, INT2NUM(end)); rb_ary_store(result, 2, names); rb_ary_store(result, 3, data); rb_ary_store(result, 4, INT2FIX(step)); return result; }
/* HandleInputLine is NOT thread safe - due to readdir issues, resolving them portably is not really simple. */ int HandleInputLine( int argc, char **argv, FILE * out) { #if defined(HAVE_OPENDIR) && defined (HAVE_READDIR) DIR *curdir; /* to read current dir with ls */ struct dirent *dent; #endif /* Reset errno to 0 before we start. */ if (RemoteMode) { if (argc > 1 && strcmp("quit", argv[1]) == 0) { if (argc != 2) { printf("ERROR: invalid parameter count for quit\n"); return (1); } exit(0); } #if defined(HAVE_OPENDIR) && defined(HAVE_READDIR) && defined(HAVE_CHDIR) && defined(HAVE_SYS_STAT_H) if (argc > 1 && strcmp("cd", argv[1]) == 0) { if (argc != 3) { printf("ERROR: invalid parameter count for cd\n"); return (1); } #if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID) if (getuid() == 0 && !ChangeRoot) { printf ("ERROR: chdir security problem - rrdtool is running as " "root but not chroot!\n"); return (1); } #endif if (chdir(argv[2]) != 0){ printf("ERROR: chdir %s %s\n", argv[2], rrd_strerror(errno)); return (1); } return (0); } if (argc > 1 && strcmp("pwd", argv[1]) == 0) { char *cwd; /* To hold current working dir on call to pwd */ if (argc != 2) { printf("ERROR: invalid parameter count for pwd\n"); return (1); } cwd = getcwd(NULL, MAXPATH); if (cwd == NULL) { printf("ERROR: getcwd %s\n", rrd_strerror(errno)); return (1); } printf("%s\n", cwd); free(cwd); return (0); } if (argc > 1 && strcmp("mkdir", argv[1]) == 0) { if (argc != 3) { printf("ERROR: invalid parameter count for mkdir\n"); return (1); } #if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID) if (getuid() == 0 && !ChangeRoot) { printf ("ERROR: mkdir security problem - rrdtool is running as " "root but not chroot!\n"); return (1); } #endif if(mkdir(argv[2], 0777)!=0){ printf("ERROR: mkdir %s: %s\n", argv[2],rrd_strerror(errno)); return (1); } return (0); } if (argc > 1 && strcmp("ls", argv[1]) == 0) { if (argc != 2) { printf("ERROR: invalid parameter count for ls\n"); return (1); } if ((curdir = opendir(".")) != NULL) { struct stat st; while ((dent = readdir(curdir)) != NULL) { if (!stat(dent->d_name, &st)) { if (S_ISDIR(st.st_mode)) { printf("d %s\n", dent->d_name); } if (strlen(dent->d_name) > 4 && S_ISREG(st.st_mode)) { if (!strcmp (dent->d_name + NAMLEN(dent) - 4, ".rrd") || !strcmp(dent->d_name + NAMLEN(dent) - 4, ".RRD")) { printf("- %s\n", dent->d_name); } } } } closedir(curdir); } else { printf("ERROR: opendir .: %s\n", rrd_strerror(errno)); return (errno); } return (0); } #endif /* opendir and readdir */ } if (argc < 3 || strcmp("help", argv[1]) == 0 || strcmp("--help", argv[1]) == 0 || strcmp("-help", argv[1]) == 0 || strcmp("-?", argv[1]) == 0 || strcmp("-h", argv[1]) == 0) { PrintUsage(""); return 0; } if (strcmp("create", argv[1]) == 0) rrd_create(argc - 1, &argv[1]); else if (strcmp("dump", argv[1]) == 0) rrd_dump(argc - 1, &argv[1]); else if (strcmp("info", argv[1]) == 0 || strcmp("updatev", argv[1]) == 0) { rrd_info_t *data; if (strcmp("info", argv[1]) == 0) data = rrd_info(argc - 1, &argv[1]); else data = rrd_update_v(argc - 1, &argv[1]); rrd_info_print(data); rrd_info_free(data); } else if (strcmp("--version", argv[1]) == 0 || strcmp("version", argv[1]) == 0 || strcmp("v", argv[1]) == 0 || strcmp("-v", argv[1]) == 0 || strcmp("-version", argv[1]) == 0) printf("RRDtool " PACKAGE_VERSION " Copyright by Tobi Oetiker, 1997-2008 (%f)\n", rrd_version()); else if (strcmp("restore", argv[1]) == 0) rrd_restore(argc - 1, &argv[1]); else if (strcmp("resize", argv[1]) == 0) rrd_resize(argc - 1, &argv[1]); else if (strcmp("last", argv[1]) == 0) printf("%ld\n", rrd_last(argc - 1, &argv[1])); else if (strcmp("lastupdate", argv[1]) == 0) { rrd_lastupdate(argc - 1, &argv[1]); } else if (strcmp("first", argv[1]) == 0) printf("%ld\n", rrd_first(argc - 1, &argv[1])); else if (strcmp("update", argv[1]) == 0) rrd_update(argc - 1, &argv[1]); else if (strcmp("fetch", argv[1]) == 0) { time_t start, end, ti; unsigned long step, ds_cnt, i, ii; rrd_value_t *data, *datai; char **ds_namv; if (rrd_fetch (argc - 1, &argv[1], &start, &end, &step, &ds_cnt, &ds_namv, &data) == 0) { datai = data; printf(" "); for (i = 0; i < ds_cnt; i++) printf("%20s", ds_namv[i]); printf("\n\n"); for (ti = start + step; ti <= end; ti += step) { printf("%10lu:", ti); for (ii = 0; ii < ds_cnt; ii++) printf(" %0.10e", *(datai++)); printf("\n"); } for (i = 0; i < ds_cnt; i++) free(ds_namv[i]); free(ds_namv); free(data); } } else if (strcmp("xport", argv[1]) == 0) { #ifdef HAVE_RRD_GRAPH time_t start, end; unsigned long step, col_cnt; rrd_value_t *data; char **legend_v; rrd_xport (argc - 1, &argv[1], NULL, &start, &end, &step, &col_cnt, &legend_v, &data); #else rrd_set_error("the instance of rrdtool has been compiled without graphics"); #endif } else if (strcmp("graph", argv[1]) == 0) { #ifdef HAVE_RRD_GRAPH char **calcpr; #ifdef notused /*XXX*/ const char *imgfile = argv[2]; /* rrd_graph changes argv pointer */ #endif int xsize, ysize; double ymin, ymax; int i; int tostdout = (strcmp(argv[2], "-") == 0); int imginfo = 0; for (i = 2; i < argc; i++) { if (strcmp(argv[i], "--imginfo") == 0 || strcmp(argv[i], "-f") == 0) { imginfo = 1; break; } } if (rrd_graph (argc - 1, &argv[1], &calcpr, &xsize, &ysize, NULL, &ymin, &ymax) == 0) { if (!tostdout && !imginfo) printf("%dx%d\n", xsize, ysize); if (calcpr) { for (i = 0; calcpr[i]; i++) { if (!tostdout) printf("%s\n", calcpr[i]); free(calcpr[i]); } free(calcpr); } } #else rrd_set_error("the instance of rrdtool has been compiled without graphics"); #endif } else if (strcmp("graphv", argv[1]) == 0) { #ifdef HAVE_RRD_GRAPH rrd_info_t *grinfo = NULL; /* 1 to distinguish it from the NULL that rrd_graph sends in */ grinfo = rrd_graph_v(argc - 1, &argv[1]); if (grinfo) { rrd_info_print(grinfo); rrd_info_free(grinfo); } #else rrd_set_error("the instance of rrdtool has been compiled without graphics"); #endif } else if (strcmp("tune", argv[1]) == 0) rrd_tune(argc - 1, &argv[1]); else if (strcmp("flushcached", argv[1]) == 0) rrd_flushcached(argc - 1, &argv[1]); else if (strcmp("modify", argv[1]) == 0) rrd_modify(argc - 1, &argv[1]); else { rrd_set_error("unknown function '%s'", argv[1]); } if (rrd_test_error()) { fprintf(out, "ERROR: %s\n", rrd_get_error()); rrd_clear_error(); return 1; } return (0); }
static PyObject *PyRRD_fetch( PyObject UNUSED(*self), PyObject * args) { PyObject *r; rrd_value_t *data, *datai; unsigned long step, ds_cnt; time_t start, end; int argc; char **argv, **ds_namv; if (create_args("fetch", args, &argc, &argv) < 0) return NULL; if (rrd_fetch(argc, argv, &start, &end, &step, &ds_cnt, &ds_namv, &data) == -1) { PyErr_SetString(ErrorObject, rrd_get_error()); rrd_clear_error(); r = NULL; } else { /* Return : ((start, end, step), (name1, name2, ...), [(data1, data2, ..), ...]) */ PyObject *range_tup, *dsnam_tup, *data_list, *t; unsigned long i, j, row; rrd_value_t dv; row = (end - start) / step; r = PyTuple_New(3); range_tup = PyTuple_New(3); dsnam_tup = PyTuple_New(ds_cnt); data_list = PyList_New(row); PyTuple_SET_ITEM(r, 0, range_tup); PyTuple_SET_ITEM(r, 1, dsnam_tup); PyTuple_SET_ITEM(r, 2, data_list); datai = data; PyTuple_SET_ITEM(range_tup, 0, PyInt_FromLong((long) start)); PyTuple_SET_ITEM(range_tup, 1, PyInt_FromLong((long) end)); PyTuple_SET_ITEM(range_tup, 2, PyInt_FromLong((long) step)); for (i = 0; i < ds_cnt; i++) PyTuple_SET_ITEM(dsnam_tup, i, PyString_FromString(ds_namv[i])); for (i = 0; i < row; i++) { t = PyTuple_New(ds_cnt); PyList_SET_ITEM(data_list, i, t); for (j = 0; j < ds_cnt; j++) { dv = *(datai++); if (isnan(dv)) { PyTuple_SET_ITEM(t, j, Py_None); Py_INCREF(Py_None); } else { PyTuple_SET_ITEM(t, j, PyFloat_FromDouble((double) dv)); } } } for (i = 0; i < ds_cnt; i++) rrd_freemem(ds_namv[i]); rrd_freemem(ds_namv); /* rrdtool don't use PyMem_Malloc :) */ rrd_freemem(data); } destroy_args(&argv); return r; }
data_row *RRD_GetDataRow(char *path, time_t when) { time_t last, frag; struct tm * t1, *t2; struct stat statbuf; char datestr1[64] , datestr2[64], rrd_filename[1024]; char *rrd_arg[10]; char **ds_namv; int ret, i, j, p, t, len, argc; unsigned long step, ds_cnt, pnum; data_row *row; rrd_value_t *data; uint64_t dummy; data = NULL; frag = when % 300; if ( frag ) { fprintf(stderr, "Round to next timeslot: offset %lld\n", (long long)frag); when -= frag; } last = RRD_LastUpdate(path); if ( when > last ) { t1 = localtime(&when); strftime(datestr1, 63, "%b %d %Y %T", t1); t2 = localtime(&last); strftime(datestr2, 63, "%b %d %Y %T", t2); fprintf(stderr, "Error get data: Requested time slot '%s' later then last available time slot '%s'\n", datestr1, datestr2); return NULL; } row = (data_row *)calloc(65536, sizeof(data_row)); if ( !row ) { perror("Memory allocation error"); return NULL; } len = snprintf(datestr1, 64, "--start=%lld", (long long)when); if ( len >= 64 ) { fprintf(stderr, "String overflow --start\n"); free(row); return NULL; } len = snprintf(datestr2, 64, "--end=%lld", (long long)when); if ( len >= 64 ) { fprintf(stderr, "String overflow --end\n"); free(row); return NULL; } for ( p=tcp; p<=udp; p++ ) { // for every protocol TCP - UDP for ( t=flows; t<=bytes; t++ ) { // for every type flows - packets - bytes for (j=0; j<64; j++) { // for all 64 RRD files in proto - type len = snprintf(rrd_filename, 1024, "%s/%s-%s-%d.rrd", path, proto[p], type[t], j); if ( len >= 1024 ) { fprintf(stderr, "Failed to concat RRD filename: string overflow"); free(row); return NULL; } // Check if RRD file exists if ( (stat(rrd_filename, &statbuf) < 0 ) || !(statbuf.st_mode & S_IFREG) ) { fprintf(stderr, "No such RRD file: '%s'\n", rrd_filename); free(row); return NULL; } // Create arg vector argc = 0; rrd_arg[argc++] = "fetch"; rrd_arg[argc++] = rrd_filename; rrd_arg[argc++] = "AVERAGE"; rrd_arg[argc++] = datestr1; rrd_arg[argc++] = datestr2; rrd_arg[argc] = NULL; optind = 0; opterr = 0; rrd_clear_error(); if ( ( ret=rrd_fetch(argc, rrd_arg, &when, &when, &step, &ds_cnt, &ds_namv, &data))) { fprintf(stderr, "RRD: %s Fetch Error: %d %s\n", rrd_filename, ret, rrd_get_error()); } if ( ds_cnt != 1024 ) { fprintf(stderr, "RRD: %s Fetch Error: Short read: Expected 1024 records got %lu\n", rrd_filename, ds_cnt); free(row); return NULL; } for ( i=0; i<1024; i++) { pnum = ( j << 10 ) + i; dummy = data[0]; row[pnum].proto[p].type[t] = dummy; } free(ds_namv); free(data); } // for all 64 rrd files } // for every type flows - packets - bytes } // for every protocol TCP - UDP return row; } // End of RRD_GetDataRow
static PyObject * _RRD_fetch(int argc, char **argv) { int rc, i, row_cnt; time_t start, end; unsigned long step, count; char **names; rrd_value_t *data; PyObject *result; PyObject *temp_obj; rc = rrd_fetch(argc, argv, &start, &end, &step, &count, &names, &data); if (rc == -1) { PyMem_DEL(argv); PyErr_SetString(PyExc_ValueError, rrd_get_error()); rrd_clear_error(); return NULL; } /* * Build (('timestamp', name1, name2, ...), [(timestamp, data1, data2, ..), ...]) * */ result = PyTuple_New(2); if (! result) { PyMem_DEL(argv); return NULL; } /* ('timestamp', name1, name2, ...) */ temp_obj = PyTuple_New(count+1); if (! temp_obj) goto error; PyTuple_SET_ITEM(result, 0, temp_obj); for (i = 0; i <= count; i++) { PyObject *str = (i>0) ? PyString_FromString(names[i-1]) : PyString_FromString("timestamp"); if (! str) goto error; PyTuple_SET_ITEM(temp_obj, i, str); } /* [(timestamp, data1, data2, ...), ... ]*/ row_cnt = ((end - start) / step) + 1; temp_obj = PyList_New(row_cnt); if (! temp_obj) goto error; PyTuple_SET_ITEM(result, 1, temp_obj); for (i=0; i < row_cnt; i++) { int ii; PyObject *tup = PyTuple_New(count+1); if (! tup) goto error; PyList_SET_ITEM(temp_obj, i, tup); for (ii=0; ii<=count; ii++) { if (ii == 0) { /* timestamp */ PyObject *obj = PyFloat_FromDouble((double)(start + step*i)); if (! obj) goto error; PyTuple_SET_ITEM(tup, ii, obj); } else { /* value */ rrd_value_t val = data[(i*count)+(ii-1)]; if (isnan(val)) { Py_INCREF(Py_None); PyTuple_SET_ITEM(tup, ii, Py_None); } else { PyObject *obj = PyFloat_FromDouble((double)val); if (! obj) goto error; PyTuple_SET_ITEM(tup, ii, obj); } } } } PyMem_DEL(argv); return result; error: Py_DECREF(result); PyMem_DEL(argv); return NULL; }
/* HandleInputLine is NOT thread safe - due to readdir issues, resolving them portably is not really simple. */ int HandleInputLine( int argc, char **argv, FILE * out) { #if defined(HAVE_OPENDIR) && defined (HAVE_READDIR) DIR *curdir; /* to read current dir with ls */ struct dirent *dent; #endif #if defined(HAVE_SYS_STAT_H) struct stat st; #endif /* Reset errno to 0 before we start. */ if (RemoteMode) { if (argc > 1 && strcmp("quit", argv[1]) == 0) { if (argc > 2) { printf("ERROR: invalid parameter count for quit\n"); return (1); } exit(0); } #if defined(HAVE_OPENDIR) && defined(HAVE_READDIR) && defined(HAVE_CHDIR) if (argc > 1 && strcmp("cd", argv[1]) == 0) { if (argc > 3) { printf("ERROR: invalid parameter count for cd\n"); return (1); } #if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID) if (getuid() == 0 && !ChangeRoot) { printf ("ERROR: chdir security problem - rrdtool is running as " "root but not chroot!\n"); return (1); } #endif if (chdir(argv[2]) != 0) { printf("ERROR: chdir %s %s\n", argv[2], rrd_strerror(errno)); return (1); } return (0); } if (argc > 1 && strcmp("pwd", argv[1]) == 0) { char *cwd; /* To hold current working dir on call to pwd */ if (argc > 2) { printf("ERROR: invalid parameter count for pwd\n"); return (1); } cwd = getcwd(NULL, MAXPATH); if (cwd == NULL) { printf("ERROR: getcwd %s\n", rrd_strerror(errno)); return (1); } printf("%s\n", cwd); free(cwd); return (0); } if (argc > 1 && strcmp("mkdir", argv[1]) == 0) { if (argc > 3) { printf("ERROR: invalid parameter count for mkdir\n"); return (1); } #if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID) if (getuid() == 0 && !ChangeRoot) { printf ("ERROR: mkdir security problem - rrdtool is running as " "root but not chroot!\n"); return (1); } #endif if(mkdir(argv[2], 0777)!=0) { printf("ERROR: mkdir %s: %s\n", argv[2],rrd_strerror(errno)); return (1); } return (0); } if (argc > 1 && strcmp("ls", argv[1]) == 0) { if (argc > 2) { printf("ERROR: invalid parameter count for ls\n"); return (1); } if ((curdir = opendir(".")) != NULL) { while ((dent = readdir(curdir)) != NULL) { if (!stat(dent->d_name, &st)) { if (S_ISDIR(st.st_mode)) { printf("d %s\n", dent->d_name); } if (strlen(dent->d_name) > 4 && S_ISREG(st.st_mode)) { if (!strcmp (dent->d_name + NAMLEN(dent) - 4, ".rrd") || !strcmp(dent->d_name + NAMLEN(dent) - 4, ".RRD")) { printf("- %s\n", dent->d_name); } } } } closedir(curdir); } else { printf("ERROR: opendir .: %s\n", rrd_strerror(errno)); return (errno); } return (0); } #endif /* opendir and readdir */ } if (argc < 3 || strcmp("help", argv[1]) == 0 || strcmp("--help", argv[1]) == 0 || strcmp("-help", argv[1]) == 0 || strcmp("-?", argv[1]) == 0 || strcmp("-h", argv[1]) == 0) { PrintUsage(""); return 0; } if (strcmp("create", argv[1]) == 0) rrd_create(argc - 1, &argv[1]); else if (strcmp("dump", argv[1]) == 0) rrd_dump(argc - 1, &argv[1]); else if (strcmp("info", argv[1]) == 0 || strcmp("updatev", argv[1]) == 0) { rrd_info_t *data; if (strcmp("info", argv[1]) == 0) data = rrd_info(argc - 1, &argv[1]); else data = rrd_update_v(argc - 1, &argv[1]); rrd_info_print(data); rrd_info_free(data); } else if (strcmp("--version", argv[1]) == 0 || strcmp("version", argv[1]) == 0 || strcmp("v", argv[1]) == 0 || strcmp("-v", argv[1]) == 0 || strcmp("-version", argv[1]) == 0) printf("RRDtool " PACKAGE_VERSION " Copyright by Tobi Oetiker, 1997-2008 (%f)\n", rrd_version()); else if (strcmp("restore", argv[1]) == 0) rrd_restore(argc - 1, &argv[1]); else if (strcmp("resize", argv[1]) == 0) rrd_resize(argc - 1, &argv[1]); else if (strcmp("last", argv[1]) == 0) printf("%ld\n", rrd_last(argc - 1, &argv[1])); else if (strcmp("lastupdate", argv[1]) == 0) { rrd_lastupdate(argc - 1, &argv[1]); } else if (strcmp("first", argv[1]) == 0) printf("%ld\n", rrd_first(argc - 1, &argv[1])); else if (strcmp("update", argv[1]) == 0) rrd_update(argc - 1, &argv[1]); else if (strcmp("fetch", argv[1]) == 0) { time_t start, end, ti; unsigned long step, ds_cnt, i, ii; rrd_value_t *data, *datai; char **ds_namv; if (rrd_fetch (argc - 1, &argv[1], &start, &end, &step, &ds_cnt, &ds_namv, &data) != -1) { datai = data; printf(" "); for (i = 0; i < ds_cnt; i++) printf("%20s", ds_namv[i]); printf("\n\n"); for (ti = start + step; ti <= end; ti += step) { printf("%10lu:", ti); for (ii = 0; ii < ds_cnt; ii++) printf(" %0.10e", *(datai++)); printf("\n"); } for (i = 0; i < ds_cnt; i++) free(ds_namv[i]); free(ds_namv); free(data); } } else if (strcmp("xport", argv[1]) == 0) { int xxsize; unsigned long int j = 0; time_t start, end, ti; unsigned long step, col_cnt, row_cnt; rrd_value_t *data, *ptr; char **legend_v; int enumds = 0; int i; size_t vtag_s = strlen(COL_DATA_TAG) + 10; char *vtag = malloc(vtag_s); for (i = 2; i < argc; i++) { if (strcmp("--enumds", argv[i]) == 0) enumds = 1; } if (rrd_xport (argc - 1, &argv[1], &xxsize, &start, &end, &step, &col_cnt, &legend_v, &data) != -1) { char *old_locale = setlocale(LC_NUMERIC, "C"); row_cnt = (end - start) / step; ptr = data; printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n\n", XML_ENCODING); printf("<%s>\n", ROOT_TAG); printf(" <%s>\n", META_TAG); printf(" <%s>%lld</%s>\n", META_START_TAG, (long long int) start + step, META_START_TAG); printf(" <%s>%lu</%s>\n", META_STEP_TAG, step, META_STEP_TAG); printf(" <%s>%lld</%s>\n", META_END_TAG, (long long int) end, META_END_TAG); printf(" <%s>%lu</%s>\n", META_ROWS_TAG, row_cnt, META_ROWS_TAG); printf(" <%s>%lu</%s>\n", META_COLS_TAG, col_cnt, META_COLS_TAG); printf(" <%s>\n", LEGEND_TAG); for (j = 0; j < col_cnt; j++) { char *entry = NULL; entry = legend_v[j]; printf(" <%s>%s</%s>\n", LEGEND_ENTRY_TAG, entry, LEGEND_ENTRY_TAG); free(entry); } free(legend_v); printf(" </%s>\n", LEGEND_TAG); printf(" </%s>\n", META_TAG); printf(" <%s>\n", DATA_TAG); for (ti = start + step; ti <= end; ti += step) { printf(" <%s>", DATA_ROW_TAG); printf("<%s>%lld</%s>", COL_TIME_TAG, (long long int)ti, COL_TIME_TAG); for (j = 0; j < col_cnt; j++) { rrd_value_t newval = DNAN; if (enumds == 1) snprintf(vtag, vtag_s, "%s%lu", COL_DATA_TAG, j); else snprintf(vtag, vtag_s, "%s", COL_DATA_TAG); newval = *ptr; if (isnan(newval)) { printf("<%s>NaN</%s>", vtag, vtag); } else { printf("<%s>%0.10e</%s>", vtag, newval, vtag); }; ptr++; } printf("</%s>\n", DATA_ROW_TAG); } free(data); printf(" </%s>\n", DATA_TAG); printf("</%s>\n", ROOT_TAG); setlocale(LC_NUMERIC, old_locale); } free(vtag); } else if (strcmp("graph", argv[1]) == 0) { char **calcpr; #ifdef notused /*XXX*/ const char *imgfile = argv[2]; /* rrd_graph changes argv pointer */ #endif int xsize, ysize; double ymin, ymax; int i; int tostdout = (strcmp(argv[2], "-") == 0); int imginfo = 0; for (i = 2; i < argc; i++) { if (strcmp(argv[i], "--imginfo") == 0 || strcmp(argv[i], "-f") == 0) { imginfo = 1; break; } } if (rrd_graph (argc - 1, &argv[1], &calcpr, &xsize, &ysize, NULL, &ymin, &ymax) != -1) { if (!tostdout && !imginfo) printf("%dx%d\n", xsize, ysize); if (calcpr) { for (i = 0; calcpr[i]; i++) { if (!tostdout) printf("%s\n", calcpr[i]); free(calcpr[i]); } free(calcpr); } } } else if (strcmp("graphv", argv[1]) == 0) { rrd_info_t *grinfo = NULL; /* 1 to distinguish it from the NULL that rrd_graph sends in */ grinfo = rrd_graph_v(argc - 1, &argv[1]); if (grinfo) { rrd_info_print(grinfo); rrd_info_free(grinfo); } } else if (strcmp("tune", argv[1]) == 0) rrd_tune(argc - 1, &argv[1]); else if (strcmp("flushcached", argv[1]) == 0) rrd_flushcached(argc - 1, &argv[1]); else { rrd_set_error("unknown function '%s'", argv[1]); } if (rrd_test_error()) { fprintf(out, "ERROR: %s\n", rrd_get_error()); rrd_clear_error(); return 1; } return (0); }
static VALUE rrrd_rrd_fetch(int argc, VALUE *argv, VALUE self) { VALUE function, options; VALUE resolution, starts_at, ends_at; rb_scan_args(argc, argv, "11", &function, &options); list arguments = rrrd_list_new(); rrrd_add_filename_arg(self, &arguments); // consolidation function function = rb_str_new2(rb_id2name(rb_to_id(function))); function = rb_funcall(function, rb_intern("upcase"), 0); rrrd_add_arg(&arguments, STR2CSTR(function)); if (RTEST(options)) { // extract options from hash resolution = rb_hash_aref(options, ID2SYM(rb_intern("resolution"))); starts_at = rb_hash_aref(options, ID2SYM(rb_intern("starts_at"))); ends_at = rb_hash_aref(options, ID2SYM(rb_intern("ends_at"))); // resolution if (RTEST(resolution)) { resolution = rb_fix2str(resolution, 10); rrrd_add_arg(&arguments, "--resolution"); rrrd_add_arg(&arguments, STR2CSTR(resolution)); } // start time if (RTEST(starts_at)) { starts_at = rb_funcall(starts_at, rb_intern("to_i"), 0); starts_at = rb_fix2str(starts_at, 10); rrrd_add_arg(&arguments, "--start"); rrrd_add_arg(&arguments, STR2CSTR(starts_at)); } // end time if (RTEST(ends_at)) { ends_at = rb_funcall(ends_at, rb_intern("to_i"), 0); ends_at = rb_fix2str(ends_at, 10); rrrd_add_arg(&arguments, "--end"); rrrd_add_arg(&arguments, STR2CSTR(ends_at)); } } time_t start, end; unsigned long step, ds_count; char **raw_names; rrd_value_t *raw_data; rrd_fetch(arguments.length, arguments.strings, &start, &end, &step, &ds_count, &raw_names, &raw_data); rrrd_free_arguments(arguments); rrrd_check_for_errors(); int i; int index = 0; VALUE data = rb_ary_new(); for (i = start + step; i <= end; i+= step) { int j; VALUE timestamp = rb_time_new(i, 0); VALUE values = rb_hash_new(); for (j = 0; j < ds_count; j++) { VALUE key = ID2SYM(rb_intern(raw_names[j])); rb_hash_aset(values, key, rb_float_new(raw_data[index++])); } VALUE args[] = { timestamp, values }; VALUE tuple = rb_class_new_instance(2, args, Tuple); rb_ary_push(data, tuple); } free(raw_data); return data; }
JNIEXPORT jobject JNICALL Java_gnu_rrd_RRDJNI_fetch (JNIEnv *env, jclass cl, jobjectArray argv) { time_t start, end; unsigned long step, ds_cnt, i, ii; rrd_value_t *data, *datai; char **ds_namv; char s[30]; char **argv2; jclass VectorClass; jmethodID VectorMethodInit; jmethodID VectorMethodAdd; jclass DateClass; jmethodID DateMethodInit; jclass DoubleClass; jmethodID DoubleMethodInit; jclass RRDRecClass; jmethodID RRDRecMethodInit; jobject dateObj; jdouble dataObj; jobject dsObj; jobject rtnObj; jlong timestamp; VectorClass = (*env)->FindClass(env, "java/util/Vector"); VectorMethodInit = (*env)->GetMethodID(env, VectorClass, "<init>", "()V"); VectorMethodAdd = (*env)->GetMethodID(env, VectorClass, "addElement", "(Ljava/lang/Object;)V"); DateClass = (*env)->FindClass(env, "java/util/Date"); DateMethodInit = (*env)->GetMethodID(env, DateClass, "<init>", "(J)V"); DoubleClass = (*env)->FindClass(env, "java/lang/Double"); DoubleMethodInit = (*env)->GetMethodID(env, DoubleClass, "<init>", "(D)V"); RRDRecClass = (*env)->FindClass(env, "gnu/rrd/RRDRec"); RRDRecMethodInit = (*env)->GetMethodID(env, RRDRecClass, "<init>", "(Ljava/util/Date;Ljava/util/Vector;)V"); rtnObj = (*env)->NewObject(env, VectorClass, VectorMethodInit, NULL); argv2 = initArgs(env, argv, "fetch"); if (rrd_fetch((*env)->GetArrayLength(env, argv)+1, argv2, &start, &end, &step, &ds_cnt, &ds_namv, &data) != -1) { datai = data; for (i = start; i <= end; i += step) { timestamp = i; dateObj = (*env)->NewObject(env, DateClass, DateMethodInit, timestamp*1000); dsObj = (*env)->NewObject(env, VectorClass, VectorMethodInit, NULL); for (ii = 0; ii < ds_cnt; ii++) { dataObj = *(datai++); (*env)->CallVoidMethod(env, dsObj, VectorMethodAdd, (*env)->NewObject(env, DoubleClass, DoubleMethodInit, dataObj)); } (*env)->CallVoidMethod(env, rtnObj, VectorMethodAdd, (*env)->NewObject(env, RRDRecClass, RRDRecMethodInit, dateObj, dsObj)); } for (i=0; i<ds_cnt; i++) free(ds_namv[i]); free(ds_namv); free(data); } freeArgs(env, argv, argv2); if (rrd_test_error()) throwException(env, "gnu/rrd/RRDException", rrd_get_error()); return rtnObj; }
int oneset(char *hostname, char *rrdname, char *starttime, char *endtime, char *colname, double subfrom, char *dsdescr) { static int firsttime = 1; time_t start, end, t; unsigned long step; unsigned long dscount; char **dsnames; rrd_value_t *data; int columnindex; char tstamp[30]; int dataindex, rowcount, havemin, havemax, missingdata; double sum, min = 0.0, max = 0.0, val; char *rrdargs[10]; int result; rrdargs[0] = "rrdfetch"; rrdargs[1] = rrdname; rrdargs[2] = "AVERAGE"; rrdargs[3] = "-s"; rrdargs[4] = starttimedate; rrdargs[5] = starttimehm; rrdargs[6] = "-e"; rrdargs[7] = endtimedate; rrdargs[8] = endtimehm; rrdargs[9] = NULL; optind = opterr = 0; rrd_clear_error(); result = rrd_fetch(9, rrdargs, &start, &end, &step, &dscount, &dsnames, &data); if (result != 0) { errprintf("RRD error: %s\n", rrd_get_error()); return 1; } for (columnindex=0; ((columnindex < dscount) && strcmp(dsnames[columnindex], colname)); columnindex++) ; if (columnindex == dscount) { errprintf("RRD error: Cannot find column %s\n", colname); return 1; } sum = 0.0; havemin = havemax = 0; rowcount = 0; switch (outform) { case O_XML: printf(" <dataset>\n"); printf(" <hostname>%s</hostname>\n", hostname); printf(" <datasource>%s</datasource>\n", rrdname); printf(" <rrdcolumn>%s</rrdcolumn>\n", colname); printf(" <measurement>%s</measurement>\n", (dsdescr ? dsdescr : colname)); printf(" <datapoints>\n"); break; case O_CSV: if (firsttime) { printf("\"hostname\"%c\"datasource\"%c\"rrdcolumn\"%c\"measurement\"%c\"time\"%c\"value\"\n", csvdelim, csvdelim, csvdelim, csvdelim, csvdelim); firsttime = 0; } break; default: break; } for (t=start+step, dataindex=columnindex, missingdata=0; (t <= end); t += step, dataindex += dscount) { if (isnan(data[dataindex]) || isnan(-data[dataindex])) { missingdata++; continue; } val = (subfrom != 0) ? subfrom - data[dataindex] : data[dataindex]; strftime(tstamp, sizeof(tstamp), "%Y%m%d%H%M%S", localtime(&t)); switch (outform) { case O_XML: printf(" <dataelement>\n"); printf(" <time>%s</time>\n", tstamp); printf(" <value>%f</value>\n", val); printf(" </dataelement>\n"); break; case O_CSV: printf("\"%s\"%c\"%s\"%c\"%s\"%c\"%s\"%c\"%s\"%c%f\n", hostname, csvdelim, rrdname, csvdelim, colname, csvdelim, (dsdescr ? dsdescr : colname), csvdelim, tstamp, csvdelim, val); break; default: break; } if (!havemax || (val > max)) { max = val; havemax = 1; } if (!havemin || (val < min)) { min = val; havemin = 1; } sum += val; rowcount++; } if (outform == O_XML) { printf(" </datapoints>\n"); printf(" <summary>\n"); if (havemin) printf(" <minimum>%f</minimum>\n", min); if (havemax) printf(" <maximum>%f</maximum>\n", max); if (rowcount) printf(" <average>%f</average>\n", (sum / rowcount)); printf(" <missingdatapoints>%d</missingdatapoints>\n", missingdata); printf(" </summary>\n"); printf(" </dataset>\n"); } return 0; }