2 * Substantial penalty for early withdrawal.
8 #include "../../src/rrd_tool.h"
10 typedef struct string_arr_t {
28 #define RRD_CHECK_ERROR \
29 if (rrd_test_error()) \
30 rb_raise(rb_eRRDError, rrd_get_error()); \
33 string_arr string_arr_new(
40 Check_Type(rb_strings, T_ARRAY);
41 a.len = RARRAY(rb_strings)->len + 1;
43 a.strings = malloc(a.len * sizeof(char *));
44 a.strings[0] = "dummy"; /* first element is a dummy element */
46 for (i = 0; i < a.len - 1; i++) {
47 VALUE v = rb_ary_entry(rb_strings, i);
51 a.strings[i + 1] = strdup(STR2CSTR(v));
54 snprintf(buf, 63, "%d", FIX2INT(v));
55 a.strings[i + 1] = strdup(buf);
58 rb_raise(rb_eTypeError,
59 "invalid argument - %s, expected T_STRING or T_FIXNUM on index %d",
60 rb_class2name(CLASS_OF(v)), i);
68 void string_arr_delete(
73 /* skip dummy first entry */
74 for (i = 1; i < a.len; i++) {
97 a = string_arr_new(args);
99 func(a.len, a.strings);
100 string_arr_delete(a);
102 RRD_CHECK_ERROR return Qnil;
109 return rrd_call(rrd_create, args);
116 return rrd_call(rrd_dump, args);
123 return rrd_call(rrd_resize, args);
126 VALUE rb_rrd_restore(
130 return rrd_call(rrd_restore, args);
137 return rrd_call(rrd_tune, args);
144 return rrd_call(rrd_update, args);
151 return rrd_call(rrd_cmd_flush, args);
155 /* Calls Returning Data via the Info Interface */
157 VALUE rb_rrd_infocall(
162 rrd_info_t *p, *data;
165 a = string_arr_new(args);
166 data = func(a.len, a.strings);
167 string_arr_delete(a);
169 RRD_CHECK_ERROR result = rb_hash_new();
173 VALUE key = rb_str_new2(data->key);
175 switch (data->type) {
177 if (isnan(data->value.u_val)) {
178 rb_hash_aset(result, key, Qnil);
180 rb_hash_aset(result, key, rb_float_new(data->value.u_val));
184 rb_hash_aset(result, key, INT2FIX(data->value.u_cnt));
187 rb_hash_aset(result, key, rb_str_new2(data->value.u_str));
190 rb_hash_aset(result, key, INT2FIX(data->value.u_int));
193 rb_hash_aset(result, key,
194 rb_str_new((char *)data->value.u_blo.ptr,
195 data->value.u_blo.size));
208 return rb_rrd_infocall(rrd_info, args);
211 VALUE rb_rrd_updatev(
215 return rb_rrd_infocall(rrd_update_v, args);
222 return rb_rrd_infocall(rrd_graph_v, args);
233 unsigned long i, j, k, step, ds_cnt;
234 rrd_value_t *raw_data;
236 VALUE data, names, result;
239 a = string_arr_new(args);
241 rrd_fetch(a.len, a.strings, &start, &end, &step, &ds_cnt, &raw_names,
243 string_arr_delete(a);
245 RRD_CHECK_ERROR names = rb_ary_new();
247 for (i = 0; i < ds_cnt; i++) {
248 rb_ary_push(names, rb_str_new2(raw_names[i]));
249 rrd_freemem(raw_names[i]);
251 rrd_freemem(raw_names);
255 for (i = start; i <= end; i += step) {
256 VALUE line = rb_ary_new2(ds_cnt);
258 for (j = 0; j < ds_cnt; j++) {
259 rb_ary_store(line, j, rb_float_new(raw_data[k]));
262 rb_ary_push(data, line);
264 rrd_freemem(raw_data);
266 result = rb_ary_new2(5);
267 rb_ary_store(result, 0, INT2NUM(start));
268 rb_ary_store(result, 1, INT2NUM(end));
269 rb_ary_store(result, 2, names);
270 rb_ary_store(result, 3, data);
271 rb_ary_store(result, 4, INT2FIX(step));
281 VALUE result, print_results;
285 a = string_arr_new(args);
287 rrd_graph(a.len, a.strings, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax);
288 string_arr_delete(a);
290 RRD_CHECK_ERROR result = rb_ary_new2(3);
292 print_results = rb_ary_new();
294 for (p = calcpr; p && *p; p++) {
295 rb_ary_push(print_results, rb_str_new2(*p));
299 rb_ary_store(result, 0, print_results);
300 rb_ary_store(result, 1, INT2FIX(xsize));
301 rb_ary_store(result, 2, INT2FIX(ysize));
313 a = string_arr_new(args);
315 last = rrd_last(a.len, a.strings);
316 string_arr_delete(a);
319 return rb_funcall(rb_cTime, rb_intern("at"), 1, UINT2NUM(last));
325 mRRD = rb_define_module("RRD");
326 rb_eRRDError = rb_define_class("RRDError", rb_eStandardError);
328 rb_define_module_function(mRRD, "create", rb_rrd_create, -2);
329 rb_define_module_function(mRRD, "dump", rb_rrd_dump, -2);
330 rb_define_module_function(mRRD, "fetch", rb_rrd_fetch, -2);
331 rb_define_module_function(mRRD, "graph", rb_rrd_graph, -2);
332 rb_define_module_function(mRRD, "last", rb_rrd_last, -2);
333 rb_define_module_function(mRRD, "resize", rb_rrd_resize, -2);
334 rb_define_module_function(mRRD, "restore", rb_rrd_restore, -2);
335 rb_define_module_function(mRRD, "tune", rb_rrd_tune, -2);
336 rb_define_module_function(mRRD, "update", rb_rrd_update, -2);
337 rb_define_module_function(mRRD, "flush", rb_rrd_flush, -2);
338 rb_define_module_function(mRRD, "info", rb_rrd_info, -2);
339 rb_define_module_function(mRRD, "updatev", rb_rrd_updatev, -2);
340 rb_define_module_function(mRRD, "graphv", rb_rrd_graphv, -2);