1 /*****************************************************************************
2 * RRDtool 1.3.2 Copyright by Tobi Oetiker, 1997-2008
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 #ifdef HAVE_LOCALE_H
49 #include <locale.h>
50 #endif
52 #if !(defined(NETWARE) || defined(WIN32))
53 extern char *tzname[2];
54 #endif
56 //Local prototypes
57 size_t rrd_dump_opt_cb_fileout(
58 const void *data,
59 size_t len,
60 void *user);
62 int rrd_dump_opt_r(
63 const char *filename,
64 char *outname,
65 int opt_noheader);
67 int rrd_dump_cb_r(
68 const char *filename,
69 int opt_header,
70 rrd_output_callback_t cb,
71 void *user)
72 {
73 unsigned int i, ii, ix, iii = 0;
74 time_t now;
75 char somestring[255];
76 rrd_value_t my_cdp;
77 off_t rra_base, rra_start, rra_next;
78 rrd_file_t *rrd_file;
79 rrd_t rrd;
80 rrd_value_t value;
81 struct tm tm;
82 char *old_locale = "";
84 //These two macros are local defines to clean up visible code from its redndancy
85 //and make it easier to read.
86 #define CB_PUTS(str) \
87 cb((str), strlen((str)), user)
88 #define CB_FMTS(...) do { \
89 char buffer[256]; \
90 snprintf (buffer, sizeof(buffer), __VA_ARGS__); \
91 CB_PUTS (buffer); \
92 } while (0)
93 //These macros are to be undefined at the end of this function
95 //Check if we got a (valid) callback method
96 if (!cb) {
97 return (-1);
98 }
100 rrd_init(&rrd);
102 rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_READAHEAD);
103 if (rrd_file == NULL) {
104 rrd_free(&rrd);
105 return (-1);
106 }
108 #ifdef HAVE_SETLOCALE
109 old_locale = setlocale(LC_NUMERIC, "C");
110 #endif
112 if (opt_header == 1) {
113 CB_PUTS("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
114 CB_PUTS("<!DOCTYPE rrd SYSTEM \"http://oss.oetiker.ch/rrdtool/rrdtool.dtd\">\n");
115 CB_PUTS("<!-- Round Robin Database Dump -->\n");
116 CB_PUTS("<rrd>\n");
117 } else if (opt_header == 2) {
118 CB_PUTS("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
119 CB_PUTS("<!-- Round Robin Database Dump -->\n");
120 CB_PUTS("<rrd xmlns=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml\" "
121 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
122 CB_PUTS("\txsi:schemaLocation=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml "
123 "http://oss.oetiker.ch/rrdtool/rrdtool-dump.xsd\">\n");
124 } else {
125 CB_PUTS("<!-- Round Robin Database Dump -->\n");
126 CB_PUTS("<rrd>\n");
127 }
129 if (atoi(rrd.stat_head->version) <= 3) {
130 CB_FMTS("\t<version>%s</version>\n", RRD_VERSION3);
131 } else {
132 CB_FMTS("\t<version>%s</version>\n", RRD_VERSION);
133 }
135 CB_FMTS("\t<step>%lu</step> <!-- Seconds -->\n",
136 rrd.stat_head->pdp_step);
138 #ifdef HAVE_STRFTIME
139 localtime_r(&rrd.live_head->last_up, &tm);
140 strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
141 #else
142 # error "Need strftime"
143 #endif
144 CB_FMTS("\t<lastupdate>%lld</lastupdate> <!-- %s -->\n\n",
145 (long long) rrd.live_head->last_up, somestring);
146 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
147 CB_PUTS("\t<ds>\n");
149 CB_FMTS("\t\t<name> %s </name>\n", rrd.ds_def[i].ds_nam);
151 CB_FMTS("\t\t<type> %s </type>\n", rrd.ds_def[i].dst);
153 if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
154 CB_FMTS("\t\t<minimal_heartbeat>%lu</minimal_heartbeat>\n",
155 rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
157 if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)) {
158 CB_PUTS("\t\t<min>NaN</min>\n");
159 } else {
160 CB_FMTS("\t\t<min>%0.10e</min>\n",
161 rrd.ds_def[i].par[DS_min_val].u_val);
162 }
164 if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)) {
165 CB_PUTS("\t\t<max>NaN</max>\n");
166 } else {
167 CB_FMTS("\t\t<max>%0.10e</max>\n",
168 rrd.ds_def[i].par[DS_max_val].u_val);
169 }
170 } else { /* DST_CDEF */
171 char *str = NULL;
173 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
174 rrd.ds_def, &str);
176 //Splitting into 3 writes to avoid allocating memory
177 //This is better compared to snprintf as str may be of arbitrary size
178 CB_PUTS("\t\t<cdef> ");
179 CB_PUTS(str);
180 CB_PUTS(" </cdef>\n");
182 free(str);
183 }
185 CB_PUTS("\n\t\t<!-- PDP Status -->\n");
186 CB_FMTS("\t\t<last_ds>%s</last_ds>\n",
187 rrd.pdp_prep[i].last_ds);
189 if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)) {
190 CB_PUTS("\t\t<value>NaN</value>\n");
191 } else {
192 CB_FMTS("\t\t<value>%0.10e</value>\n",
193 rrd.pdp_prep[i].scratch[PDP_val].u_val);
194 }
196 CB_FMTS("\t\t<unknown_sec> %lu </unknown_sec>\n",
197 rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
199 CB_PUTS("\t</ds>\n\n");
200 }
202 CB_PUTS("\t<!-- Round Robin Archives -->\n");
204 rra_base = rrd_file->header_len;
205 rra_next = rra_base;
207 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
209 long timer = 0;
211 rra_start = rra_next;
212 rra_next += (rrd.stat_head->ds_cnt
213 * rrd.rra_def[i].row_cnt * sizeof(rrd_value_t));
215 CB_PUTS("\t<rra>\n");
217 CB_FMTS("\t\t<cf>%s</cf>\n", rrd.rra_def[i].cf_nam);
219 CB_FMTS("\t\t<pdp_per_row>%lu</pdp_per_row> <!-- %lu seconds -->\n\n",
220 rrd.rra_def[i].pdp_cnt,
221 rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
223 /* support for RRA parameters */
224 CB_PUTS("\t\t<params>\n");
226 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
227 case CF_HWPREDICT:
228 case CF_MHWPREDICT:
229 CB_FMTS("\t\t<hw_alpha>%0.10e</hw_alpha>\n",
230 rrd.rra_def[i].par[RRA_hw_alpha].u_val);
232 CB_FMTS("\t\t<hw_beta>%0.10e</hw_beta>\n",
233 rrd.rra_def[i].par[RRA_hw_beta].u_val);
235 CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
236 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
237 break;
238 case CF_SEASONAL:
239 case CF_DEVSEASONAL:
240 CB_FMTS("\t\t<seasonal_gamma>%0.10e</seasonal_gamma>\n",
241 rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
243 CB_FMTS("\t\t<seasonal_smooth_idx>%lu</seasonal_smooth_idx>\n",
244 rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
246 if (atoi(rrd.stat_head->version) >= 4) {
247 CB_FMTS("\t\t<smoothing_window>%0.10e</smoothing_window>\n",
248 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val);
249 }
251 CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
252 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
253 break;
254 case CF_FAILURES:
255 CB_FMTS("\t\t<delta_pos>%0.10e</delta_pos>\n",
256 rrd.rra_def[i].par[RRA_delta_pos].u_val);
258 CB_FMTS("\t\t<delta_neg>%0.10e</delta_neg>\n",
259 rrd.rra_def[i].par[RRA_delta_neg].u_val);
261 CB_FMTS("\t\t<window_len>%lu</window_len>\n",
262 rrd.rra_def[i].par[RRA_window_len].u_cnt);
264 CB_FMTS("\t\t<failure_threshold>%lu</failure_threshold>\n",
265 rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
267 /* fall thru */
268 case CF_DEVPREDICT:
269 CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
270 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
271 break;
272 case CF_AVERAGE:
273 case CF_MAXIMUM:
274 case CF_MINIMUM:
275 case CF_LAST:
276 default:
277 CB_FMTS("\t\t<xff>%0.10e</xff>\n",
278 rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
279 break;
280 }
282 CB_PUTS("\t\t</params>\n");
283 CB_PUTS("\t\t<cdp_prep>\n");
285 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
286 unsigned long ivalue;
288 CB_PUTS("\t\t\t<ds>\n");
289 /* support for exporting all CDP parameters */
290 /* parameters common to all CFs */
291 /* primary_val and secondary_val do not need to be saved between updates
292 * so strictly speaking they could be omitted.
293 * However, they can be useful for diagnostic purposes, so are included here. */
294 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
295 scratch[CDP_primary_val].u_val;
296 if (isnan(value)) {
297 CB_PUTS("\t\t\t<primary_value>NaN</primary_value>\n");
298 } else {
299 CB_FMTS("\t\t\t<primary_value>%0.10e</primary_value>\n", value);
300 }
302 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
303 scratch[CDP_secondary_val].u_val;
304 if (isnan(value)) {
305 CB_PUTS("\t\t\t<secondary_value>NaN</secondary_value>\n");
306 } else {
307 CB_FMTS("\t\t\t<secondary_value>%0.10e</secondary_value>\n", value);
308 }
310 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
311 case CF_HWPREDICT:
312 case CF_MHWPREDICT:
313 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
314 scratch[CDP_hw_intercept].u_val;
315 if (isnan(value)) {
316 CB_PUTS("\t\t\t<intercept>NaN</intercept>\n");
317 } else {
318 CB_FMTS("\t\t\t<intercept>%0.10e</intercept>\n", value);
319 }
321 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
322 scratch[CDP_hw_last_intercept].u_val;
323 if (isnan(value)) {
324 CB_PUTS("\t\t\t<last_intercept>NaN</last_intercept>\n");
325 } else {
326 CB_FMTS("\t\t\t<last_intercept>%0.10e</last_intercept>\n", value);
327 }
329 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
330 scratch[CDP_hw_slope].u_val;
331 if (isnan(value)) {
332 CB_PUTS("\t\t\t<slope>NaN</slope>\n");
333 } else {
334 CB_FMTS("\t\t\t<slope>%0.10e</slope>\n", value);
335 }
337 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
338 scratch[CDP_hw_last_slope].u_val;
339 if (isnan(value)) {
340 CB_PUTS("\t\t\t<last_slope>NaN</last_slope>\n");
341 } else {
342 CB_FMTS("\t\t\t<last_slope>%0.10e</last_slope>\n", value);
343 }
345 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
346 scratch[CDP_null_count].u_cnt;
347 CB_FMTS("\t\t\t<nan_count>%lu</nan_count>\n", ivalue);
349 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
350 scratch[CDP_last_null_count].u_cnt;
351 CB_FMTS("\t\t\t<last_nan_count>%lu</last_nan_count>\n", ivalue);
352 break;
353 case CF_SEASONAL:
354 case CF_DEVSEASONAL:
355 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
356 scratch[CDP_hw_seasonal].u_val;
357 if (isnan(value)) {
358 CB_PUTS("\t\t\t<seasonal>NaN</seasonal>\n");
359 } else {
360 CB_FMTS("\t\t\t<seasonal>%0.10e</seasonal>\n", value);
361 }
363 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
364 scratch[CDP_hw_last_seasonal].u_val;
365 if (isnan(value)) {
366 CB_PUTS("\t\t\t<last_seasonal>NaN</last_seasonal>\n");
367 } else {
368 CB_FMTS("\t\t\t<last_seasonal>%0.10e</last_seasonal>\n", value);
369 }
371 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
372 scratch[CDP_init_seasonal].u_cnt;
373 CB_FMTS("\t\t\t<init_flag>%lu</init_flag>\n", ivalue);
374 break;
375 case CF_DEVPREDICT:
376 break;
377 case CF_FAILURES:
378 {
379 unsigned short vidx;
380 char *violations_array = (char *) ((void *)
381 rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch);
382 CB_PUTS("\t\t\t<history>");
383 for (vidx = 0;
384 vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
385 ++vidx) {
386 CB_FMTS("%d", violations_array[vidx]);
387 }
388 CB_PUTS("</history>\n");
389 }
390 break;
391 case CF_AVERAGE:
392 case CF_MAXIMUM:
393 case CF_MINIMUM:
394 case CF_LAST:
395 default:
396 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch[CDP_val].u_val;
397 if (isnan(value)) {
398 CB_PUTS("\t\t\t<value>NaN</value>\n");
399 } else {
400 CB_FMTS("\t\t\t<value>%0.10e</value>\n", value);
401 }
403 CB_FMTS("\t\t\t<unknown_datapoints>%lu</unknown_datapoints>\n",
404 rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
405 scratch[CDP_unkn_pdp_cnt].u_cnt);
406 break;
407 }
408 CB_PUTS("\t\t\t</ds>\n");
409 }
410 CB_PUTS("\t\t</cdp_prep>\n");
412 CB_PUTS("\t\t<database>\n");
413 rrd_seek(rrd_file, (rra_start + (rrd.rra_ptr[i].cur_row + 1)
414 * rrd.stat_head->ds_cnt
415 * sizeof(rrd_value_t)), SEEK_SET);
416 timer = -(long)(rrd.rra_def[i].row_cnt - 1);
417 ii = rrd.rra_ptr[i].cur_row;
418 for (ix = 0; ix < rrd.rra_def[i].row_cnt; ix++) {
419 ii++;
420 if (ii >= rrd.rra_def[i].row_cnt) {
421 rrd_seek(rrd_file, rra_start, SEEK_SET);
422 ii = 0; /* wrap if max row cnt is reached */
423 }
424 now = (rrd.live_head->last_up
425 - rrd.live_head->last_up
426 % (rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step))
427 + (timer * rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
429 timer++;
430 #if HAVE_STRFTIME
431 localtime_r(&now, &tm);
432 strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
433 #else
434 # error "Need strftime"
435 #endif
436 CB_FMTS("\t\t\t<!-- %s / %lld --> <row>", somestring, (long long) now);
437 for (iii = 0; iii < rrd.stat_head->ds_cnt; iii++) {
438 rrd_read(rrd_file, &my_cdp, sizeof(rrd_value_t) * 1);
439 if (isnan(my_cdp)) {
440 CB_PUTS("<v>NaN</v>");
441 } else {
442 CB_FMTS("<v>%0.10e</v>", my_cdp);
443 }
444 }
445 CB_PUTS("</row>\n");
446 }
447 CB_PUTS("\t\t</database>\n\t</rra>\n");
448 }
450 CB_PUTS("</rrd>\n");
452 rrd_free(&rrd);
454 #ifdef HAVE_SETLOCALE
455 setlocale(LC_NUMERIC, old_locale);
456 #endif
458 return rrd_close(rrd_file);
460 //Undefining the previously defined shortcuts
461 //See start of this function
462 #undef CB_PUTS
463 #undef CB_FMTS
464 //End of macro undefining
466 }
468 size_t rrd_dump_opt_cb_fileout(
469 const void *data,
470 size_t len,
471 void *user)
472 {
473 return fwrite(data, 1, len, (FILE *)user);
474 }
476 int rrd_dump_opt_r(
477 const char *filename,
478 char *outname,
479 int opt_noheader)
480 {
481 FILE *out_file;
482 int res;
484 out_file = NULL;
485 if (outname) {
486 if (!(out_file = fopen(outname, "w"))) {
487 return (-1);
488 }
489 } else {
490 out_file = stdout;
491 }
493 res = rrd_dump_cb_r(filename, opt_noheader, rrd_dump_opt_cb_fileout, (void *)out_file);
495 if (out_file != stdout) {
496 fclose(out_file);
497 }
499 return res;
500 }
502 /* backward compatibility with 1.2.x */
503 int rrd_dump_r(
504 const char *filename,
505 char *outname)
506 {
507 return rrd_dump_opt_r(filename, outname, 0);
508 }
510 int rrd_dump(
511 int argc,
512 char **argv)
513 {
514 int rc;
515 /**
516 * 0 = no header
517 * 1 = dtd header
518 * 2 = xsd header
519 */
520 int opt_header = 1;
521 char *opt_daemon = NULL;
523 /* init rrd clean */
525 optind = 0;
526 opterr = 0; /* initialize getopt */
528 while (42) {/* ha ha */
529 int opt;
530 int option_index = 0;
531 static struct option long_options[] = {
532 {"daemon", required_argument, 0, 'd'},
533 {"header", required_argument, 0, 'h'},
534 {"no-header", no_argument, 0, 'n'},
535 {0, 0, 0, 0}
536 };
538 opt = getopt_long(argc, argv, "d:h:n", long_options, &option_index);
540 if (opt == EOF)
541 break;
543 switch (opt) {
544 case 'd':
545 if (opt_daemon != NULL)
546 free (opt_daemon);
547 opt_daemon = strdup (optarg);
548 if (opt_daemon == NULL)
549 {
550 rrd_set_error ("strdup failed.");
551 return (-1);
552 }
553 break;
555 case 'n':
556 opt_header = 0;
557 break;
559 case 'h':
560 if (strcmp(optarg, "dtd") == 0) {
561 opt_header = 1;
562 } else if (strcmp(optarg, "xsd") == 0) {
563 opt_header = 2;
564 } else if (strcmp(optarg, "none") == 0) {
565 opt_header = 0;
566 }
567 break;
569 default:
570 rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}] [--no-header]"
571 "file.rrd [file.xml]", argv[0]);
572 return (-1);
573 break;
574 }
575 } /* while (42) */
577 if ((argc - optind) < 1 || (argc - optind) > 2) {
578 rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}] [--no-header]"
579 "file.rrd [file.xml]", argv[0]);
580 return (-1);
581 }
583 rc = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
584 if (opt_daemon) free(opt_daemon);
585 if (rc) return (rc);
587 if ((argc - optind) == 2) {
588 rc = rrd_dump_opt_r(argv[optind], argv[optind + 1], opt_header);
589 } else {
590 rc = rrd_dump_opt_r(argv[optind], NULL, opt_header);
591 }
593 return rc;
594 }