70
74
rrd_value_t my_cdp;
71
75
off_t rra_base, rra_start, rra_next;
72
76
rrd_file_t *rrd_file;
77
80
char *old_locale = "";
82
//These two macros are local defines to clean up visible code from its redndancy
83
//and make it easier to read.
84
#define CB_PUTS(str) \
85
cb((str), strlen((str)), user)
86
#define CB_FMTS(...) do { \
88
snprintf (buffer, sizeof(buffer), __VA_ARGS__); \
91
//These macros are to be undefined at the end of this function
93
//Check if we got a (valid) callback method
78
100
rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_READAHEAD);
79
101
if (rrd_file == NULL) {
86
if (!(out_file = fopen(outname, "w"))) {
93
106
old_locale = setlocale(LC_NUMERIC, "C");
96
fputs("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n", out_file);
98
("<!DOCTYPE rrd SYSTEM \"http://oss.oetiker.ch/rrdtool/rrdtool.dtd\">\n",
109
if (opt_header == 1) {
110
CB_PUTS("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
111
CB_PUTS("<!DOCTYPE rrd SYSTEM \"http://oss.oetiker.ch/rrdtool/rrdtool.dtd\">\n");
112
CB_PUTS("<!-- Round Robin Database Dump -->\n");
114
} else if (opt_header == 2) {
115
CB_PUTS("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
116
CB_PUTS("<!-- Round Robin Database Dump -->\n");
117
CB_PUTS("<rrd xmlns=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml\" "
118
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
119
CB_PUTS("\txsi:schemaLocation=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml "
120
"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xsd\">\n");
122
CB_PUTS("<!-- Round Robin Database Dump -->\n");
101
fputs("<!-- Round Robin Database Dump -->", out_file);
102
fputs("<rrd>", out_file);
103
126
if (atoi(rrd.stat_head->version) <= 3) {
104
fprintf(out_file, "\t<version> %s </version>\n", RRD_VERSION3);
127
CB_FMTS("\t<version>%s</version>\n", RRD_VERSION3);
106
fprintf(out_file, "\t<version> %s </version>\n", RRD_VERSION);
129
CB_FMTS("\t<version>%s</version>\n", RRD_VERSION);
108
fprintf(out_file, "\t<step> %lu </step> <!-- Seconds -->\n",
109
rrd.stat_head->pdp_step);
132
CB_FMTS("\t<step>%lu</step> <!-- Seconds -->\n",
133
rrd.stat_head->pdp_step);
110
135
#ifdef HAVE_STRFTIME
111
136
localtime_r(&rrd.live_head->last_up, &tm);
112
strftime(somestring, 200, "%Y-%m-%d %H:%M:%S %Z", &tm);
137
strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
114
139
# error "Need strftime"
116
fprintf(out_file, "\t<lastupdate> %lu </lastupdate> <!-- %s -->\n\n",
117
(unsigned long) rrd.live_head->last_up, somestring);
141
CB_FMTS("\t<lastupdate>%lld</lastupdate> <!-- %s -->\n\n",
142
(long long) rrd.live_head->last_up, somestring);
118
143
for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
119
fprintf(out_file, "\t<ds>\n");
120
fprintf(out_file, "\t\t<name> %s </name>\n", rrd.ds_def[i].ds_nam);
121
fprintf(out_file, "\t\t<type> %s </type>\n", rrd.ds_def[i].dst);
146
CB_FMTS("\t\t<name> %s </name>\n", rrd.ds_def[i].ds_nam);
148
CB_FMTS("\t\t<type> %s </type>\n", rrd.ds_def[i].dst);
122
150
if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
124
"\t\t<minimal_heartbeat> %lu </minimal_heartbeat>\n",
151
CB_FMTS("\t\t<minimal_heartbeat>%lu</minimal_heartbeat>\n",
125
152
rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
126
154
if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)) {
127
fprintf(out_file, "\t\t<min> NaN </min>\n");
155
CB_PUTS("\t\t<min>NaN</min>\n");
129
fprintf(out_file, "\t\t<min> %0.10e </min>\n",
130
rrd.ds_def[i].par[DS_min_val].u_val);
157
CB_FMTS("\t\t<min>%0.10e</min>\n",
158
rrd.ds_def[i].par[DS_min_val].u_val);
132
161
if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)) {
133
fprintf(out_file, "\t\t<max> NaN </max>\n");
162
CB_PUTS("\t\t<max>NaN</max>\n");
135
fprintf(out_file, "\t\t<max> %0.10e </max>\n",
136
rrd.ds_def[i].par[DS_max_val].u_val);
164
CB_FMTS("\t\t<max>%0.10e</max>\n",
165
rrd.ds_def[i].par[DS_max_val].u_val);
138
167
} else { /* DST_CDEF */
139
168
char *str = NULL;
141
170
rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
143
fprintf(out_file, "\t\t<cdef> %s </cdef>\n", str);
173
//Splitting into 3 writes to avoid allocating memory
174
//This is better compared to snprintf as str may be of arbitrary size
175
CB_PUTS("\t\t<cdef> ");
177
CB_PUTS(" </cdef>\n");
146
fprintf(out_file, "\n\t\t<!-- PDP Status -->\n");
147
fprintf(out_file, "\t\t<last_ds> %s </last_ds>\n",
148
rrd.pdp_prep[i].last_ds);
182
CB_PUTS("\n\t\t<!-- PDP Status -->\n");
183
CB_FMTS("\t\t<last_ds>%s</last_ds>\n",
184
rrd.pdp_prep[i].last_ds);
149
186
if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)) {
150
fprintf(out_file, "\t\t<value> NaN </value>\n");
187
CB_PUTS("\t\t<value>NaN</value>\n");
152
fprintf(out_file, "\t\t<value> %0.10e </value>\n",
153
rrd.pdp_prep[i].scratch[PDP_val].u_val);
189
CB_FMTS("\t\t<value>%0.10e</value>\n",
190
rrd.pdp_prep[i].scratch[PDP_val].u_val);
155
fprintf(out_file, "\t\t<unknown_sec> %lu </unknown_sec>\n",
156
rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
158
fprintf(out_file, "\t</ds>\n\n");
193
CB_FMTS("\t\t<unknown_sec> %lu </unknown_sec>\n",
194
rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
196
CB_PUTS("\t</ds>\n\n");
161
fputs("<!-- Round Robin Archives -->", out_file);
199
CB_PUTS("\t<!-- Round Robin Archives -->\n");
163
201
rra_base = rrd_file->header_len;
164
202
rra_next = rra_base;
170
208
rra_start = rra_next;
171
209
rra_next += (rrd.stat_head->ds_cnt
172
210
* rrd.rra_def[i].row_cnt * sizeof(rrd_value_t));
173
fprintf(out_file, "\t<rra>\n");
174
fprintf(out_file, "\t\t<cf> %s </cf>\n", rrd.rra_def[i].cf_nam);
176
"\t\t<pdp_per_row> %lu </pdp_per_row> <!-- %lu seconds -->\n\n",
177
rrd.rra_def[i].pdp_cnt,
178
rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
212
CB_PUTS("\t<rra>\n");
214
CB_FMTS("\t\t<cf>%s</cf>\n", rrd.rra_def[i].cf_nam);
216
CB_FMTS("\t\t<pdp_per_row>%lu</pdp_per_row> <!-- %lu seconds -->\n\n",
217
rrd.rra_def[i].pdp_cnt,
218
rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
179
220
/* support for RRA parameters */
180
fprintf(out_file, "\t\t<params>\n");
221
CB_PUTS("\t\t<params>\n");
181
223
switch (cf_conv(rrd.rra_def[i].cf_nam)) {
182
224
case CF_HWPREDICT:
183
225
case CF_MHWPREDICT:
184
fprintf(out_file, "\t\t<hw_alpha> %0.10e </hw_alpha>\n",
185
rrd.rra_def[i].par[RRA_hw_alpha].u_val);
186
fprintf(out_file, "\t\t<hw_beta> %0.10e </hw_beta>\n",
187
rrd.rra_def[i].par[RRA_hw_beta].u_val);
189
"\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
190
rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
226
CB_FMTS("\t\t<hw_alpha>%0.10e</hw_alpha>\n",
227
rrd.rra_def[i].par[RRA_hw_alpha].u_val);
229
CB_FMTS("\t\t<hw_beta>%0.10e</hw_beta>\n",
230
rrd.rra_def[i].par[RRA_hw_beta].u_val);
232
CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
233
rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
192
235
case CF_SEASONAL:
193
236
case CF_DEVSEASONAL:
195
"\t\t<seasonal_gamma> %0.10e </seasonal_gamma>\n",
196
rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
198
"\t\t<seasonal_smooth_idx> %lu </seasonal_smooth_idx>\n",
199
rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
237
CB_FMTS("\t\t<seasonal_gamma>%0.10e</seasonal_gamma>\n",
238
rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
240
CB_FMTS("\t\t<seasonal_smooth_idx>%lu</seasonal_smooth_idx>\n",
241
rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
200
243
if (atoi(rrd.stat_head->version) >= 4) {
202
"\t\t<smoothing_window> %0.10e </smoothing_window>\n",
203
rrd.rra_def[i].par[RRA_seasonal_smoothing_window].
244
CB_FMTS("\t\t<smoothing_window>%0.10e</smoothing_window>\n",
245
rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val);
207
"\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
208
rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
248
CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
249
rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
210
251
case CF_FAILURES:
211
fprintf(out_file, "\t\t<delta_pos> %0.10e </delta_pos>\n",
212
rrd.rra_def[i].par[RRA_delta_pos].u_val);
213
fprintf(out_file, "\t\t<delta_neg> %0.10e </delta_neg>\n",
214
rrd.rra_def[i].par[RRA_delta_neg].u_val);
215
fprintf(out_file, "\t\t<window_len> %lu </window_len>\n",
216
rrd.rra_def[i].par[RRA_window_len].u_cnt);
218
"\t\t<failure_threshold> %lu </failure_threshold>\n",
219
rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
252
CB_FMTS("\t\t<delta_pos>%0.10e</delta_pos>\n",
253
rrd.rra_def[i].par[RRA_delta_pos].u_val);
255
CB_FMTS("\t\t<delta_neg>%0.10e</delta_neg>\n",
256
rrd.rra_def[i].par[RRA_delta_neg].u_val);
258
CB_FMTS("\t\t<window_len>%lu</window_len>\n",
259
rrd.rra_def[i].par[RRA_window_len].u_cnt);
261
CB_FMTS("\t\t<failure_threshold>%lu</failure_threshold>\n",
262
rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
221
265
case CF_DEVPREDICT:
223
"\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
224
rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
266
CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
267
rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
231
fprintf(out_file, "\t\t<xff> %0.10e </xff>\n",
232
rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
274
CB_FMTS("\t\t<xff>%0.10e</xff>\n",
275
rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
235
fprintf(out_file, "\t\t</params>\n");
236
fprintf(out_file, "\t\t<cdp_prep>\n");
279
CB_PUTS("\t\t</params>\n");
280
CB_PUTS("\t\t<cdp_prep>\n");
237
282
for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
238
283
unsigned long ivalue;
240
fprintf(out_file, "\t\t\t<ds>\n");
285
CB_PUTS("\t\t\t<ds>\n");
241
286
/* support for exporting all CDP parameters */
242
287
/* parameters common to all CFs */
243
288
/* primary_val and secondary_val do not need to be saved between updates
244
289
* so strictly speaking they could be omitted.
245
290
* However, they can be useful for diagnostic purposes, so are included here. */
246
value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt
247
+ ii].scratch[CDP_primary_val].u_val;
250
"\t\t\t<primary_value> NaN </primary_value>\n");
253
"\t\t\t<primary_value> %0.10e </primary_value>\n",
257
rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
258
ii].scratch[CDP_secondary_val].u_val;
261
"\t\t\t<secondary_value> NaN </secondary_value>\n");
264
"\t\t\t<secondary_value> %0.10e </secondary_value>\n",
291
value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
292
scratch[CDP_primary_val].u_val;
294
CB_PUTS("\t\t\t<primary_value>NaN</primary_value>\n");
296
CB_FMTS("\t\t\t<primary_value>%0.10e</primary_value>\n", value);
299
value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
300
scratch[CDP_secondary_val].u_val;
302
CB_PUTS("\t\t\t<secondary_value>NaN</secondary_value>\n");
304
CB_FMTS("\t\t\t<secondary_value>%0.10e</secondary_value>\n", value);
267
307
switch (cf_conv(rrd.rra_def[i].cf_nam)) {
268
308
case CF_HWPREDICT:
269
309
case CF_MHWPREDICT:
271
rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
272
ii].scratch[CDP_hw_intercept].u_val;
274
fprintf(out_file, "\t\t\t<intercept> NaN </intercept>\n");
277
"\t\t\t<intercept> %0.10e </intercept>\n", value);
280
rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
281
ii].scratch[CDP_hw_last_intercept].u_val;
284
"\t\t\t<last_intercept> NaN </last_intercept>\n");
287
"\t\t\t<last_intercept> %0.10e </last_intercept>\n",
291
rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
292
ii].scratch[CDP_hw_slope].u_val;
294
fprintf(out_file, "\t\t\t<slope> NaN </slope>\n");
296
fprintf(out_file, "\t\t\t<slope> %0.10e </slope>\n",
300
rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
301
ii].scratch[CDP_hw_last_slope].u_val;
304
"\t\t\t<last_slope> NaN </last_slope>\n");
307
"\t\t\t<last_slope> %0.10e </last_slope>\n",
311
rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
312
ii].scratch[CDP_null_count].u_cnt;
313
fprintf(out_file, "\t\t\t<nan_count> %lu </nan_count>\n",
316
rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
317
ii].scratch[CDP_last_null_count].u_cnt;
319
"\t\t\t<last_nan_count> %lu </last_nan_count>\n",
310
value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
311
scratch[CDP_hw_intercept].u_val;
313
CB_PUTS("\t\t\t<intercept>NaN</intercept>\n");
315
CB_FMTS("\t\t\t<intercept>%0.10e</intercept>\n", value);
318
value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
319
scratch[CDP_hw_last_intercept].u_val;
321
CB_PUTS("\t\t\t<last_intercept>NaN</last_intercept>\n");
323
CB_FMTS("\t\t\t<last_intercept>%0.10e</last_intercept>\n", value);
326
value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
327
scratch[CDP_hw_slope].u_val;
329
CB_PUTS("\t\t\t<slope>NaN</slope>\n");
331
CB_FMTS("\t\t\t<slope>%0.10e</slope>\n", value);
334
value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
335
scratch[CDP_hw_last_slope].u_val;
337
CB_PUTS("\t\t\t<last_slope>NaN</last_slope>\n");
339
CB_FMTS("\t\t\t<last_slope>%0.10e</last_slope>\n", value);
342
ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
343
scratch[CDP_null_count].u_cnt;
344
CB_FMTS("\t\t\t<nan_count>%lu</nan_count>\n", ivalue);
346
ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
347
scratch[CDP_last_null_count].u_cnt;
348
CB_FMTS("\t\t\t<last_nan_count>%lu</last_nan_count>\n", ivalue);
322
350
case CF_SEASONAL:
323
351
case CF_DEVSEASONAL:
325
rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
326
ii].scratch[CDP_hw_seasonal].u_val;
328
fprintf(out_file, "\t\t\t<seasonal> NaN </seasonal>\n");
330
fprintf(out_file, "\t\t\t<seasonal> %0.10e </seasonal>\n",
334
rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
335
ii].scratch[CDP_hw_last_seasonal].u_val;
338
"\t\t\t<last_seasonal> NaN </last_seasonal>\n");
341
"\t\t\t<last_seasonal> %0.10e </last_seasonal>\n",
345
rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
346
ii].scratch[CDP_init_seasonal].u_cnt;
347
fprintf(out_file, "\t\t\t<init_flag> %lu </init_flag>\n",
352
value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
353
scratch[CDP_hw_seasonal].u_val;
355
CB_PUTS("\t\t\t<seasonal>NaN</seasonal>\n");
357
CB_FMTS("\t\t\t<seasonal>%0.10e</seasonal>\n", value);
360
value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
361
scratch[CDP_hw_last_seasonal].u_val;
363
CB_PUTS("\t\t\t<last_seasonal>NaN</last_seasonal>\n");
365
CB_FMTS("\t\t\t<last_seasonal>%0.10e</last_seasonal>\n", value);
368
ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
369
scratch[CDP_init_seasonal].u_cnt;
370
CB_FMTS("\t\t\t<init_flag>%lu</init_flag>\n", ivalue);
350
372
case CF_DEVPREDICT:
352
374
case CF_FAILURES:
354
376
unsigned short vidx;
355
char *violations_array = (char *) ((void *)
362
fprintf(out_file, "\t\t\t<history> ");
377
char *violations_array = (char *) ((void *)
378
rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch);
379
CB_PUTS("\t\t\t<history>");
364
vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
366
fprintf(out_file, "%d", violations_array[vidx]);
381
vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
383
CB_FMTS("%d", violations_array[vidx]);
368
fprintf(out_file, " </history>\n");
385
CB_PUTS("</history>\n");
413
427
#if HAVE_STRFTIME
414
428
localtime_r(&now, &tm);
415
strftime(somestring, 200, "%Y-%m-%d %H:%M:%S %Z", &tm);
429
strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
417
431
# error "Need strftime"
419
fprintf(out_file, "\t\t\t<!-- %s / %d --> <row>", somestring,
433
CB_FMTS("\t\t\t<!-- %s / %lld --> <row>", somestring, (long long) now);
421
434
for (iii = 0; iii < rrd.stat_head->ds_cnt; iii++) {
422
435
rrd_read(rrd_file, &my_cdp, sizeof(rrd_value_t) * 1);
423
436
if (isnan(my_cdp)) {
424
fprintf(out_file, "<v> NaN </v>");
437
CB_PUTS("<v>NaN</v>");
426
fprintf(out_file, "<v> %0.10e </v>", my_cdp);
439
CB_FMTS("<v>%0.10e</v>", my_cdp);
429
fprintf(out_file, "</row>\n");
431
fprintf(out_file, "\t\t</database>\n\t</rra>\n");
444
CB_PUTS("\t\t</database>\n\t</rra>\n");
434
fprintf(out_file, "</rrd>\n");
451
setlocale(LC_NUMERIC, old_locale);
453
return rrd_close(rrd_file);
455
//Undefining the previously defined shortcuts
456
//See start of this function
459
//End of macro undefining
463
size_t rrd_dump_opt_cb_fileout(
468
return fwrite(data, 1, len, (FILE *)user);
472
const char *filename,
481
if (!(out_file = fopen(outname, "w"))) {
488
res = rrd_dump_cb_r(filename, opt_noheader, rrd_dump_opt_cb_fileout, (void *)out_file);
436
490
if (out_file != stdout) {
437
491
fclose(out_file);
439
#ifdef HAVE_SETLOCALE
440
setlocale(LC_NUMERIC, old_locale);
442
return rrd_close(rrd_file);
445
497
/* backward compatibility with 1.2.x */