X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=bindings%2Fruby%2Fmain.c;h=6dc229b8691d1625404ea3ca54311f7d68a6adca;hp=90f883bc237ecdde64ac1d3c28fa4af695c907e6;hb=513304e8a967fa471a0028711ddd3d95ce812d7d;hpb=bbe14939aca831605f56753b1b0023ff46cad7b7 diff --git a/bindings/ruby/main.c b/bindings/ruby/main.c index 90f883b..6dc229b 100644 --- a/bindings/ruby/main.c +++ b/bindings/ruby/main.c @@ -4,6 +4,7 @@ #include #include +#include #include "../../src/rrd_tool.h" typedef struct string_arr_t { @@ -19,6 +20,11 @@ typedef int ( int argc, char **argv); +typedef rrd_info_t *( + *RRDINFOFUNC) ( + int argc, + char **argv); + #define RRD_CHECK_ERROR \ if (rrd_test_error()) \ rb_raise(rb_eRRDError, rrd_get_error()); \ @@ -32,7 +38,7 @@ string_arr string_arr_new( int i; Check_Type(rb_strings, T_ARRAY); - a.len = RARRAY(rb_strings)->len + 1; + a.len = RARRAY_LEN(rb_strings) + 1; a.strings = malloc(a.len * sizeof(char *)); a.strings[0] = "dummy"; /* first element is a dummy element */ @@ -42,7 +48,7 @@ string_arr string_arr_new( switch (TYPE(v)) { case T_STRING: - a.strings[i + 1] = strdup(STR2CSTR(v)); + a.strings[i + 1] = strdup(StringValuePtr(v)); break; case T_FIXNUM: snprintf(buf, 63, "%d", FIX2INT(v)); @@ -80,6 +86,8 @@ void reset_rrd_state( rrd_clear_error(); } +/* Simple Calls */ + VALUE rrd_call( RRDFUNC func, VALUE args) @@ -108,6 +116,116 @@ VALUE rb_rrd_dump( return rrd_call(rrd_dump, args); } +VALUE rb_rrd_resize( + VALUE self, + VALUE args) +{ + return rrd_call(rrd_resize, args); +} + +VALUE rb_rrd_restore( + VALUE self, + VALUE args) +{ + return rrd_call(rrd_restore, args); +} + +VALUE rb_rrd_tune( + VALUE self, + VALUE args) +{ + return rrd_call(rrd_tune, args); +} + +VALUE rb_rrd_update( + VALUE self, + VALUE args) +{ + return rrd_call(rrd_update, args); +} + +VALUE rb_rrd_flushcached( + VALUE self, + VALUE args) +{ + return rrd_call(rrd_flushcached, args); +} + + +/* Calls Returning Data via the Info Interface */ + +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; +} + +VALUE rb_rrd_info( + VALUE self, + VALUE args) +{ + return rb_rrd_infocall(rrd_info, args); +} + +VALUE rb_rrd_updatev( + VALUE self, + VALUE args) +{ + return rb_rrd_infocall(rrd_update_v, args); +} + +VALUE rb_rrd_graphv( + VALUE self, + VALUE args) +{ + return rb_rrd_infocall(rrd_graph_v, args); +} + + +/* Other Calls */ + VALUE rb_rrd_fetch( VALUE self, VALUE args) @@ -129,9 +247,9 @@ VALUE rb_rrd_fetch( for (i = 0; i < ds_cnt; i++) { rb_ary_push(names, rb_str_new2(raw_names[i])); - free(raw_names[i]); + rrd_freemem(raw_names[i]); } - free(raw_names); + rrd_freemem(raw_names); k = 0; data = rb_ary_new(); @@ -144,7 +262,7 @@ VALUE rb_rrd_fetch( } rb_ary_push(data, line); } - free(raw_data); + rrd_freemem(raw_data); result = rb_ary_new2(5); rb_ary_store(result, 0, INT2NUM(start)); @@ -176,53 +294,15 @@ VALUE rb_rrd_graph( p = calcpr; for (p = calcpr; p && *p; p++) { rb_ary_push(print_results, rb_str_new2(*p)); - free(*p); + rrd_freemem(*p); } - free(calcpr); + 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; } -VALUE rb_rrd_info( - VALUE self, - VALUE args) -{ - string_arr a; - info_t *p, *data; - VALUE result; - - a = string_arr_new(args); - data = rrd_info(a.len, a.strings); - string_arr_delete(a); - - RRD_CHECK_ERROR result = rb_hash_new(); - 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)); - free(data->value.u_str); - break; - } - p = data; - data = data->next; - free(p); - } - return result; -} VALUE rb_rrd_last( VALUE self, @@ -237,35 +317,54 @@ VALUE rb_rrd_last( string_arr_delete(a); RRD_CHECK_ERROR - return rb_funcall(rb_cTime, rb_intern("at"), 1, INT2FIX(last)); + return rb_funcall(rb_cTime, rb_intern("at"), 1, UINT2NUM(last)); } -VALUE rb_rrd_resize( +VALUE rb_rrd_xport( VALUE self, VALUE args) { - return rrd_call(rrd_resize, 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; -VALUE rb_rrd_restore( - VALUE self, - VALUE args) -{ - return rrd_call(rrd_restore, args); -} + a = string_arr_new(args); + rrd_xport(a.len, a.strings, &xxsize, &start, &end, &step, &col_cnt, &legend_v, &data); + string_arr_delete(a); -VALUE rb_rrd_tune( - VALUE self, - VALUE args) -{ - return rrd_call(rrd_tune, args); -} + 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); -VALUE rb_rrd_update( - VALUE self, - VALUE args) -{ - return rrd_call(rrd_update, args); + 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; } void Init_RRD( @@ -283,5 +382,9 @@ void Init_RRD( rb_define_module_function(mRRD, "restore", rb_rrd_restore, -2); rb_define_module_function(mRRD, "tune", rb_rrd_tune, -2); rb_define_module_function(mRRD, "update", rb_rrd_update, -2); + rb_define_module_function(mRRD, "flushcached", rb_rrd_flushcached, -2); rb_define_module_function(mRRD, "info", rb_rrd_info, -2); + rb_define_module_function(mRRD, "updatev", rb_rrd_updatev, -2); + rb_define_module_function(mRRD, "graphv", rb_rrd_graphv, -2); + rb_define_module_function(mRRD, "xport", rb_rrd_xport, -2); }