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 #if !(defined(NETWARE) || defined(WIN32))
49 extern char *tzname[2];
50 #endif
52 static int rrd_dump_opt_r(
53 const char *filename,
54 char *outname,
55 int opt_header)
56 {
57 unsigned int i, ii, ix, iii = 0;
58 time_t now;
59 char somestring[255];
60 rrd_value_t my_cdp;
61 off_t rra_base, rra_start, rra_next;
62 rrd_file_t *rrd_file;
63 FILE *out_file;
64 rrd_t rrd;
65 rrd_value_t value;
66 struct tm tm;
68 rrd_init(&rrd);
69 rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_READAHEAD);
70 if (rrd_file == NULL) {
71 rrd_free(&rrd);
72 return (-1);
73 }
75 out_file = NULL;
76 if (outname) {
77 if (!(out_file = fopen(outname, "w"))) {
78 return (-1);
79 }
80 } else {
81 out_file = stdout;
82 }
84 if (opt_header == 1) {
85 fputs("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n", out_file);
86 fputs
87 ("<!DOCTYPE rrd SYSTEM \"http://oss.oetiker.ch/rrdtool/rrdtool.dtd\">\n",
88 out_file);
89 fputs("<!-- Round Robin Database Dump -->\n", out_file);
90 fputs("<rrd>\n", out_file);
91 } else if (opt_header == 2) {
92 fputs("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n", out_file);
93 fputs("<!-- Round Robin Database Dump -->\n", out_file);
94 fputs("<rrd xmlns=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n", out_file);
95 fputs("\txsi:schemaLocation=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml http://oss.oetiker.ch/rrdtool/rrdtool-dump.xsd\">\n", out_file);
96 } else {
97 fputs("<!-- Round Robin Database Dump -->\n", out_file);
98 fputs("<rrd>\n", out_file);
99 }
101 if (atoi(rrd.stat_head->version) <= 3) {
102 fprintf(out_file, "\t<version>%s</version>\n", RRD_VERSION3);
103 } else {
104 fprintf(out_file, "\t<version>%s</version>\n", RRD_VERSION);
105 }
106 fprintf(out_file, "\t<step>%lu</step> <!-- Seconds -->\n",
107 rrd.stat_head->pdp_step);
108 #if HAVE_STRFTIME
109 localtime_r(&rrd.live_head->last_up, &tm);
110 strftime(somestring, 200, "%Y-%m-%d %H:%M:%S %Z", &tm);
111 #else
112 # error "Need strftime"
113 #endif
114 fprintf(out_file, "\t<lastupdate>%lu</lastupdate> <!-- %s -->\n\n",
115 (unsigned long) rrd.live_head->last_up, somestring);
116 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
117 fprintf(out_file, "\t<ds>\n");
118 fprintf(out_file, "\t\t<name>%s</name>\n", rrd.ds_def[i].ds_nam);
119 fprintf(out_file, "\t\t<type>%s</type>\n", rrd.ds_def[i].dst);
120 if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
121 fprintf(out_file,
122 "\t\t<minimal_heartbeat>%lu</minimal_heartbeat>\n",
123 rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
124 if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)) {
125 fprintf(out_file, "\t\t<min>NaN</min>\n");
126 } else {
127 fprintf(out_file, "\t\t<min>%0.10e</min>\n",
128 rrd.ds_def[i].par[DS_min_val].u_val);
129 }
130 if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)) {
131 fprintf(out_file, "\t\t<max>NaN</max>\n");
132 } else {
133 fprintf(out_file, "\t\t<max>%0.10e</max>\n",
134 rrd.ds_def[i].par[DS_max_val].u_val);
135 }
136 } else { /* DST_CDEF */
137 char *str = NULL;
139 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
140 rrd.ds_def, &str);
141 fprintf(out_file, "\t\t<cdef>%s</cdef>\n", str);
142 free(str);
143 }
144 fprintf(out_file, "\n\t\t<!-- PDP Status -->\n");
145 fprintf(out_file, "\t\t<last_ds>%s</last_ds>\n",
146 rrd.pdp_prep[i].last_ds);
147 if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)) {
148 fprintf(out_file, "\t\t<value>NaN</value>\n");
149 } else {
150 fprintf(out_file, "\t\t<value>%0.10e</value>\n",
151 rrd.pdp_prep[i].scratch[PDP_val].u_val);
152 }
153 fprintf(out_file, "\t\t<unknown_sec>%lu</unknown_sec>\n",
154 rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
156 fprintf(out_file, "\t</ds>\n\n");
157 }
159 fputs("<!-- Round Robin Archives -->\n", out_file);
161 rra_base = rrd_file->header_len;
162 rra_next = rra_base;
164 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
166 long timer = 0;
168 rra_start = rra_next;
169 rra_next += (rrd.stat_head->ds_cnt
170 * rrd.rra_def[i].row_cnt * sizeof(rrd_value_t));
171 fprintf(out_file, "\t<rra>\n");
172 fprintf(out_file, "\t\t<cf>%s</cf>\n", rrd.rra_def[i].cf_nam);
173 fprintf(out_file,
174 "\t\t<pdp_per_row>%lu</pdp_per_row> <!-- %lu seconds -->\n\n",
175 rrd.rra_def[i].pdp_cnt,
176 rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
177 /* support for RRA parameters */
178 fprintf(out_file, "\t\t<params>\n");
179 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
180 case CF_HWPREDICT:
181 case CF_MHWPREDICT:
182 fprintf(out_file, "\t\t<hw_alpha>%0.10e</hw_alpha>\n",
183 rrd.rra_def[i].par[RRA_hw_alpha].u_val);
184 fprintf(out_file, "\t\t<hw_beta>%0.10e</hw_beta>\n",
185 rrd.rra_def[i].par[RRA_hw_beta].u_val);
186 fprintf(out_file,
187 "\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
188 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
189 break;
190 case CF_SEASONAL:
191 case CF_DEVSEASONAL:
192 fprintf(out_file,
193 "\t\t<seasonal_gamma>%0.10e</seasonal_gamma>\n",
194 rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
195 fprintf(out_file,
196 "\t\t<seasonal_smooth_idx>%lu</seasonal_smooth_idx>\n",
197 rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
198 if (atoi(rrd.stat_head->version) >= 4) {
199 fprintf(out_file,
200 "\t\t<smoothing_window>%0.10e</smoothing_window>\n",
201 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].
202 u_val);
203 }
204 fprintf(out_file,
205 "\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
206 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
207 break;
208 case CF_FAILURES:
209 fprintf(out_file, "\t\t<delta_pos>%0.10e</delta_pos>\n",
210 rrd.rra_def[i].par[RRA_delta_pos].u_val);
211 fprintf(out_file, "\t\t<delta_neg>%0.10e</delta_neg>\n",
212 rrd.rra_def[i].par[RRA_delta_neg].u_val);
213 fprintf(out_file, "\t\t<window_len>%lu</window_len>\n",
214 rrd.rra_def[i].par[RRA_window_len].u_cnt);
215 fprintf(out_file,
216 "\t\t<failure_threshold>%lu</failure_threshold>\n",
217 rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
218 /* fall thru */
219 case CF_DEVPREDICT:
220 fprintf(out_file,
221 "\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
222 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
223 break;
224 case CF_AVERAGE:
225 case CF_MAXIMUM:
226 case CF_MINIMUM:
227 case CF_LAST:
228 default:
229 fprintf(out_file, "\t\t<xff>%0.10e</xff>\n",
230 rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
231 break;
232 }
233 fprintf(out_file, "\t\t</params>\n");
234 fprintf(out_file, "\t\t<cdp_prep>\n");
235 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
236 unsigned long ivalue;
238 fprintf(out_file, "\t\t\t<ds>\n");
239 /* support for exporting all CDP parameters */
240 /* parameters common to all CFs */
241 /* primary_val and secondary_val do not need to be saved between updates
242 * so strictly speaking they could be omitted.
243 * However, they can be useful for diagnostic purposes, so are included here. */
244 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt
245 + ii].scratch[CDP_primary_val].u_val;
246 if (isnan(value)) {
247 fprintf(out_file,
248 "\t\t\t<primary_value>NaN</primary_value>\n");
249 } else {
250 fprintf(out_file,
251 "\t\t\t<primary_value>%0.10e</primary_value>\n",
252 value);
253 }
254 value =
255 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
256 ii].scratch[CDP_secondary_val].u_val;
257 if (isnan(value)) {
258 fprintf(out_file,
259 "\t\t\t<secondary_value>NaN</secondary_value>\n");
260 } else {
261 fprintf(out_file,
262 "\t\t\t<secondary_value>%0.10e</secondary_value>\n",
263 value);
264 }
265 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
266 case CF_HWPREDICT:
267 case CF_MHWPREDICT:
268 value =
269 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
270 ii].scratch[CDP_hw_intercept].u_val;
271 if (isnan(value)) {
272 fprintf(out_file, "\t\t\t<intercept>NaN</intercept>\n");
273 } else {
274 fprintf(out_file,
275 "\t\t\t<intercept>%0.10e</intercept>\n", value);
276 }
277 value =
278 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
279 ii].scratch[CDP_hw_last_intercept].u_val;
280 if (isnan(value)) {
281 fprintf(out_file,
282 "\t\t\t<last_intercept>NaN</last_intercept>\n");
283 } else {
284 fprintf(out_file,
285 "\t\t\t<last_intercept>%0.10e</last_intercept>\n",
286 value);
287 }
288 value =
289 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
290 ii].scratch[CDP_hw_slope].u_val;
291 if (isnan(value)) {
292 fprintf(out_file, "\t\t\t<slope>NaN</slope>\n");
293 } else {
294 fprintf(out_file, "\t\t\t<slope>%0.10e</slope>\n",
295 value);
296 }
297 value =
298 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
299 ii].scratch[CDP_hw_last_slope].u_val;
300 if (isnan(value)) {
301 fprintf(out_file,
302 "\t\t\t<last_slope>NaN</last_slope>\n");
303 } else {
304 fprintf(out_file,
305 "\t\t\t<last_slope>%0.10e</last_slope>\n",
306 value);
307 }
308 ivalue =
309 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
310 ii].scratch[CDP_null_count].u_cnt;
311 fprintf(out_file, "\t\t\t<nan_count>%lu</nan_count>\n",
312 ivalue);
313 ivalue =
314 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
315 ii].scratch[CDP_last_null_count].u_cnt;
316 fprintf(out_file,
317 "\t\t\t<last_nan_count>%lu</last_nan_count>\n",
318 ivalue);
319 break;
320 case CF_SEASONAL:
321 case CF_DEVSEASONAL:
322 value =
323 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
324 ii].scratch[CDP_hw_seasonal].u_val;
325 if (isnan(value)) {
326 fprintf(out_file, "\t\t\t<seasonal>NaN</seasonal>\n");
327 } else {
328 fprintf(out_file, "\t\t\t<seasonal>%0.10e</seasonal>\n",
329 value);
330 }
331 value =
332 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
333 ii].scratch[CDP_hw_last_seasonal].u_val;
334 if (isnan(value)) {
335 fprintf(out_file,
336 "\t\t\t<last_seasonal>NaN</last_seasonal>\n");
337 } else {
338 fprintf(out_file,
339 "\t\t\t<last_seasonal>%0.10e</last_seasonal>\n",
340 value);
341 }
342 ivalue =
343 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
344 ii].scratch[CDP_init_seasonal].u_cnt;
345 fprintf(out_file, "\t\t\t<init_flag>%lu</init_flag>\n",
346 ivalue);
347 break;
348 case CF_DEVPREDICT:
349 break;
350 case CF_FAILURES:
351 {
352 unsigned short vidx;
353 char *violations_array = (char *) ((void *)
354 rrd.cdp_prep[i *
355 rrd.
356 stat_head->
357 ds_cnt +
358 ii].
359 scratch);
360 fprintf(out_file, "\t\t\t<history> ");
361 for (vidx = 0;
362 vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
363 ++vidx) {
364 fprintf(out_file, "%d", violations_array[vidx]);
365 }
366 fprintf(out_file, " </history>\n");
367 }
368 break;
369 case CF_AVERAGE:
370 case CF_MAXIMUM:
371 case CF_MINIMUM:
372 case CF_LAST:
373 default:
374 value =
375 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
376 ii].scratch[CDP_val].u_val;
377 if (isnan(value)) {
378 fprintf(out_file, "\t\t\t<value>NaN</value>\n");
379 } else {
380 fprintf(out_file, "\t\t\t<value>%0.10e</value>\n",
381 value);
382 }
383 fprintf(out_file,
384 "\t\t\t<unknown_datapoints>%lu</unknown_datapoints>\n",
385 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
386 ii].scratch[CDP_unkn_pdp_cnt].u_cnt);
387 break;
388 }
389 fprintf(out_file, "\t\t\t</ds>\n");
390 }
391 fprintf(out_file, "\t\t</cdp_prep>\n");
393 fprintf(out_file, "\t\t<database>\n");
394 rrd_seek(rrd_file, (rra_start + (rrd.rra_ptr[i].cur_row + 1)
395 * rrd.stat_head->ds_cnt
396 * sizeof(rrd_value_t)), SEEK_SET);
397 timer = -(rrd.rra_def[i].row_cnt - 1);
398 ii = rrd.rra_ptr[i].cur_row;
399 for (ix = 0; ix < rrd.rra_def[i].row_cnt; ix++) {
400 ii++;
401 if (ii >= rrd.rra_def[i].row_cnt) {
402 rrd_seek(rrd_file, rra_start, SEEK_SET);
403 ii = 0; /* wrap if max row cnt is reached */
404 }
405 now = (rrd.live_head->last_up
406 - rrd.live_head->last_up
407 % (rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step))
408 + (timer * rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
410 timer++;
411 #if HAVE_STRFTIME
412 localtime_r(&now, &tm);
413 strftime(somestring, 200, "%Y-%m-%d %H:%M:%S %Z", &tm);
414 #else
415 # error "Need strftime"
416 #endif
417 fprintf(out_file, "\t\t\t<!-- %s / %d --> <row>", somestring,
418 (int) now);
419 for (iii = 0; iii < rrd.stat_head->ds_cnt; iii++) {
420 rrd_read(rrd_file, &my_cdp, sizeof(rrd_value_t) * 1);
421 if (isnan(my_cdp)) {
422 fprintf(out_file, "<v>NaN</v>");
423 } else {
424 fprintf(out_file, "<v>%0.10e</v>", my_cdp);
425 };
426 }
427 fprintf(out_file, "</row>\n");
428 }
429 fprintf(out_file, "\t\t</database>\n\t</rra>\n");
431 }
432 fprintf(out_file, "</rrd>\n");
433 rrd_free(&rrd);
434 if (out_file != stdout) {
435 fclose(out_file);
436 }
437 return rrd_close(rrd_file);
438 }
440 /* backward compatibility with 1.2.x */
441 int rrd_dump_r(
442 const char *filename,
443 char *outname)
444 {
445 return rrd_dump_opt_r(filename, outname, 0);
446 }
448 int rrd_dump(
449 int argc,
450 char **argv)
451 {
452 int rc;
453 /**
454 * 0 = no header
455 * 1 = dtd header
456 * 2 = xsd header
457 */
458 int opt_header = 0;
459 char *opt_daemon = NULL;
461 /* init rrd clean */
463 optind = 0;
464 opterr = 0; /* initialize getopt */
466 while (42) {
467 int opt;
468 int option_index = 0;
469 static struct option long_options[] = {
470 {"daemon", required_argument, 0, 'd'},
471 {"header", required_argument, 0, 'h'},
472 {0, 0, 0, 0}
473 };
475 opt = getopt_long(argc, argv, "d:h:", long_options, &option_index);
477 if (opt == EOF)
478 break;
480 switch (opt) {
481 case 'd':
482 if (opt_daemon != NULL)
483 free (opt_daemon);
484 opt_daemon = strdup (optarg);
485 if (opt_daemon == NULL)
486 {
487 rrd_set_error ("strdup failed.");
488 return (-1);
489 }
490 break;
492 case 'h':
493 if (strcmp(optarg, "dtd") == 0) {
494 opt_header = 1;
495 } else if (strcmp(optarg, "xsd") == 0) {
496 opt_header = 2;
497 }
498 break;
500 default:
501 rrd_set_error("usage rrdtool %s [--header|-h {xsd,dtd}] "
502 "file.rrd [file.xml]", argv[0]);
503 return (-1);
504 break;
505 }
506 } /* while (42) */
508 if ((argc - optind) < 1 || (argc - optind) > 2) {
509 rrd_set_error("usage rrdtool %s [--header|-h {xsd,dtd}] "
510 "file.rrd [file.xml]", argv[0]);
511 return (-1);
512 }
514 rc = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
515 if (opt_daemon) free(opt_daemon);
516 if (rc) return (rc);
518 if ((argc - optind) == 2) {
519 rc = rrd_dump_opt_r(argv[optind], argv[optind + 1], opt_header);
520 } else {
521 rc = rrd_dump_opt_r(argv[optind], NULL, opt_header);
522 }
524 return rc;
525 }