librrd 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.md +5 -0
- data/COPYING +280 -0
- data/COPYRIGHT +91 -0
- data/README.md +45 -0
- data/Rakefile +112 -0
- data/VERSION +1 -0
- data/ext/librrd/1.3/main.c +324 -0
- data/ext/librrd/1.4/main.c +390 -0
- data/ext/librrd/extconf.rb +17 -0
- data/librrd.gemspec +49 -0
- data/test.rb +76 -0
- metadata +93 -0
@@ -0,0 +1,324 @@
|
|
1
|
+
/* $Id: main.c 1791 2009-04-14 13:55:29Z oetiker $
|
2
|
+
* Substantial penalty for early withdrawal.
|
3
|
+
*/
|
4
|
+
|
5
|
+
#include <unistd.h>
|
6
|
+
#include <ruby.h>
|
7
|
+
#include <rrd.h>
|
8
|
+
|
9
|
+
typedef struct string_arr_t {
|
10
|
+
int len;
|
11
|
+
char **strings;
|
12
|
+
} string_arr;
|
13
|
+
|
14
|
+
VALUE mRRD;
|
15
|
+
VALUE rb_eRRDError;
|
16
|
+
|
17
|
+
typedef int (
|
18
|
+
*RRDFUNC) (
|
19
|
+
int argc,
|
20
|
+
char **argv);
|
21
|
+
|
22
|
+
#define RRD_CHECK_ERROR \
|
23
|
+
if (rrd_test_error()) \
|
24
|
+
rb_raise(rb_eRRDError, rrd_get_error()); \
|
25
|
+
rrd_clear_error();
|
26
|
+
|
27
|
+
string_arr string_arr_new(
|
28
|
+
VALUE rb_strings)
|
29
|
+
{
|
30
|
+
string_arr a;
|
31
|
+
char buf[64];
|
32
|
+
int i;
|
33
|
+
|
34
|
+
Check_Type(rb_strings, T_ARRAY);
|
35
|
+
a.len = RARRAY_LEN(rb_strings) + 1;
|
36
|
+
|
37
|
+
a.strings = malloc(a.len * sizeof(char *));
|
38
|
+
a.strings[0] = "dummy"; /* first element is a dummy element */
|
39
|
+
|
40
|
+
for (i = 0; i < a.len - 1; i++) {
|
41
|
+
VALUE v = rb_ary_entry(rb_strings, i);
|
42
|
+
|
43
|
+
switch (TYPE(v)) {
|
44
|
+
case T_STRING:
|
45
|
+
a.strings[i + 1] = strdup(STR2CSTR(v));
|
46
|
+
break;
|
47
|
+
case T_FIXNUM:
|
48
|
+
snprintf(buf, 63, "%d", FIX2INT(v));
|
49
|
+
a.strings[i + 1] = strdup(buf);
|
50
|
+
break;
|
51
|
+
default:
|
52
|
+
rb_raise(rb_eTypeError,
|
53
|
+
"invalid argument - %s, expected T_STRING or T_FIXNUM on index %d",
|
54
|
+
rb_class2name(CLASS_OF(v)), i);
|
55
|
+
break;
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
return a;
|
60
|
+
}
|
61
|
+
|
62
|
+
void string_arr_delete(
|
63
|
+
string_arr a)
|
64
|
+
{
|
65
|
+
int i;
|
66
|
+
|
67
|
+
/* skip dummy first entry */
|
68
|
+
for (i = 1; i < a.len; i++) {
|
69
|
+
free(a.strings[i]);
|
70
|
+
}
|
71
|
+
|
72
|
+
free(a.strings);
|
73
|
+
}
|
74
|
+
|
75
|
+
void reset_rrd_state(
|
76
|
+
)
|
77
|
+
{
|
78
|
+
optind = 0;
|
79
|
+
opterr = 0;
|
80
|
+
rrd_clear_error();
|
81
|
+
}
|
82
|
+
|
83
|
+
/* Simple Calls */
|
84
|
+
|
85
|
+
VALUE rrd_call(
|
86
|
+
RRDFUNC func,
|
87
|
+
VALUE args)
|
88
|
+
{
|
89
|
+
string_arr a;
|
90
|
+
|
91
|
+
a = string_arr_new(args);
|
92
|
+
reset_rrd_state();
|
93
|
+
func(a.len, a.strings);
|
94
|
+
string_arr_delete(a);
|
95
|
+
|
96
|
+
RRD_CHECK_ERROR return Qnil;
|
97
|
+
}
|
98
|
+
|
99
|
+
VALUE rb_rrd_create(
|
100
|
+
VALUE self,
|
101
|
+
VALUE args)
|
102
|
+
{
|
103
|
+
return rrd_call(rrd_create, args);
|
104
|
+
}
|
105
|
+
|
106
|
+
VALUE rb_rrd_dump(
|
107
|
+
VALUE self,
|
108
|
+
VALUE args)
|
109
|
+
{
|
110
|
+
return rrd_call(rrd_dump, args);
|
111
|
+
}
|
112
|
+
|
113
|
+
VALUE rb_rrd_resize(
|
114
|
+
VALUE self,
|
115
|
+
VALUE args)
|
116
|
+
{
|
117
|
+
return rrd_call(rrd_resize, args);
|
118
|
+
}
|
119
|
+
|
120
|
+
VALUE rb_rrd_restore(
|
121
|
+
VALUE self,
|
122
|
+
VALUE args)
|
123
|
+
{
|
124
|
+
return rrd_call(rrd_restore, args);
|
125
|
+
}
|
126
|
+
|
127
|
+
VALUE rb_rrd_tune(
|
128
|
+
VALUE self,
|
129
|
+
VALUE args)
|
130
|
+
{
|
131
|
+
return rrd_call(rrd_tune, args);
|
132
|
+
}
|
133
|
+
|
134
|
+
VALUE rb_rrd_update(
|
135
|
+
VALUE self,
|
136
|
+
VALUE args)
|
137
|
+
{
|
138
|
+
return rrd_call(rrd_update, args);
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
/* Calls Returning Data via the Info Interface */
|
143
|
+
|
144
|
+
VALUE rb_rrd_infocall(
|
145
|
+
RRDFUNC func,
|
146
|
+
VALUE args)
|
147
|
+
{
|
148
|
+
string_arr a;
|
149
|
+
rrd_info_t *p, *data;
|
150
|
+
VALUE result;
|
151
|
+
|
152
|
+
a = string_arr_new(args);
|
153
|
+
data = func(a.len, a.strings);
|
154
|
+
string_arr_delete(a);
|
155
|
+
|
156
|
+
RRD_CHECK_ERROR result = rb_hash_new();
|
157
|
+
|
158
|
+
p = data;
|
159
|
+
while (data) {
|
160
|
+
VALUE key = rb_str_new2(data->key);
|
161
|
+
|
162
|
+
switch (data->type) {
|
163
|
+
case RD_I_VAL:
|
164
|
+
if (isnan(data->value.u_val)) {
|
165
|
+
rb_hash_aset(result, key, Qnil);
|
166
|
+
} else {
|
167
|
+
rb_hash_aset(result, key, rb_float_new(data->value.u_val));
|
168
|
+
}
|
169
|
+
break;
|
170
|
+
case RD_I_CNT:
|
171
|
+
rb_hash_aset(result, key, INT2FIX(data->value.u_cnt));
|
172
|
+
break;
|
173
|
+
case RD_I_STR:
|
174
|
+
rb_hash_aset(result, key, rb_str_new2(data->value.u_str));
|
175
|
+
break;
|
176
|
+
case RD_I_BLO:
|
177
|
+
rb_hash_aset(result, key,
|
178
|
+
rb_str_new(data->value.u_blo.ptr,
|
179
|
+
data->value.u_blo.size));
|
180
|
+
break;
|
181
|
+
}
|
182
|
+
data = data->next;
|
183
|
+
}
|
184
|
+
rrd_info_free(p);
|
185
|
+
return result;
|
186
|
+
}
|
187
|
+
|
188
|
+
VALUE rb_rrd_info(
|
189
|
+
VALUE self,
|
190
|
+
VALUE args)
|
191
|
+
{
|
192
|
+
return rb_rrd_infocall(rrd_info, args);
|
193
|
+
}
|
194
|
+
|
195
|
+
VALUE rb_rrd_updatev(
|
196
|
+
VALUE self,
|
197
|
+
VALUE args)
|
198
|
+
{
|
199
|
+
return rb_rrd_infocall(rrd_update_v, args);
|
200
|
+
}
|
201
|
+
|
202
|
+
VALUE rb_rrd_graphv(
|
203
|
+
VALUE self,
|
204
|
+
VALUE args)
|
205
|
+
{
|
206
|
+
return rb_rrd_infocall(rrd_graph_v, args);
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
/* Other Calls */
|
211
|
+
|
212
|
+
VALUE rb_rrd_fetch(
|
213
|
+
VALUE self,
|
214
|
+
VALUE args)
|
215
|
+
{
|
216
|
+
string_arr a;
|
217
|
+
unsigned long i, j, k, step, ds_cnt;
|
218
|
+
rrd_value_t *raw_data;
|
219
|
+
char **raw_names;
|
220
|
+
VALUE data, names, result;
|
221
|
+
time_t start, end;
|
222
|
+
|
223
|
+
a = string_arr_new(args);
|
224
|
+
reset_rrd_state();
|
225
|
+
rrd_fetch(a.len, a.strings, &start, &end, &step, &ds_cnt, &raw_names,
|
226
|
+
&raw_data);
|
227
|
+
string_arr_delete(a);
|
228
|
+
|
229
|
+
RRD_CHECK_ERROR names = rb_ary_new();
|
230
|
+
|
231
|
+
for (i = 0; i < ds_cnt; i++) {
|
232
|
+
rb_ary_push(names, rb_str_new2(raw_names[i]));
|
233
|
+
rrd_freemem(raw_names[i]);
|
234
|
+
}
|
235
|
+
rrd_freemem(raw_names);
|
236
|
+
|
237
|
+
k = 0;
|
238
|
+
data = rb_ary_new();
|
239
|
+
for (i = start; i <= end; i += step) {
|
240
|
+
VALUE line = rb_ary_new2(ds_cnt);
|
241
|
+
|
242
|
+
for (j = 0; j < ds_cnt; j++) {
|
243
|
+
rb_ary_store(line, j, rb_float_new(raw_data[k]));
|
244
|
+
k++;
|
245
|
+
}
|
246
|
+
rb_ary_push(data, line);
|
247
|
+
}
|
248
|
+
rrd_freemem(raw_data);
|
249
|
+
|
250
|
+
result = rb_ary_new2(5);
|
251
|
+
rb_ary_store(result, 0, INT2NUM(start));
|
252
|
+
rb_ary_store(result, 1, INT2NUM(end));
|
253
|
+
rb_ary_store(result, 2, names);
|
254
|
+
rb_ary_store(result, 3, data);
|
255
|
+
rb_ary_store(result, 4, INT2FIX(step));
|
256
|
+
return result;
|
257
|
+
}
|
258
|
+
|
259
|
+
VALUE rb_rrd_graph(
|
260
|
+
VALUE self,
|
261
|
+
VALUE args)
|
262
|
+
{
|
263
|
+
string_arr a;
|
264
|
+
char **calcpr, **p;
|
265
|
+
VALUE result, print_results;
|
266
|
+
int xsize, ysize;
|
267
|
+
double ymin, ymax;
|
268
|
+
|
269
|
+
a = string_arr_new(args);
|
270
|
+
reset_rrd_state();
|
271
|
+
rrd_graph(a.len, a.strings, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax);
|
272
|
+
string_arr_delete(a);
|
273
|
+
|
274
|
+
RRD_CHECK_ERROR result = rb_ary_new2(3);
|
275
|
+
|
276
|
+
print_results = rb_ary_new();
|
277
|
+
p = calcpr;
|
278
|
+
for (p = calcpr; p && *p; p++) {
|
279
|
+
rb_ary_push(print_results, rb_str_new2(*p));
|
280
|
+
rrd_freemem(*p);
|
281
|
+
}
|
282
|
+
rrd_freemem(calcpr);
|
283
|
+
rb_ary_store(result, 0, print_results);
|
284
|
+
rb_ary_store(result, 1, INT2FIX(xsize));
|
285
|
+
rb_ary_store(result, 2, INT2FIX(ysize));
|
286
|
+
return result;
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
VALUE rb_rrd_last(
|
291
|
+
VALUE self,
|
292
|
+
VALUE args)
|
293
|
+
{
|
294
|
+
string_arr a;
|
295
|
+
time_t last;
|
296
|
+
|
297
|
+
a = string_arr_new(args);
|
298
|
+
reset_rrd_state();
|
299
|
+
last = rrd_last(a.len, a.strings);
|
300
|
+
string_arr_delete(a);
|
301
|
+
|
302
|
+
RRD_CHECK_ERROR
|
303
|
+
return rb_funcall(rb_cTime, rb_intern("at"), 1, UINT2NUM(last));
|
304
|
+
}
|
305
|
+
|
306
|
+
void Init_RRD(
|
307
|
+
)
|
308
|
+
{
|
309
|
+
mRRD = rb_define_module("RRD");
|
310
|
+
rb_eRRDError = rb_define_class("RRDError", rb_eStandardError);
|
311
|
+
|
312
|
+
rb_define_module_function(mRRD, "create", rb_rrd_create, -2);
|
313
|
+
rb_define_module_function(mRRD, "dump", rb_rrd_dump, -2);
|
314
|
+
rb_define_module_function(mRRD, "fetch", rb_rrd_fetch, -2);
|
315
|
+
rb_define_module_function(mRRD, "graph", rb_rrd_graph, -2);
|
316
|
+
rb_define_module_function(mRRD, "last", rb_rrd_last, -2);
|
317
|
+
rb_define_module_function(mRRD, "resize", rb_rrd_resize, -2);
|
318
|
+
rb_define_module_function(mRRD, "restore", rb_rrd_restore, -2);
|
319
|
+
rb_define_module_function(mRRD, "tune", rb_rrd_tune, -2);
|
320
|
+
rb_define_module_function(mRRD, "update", rb_rrd_update, -2);
|
321
|
+
rb_define_module_function(mRRD, "info", rb_rrd_info, -2);
|
322
|
+
rb_define_module_function(mRRD, "updatev", rb_rrd_updatev, -2);
|
323
|
+
rb_define_module_function(mRRD, "graphv", rb_rrd_graphv, -2);
|
324
|
+
}
|
@@ -0,0 +1,390 @@
|
|
1
|
+
/* $Id: main.c 2022 2010-02-16 13:04:18Z oetiker $
|
2
|
+
* Substantial penalty for early withdrawal.
|
3
|
+
*/
|
4
|
+
|
5
|
+
#include <unistd.h>
|
6
|
+
#include <ruby.h>
|
7
|
+
#include <math.h>
|
8
|
+
#include <rrd.h>
|
9
|
+
|
10
|
+
typedef struct string_arr_t {
|
11
|
+
int len;
|
12
|
+
char **strings;
|
13
|
+
} string_arr;
|
14
|
+
|
15
|
+
VALUE mRRD;
|
16
|
+
VALUE rb_eRRDError;
|
17
|
+
|
18
|
+
typedef int (
|
19
|
+
*RRDFUNC) (
|
20
|
+
int argc,
|
21
|
+
char **argv);
|
22
|
+
|
23
|
+
typedef rrd_info_t *(
|
24
|
+
*RRDINFOFUNC) (
|
25
|
+
int argc,
|
26
|
+
char **argv);
|
27
|
+
|
28
|
+
#define RRD_CHECK_ERROR \
|
29
|
+
if (rrd_test_error()) \
|
30
|
+
rb_raise(rb_eRRDError, rrd_get_error()); \
|
31
|
+
rrd_clear_error();
|
32
|
+
|
33
|
+
string_arr string_arr_new(
|
34
|
+
VALUE rb_strings)
|
35
|
+
{
|
36
|
+
string_arr a;
|
37
|
+
char buf[64];
|
38
|
+
int i;
|
39
|
+
|
40
|
+
Check_Type(rb_strings, T_ARRAY);
|
41
|
+
a.len = RARRAY_LEN(rb_strings) + 1;
|
42
|
+
|
43
|
+
a.strings = malloc(a.len * sizeof(char *));
|
44
|
+
a.strings[0] = "dummy"; /* first element is a dummy element */
|
45
|
+
|
46
|
+
for (i = 0; i < a.len - 1; i++) {
|
47
|
+
VALUE v = rb_ary_entry(rb_strings, i);
|
48
|
+
|
49
|
+
switch (TYPE(v)) {
|
50
|
+
case T_STRING:
|
51
|
+
a.strings[i + 1] = strdup(STR2CSTR(v));
|
52
|
+
break;
|
53
|
+
case T_FIXNUM:
|
54
|
+
snprintf(buf, 63, "%d", FIX2INT(v));
|
55
|
+
a.strings[i + 1] = strdup(buf);
|
56
|
+
break;
|
57
|
+
default:
|
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);
|
61
|
+
break;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
return a;
|
66
|
+
}
|
67
|
+
|
68
|
+
void string_arr_delete(
|
69
|
+
string_arr a)
|
70
|
+
{
|
71
|
+
int i;
|
72
|
+
|
73
|
+
/* skip dummy first entry */
|
74
|
+
for (i = 1; i < a.len; i++) {
|
75
|
+
free(a.strings[i]);
|
76
|
+
}
|
77
|
+
|
78
|
+
free(a.strings);
|
79
|
+
}
|
80
|
+
|
81
|
+
void reset_rrd_state(
|
82
|
+
)
|
83
|
+
{
|
84
|
+
optind = 0;
|
85
|
+
opterr = 0;
|
86
|
+
rrd_clear_error();
|
87
|
+
}
|
88
|
+
|
89
|
+
/* Simple Calls */
|
90
|
+
|
91
|
+
VALUE rrd_call(
|
92
|
+
RRDFUNC func,
|
93
|
+
VALUE args)
|
94
|
+
{
|
95
|
+
string_arr a;
|
96
|
+
|
97
|
+
a = string_arr_new(args);
|
98
|
+
reset_rrd_state();
|
99
|
+
func(a.len, a.strings);
|
100
|
+
string_arr_delete(a);
|
101
|
+
|
102
|
+
RRD_CHECK_ERROR return Qnil;
|
103
|
+
}
|
104
|
+
|
105
|
+
VALUE rb_rrd_create(
|
106
|
+
VALUE self,
|
107
|
+
VALUE args)
|
108
|
+
{
|
109
|
+
return rrd_call(rrd_create, args);
|
110
|
+
}
|
111
|
+
|
112
|
+
VALUE rb_rrd_dump(
|
113
|
+
VALUE self,
|
114
|
+
VALUE args)
|
115
|
+
{
|
116
|
+
return rrd_call(rrd_dump, args);
|
117
|
+
}
|
118
|
+
|
119
|
+
VALUE rb_rrd_resize(
|
120
|
+
VALUE self,
|
121
|
+
VALUE args)
|
122
|
+
{
|
123
|
+
return rrd_call(rrd_resize, args);
|
124
|
+
}
|
125
|
+
|
126
|
+
VALUE rb_rrd_restore(
|
127
|
+
VALUE self,
|
128
|
+
VALUE args)
|
129
|
+
{
|
130
|
+
return rrd_call(rrd_restore, args);
|
131
|
+
}
|
132
|
+
|
133
|
+
VALUE rb_rrd_tune(
|
134
|
+
VALUE self,
|
135
|
+
VALUE args)
|
136
|
+
{
|
137
|
+
return rrd_call(rrd_tune, args);
|
138
|
+
}
|
139
|
+
|
140
|
+
VALUE rb_rrd_update(
|
141
|
+
VALUE self,
|
142
|
+
VALUE args)
|
143
|
+
{
|
144
|
+
return rrd_call(rrd_update, args);
|
145
|
+
}
|
146
|
+
|
147
|
+
VALUE rb_rrd_flushcached(
|
148
|
+
VALUE self,
|
149
|
+
VALUE args)
|
150
|
+
{
|
151
|
+
return rrd_call(rrd_flushcached, args);
|
152
|
+
}
|
153
|
+
|
154
|
+
|
155
|
+
/* Calls Returning Data via the Info Interface */
|
156
|
+
|
157
|
+
VALUE rb_rrd_infocall(
|
158
|
+
RRDINFOFUNC func,
|
159
|
+
VALUE args)
|
160
|
+
{
|
161
|
+
string_arr a;
|
162
|
+
rrd_info_t *p, *data;
|
163
|
+
VALUE result;
|
164
|
+
|
165
|
+
a = string_arr_new(args);
|
166
|
+
reset_rrd_state();
|
167
|
+
data = func(a.len, a.strings);
|
168
|
+
string_arr_delete(a);
|
169
|
+
|
170
|
+
RRD_CHECK_ERROR result = rb_hash_new();
|
171
|
+
|
172
|
+
p = data;
|
173
|
+
while (data) {
|
174
|
+
VALUE key = rb_str_new2(data->key);
|
175
|
+
|
176
|
+
switch (data->type) {
|
177
|
+
case RD_I_VAL:
|
178
|
+
if (isnan(data->value.u_val)) {
|
179
|
+
rb_hash_aset(result, key, Qnil);
|
180
|
+
} else {
|
181
|
+
rb_hash_aset(result, key, rb_float_new(data->value.u_val));
|
182
|
+
}
|
183
|
+
break;
|
184
|
+
case RD_I_CNT:
|
185
|
+
rb_hash_aset(result, key, INT2FIX(data->value.u_cnt));
|
186
|
+
break;
|
187
|
+
case RD_I_STR:
|
188
|
+
rb_hash_aset(result, key, rb_str_new2(data->value.u_str));
|
189
|
+
break;
|
190
|
+
case RD_I_INT:
|
191
|
+
rb_hash_aset(result, key, INT2FIX(data->value.u_int));
|
192
|
+
break;
|
193
|
+
case RD_I_BLO:
|
194
|
+
rb_hash_aset(result, key,
|
195
|
+
rb_str_new((char *)data->value.u_blo.ptr,
|
196
|
+
data->value.u_blo.size));
|
197
|
+
break;
|
198
|
+
}
|
199
|
+
data = data->next;
|
200
|
+
}
|
201
|
+
rrd_info_free(p);
|
202
|
+
return result;
|
203
|
+
}
|
204
|
+
|
205
|
+
VALUE rb_rrd_info(
|
206
|
+
VALUE self,
|
207
|
+
VALUE args)
|
208
|
+
{
|
209
|
+
return rb_rrd_infocall(rrd_info, args);
|
210
|
+
}
|
211
|
+
|
212
|
+
VALUE rb_rrd_updatev(
|
213
|
+
VALUE self,
|
214
|
+
VALUE args)
|
215
|
+
{
|
216
|
+
return rb_rrd_infocall(rrd_update_v, args);
|
217
|
+
}
|
218
|
+
|
219
|
+
VALUE rb_rrd_graphv(
|
220
|
+
VALUE self,
|
221
|
+
VALUE args)
|
222
|
+
{
|
223
|
+
return rb_rrd_infocall(rrd_graph_v, args);
|
224
|
+
}
|
225
|
+
|
226
|
+
|
227
|
+
/* Other Calls */
|
228
|
+
|
229
|
+
VALUE rb_rrd_fetch(
|
230
|
+
VALUE self,
|
231
|
+
VALUE args)
|
232
|
+
{
|
233
|
+
string_arr a;
|
234
|
+
unsigned long i, j, k, step, ds_cnt;
|
235
|
+
rrd_value_t *raw_data;
|
236
|
+
char **raw_names;
|
237
|
+
VALUE data, names, result;
|
238
|
+
time_t start, end;
|
239
|
+
|
240
|
+
a = string_arr_new(args);
|
241
|
+
reset_rrd_state();
|
242
|
+
rrd_fetch(a.len, a.strings, &start, &end, &step, &ds_cnt, &raw_names,
|
243
|
+
&raw_data);
|
244
|
+
string_arr_delete(a);
|
245
|
+
|
246
|
+
RRD_CHECK_ERROR names = rb_ary_new();
|
247
|
+
|
248
|
+
for (i = 0; i < ds_cnt; i++) {
|
249
|
+
rb_ary_push(names, rb_str_new2(raw_names[i]));
|
250
|
+
rrd_freemem(raw_names[i]);
|
251
|
+
}
|
252
|
+
rrd_freemem(raw_names);
|
253
|
+
|
254
|
+
k = 0;
|
255
|
+
data = rb_ary_new();
|
256
|
+
for (i = start; i <= end; i += step) {
|
257
|
+
VALUE line = rb_ary_new2(ds_cnt);
|
258
|
+
|
259
|
+
for (j = 0; j < ds_cnt; j++) {
|
260
|
+
rb_ary_store(line, j, rb_float_new(raw_data[k]));
|
261
|
+
k++;
|
262
|
+
}
|
263
|
+
rb_ary_push(data, line);
|
264
|
+
}
|
265
|
+
rrd_freemem(raw_data);
|
266
|
+
|
267
|
+
result = rb_ary_new2(5);
|
268
|
+
rb_ary_store(result, 0, INT2NUM(start));
|
269
|
+
rb_ary_store(result, 1, INT2NUM(end));
|
270
|
+
rb_ary_store(result, 2, names);
|
271
|
+
rb_ary_store(result, 3, data);
|
272
|
+
rb_ary_store(result, 4, INT2FIX(step));
|
273
|
+
return result;
|
274
|
+
}
|
275
|
+
|
276
|
+
VALUE rb_rrd_graph(
|
277
|
+
VALUE self,
|
278
|
+
VALUE args)
|
279
|
+
{
|
280
|
+
string_arr a;
|
281
|
+
char **calcpr, **p;
|
282
|
+
VALUE result, print_results;
|
283
|
+
int xsize, ysize;
|
284
|
+
double ymin, ymax;
|
285
|
+
|
286
|
+
a = string_arr_new(args);
|
287
|
+
reset_rrd_state();
|
288
|
+
rrd_graph(a.len, a.strings, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax);
|
289
|
+
string_arr_delete(a);
|
290
|
+
|
291
|
+
RRD_CHECK_ERROR result = rb_ary_new2(3);
|
292
|
+
|
293
|
+
print_results = rb_ary_new();
|
294
|
+
p = calcpr;
|
295
|
+
for (p = calcpr; p && *p; p++) {
|
296
|
+
rb_ary_push(print_results, rb_str_new2(*p));
|
297
|
+
rrd_freemem(*p);
|
298
|
+
}
|
299
|
+
rrd_freemem(calcpr);
|
300
|
+
rb_ary_store(result, 0, print_results);
|
301
|
+
rb_ary_store(result, 1, INT2FIX(xsize));
|
302
|
+
rb_ary_store(result, 2, INT2FIX(ysize));
|
303
|
+
return result;
|
304
|
+
}
|
305
|
+
|
306
|
+
|
307
|
+
VALUE rb_rrd_last(
|
308
|
+
VALUE self,
|
309
|
+
VALUE args)
|
310
|
+
{
|
311
|
+
string_arr a;
|
312
|
+
time_t last;
|
313
|
+
|
314
|
+
a = string_arr_new(args);
|
315
|
+
reset_rrd_state();
|
316
|
+
last = rrd_last(a.len, a.strings);
|
317
|
+
string_arr_delete(a);
|
318
|
+
|
319
|
+
RRD_CHECK_ERROR
|
320
|
+
return rb_funcall(rb_cTime, rb_intern("at"), 1, UINT2NUM(last));
|
321
|
+
}
|
322
|
+
|
323
|
+
VALUE rb_rrd_xport(
|
324
|
+
VALUE self,
|
325
|
+
VALUE args)
|
326
|
+
{
|
327
|
+
string_arr a;
|
328
|
+
unsigned long i, j, k, step, col_cnt;
|
329
|
+
int xxsize;
|
330
|
+
rrd_value_t *data;
|
331
|
+
char **legend_v;
|
332
|
+
VALUE legend, result, rdata;
|
333
|
+
time_t start, end;
|
334
|
+
|
335
|
+
a = string_arr_new(args);
|
336
|
+
rrd_xport(a.len, a.strings, &xxsize, &start, &end, &step, &col_cnt, &legend_v, &data);
|
337
|
+
string_arr_delete(a);
|
338
|
+
|
339
|
+
RRD_CHECK_ERROR;
|
340
|
+
|
341
|
+
legend = rb_ary_new();
|
342
|
+
for (i = 0; i < col_cnt; i++) {
|
343
|
+
rb_ary_push(legend, rb_str_new2(legend_v[i]));
|
344
|
+
free(legend_v[i]);
|
345
|
+
}
|
346
|
+
free(legend_v);
|
347
|
+
|
348
|
+
k = 0;
|
349
|
+
rdata = rb_ary_new();
|
350
|
+
for (i = start; i <= end; i += step) {
|
351
|
+
VALUE line = rb_ary_new2(col_cnt);
|
352
|
+
for (j = 0; j < col_cnt; j++) {
|
353
|
+
rb_ary_store(line, j, rb_float_new(data[k]));
|
354
|
+
k++;
|
355
|
+
}
|
356
|
+
rb_ary_push(rdata, line);
|
357
|
+
}
|
358
|
+
free(data);
|
359
|
+
|
360
|
+
result = rb_ary_new2(6);
|
361
|
+
rb_ary_store(result, 0, INT2FIX(start));
|
362
|
+
rb_ary_store(result, 1, INT2FIX(end));
|
363
|
+
rb_ary_store(result, 2, INT2FIX(step));
|
364
|
+
rb_ary_store(result, 3, INT2FIX(col_cnt));
|
365
|
+
rb_ary_store(result, 4, legend);
|
366
|
+
rb_ary_store(result, 5, rdata);
|
367
|
+
return result;
|
368
|
+
}
|
369
|
+
|
370
|
+
void Init_RRD(
|
371
|
+
)
|
372
|
+
{
|
373
|
+
mRRD = rb_define_module("RRD");
|
374
|
+
rb_eRRDError = rb_define_class("RRDError", rb_eStandardError);
|
375
|
+
|
376
|
+
rb_define_module_function(mRRD, "create", rb_rrd_create, -2);
|
377
|
+
rb_define_module_function(mRRD, "dump", rb_rrd_dump, -2);
|
378
|
+
rb_define_module_function(mRRD, "fetch", rb_rrd_fetch, -2);
|
379
|
+
rb_define_module_function(mRRD, "graph", rb_rrd_graph, -2);
|
380
|
+
rb_define_module_function(mRRD, "last", rb_rrd_last, -2);
|
381
|
+
rb_define_module_function(mRRD, "resize", rb_rrd_resize, -2);
|
382
|
+
rb_define_module_function(mRRD, "restore", rb_rrd_restore, -2);
|
383
|
+
rb_define_module_function(mRRD, "tune", rb_rrd_tune, -2);
|
384
|
+
rb_define_module_function(mRRD, "update", rb_rrd_update, -2);
|
385
|
+
rb_define_module_function(mRRD, "flushcached", rb_rrd_flushcached, -2);
|
386
|
+
rb_define_module_function(mRRD, "info", rb_rrd_info, -2);
|
387
|
+
rb_define_module_function(mRRD, "updatev", rb_rrd_updatev, -2);
|
388
|
+
rb_define_module_function(mRRD, "graphv", rb_rrd_graphv, -2);
|
389
|
+
rb_define_module_function(mRRD, "xport", rb_rrd_xport, -2);
|
390
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
|
3
|
+
dir_config("rrd")
|
4
|
+
|
5
|
+
# Try to detect the librrd version.
|
6
|
+
# Please adjust if needed and open an issue on github
|
7
|
+
# or submit a pull request. Thanks!
|
8
|
+
if have_library("rrd", "rrd_flushcached")
|
9
|
+
src_prefix = '1.4'
|
10
|
+
elsif have_library("rrd", "rrd_create")
|
11
|
+
src_prefix = '1.3'
|
12
|
+
else
|
13
|
+
puts "Unsupported librrd version, abort."
|
14
|
+
exit 1
|
15
|
+
end
|
16
|
+
|
17
|
+
create_makefile("RRD", '$(srcdir)/' + src_prefix)
|