1 /*****************************************************************************
2 * RRDtool 1.3rc2 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"
47 #if !(defined(NETWARE) || defined(WIN32))
48 extern char *tzname[2];
60 opterr = 0; /* initialize getopt */
65 static struct option long_options[] = {
66 {"no-header", no_argument, 0, 'n'},
70 opt = getopt_long(argc, argv, "n", long_options, &option_index);
81 rrd_set_error("usage rrdtool %s [--no-header|-n] "
82 "file.rrd [file.xml]", argv[0]);
88 if ((argc - optind) < 2) {
89 rrd_set_error("usage rrdtool %s [--no-header|-n] "
90 "file.rrd [file.xml]", argv[0]);
95 rc = rrd_dump_opt_r(argv[1], argv[2],opt_noheader);
97 rc = rrd_dump_opt_r(argv[1], NULL,opt_noheader);
105 const char *filename,
110 unsigned int i, ii, ix, iii = 0;
112 char somestring[255];
114 off_t rra_base, rra_start, rra_next;
115 rrd_file_t *rrd_file;
121 rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_READAHEAD);
122 if (rrd_file == NULL) {
129 if (!(out_file = fopen(outname, "w"))) {
137 fputs("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n", out_file);
139 ("<!DOCTYPE rrd SYSTEM \"http://oss.oetiker.ch/rrdtool/rrdtool.dtd\">\n",
142 fputs("<!-- Round Robin Database Dump -->", out_file);
143 fputs("<rrd>", out_file);
144 if (atoi(rrd.stat_head->version) <= 3) {
145 fprintf(out_file, "\t<version> %s </version>\n", RRD_VERSION3);
147 fprintf(out_file, "\t<version> %s </version>\n", RRD_VERSION);
149 fprintf(out_file, "\t<step> %lu </step> <!-- Seconds -->\n",
150 rrd.stat_head->pdp_step);
152 localtime_r(&rrd.live_head->last_up, &tm);
153 strftime(somestring, 200, "%Y-%m-%d %H:%M:%S %Z", &tm);
155 # error "Need strftime"
157 fprintf(out_file, "\t<lastupdate> %lu </lastupdate> <!-- %s -->\n\n",
158 (unsigned long) rrd.live_head->last_up, somestring);
159 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
160 fprintf(out_file, "\t<ds>\n");
161 fprintf(out_file, "\t\t<name> %s </name>\n", rrd.ds_def[i].ds_nam);
162 fprintf(out_file, "\t\t<type> %s </type>\n", rrd.ds_def[i].dst);
163 if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
165 "\t\t<minimal_heartbeat> %lu </minimal_heartbeat>\n",
166 rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
167 if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)) {
168 fprintf(out_file, "\t\t<min> NaN </min>\n");
170 fprintf(out_file, "\t\t<min> %0.10e </min>\n",
171 rrd.ds_def[i].par[DS_min_val].u_val);
173 if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)) {
174 fprintf(out_file, "\t\t<max> NaN </max>\n");
176 fprintf(out_file, "\t\t<max> %0.10e </max>\n",
177 rrd.ds_def[i].par[DS_max_val].u_val);
179 } else { /* DST_CDEF */
182 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
184 fprintf(out_file, "\t\t<cdef> %s </cdef>\n", str);
187 fprintf(out_file, "\n\t\t<!-- PDP Status -->\n");
188 fprintf(out_file, "\t\t<last_ds> %s </last_ds>\n",
189 rrd.pdp_prep[i].last_ds);
190 if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)) {
191 fprintf(out_file, "\t\t<value> NaN </value>\n");
193 fprintf(out_file, "\t\t<value> %0.10e </value>\n",
194 rrd.pdp_prep[i].scratch[PDP_val].u_val);
196 fprintf(out_file, "\t\t<unknown_sec> %lu </unknown_sec>\n",
197 rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
199 fprintf(out_file, "\t</ds>\n\n");
202 fputs("<!-- Round Robin Archives -->", out_file);
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));
214 fprintf(out_file, "\t<rra>\n");
215 fprintf(out_file, "\t\t<cf> %s </cf>\n", rrd.rra_def[i].cf_nam);
217 "\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);
220 /* support for RRA parameters */
221 fprintf(out_file, "\t\t<params>\n");
222 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
225 fprintf(out_file, "\t\t<hw_alpha> %0.10e </hw_alpha>\n",
226 rrd.rra_def[i].par[RRA_hw_alpha].u_val);
227 fprintf(out_file, "\t\t<hw_beta> %0.10e </hw_beta>\n",
228 rrd.rra_def[i].par[RRA_hw_beta].u_val);
230 "\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
231 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
236 "\t\t<seasonal_gamma> %0.10e </seasonal_gamma>\n",
237 rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
239 "\t\t<seasonal_smooth_idx> %lu </seasonal_smooth_idx>\n",
240 rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
241 if (atoi(rrd.stat_head->version) >= 4) {
243 "\t\t<smoothing_window> %0.10e </smoothing_window>\n",
244 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].
248 "\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
249 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
252 fprintf(out_file, "\t\t<delta_pos> %0.10e </delta_pos>\n",
253 rrd.rra_def[i].par[RRA_delta_pos].u_val);
254 fprintf(out_file, "\t\t<delta_neg> %0.10e </delta_neg>\n",
255 rrd.rra_def[i].par[RRA_delta_neg].u_val);
256 fprintf(out_file, "\t\t<window_len> %lu </window_len>\n",
257 rrd.rra_def[i].par[RRA_window_len].u_cnt);
259 "\t\t<failure_threshold> %lu </failure_threshold>\n",
260 rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
264 "\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
265 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
272 fprintf(out_file, "\t\t<xff> %0.10e </xff>\n",
273 rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
276 fprintf(out_file, "\t\t</params>\n");
277 fprintf(out_file, "\t\t<cdp_prep>\n");
278 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
279 unsigned long ivalue;
281 fprintf(out_file, "\t\t\t<ds>\n");
282 /* support for exporting all CDP parameters */
283 /* parameters common to all CFs */
284 /* primary_val and secondary_val do not need to be saved between updates
285 * so strictly speaking they could be omitted.
286 * However, they can be useful for diagnostic purposes, so are included here. */
287 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt
288 + ii].scratch[CDP_primary_val].u_val;
291 "\t\t\t<primary_value> NaN </primary_value>\n");
294 "\t\t\t<primary_value> %0.10e </primary_value>\n",
298 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
299 ii].scratch[CDP_secondary_val].u_val;
302 "\t\t\t<secondary_value> NaN </secondary_value>\n");
305 "\t\t\t<secondary_value> %0.10e </secondary_value>\n",
308 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
312 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
313 ii].scratch[CDP_hw_intercept].u_val;
315 fprintf(out_file, "\t\t\t<intercept> NaN </intercept>\n");
318 "\t\t\t<intercept> %0.10e </intercept>\n", value);
321 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
322 ii].scratch[CDP_hw_last_intercept].u_val;
325 "\t\t\t<last_intercept> NaN </last_intercept>\n");
328 "\t\t\t<last_intercept> %0.10e </last_intercept>\n",
332 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
333 ii].scratch[CDP_hw_slope].u_val;
335 fprintf(out_file, "\t\t\t<slope> NaN </slope>\n");
337 fprintf(out_file, "\t\t\t<slope> %0.10e </slope>\n",
341 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
342 ii].scratch[CDP_hw_last_slope].u_val;
345 "\t\t\t<last_slope> NaN </last_slope>\n");
348 "\t\t\t<last_slope> %0.10e </last_slope>\n",
352 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
353 ii].scratch[CDP_null_count].u_cnt;
354 fprintf(out_file, "\t\t\t<nan_count> %lu </nan_count>\n",
357 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
358 ii].scratch[CDP_last_null_count].u_cnt;
360 "\t\t\t<last_nan_count> %lu </last_nan_count>\n",
366 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
367 ii].scratch[CDP_hw_seasonal].u_val;
369 fprintf(out_file, "\t\t\t<seasonal> NaN </seasonal>\n");
371 fprintf(out_file, "\t\t\t<seasonal> %0.10e </seasonal>\n",
375 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
376 ii].scratch[CDP_hw_last_seasonal].u_val;
379 "\t\t\t<last_seasonal> NaN </last_seasonal>\n");
382 "\t\t\t<last_seasonal> %0.10e </last_seasonal>\n",
386 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
387 ii].scratch[CDP_init_seasonal].u_cnt;
388 fprintf(out_file, "\t\t\t<init_flag> %lu </init_flag>\n",
396 char *violations_array = (char *) ((void *)
403 fprintf(out_file, "\t\t\t<history> ");
405 vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
407 fprintf(out_file, "%d", violations_array[vidx]);
409 fprintf(out_file, " </history>\n");
418 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
419 ii].scratch[CDP_val].u_val;
421 fprintf(out_file, "\t\t\t<value> NaN </value>\n");
423 fprintf(out_file, "\t\t\t<value> %0.10e </value>\n",
427 "\t\t\t<unknown_datapoints> %lu </unknown_datapoints>\n",
428 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
429 ii].scratch[CDP_unkn_pdp_cnt].u_cnt);
432 fprintf(out_file, "\t\t\t</ds>\n");
434 fprintf(out_file, "\t\t</cdp_prep>\n");
436 fprintf(out_file, "\t\t<database>\n");
437 rrd_seek(rrd_file, (rra_start + (rrd.rra_ptr[i].cur_row + 1)
438 * rrd.stat_head->ds_cnt
439 * sizeof(rrd_value_t)), SEEK_SET);
440 timer = -(rrd.rra_def[i].row_cnt - 1);
441 ii = rrd.rra_ptr[i].cur_row;
442 for (ix = 0; ix < rrd.rra_def[i].row_cnt; ix++) {
444 if (ii >= rrd.rra_def[i].row_cnt) {
445 rrd_seek(rrd_file, rra_start, SEEK_SET);
446 ii = 0; /* wrap if max row cnt is reached */
448 now = (rrd.live_head->last_up
449 - rrd.live_head->last_up
450 % (rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step))
451 + (timer * rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
455 localtime_r(&now, &tm);
456 strftime(somestring, 200, "%Y-%m-%d %H:%M:%S %Z", &tm);
458 # error "Need strftime"
460 fprintf(out_file, "\t\t\t<!-- %s / %d --> <row>", somestring,
462 for (iii = 0; iii < rrd.stat_head->ds_cnt; iii++) {
463 rrd_read(rrd_file, &my_cdp, sizeof(rrd_value_t) * 1);
465 fprintf(out_file, "<v> NaN </v>");
467 fprintf(out_file, "<v> %0.10e </v>", my_cdp);
470 fprintf(out_file, "</row>\n");
472 fprintf(out_file, "\t\t</database>\n\t</rra>\n");
475 fprintf(out_file, "</rrd>\n");
477 if (out_file != stdout) {
480 return rrd_close(rrd_file);
483 /* backward compatibility with 1.2.x */
485 const char *filename,
488 rrd_dump_opt_r(filename,outname,0);