1 /*****************************************************************************
2 * RRDtool 1.2.99907080300 Copyright by Tobi Oetiker, 1997-2007
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"
47 #if !(defined(NETWARE) || defined(WIN32))
48 extern char *tzname[2];
58 rrd_set_error("Not enough arguments");
63 rc = rrd_dump_r(argv[1], argv[2]);
65 rc = rrd_dump_r(argv[1], NULL);
75 unsigned int i, ii, ix, iii = 0;
79 off_t rra_base, rra_start, rra_next;
86 rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_READAHEAD);
87 if (rrd_file == NULL) {
94 if (!(out_file = fopen(outname, "w"))) {
101 fputs("<?xml version="1.0" encoding=\"utf-8\"?>", out_file);
102 fputs("<!DOCTYPE rrd SYSTEM \"http://oss.oetiker.ch/rrdtool/rrdtool.dtd\">", out_file);
103 fputs("<!-- Round Robin Database Dump -->", out_file);
104 fputs("<rrd>", out_file);
105 if (atoi(rrd.stat_head->version) <= 3) {
106 fprintf(out_file, "\t<version> %s </version>\n", RRD_VERSION3);
108 fprintf(out_file, "\t<version> %s </version>\n", RRD_VERSION);
110 fprintf(out_file, "\t<step> %lu </step> <!-- Seconds -->\n",
111 rrd.stat_head->pdp_step);
113 localtime_r(&rrd.live_head->last_up, &tm);
114 strftime(somestring, 200, "%Y-%m-%d %H:%M:%S %Z", &tm);
116 # error "Need strftime"
118 fprintf(out_file, "\t<lastupdate> %lu </lastupdate> <!-- %s -->\n\n",
119 (unsigned long) rrd.live_head->last_up, somestring);
120 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
121 fprintf(out_file, "\t<ds>\n");
122 fprintf(out_file, "\t\t<name> %s </name>\n", rrd.ds_def[i].ds_nam);
123 fprintf(out_file, "\t\t<type> %s </type>\n", rrd.ds_def[i].dst);
124 if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
126 "\t\t<minimal_heartbeat> %lu </minimal_heartbeat>\n",
127 rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
128 if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)) {
129 fprintf(out_file, "\t\t<min> NaN </min>\n");
131 fprintf(out_file, "\t\t<min> %0.10e </min>\n",
132 rrd.ds_def[i].par[DS_min_val].u_val);
134 if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)) {
135 fprintf(out_file, "\t\t<max> NaN </max>\n");
137 fprintf(out_file, "\t\t<max> %0.10e </max>\n",
138 rrd.ds_def[i].par[DS_max_val].u_val);
140 } else { /* DST_CDEF */
143 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
145 fprintf(out_file, "\t\t<cdef> %s </cdef>\n", str);
148 fprintf(out_file, "\n\t\t<!-- PDP Status -->\n");
149 fprintf(out_file, "\t\t<last_ds> %s </last_ds>\n",
150 rrd.pdp_prep[i].last_ds);
151 if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)) {
152 fprintf(out_file, "\t\t<value> NaN </value>\n");
154 fprintf(out_file, "\t\t<value> %0.10e </value>\n",
155 rrd.pdp_prep[i].scratch[PDP_val].u_val);
157 fprintf(out_file, "\t\t<unknown_sec> %lu </unknown_sec>\n",
158 rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
160 fprintf(out_file, "\t</ds>\n\n");
163 fputs("<!-- Round Robin Archives -->", out_file);
165 rra_base = rrd_file->header_len;
168 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
172 rra_start = rra_next;
173 rra_next += (rrd.stat_head->ds_cnt
174 * rrd.rra_def[i].row_cnt * sizeof(rrd_value_t));
175 fprintf(out_file, "\t<rra>\n");
176 fprintf(out_file, "\t\t<cf> %s </cf>\n", rrd.rra_def[i].cf_nam);
178 "\t\t<pdp_per_row> %lu </pdp_per_row> <!-- %lu seconds -->\n\n",
179 rrd.rra_def[i].pdp_cnt,
180 rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
181 /* support for RRA parameters */
182 fprintf(out_file, "\t\t<params>\n");
183 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
186 fprintf(out_file, "\t\t<hw_alpha> %0.10e </hw_alpha>\n",
187 rrd.rra_def[i].par[RRA_hw_alpha].u_val);
188 fprintf(out_file, "\t\t<hw_beta> %0.10e </hw_beta>\n",
189 rrd.rra_def[i].par[RRA_hw_beta].u_val);
191 "\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
192 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
197 "\t\t<seasonal_gamma> %0.10e </seasonal_gamma>\n",
198 rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
200 "\t\t<seasonal_smooth_idx> %lu </seasonal_smooth_idx>\n",
201 rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
202 if (atoi(rrd.stat_head->version) >= 4) {
204 "\t\t<smoothing_window> %0.10e </smoothing_window>\n",
205 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].
209 "\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
210 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
213 fprintf(out_file, "\t\t<delta_pos> %0.10e </delta_pos>\n",
214 rrd.rra_def[i].par[RRA_delta_pos].u_val);
215 fprintf(out_file, "\t\t<delta_neg> %0.10e </delta_neg>\n",
216 rrd.rra_def[i].par[RRA_delta_neg].u_val);
217 fprintf(out_file, "\t\t<window_len> %lu </window_len>\n",
218 rrd.rra_def[i].par[RRA_window_len].u_cnt);
220 "\t\t<failure_threshold> %lu </failure_threshold>\n",
221 rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
225 "\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
226 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
233 fprintf(out_file, "\t\t<xff> %0.10e </xff>\n",
234 rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
237 fprintf(out_file, "\t\t</params>\n");
238 fprintf(out_file, "\t\t<cdp_prep>\n");
239 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
240 unsigned long ivalue;
242 fprintf(out_file, "\t\t\t<ds>\n");
243 /* support for exporting all CDP parameters */
244 /* parameters common to all CFs */
245 /* primary_val and secondary_val do not need to be saved between updates
246 * so strictly speaking they could be omitted.
247 * However, they can be useful for diagnostic purposes, so are included here. */
248 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt
249 + ii].scratch[CDP_primary_val].u_val;
252 "\t\t\t<primary_value> NaN </primary_value>\n");
255 "\t\t\t<primary_value> %0.10e </primary_value>\n",
259 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
260 ii].scratch[CDP_secondary_val].u_val;
263 "\t\t\t<secondary_value> NaN </secondary_value>\n");
266 "\t\t\t<secondary_value> %0.10e </secondary_value>\n",
269 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
273 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
274 ii].scratch[CDP_hw_intercept].u_val;
276 fprintf(out_file, "\t\t\t<intercept> NaN </intercept>\n");
279 "\t\t\t<intercept> %0.10e </intercept>\n", value);
282 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
283 ii].scratch[CDP_hw_last_intercept].u_val;
286 "\t\t\t<last_intercept> NaN </last_intercept>\n");
289 "\t\t\t<last_intercept> %0.10e </last_intercept>\n",
293 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
294 ii].scratch[CDP_hw_slope].u_val;
296 fprintf(out_file, "\t\t\t<slope> NaN </slope>\n");
298 fprintf(out_file, "\t\t\t<slope> %0.10e </slope>\n",
302 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
303 ii].scratch[CDP_hw_last_slope].u_val;
306 "\t\t\t<last_slope> NaN </last_slope>\n");
309 "\t\t\t<last_slope> %0.10e </last_slope>\n",
313 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
314 ii].scratch[CDP_null_count].u_cnt;
315 fprintf(out_file, "\t\t\t<nan_count> %lu </nan_count>\n",
318 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
319 ii].scratch[CDP_last_null_count].u_cnt;
321 "\t\t\t<last_nan_count> %lu </last_nan_count>\n",
327 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
328 ii].scratch[CDP_hw_seasonal].u_val;
330 fprintf(out_file, "\t\t\t<seasonal> NaN </seasonal>\n");
332 fprintf(out_file, "\t\t\t<seasonal> %0.10e </seasonal>\n",
336 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
337 ii].scratch[CDP_hw_last_seasonal].u_val;
340 "\t\t\t<last_seasonal> NaN </last_seasonal>\n");
343 "\t\t\t<last_seasonal> %0.10e </last_seasonal>\n",
347 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
348 ii].scratch[CDP_init_seasonal].u_cnt;
349 fprintf(out_file, "\t\t\t<init_flag> %lu </init_flag>\n",
357 char *violations_array = (char *) ((void *)
364 fprintf(out_file, "\t\t\t<history> ");
366 vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
368 fprintf(out_file, "%d", violations_array[vidx]);
370 fprintf(out_file, " </history>\n");
379 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
380 ii].scratch[CDP_val].u_val;
382 fprintf(out_file, "\t\t\t<value> NaN </value>\n");
384 fprintf(out_file, "\t\t\t<value> %0.10e </value>\n",
388 "\t\t\t<unknown_datapoints> %lu </unknown_datapoints>\n",
389 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
390 ii].scratch[CDP_unkn_pdp_cnt].u_cnt);
393 fprintf(out_file, "\t\t\t</ds>\n");
395 fprintf(out_file, "\t\t</cdp_prep>\n");
397 fprintf(out_file, "\t\t<database>\n");
398 rrd_seek(rrd_file, (rra_start + (rrd.rra_ptr[i].cur_row + 1)
399 * rrd.stat_head->ds_cnt
400 * sizeof(rrd_value_t)), SEEK_SET);
401 timer = -(rrd.rra_def[i].row_cnt - 1);
402 ii = rrd.rra_ptr[i].cur_row;
403 for (ix = 0; ix < rrd.rra_def[i].row_cnt; ix++) {
405 if (ii >= rrd.rra_def[i].row_cnt) {
406 rrd_seek(rrd_file, rra_start, SEEK_SET);
407 ii = 0; /* wrap if max row cnt is reached */
409 now = (rrd.live_head->last_up
410 - rrd.live_head->last_up
411 % (rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step))
412 + (timer * rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
416 localtime_r(&now, &tm);
417 strftime(somestring, 200, "%Y-%m-%d %H:%M:%S %Z", &tm);
419 # error "Need strftime"
421 fprintf(out_file, "\t\t\t<!-- %s / %d --> <row>", somestring,
423 for (iii = 0; iii < rrd.stat_head->ds_cnt; iii++) {
424 rrd_read(rrd_file, &my_cdp, sizeof(rrd_value_t) * 1);
426 fprintf(out_file, "<v> NaN </v>");
428 fprintf(out_file, "<v> %0.10e </v>", my_cdp);
431 fprintf(out_file, "</row>\n");
433 fprintf(out_file, "\t\t</database>\n\t</rra>\n");
436 fprintf(out_file, "</rrd>\n");
438 if (out_file != stdout) {
441 return rrd_close(rrd_file);