Exemple #1
0
VALUE rb_rrd_graph(
    VALUE self,
    VALUE args)
{
    string_arr a;
    char    **calcpr, **p;
    VALUE     result, print_results;
    int       xsize, ysize;
    double    ymin, ymax;

    a = string_arr_new(args);
    reset_rrd_state();
    rrd_graph(a.len, a.strings, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax);
    string_arr_delete(a);

    RRD_CHECK_ERROR result = rb_ary_new2(3);

    print_results = rb_ary_new();
    p = calcpr;
    for (p = calcpr; p && *p; p++) {
        rb_ary_push(print_results, rb_str_new2(*p));
        rrd_freemem(*p);
    }
    rrd_freemem(calcpr);
    rb_ary_store(result, 0, print_results);
    rb_ary_store(result, 1, INT2FIX(xsize));
    rb_ary_store(result, 2, INT2FIX(ysize));
    return result;
}
Exemple #2
0
static int rrd_common_call (lua_State *L, const char *cmd,
			    RRD_FUNCTION rrd_function) {
  char **argv;
  int argc = lua_gettop(L) + 1;

  if(ntop->getGlobals()->isShutdown()) return(CONST_LUA_PARAM_ERROR);

  ntop->rrdLock(__FILE__, __LINE__);
  rrd_clear_error();
  argv = make_argv(cmd, L);
  reset_rrd_state();
  rrd_function(argc, argv);
  free(argv);
  if(rrd_test_error()) {
    char *err =  rrd_get_error();

    if(err != NULL) {
      /*
	IMPORTANT

	It is important to unlock now as if luaL_error is called the
	function returns and no unlock will take place
      */
      ntop->rrdUnlock(__FILE__, __LINE__);
      luaL_error(L, err);
    }
  }

  ntop->rrdUnlock(__FILE__, __LINE__);

  return 0;
}
Exemple #3
0
VALUE rrd_call(
    RRDFUNC func,
    VALUE args)
{
    string_arr a;

    a = string_arr_new(args);
    reset_rrd_state();
    func(a.len, a.strings);
    string_arr_delete(a);

    RRD_CHECK_ERROR return Qnil;
}
Exemple #4
0
VALUE rb_rrd_last(VALUE self, VALUE args)
{
    string_arr a;
    time_t last;

    a = string_arr_new(args);
    reset_rrd_state();
    last = rrd_last(a.len, a.strings);
    string_arr_delete(a);

    RRD_CHECK_ERROR

    return rb_funcall(rb_cTime, rb_intern("at"), 1, INT2FIX(last));
}
Exemple #5
0
VALUE rb_rrd_xport(
    VALUE self,
    VALUE args)
{
    string_arr a;
    unsigned long i, j, k, step, col_cnt;
    int xxsize;
    rrd_value_t *data;
    char **legend_v;
    VALUE legend, result, rdata;
    time_t start, end;

    a = string_arr_new(args);
    reset_rrd_state();
    rrd_xport(a.len, a.strings, &xxsize, &start, &end, &step, &col_cnt, &legend_v, &data);
    string_arr_delete(a);

    RRD_CHECK_ERROR;
            
    legend = rb_ary_new();
    for (i = 0; i < col_cnt; i++) {
        rb_ary_push(legend, rb_str_new2(legend_v[i]));
        free(legend_v[i]);
    }
    free(legend_v);

    k = 0;
    rdata = rb_ary_new();
    for (i = start; i <= end; i += step) {
        VALUE line = rb_ary_new2(col_cnt);
        for (j = 0; j < col_cnt; j++) {
            rb_ary_store(line, j, rb_float_new(data[k]));
            k++;
        }
        rb_ary_push(rdata, line);
    }
    free(data);

    result = rb_ary_new2(6);
    rb_ary_store(result, 0, INT2FIX(start));
    rb_ary_store(result, 1, INT2FIX(end));
    rb_ary_store(result, 2, INT2FIX(step));
    rb_ary_store(result, 3, INT2FIX(col_cnt));
    rb_ary_store(result, 4, legend);
    rb_ary_store(result, 5, rdata);
    return result;
}
Exemple #6
0
VALUE rb_rrd_infocall(
    RRDINFOFUNC func,
    VALUE args)
{
    string_arr a;
    rrd_info_t *p, *data;
    VALUE     result;

    a = string_arr_new(args);
    reset_rrd_state();
    data = func(a.len, a.strings);
    string_arr_delete(a);

    RRD_CHECK_ERROR result = rb_hash_new();

    p = data;
    while (data) {
        VALUE     key = rb_str_new2(data->key);

        switch (data->type) {
        case RD_I_VAL:
            if (isnan(data->value.u_val)) {
                rb_hash_aset(result, key, Qnil);
            } else {
                rb_hash_aset(result, key, rb_float_new(data->value.u_val));
            }
            break;
        case RD_I_CNT:
            rb_hash_aset(result, key, INT2FIX(data->value.u_cnt));
            break;
        case RD_I_STR:
            rb_hash_aset(result, key, rb_str_new2(data->value.u_str));
            break;
        case RD_I_INT:
            rb_hash_aset(result, key, INT2FIX(data->value.u_int));
            break;
        case RD_I_BLO:
            rb_hash_aset(result, key,
                         rb_str_new((char *)data->value.u_blo.ptr,
                                    data->value.u_blo.size));
            break;
        }
        data = data->next;
    }
    rrd_info_free(p);
    return result;
}
Exemple #7
0
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;
}
Exemple #8
0
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;
}