-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrrd_csv.c
316 lines (273 loc) · 8.7 KB
/
rrd_csv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*****************************************************************************
* RRDtool 1.GIT, Copyright by Tobi Oetiker
*****************************************************************************
* rrd_csv Dumps RRD to CSV format
*****************************************************************************
* $Id$
* $Log$
* Revision 1.0 2004/05/25 20:53:21 fbsanchez
* First version of this component
*
*****************************************************************************/
#include "rrd_tool.h"
#include "rrd_rpncalc.h"
#include "rrd_client.h"
#include "rrd_snprintf.h"
#include "rrd.h"
#if !(defined(NETWARE) || defined(WIN32))
extern char *tzname[2];
#endif
void PrintUsage() {
printf("* dump an RRD to CSV\nUsage:\n"
"\trrd2csv [--csv|-c]\n"
"\t\t[--cf|-f avg|max|min|last]\n"
"\t\tfile.rrd [file.xml]\n\n");
}
int RRD2CSV(
const char *filename,
int opt_header,
rrd_output_callback_t cb,
void *user,
int opt_mode,
char *opt_cf)
{
unsigned int i, ii, ix, iii = 0;
time_t now;
rrd_value_t my_cdp;
off_t rra_base, rra_start, rra_next;
rrd_file_t *rrd_file;
rrd_t rrd;
//These two macros are local defines to clean up visible code from its redundancy
//and make it easier to read.
#define CB_PUTS(str) \
do { \
size_t len = strlen(str); \
\
if (cb((str), len, user) != len) \
goto err_out; \
} while (0);
#define CB_FMTS(...) do { \
char buffer[256]; \
rrd_snprintf (buffer, sizeof(buffer), __VA_ARGS__); \
CB_PUTS (buffer); \
} while (0)
//These macros are to be undefined at the end of this function
//Check if we got a (valid) callback method
if (!cb) {
return (-1);
}
rrd_init(&rrd);
rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_LOCK |
RRD_READAHEAD);
if (rrd_file == NULL) {
rrd_free(&rrd);
return (-1);
}
// Added CSV format output
int data_source = CF_AVERAGE;
if (opt_cf != NULL) {
if(strcmp(opt_cf, "max") == 0) {
data_source = CF_MAXIMUM;
}
else if (strcmp(opt_cf, "min") == 0) {
data_source = CF_MINIMUM;
}
else if (strcmp(opt_cf, "last") == 0) {
data_source = CF_LAST;
}
}
CB_FMTS("utimestamp;");
for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
CB_FMTS("%s ", rrd.ds_def[i].ds_nam);
CB_FMTS("(%s);", rrd.ds_def[i].dst);
}
CB_FMTS("\n");
rra_base = rrd_file->header_len;
rra_next = rra_base;
for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
long timer = 0;
rra_start = rra_next;
rra_next += (rrd.stat_head->ds_cnt
* rrd.rra_def[i].row_cnt * sizeof(rrd_value_t));
if (rrd_cf_conv(rrd.rra_def[i].cf_nam) == data_source) {
rrd_seek(rrd_file, (rra_start + (rrd.rra_ptr[i].cur_row + 1)
* rrd.stat_head->ds_cnt
* sizeof(rrd_value_t)), SEEK_SET);
timer = -(long)(rrd.rra_def[i].row_cnt - 1);
ii = rrd.rra_ptr[i].cur_row;
for (ix = 0; ix < rrd.rra_def[i].row_cnt; ix++) {
ii++;
if (ii >= rrd.rra_def[i].row_cnt) {
rrd_seek(rrd_file, rra_start, SEEK_SET);
ii = 0; /* wrap if max row cnt is reached */
}
now = (rrd.live_head->last_up
- rrd.live_head->last_up
% (rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step))
+ (timer * (long)rrd.rra_def[i].pdp_cnt * (long)rrd.stat_head->pdp_step);
timer++;
CB_FMTS("%lld;", (long long int) now);
for (iii = 0; iii < rrd.stat_head->ds_cnt; iii++) {
rrd_read(rrd_file, &my_cdp, sizeof(rrd_value_t) * 1);
if (isnan(my_cdp)) {
// ignore empty data
CB_PUTS(";");
} else {
CB_FMTS("%0.6f;", my_cdp);
}
}
CB_PUTS("\n");
}
}
}
rrd_free(&rrd);
return rrd_close(rrd_file);
err_out:
rrd_set_error("error writing output file: %s", rrd_strerror(errno));
rrd_free(&rrd);
rrd_close(rrd_file);
return (-1);
//Undefining the previously defined shortcuts
//See start of this function
#undef CB_PUTS
#undef CB_FMTS
//End of macro undefining
}
int rrd_dump_opt_r(
const char *filename,
char *outname,
int opt_noheader,
int opt_mode,
char *opt_cf)
{
FILE *out_file;
int res;
out_file = NULL;
if (outname) {
if (!(out_file = fopen(outname, "w"))) {
return (-1);
}
} else {
out_file = stdout;
}
res = RRD2CSV(filename, opt_noheader, printf, (void *)out_file, opt_mode, opt_cf);
if (fflush(out_file) != 0) {
rrd_set_error("error flushing output: %s", rrd_strerror(errno));
res = -1;
}
if (out_file != stdout) {
fclose(out_file);
if (res != 0)
unlink(outname);
}
return res;
}
int rrd_csv(
int argc,
char **argv)
{
int opt;
struct optparse_long longopts[] = {
{"daemon", 'd', OPTPARSE_REQUIRED},
{"header", 'h', OPTPARSE_REQUIRED},
{"no-header", 'n', OPTPARSE_NONE},
{"csv", 'c', OPTPARSE_NONE},
{"cf", 'f', OPTPARSE_REQUIRED},
{0},
};
struct optparse options;
int rc;
/**
* 0 = no header
* 1 = dtd header
* 2 = xsd header
*/
int opt_header = 1;
int opt_mode = 0;
char *opt_daemon = NULL;
char *opt_cf = NULL;
/* init rrd clean */
optparse_init(&options, argc, argv);
while ((opt = optparse_long(&options, longopts, NULL)) != -1) {
switch (opt) {
case 'd':
if (opt_daemon != NULL) {
free (opt_daemon);
}
opt_daemon = strdup(options.optarg);
if (opt_daemon == NULL)
{
rrd_set_error ("strdup failed.");
return (-1);
}
break;
case 'f':
if (opt_cf != NULL) {
free (opt_cf);
}
opt_cf = strdup(options.optarg);
if (opt_cf == NULL)
{
rrd_set_error ("strdup failed.");
return (-1);
}
break;
case 'n':
opt_header = 0;
break;
case 'c':
opt_mode = 1;
break;
case 'h':
if (strcmp(options.optarg, "dtd") == 0) {
opt_header = 1;
} else if (strcmp(options.optarg, "xsd") == 0) {
opt_header = 2;
} else if (strcmp(options.optarg, "none") == 0) {
opt_header = 0;
}
break;
default:
rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}]\n"
"[--no-header|-n]\n"
"[--csv|-c]\n"
"[--cf|-f avg|max|min|last]\n"
"[--daemon|-d address]\n"
"file.rrd [file.xml]", options.argv[0]);
if (opt_daemon != NULL) {
free(opt_daemon);
}
if (opt_cf != NULL) {
free(opt_cf);
}
return (-1);
break;
}
} /* while (opt != -1) */
if ((options.argc - options.optind) < 1 || (options.argc - options.optind) > 2) {
rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}]\n"
"[--no-header|-n]\n"
"[--csv|-c]\n"
"[--cf|-f avg|max|min|last]\n"
"[--daemon|-d address]\n"
"file.rrd [file.xml]", options.argv[0]);
if (opt_daemon != NULL) {
free(opt_daemon);
}
return (-1);
}
if ((options.argc - options.optind) == 2) {
rc = rrd_dump_opt_r(options.argv[options.optind], options.argv[options.optind + 1], opt_header, opt_mode, opt_cf);
} else {
rc = rrd_dump_opt_r(options.argv[options.optind], NULL, opt_header, opt_mode, opt_cf);
}
return rc;
}
int main(int argc,char **argv) {
if (argc == 1) {
PrintUsage("");
return 0;
}
rrd_csv(argc,argv);
return 0;
}