1 /*****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
3 *****************************************************************************
4 * rrd_dump Display a RRD
5 *****************************************************************************
6 * $Id$
7 * $Log$
8 * Revision 1.7 2004/05/25 20:53:21 oetiker
9 * prevent small leak when resources are exhausted -- Mike Slifcak
10 *
11 * Revision 1.6 2004/05/25 20:51:49 oetiker
12 * Update displayed copyright messages to be consistent. -- Mike Slifcak
13 *
14 * Revision 1.5 2003/02/13 07:05:27 oetiker
15 * Find attached the patch I promised to send to you. Please note that there
16 * are three new source files (src/rrd_is_thread_safe.h, src/rrd_thread_safe.c
17 * and src/rrd_not_thread_safe.c) and the introduction of librrd_th. This
18 * library is identical to librrd, but it contains support code for per-thread
19 * global variables currently used for error information only. This is similar
20 * to how errno per-thread variables are implemented. librrd_th must be linked
21 * alongside of libpthred
22 *
23 * There is also a new file "THREADS", holding some documentation.
24 *
25 * -- Peter Stamfest <peter@stamfest.at>
26 *
27 * Revision 1.4 2002/02/01 20:34:49 oetiker
28 * fixed version number and date/time
29 *
30 * Revision 1.3 2001/03/10 23:54:39 oetiker
31 * Support for COMPUTE data sources (CDEF data sources). Removes the RPN
32 * parser and calculator from rrd_graph and puts then in a new file,
33 * rrd_rpncalc.c. Changes to core files rrd_create and rrd_update. Some
34 * clean-up of aberrant behavior stuff, including a bug fix.
35 * Documentation update (rrdcreate.pod, rrdupdate.pod). Change xml format.
36 * -- Jake Brutlag <jakeb@corp.webtv.net>
37 *
38 * Revision 1.2 2001/03/04 13:01:55 oetiker
39 *
40 * Revision 1.1.1.1 2001/02/25 22:25:05 oetiker
41 * checkin
42 *
43 *****************************************************************************/
44 #include "rrd_tool.h"
45 #include "rrd_rpncalc.h"
46 #include "rrd_client.h"
48 #include <locale.h>
50 #if !(defined(NETWARE) || defined(WIN32))
51 extern char *tzname[2];
52 #endif
54 //Local prototypes
55 size_t rrd_dump_opt_cb_fileout(
56 const void *data,
57 size_t len,
58 void *user);
60 int rrd_dump_opt_r(
61 const char *filename,
62 char *outname,
63 int opt_noheader);
65 int rrd_dump_cb_r(
66 const char *filename,
67 int opt_header,
68 rrd_output_callback_t cb,
69 void *user)
70 {
71 unsigned int i, ii, ix, iii = 0;
72 time_t now;
73 char somestring[255];
74 rrd_value_t my_cdp;
75 off_t rra_base, rra_start, rra_next;
76 rrd_file_t *rrd_file;
77 rrd_t rrd;
78 rrd_value_t value;
79 struct tm tm;
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 { \
87 char buffer[256]; \
88 snprintf (buffer, sizeof(buffer), __VA_ARGS__); \
89 CB_PUTS (buffer); \
90 } while (0)
91 //These macros are to be undefined at the end of this function
93 //Check if we got a (valid) callback method
94 if (!cb) {
95 return (-1);
96 }
98 rrd_init(&rrd);
100 rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_READAHEAD);
101 if (rrd_file == NULL) {
102 rrd_free(&rrd);
103 return (-1);
104 }
106 old_locale = setlocale(LC_NUMERIC, NULL);
107 setlocale(LC_NUMERIC, "C");
110 if (opt_header == 1) {
111 CB_PUTS("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
112 CB_PUTS("<!DOCTYPE rrd SYSTEM \"http://oss.oetiker.ch/rrdtool/rrdtool.dtd\">\n");
113 CB_PUTS("<!-- Round Robin Database Dump -->\n");
114 CB_PUTS("<rrd>\n");
115 } else if (opt_header == 2) {
116 CB_PUTS("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
117 CB_PUTS("<!-- Round Robin Database Dump -->\n");
118 CB_PUTS("<rrd xmlns=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml\" "
119 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
120 CB_PUTS("\txsi:schemaLocation=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml "
121 "http://oss.oetiker.ch/rrdtool/rrdtool-dump.xsd\">\n");
122 } else {
123 CB_PUTS("<!-- Round Robin Database Dump -->\n");
124 CB_PUTS("<rrd>\n");
125 }
127 if (atoi(rrd.stat_head->version) <= 3) {
128 CB_FMTS("\t<version>%s</version>\n", RRD_VERSION3);
129 } else {
130 CB_FMTS("\t<version>%s</version>\n", RRD_VERSION);
131 }
133 CB_FMTS("\t<step>%lu</step> <!-- Seconds -->\n",
134 rrd.stat_head->pdp_step);
136 #ifdef HAVE_STRFTIME
137 localtime_r(&rrd.live_head->last_up, &tm);
138 strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
139 #else
140 # error "Need strftime"
141 #endif
142 CB_FMTS("\t<lastupdate>%lld</lastupdate> <!-- %s -->\n\n",
143 (long long int) rrd.live_head->last_up, somestring);
144 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
145 CB_PUTS("\t<ds>\n");
147 CB_FMTS("\t\t<name> %s </name>\n", rrd.ds_def[i].ds_nam);
149 CB_FMTS("\t\t<type> %s </type>\n", rrd.ds_def[i].dst);
151 if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
152 CB_FMTS("\t\t<minimal_heartbeat>%lu</minimal_heartbeat>\n",
153 rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
155 if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)) {
156 CB_PUTS("\t\t<min>NaN</min>\n");
157 } else {
158 CB_FMTS("\t\t<min>%0.10e</min>\n",
159 rrd.ds_def[i].par[DS_min_val].u_val);
160 }
162 if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)) {
163 CB_PUTS("\t\t<max>NaN</max>\n");
164 } else {
165 CB_FMTS("\t\t<max>%0.10e</max>\n",
166 rrd.ds_def[i].par[DS_max_val].u_val);
167 }
168 } else { /* DST_CDEF */
169 char *str = NULL;
171 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
172 rrd.ds_def, &str);
174 //Splitting into 3 writes to avoid allocating memory
175 //This is better compared to snprintf as str may be of arbitrary size
176 CB_PUTS("\t\t<cdef> ");
177 CB_PUTS(str);
178 CB_PUTS(" </cdef>\n");
180 free(str);
181 }
183 CB_PUTS("\n\t\t<!-- PDP Status -->\n");
184 CB_FMTS("\t\t<last_ds>%s</last_ds>\n",
185 rrd.pdp_prep[i].last_ds);
187 if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)) {
188 CB_PUTS("\t\t<value>NaN</value>\n");
189 } else {
190 CB_FMTS("\t\t<value>%0.10e</value>\n",
191 rrd.pdp_prep[i].scratch[PDP_val].u_val);
192 }
194 CB_FMTS("\t\t<unknown_sec> %lu </unknown_sec>\n",
195 rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
197 CB_PUTS("\t</ds>\n\n");
198 }
200 CB_PUTS("\t<!-- Round Robin Archives -->\n");
202 rra_base = rrd_file->header_len;
203 rra_next = rra_base;
205 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
207 long timer = 0;
209 rra_start = rra_next;
210 rra_next += (rrd.stat_head->ds_cnt
211 * rrd.rra_def[i].row_cnt * sizeof(rrd_value_t));
213 CB_PUTS("\t<rra>\n");
215 CB_FMTS("\t\t<cf>%s</cf>\n", rrd.rra_def[i].cf_nam);
217 CB_FMTS("\t\t<pdp_per_row>%lu</pdp_per_row> <!-- %lu seconds -->\n\n",
218 rrd.rra_def[i].pdp_cnt,
219 rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
221 /* support for RRA parameters */
222 CB_PUTS("\t\t<params>\n");
224 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
225 case CF_HWPREDICT:
226 case CF_MHWPREDICT:
227 CB_FMTS("\t\t<hw_alpha>%0.10e</hw_alpha>\n",
228 rrd.rra_def[i].par[RRA_hw_alpha].u_val);
230 CB_FMTS("\t\t<hw_beta>%0.10e</hw_beta>\n",
231 rrd.rra_def[i].par[RRA_hw_beta].u_val);
233 CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
234 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
235 break;
236 case CF_SEASONAL:
237 case CF_DEVSEASONAL:
238 CB_FMTS("\t\t<seasonal_gamma>%0.10e</seasonal_gamma>\n",
239 rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
241 CB_FMTS("\t\t<seasonal_smooth_idx>%lu</seasonal_smooth_idx>\n",
242 rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
244 if (atoi(rrd.stat_head->version) >= 4) {
245 CB_FMTS("\t\t<smoothing_window>%0.10e</smoothing_window>\n",
246 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val);
247 }
249 CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
250 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
251 break;
252 case CF_FAILURES:
253 CB_FMTS("\t\t<delta_pos>%0.10e</delta_pos>\n",
254 rrd.rra_def[i].par[RRA_delta_pos].u_val);
256 CB_FMTS("\t\t<delta_neg>%0.10e</delta_neg>\n",
257 rrd.rra_def[i].par[RRA_delta_neg].u_val);
259 CB_FMTS("\t\t<window_len>%lu</window_len>\n",
260 rrd.rra_def[i].par[RRA_window_len].u_cnt);
262 CB_FMTS("\t\t<failure_threshold>%lu</failure_threshold>\n",
263 rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
265 /* fall thru */
266 case CF_DEVPREDICT:
267 CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
268 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
269 break;
270 case CF_AVERAGE:
271 case CF_MAXIMUM:
272 case CF_MINIMUM:
273 case CF_LAST:
274 default:
275 CB_FMTS("\t\t<xff>%0.10e</xff>\n",
276 rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
277 break;
278 }
280 CB_PUTS("\t\t</params>\n");
281 CB_PUTS("\t\t<cdp_prep>\n");
283 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
284 unsigned long ivalue;
286 CB_PUTS("\t\t\t<ds>\n");
287 /* support for exporting all CDP parameters */
288 /* parameters common to all CFs */
289 /* primary_val and secondary_val do not need to be saved between updates
290 * so strictly speaking they could be omitted.
291 * However, they can be useful for diagnostic purposes, so are included here. */
292 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
293 scratch[CDP_primary_val].u_val;
294 if (isnan(value)) {
295 CB_PUTS("\t\t\t<primary_value>NaN</primary_value>\n");
296 } else {
297 CB_FMTS("\t\t\t<primary_value>%0.10e</primary_value>\n", value);
298 }
300 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
301 scratch[CDP_secondary_val].u_val;
302 if (isnan(value)) {
303 CB_PUTS("\t\t\t<secondary_value>NaN</secondary_value>\n");
304 } else {
305 CB_FMTS("\t\t\t<secondary_value>%0.10e</secondary_value>\n", value);
306 }
308 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
309 case CF_HWPREDICT:
310 case CF_MHWPREDICT:
311 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
312 scratch[CDP_hw_intercept].u_val;
313 if (isnan(value)) {
314 CB_PUTS("\t\t\t<intercept>NaN</intercept>\n");
315 } else {
316 CB_FMTS("\t\t\t<intercept>%0.10e</intercept>\n", value);
317 }
319 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
320 scratch[CDP_hw_last_intercept].u_val;
321 if (isnan(value)) {
322 CB_PUTS("\t\t\t<last_intercept>NaN</last_intercept>\n");
323 } else {
324 CB_FMTS("\t\t\t<last_intercept>%0.10e</last_intercept>\n", value);
325 }
327 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
328 scratch[CDP_hw_slope].u_val;
329 if (isnan(value)) {
330 CB_PUTS("\t\t\t<slope>NaN</slope>\n");
331 } else {
332 CB_FMTS("\t\t\t<slope>%0.10e</slope>\n", value);
333 }
335 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
336 scratch[CDP_hw_last_slope].u_val;
337 if (isnan(value)) {
338 CB_PUTS("\t\t\t<last_slope>NaN</last_slope>\n");
339 } else {
340 CB_FMTS("\t\t\t<last_slope>%0.10e</last_slope>\n", value);
341 }
343 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
344 scratch[CDP_null_count].u_cnt;
345 CB_FMTS("\t\t\t<nan_count>%lu</nan_count>\n", ivalue);
347 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
348 scratch[CDP_last_null_count].u_cnt;
349 CB_FMTS("\t\t\t<last_nan_count>%lu</last_nan_count>\n", ivalue);
350 break;
351 case CF_SEASONAL:
352 case CF_DEVSEASONAL:
353 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
354 scratch[CDP_hw_seasonal].u_val;
355 if (isnan(value)) {
356 CB_PUTS("\t\t\t<seasonal>NaN</seasonal>\n");
357 } else {
358 CB_FMTS("\t\t\t<seasonal>%0.10e</seasonal>\n", value);
359 }
361 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
362 scratch[CDP_hw_last_seasonal].u_val;
363 if (isnan(value)) {
364 CB_PUTS("\t\t\t<last_seasonal>NaN</last_seasonal>\n");
365 } else {
366 CB_FMTS("\t\t\t<last_seasonal>%0.10e</last_seasonal>\n", value);
367 }
369 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
370 scratch[CDP_init_seasonal].u_cnt;
371 CB_FMTS("\t\t\t<init_flag>%lu</init_flag>\n", ivalue);
372 break;
373 case CF_DEVPREDICT:
374 break;
375 case CF_FAILURES:
376 {
377 unsigned short vidx;
378 char *violations_array = (char *) ((void *)
379 rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch);
380 CB_PUTS("\t\t\t<history>");
381 for (vidx = 0;
382 vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
383 ++vidx) {
384 CB_FMTS("%d", violations_array[vidx]);
385 }
386 CB_PUTS("</history>\n");
387 }
388 break;
389 case CF_AVERAGE:
390 case CF_MAXIMUM:
391 case CF_MINIMUM:
392 case CF_LAST:
393 default:
394 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch[CDP_val].u_val;
395 if (isnan(value)) {
396 CB_PUTS("\t\t\t<value>NaN</value>\n");
397 } else {
398 CB_FMTS("\t\t\t<value>%0.10e</value>\n", value);
399 }
401 CB_FMTS("\t\t\t<unknown_datapoints>%lu</unknown_datapoints>\n",
402 rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
403 scratch[CDP_unkn_pdp_cnt].u_cnt);
404 break;
405 }
406 CB_PUTS("\t\t\t</ds>\n");
407 }
408 CB_PUTS("\t\t</cdp_prep>\n");
410 CB_PUTS("\t\t<database>\n");
411 rrd_seek(rrd_file, (rra_start + (rrd.rra_ptr[i].cur_row + 1)
412 * rrd.stat_head->ds_cnt
413 * sizeof(rrd_value_t)), SEEK_SET);
414 timer = -(long)(rrd.rra_def[i].row_cnt - 1);
415 ii = rrd.rra_ptr[i].cur_row;
416 for (ix = 0; ix < rrd.rra_def[i].row_cnt; ix++) {
417 ii++;
418 if (ii >= rrd.rra_def[i].row_cnt) {
419 rrd_seek(rrd_file, rra_start, SEEK_SET);
420 ii = 0; /* wrap if max row cnt is reached */
421 }
422 now = (rrd.live_head->last_up
423 - rrd.live_head->last_up
424 % (rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step))
425 + (timer * rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
427 timer++;
428 #if HAVE_STRFTIME
429 localtime_r(&now, &tm);
430 strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
431 #else
432 # error "Need strftime"
433 #endif
434 CB_FMTS("\t\t\t<!-- %s / %lld --> <row>", somestring, (long long int) now);
435 for (iii = 0; iii < rrd.stat_head->ds_cnt; iii++) {
436 rrd_read(rrd_file, &my_cdp, sizeof(rrd_value_t) * 1);
437 if (isnan(my_cdp)) {
438 CB_PUTS("<v>NaN</v>");
439 } else {
440 CB_FMTS("<v>%0.10e</v>", my_cdp);
441 }
442 }
443 CB_PUTS("</row>\n");
444 }
445 CB_PUTS("\t\t</database>\n\t</rra>\n");
446 }
448 CB_PUTS("</rrd>\n");
450 rrd_free(&rrd);
452 setlocale(LC_NUMERIC, old_locale);
454 return rrd_close(rrd_file);
456 //Undefining the previously defined shortcuts
457 //See start of this function
458 #undef CB_PUTS
459 #undef CB_FMTS
460 //End of macro undefining
462 }
464 size_t rrd_dump_opt_cb_fileout(
465 const void *data,
466 size_t len,
467 void *user)
468 {
469 return fwrite(data, 1, len, (FILE *)user);
470 }
472 int rrd_dump_opt_r(
473 const char *filename,
474 char *outname,
475 int opt_noheader)
476 {
477 FILE *out_file;
478 int res;
480 out_file = NULL;
481 if (outname) {
482 if (!(out_file = fopen(outname, "w"))) {
483 return (-1);
484 }
485 } else {
486 out_file = stdout;
487 }
489 res = rrd_dump_cb_r(filename, opt_noheader, rrd_dump_opt_cb_fileout, (void *)out_file);
491 if (out_file != stdout) {
492 fclose(out_file);
493 }
495 return res;
496 }
498 /* backward compatibility with 1.2.x */
499 int rrd_dump_r(
500 const char *filename,
501 char *outname)
502 {
503 return rrd_dump_opt_r(filename, outname, 0);
504 }
506 int rrd_dump(
507 int argc,
508 char **argv)
509 {
510 int rc;
511 /**
512 * 0 = no header
513 * 1 = dtd header
514 * 2 = xsd header
515 */
516 int opt_header = 1;
517 char *opt_daemon = NULL;
519 /* init rrd clean */
521 optind = 0;
522 opterr = 0; /* initialize getopt */
524 while (42) {/* ha ha */
525 int opt;
526 int option_index = 0;
527 static struct option long_options[] = {
528 {"daemon", required_argument, 0, 'd'},
529 {"header", required_argument, 0, 'h'},
530 {"no-header", no_argument, 0, 'n'},
531 {0, 0, 0, 0}
532 };
534 opt = getopt_long(argc, argv, "d:h:n", long_options, &option_index);
536 if (opt == EOF)
537 break;
539 switch (opt) {
540 case 'd':
541 if (opt_daemon != NULL)
542 free (opt_daemon);
543 opt_daemon = strdup (optarg);
544 if (opt_daemon == NULL)
545 {
546 rrd_set_error ("strdup failed.");
547 return (-1);
548 }
549 break;
551 case 'n':
552 opt_header = 0;
553 break;
555 case 'h':
556 if (strcmp(optarg, "dtd") == 0) {
557 opt_header = 1;
558 } else if (strcmp(optarg, "xsd") == 0) {
559 opt_header = 2;
560 } else if (strcmp(optarg, "none") == 0) {
561 opt_header = 0;
562 }
563 break;
565 default:
566 rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}] [--no-header]"
567 "file.rrd [file.xml]", argv[0]);
568 return (-1);
569 break;
570 }
571 } /* while (42) */
573 if ((argc - optind) < 1 || (argc - optind) > 2) {
574 rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}] [--no-header]"
575 "file.rrd [file.xml]", argv[0]);
576 return (-1);
577 }
579 rc = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
580 if (opt_daemon) free(opt_daemon);
581 if (rc) return (rc);
583 if ((argc - optind) == 2) {
584 rc = rrd_dump_opt_r(argv[optind], argv[optind + 1], opt_header);
585 } else {
586 rc = rrd_dump_opt_r(argv[optind], NULL, opt_header);
587 }
589 return rc;
590 }