1 /*****************************************************************************
2 * RRDtool 1.3.2 Copyright by Tobi Oetiker, 1997-2008
3 *****************************************************************************
4 * rrd_dump Display a RRD
5 *****************************************************************************
8 * Revision 1.7 2004/05/25 20:53:21 oetiker
9 * prevent small leak when resources are exhausted -- Mike Slifcak
11 * Revision 1.6 2004/05/25 20:51:49 oetiker
12 * Update displayed copyright messages to be consistent. -- Mike Slifcak
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
23 * There is also a new file "THREADS", holding some documentation.
25 * -- Peter Stamfest <peter@stamfest.at>
27 * Revision 1.4 2002/02/01 20:34:49 oetiker
28 * fixed version number and date/time
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>
38 * Revision 1.2 2001/03/04 13:01:55 oetiker
40 * Revision 1.1.1.1 2001/02/25 22:25:05 oetiker
43 *****************************************************************************/
45 #include "rrd_rpncalc.h"
46 #include "rrd_client.h"
52 #if !(defined(NETWARE) || defined(WIN32))
53 extern char *tzname[2];
57 size_t rrd_dump_opt_cb_fileout(
70 rrd_output_callback_t cb,
73 unsigned int i, ii, ix, iii = 0;
77 off_t rra_base, rra_start, rra_next;
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 { \
90 snprintf (buffer, sizeof(buffer), __VA_ARGS__); \
93 //These macros are to be undefined at the end of this function
95 //Check if we got a (valid) callback method
102 rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_READAHEAD);
103 if (rrd_file == NULL) {
108 #ifdef HAVE_SETLOCALE
109 old_locale = setlocale(LC_NUMERIC, "C");
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");
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");
125 CB_PUTS("<!-- Round Robin Database Dump -->\n");
129 if (atoi(rrd.stat_head->version) <= 3) {
130 CB_FMTS("\t<version>%s</version>\n", RRD_VERSION3);
132 CB_FMTS("\t<version>%s</version>\n", RRD_VERSION);
135 CB_FMTS("\t<step>%lu</step> <!-- Seconds -->\n",
136 rrd.stat_head->pdp_step);
139 localtime_r(&rrd.live_head->last_up, &tm);
140 strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
142 # error "Need strftime"
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++) {
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");
160 CB_FMTS("\t\t<min>%0.10e</min>\n",
161 rrd.ds_def[i].par[DS_min_val].u_val);
164 if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)) {
165 CB_PUTS("\t\t<max>NaN</max>\n");
167 CB_FMTS("\t\t<max>%0.10e</max>\n",
168 rrd.ds_def[i].par[DS_max_val].u_val);
170 } else { /* DST_CDEF */
173 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
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> ");
180 CB_PUTS(" </cdef>\n");
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");
192 CB_FMTS("\t\t<value>%0.10e</value>\n",
193 rrd.pdp_prep[i].scratch[PDP_val].u_val);
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");
202 CB_PUTS("\t<!-- Round Robin Archives -->\n");
204 rra_base = rrd_file->header_len;
207 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
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)) {
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);
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);
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);
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);
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);
277 CB_FMTS("\t\t<xff>%0.10e</xff>\n",
278 rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
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;
297 CB_PUTS("\t\t\t<primary_value>NaN</primary_value>\n");
299 CB_FMTS("\t\t\t<primary_value>%0.10e</primary_value>\n", value);
302 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
303 scratch[CDP_secondary_val].u_val;
305 CB_PUTS("\t\t\t<secondary_value>NaN</secondary_value>\n");
307 CB_FMTS("\t\t\t<secondary_value>%0.10e</secondary_value>\n", value);
310 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
313 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
314 scratch[CDP_hw_intercept].u_val;
316 CB_PUTS("\t\t\t<intercept>NaN</intercept>\n");
318 CB_FMTS("\t\t\t<intercept>%0.10e</intercept>\n", value);
321 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
322 scratch[CDP_hw_last_intercept].u_val;
324 CB_PUTS("\t\t\t<last_intercept>NaN</last_intercept>\n");
326 CB_FMTS("\t\t\t<last_intercept>%0.10e</last_intercept>\n", value);
329 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
330 scratch[CDP_hw_slope].u_val;
332 CB_PUTS("\t\t\t<slope>NaN</slope>\n");
334 CB_FMTS("\t\t\t<slope>%0.10e</slope>\n", value);
337 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
338 scratch[CDP_hw_last_slope].u_val;
340 CB_PUTS("\t\t\t<last_slope>NaN</last_slope>\n");
342 CB_FMTS("\t\t\t<last_slope>%0.10e</last_slope>\n", value);
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);
355 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
356 scratch[CDP_hw_seasonal].u_val;
358 CB_PUTS("\t\t\t<seasonal>NaN</seasonal>\n");
360 CB_FMTS("\t\t\t<seasonal>%0.10e</seasonal>\n", value);
363 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
364 scratch[CDP_hw_last_seasonal].u_val;
366 CB_PUTS("\t\t\t<last_seasonal>NaN</last_seasonal>\n");
368 CB_FMTS("\t\t\t<last_seasonal>%0.10e</last_seasonal>\n", value);
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);
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>");
384 vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
386 CB_FMTS("%d", violations_array[vidx]);
388 CB_PUTS("</history>\n");
396 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch[CDP_val].u_val;
398 CB_PUTS("\t\t\t<value>NaN</value>\n");
400 CB_FMTS("\t\t\t<value>%0.10e</value>\n", value);
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);
408 CB_PUTS("\t\t\t</ds>\n");
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++) {
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 */
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);
431 localtime_r(&now, &tm);
432 strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
434 # error "Need strftime"
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);
440 CB_PUTS("<v>NaN</v>");
442 CB_FMTS("<v>%0.10e</v>", my_cdp);
447 CB_PUTS("\t\t</database>\n\t</rra>\n");
454 #ifdef HAVE_SETLOCALE
455 setlocale(LC_NUMERIC, old_locale);
458 return rrd_close(rrd_file);
460 //Undefining the previously defined shortcuts
461 //See start of this function
464 //End of macro undefining
468 size_t rrd_dump_opt_cb_fileout(
473 return fwrite(data, 1, len, (FILE *)user);
477 const char *filename,
486 if (!(out_file = fopen(outname, "w"))) {
493 res = rrd_dump_cb_r(filename, opt_noheader, rrd_dump_opt_cb_fileout, (void *)out_file);
495 if (out_file != stdout) {
502 /* backward compatibility with 1.2.x */
504 const char *filename,
507 return rrd_dump_opt_r(filename, outname, 0);
521 char *opt_daemon = NULL;
526 opterr = 0; /* initialize getopt */
528 while (42) {/* ha ha */
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'},
538 opt = getopt_long(argc, argv, "d:h:n", long_options, &option_index);
545 if (opt_daemon != NULL)
547 opt_daemon = strdup (optarg);
548 if (opt_daemon == NULL)
550 rrd_set_error ("strdup failed.");
560 if (strcmp(optarg, "dtd") == 0) {
562 } else if (strcmp(optarg, "xsd") == 0) {
564 } else if (strcmp(optarg, "none") == 0) {
570 rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}] [--no-header]"
571 "file.rrd [file.xml]", argv[0]);
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]);
583 rc = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
584 if (opt_daemon) free(opt_daemon);
587 if ((argc - optind) == 2) {
588 rc = rrd_dump_opt_r(argv[optind], argv[optind + 1], opt_header);
590 rc = rrd_dump_opt_r(argv[optind], NULL, opt_header);