1 /*****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
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"
50 #if !(defined(NETWARE) || defined(WIN32))
51 extern char *tzname[2];
55 size_t rrd_dump_opt_cb_fileout(
68 rrd_output_callback_t cb,
71 unsigned int i, ii, ix, iii = 0;
75 off_t rra_base, rra_start, rra_next;
80 char *old_locale = "";
82 //These two macros are local defines to clean up visible code from its redndancy
83 //and make it easier to read.
84 #define CB_PUTS(str) \
85 cb((str), strlen((str)), user)
86 #define CB_FMTS(...) do { \
88 snprintf (buffer, sizeof(buffer), __VA_ARGS__); \
91 //These macros are to be undefined at the end of this function
93 //Check if we got a (valid) callback method
100 rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_READAHEAD);
101 if (rrd_file == NULL) {
106 old_locale = setlocale(LC_NUMERIC, NULL);
107 setlocale(LC_NUMERIC, "C");
110 if (opt_header == 1) {
111 CB_PUTS("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
112 CB_PUTS("<!DOCTYPE rrd SYSTEM \"http://oss.oetiker.ch/rrdtool/rrdtool.dtd\">\n");
113 CB_PUTS("<!-- Round Robin Database Dump -->\n");
115 } else if (opt_header == 2) {
116 CB_PUTS("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
117 CB_PUTS("<!-- Round Robin Database Dump -->\n");
118 CB_PUTS("<rrd xmlns=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml\" "
119 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
120 CB_PUTS("\txsi:schemaLocation=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml "
121 "http://oss.oetiker.ch/rrdtool/rrdtool-dump.xsd\">\n");
123 CB_PUTS("<!-- Round Robin Database Dump -->\n");
127 if (atoi(rrd.stat_head->version) <= 3) {
128 CB_FMTS("\t<version>%s</version>\n", RRD_VERSION3);
130 CB_FMTS("\t<version>%s</version>\n", RRD_VERSION);
133 CB_FMTS("\t<step>%lu</step> <!-- Seconds -->\n",
134 rrd.stat_head->pdp_step);
137 localtime_r(&rrd.live_head->last_up, &tm);
138 strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
140 # error "Need strftime"
142 CB_FMTS("\t<lastupdate>%lld</lastupdate> <!-- %s -->\n\n",
143 (long long int) rrd.live_head->last_up, somestring);
144 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
147 CB_FMTS("\t\t<name> %s </name>\n", rrd.ds_def[i].ds_nam);
149 CB_FMTS("\t\t<type> %s </type>\n", rrd.ds_def[i].dst);
151 if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
152 CB_FMTS("\t\t<minimal_heartbeat>%lu</minimal_heartbeat>\n",
153 rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
155 if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)) {
156 CB_PUTS("\t\t<min>NaN</min>\n");
158 CB_FMTS("\t\t<min>%0.10e</min>\n",
159 rrd.ds_def[i].par[DS_min_val].u_val);
162 if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)) {
163 CB_PUTS("\t\t<max>NaN</max>\n");
165 CB_FMTS("\t\t<max>%0.10e</max>\n",
166 rrd.ds_def[i].par[DS_max_val].u_val);
168 } else { /* DST_CDEF */
171 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
174 //Splitting into 3 writes to avoid allocating memory
175 //This is better compared to snprintf as str may be of arbitrary size
176 CB_PUTS("\t\t<cdef> ");
178 CB_PUTS(" </cdef>\n");
183 CB_PUTS("\n\t\t<!-- PDP Status -->\n");
184 CB_FMTS("\t\t<last_ds>%s</last_ds>\n",
185 rrd.pdp_prep[i].last_ds);
187 if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)) {
188 CB_PUTS("\t\t<value>NaN</value>\n");
190 CB_FMTS("\t\t<value>%0.10e</value>\n",
191 rrd.pdp_prep[i].scratch[PDP_val].u_val);
194 CB_FMTS("\t\t<unknown_sec> %lu </unknown_sec>\n",
195 rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
197 CB_PUTS("\t</ds>\n\n");
200 CB_PUTS("\t<!-- Round Robin Archives -->\n");
202 rra_base = rrd_file->header_len;
205 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
209 rra_start = rra_next;
210 rra_next += (rrd.stat_head->ds_cnt
211 * rrd.rra_def[i].row_cnt * sizeof(rrd_value_t));
213 CB_PUTS("\t<rra>\n");
215 CB_FMTS("\t\t<cf>%s</cf>\n", rrd.rra_def[i].cf_nam);
217 CB_FMTS("\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);
221 /* support for RRA parameters */
222 CB_PUTS("\t\t<params>\n");
224 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
227 CB_FMTS("\t\t<hw_alpha>%0.10e</hw_alpha>\n",
228 rrd.rra_def[i].par[RRA_hw_alpha].u_val);
230 CB_FMTS("\t\t<hw_beta>%0.10e</hw_beta>\n",
231 rrd.rra_def[i].par[RRA_hw_beta].u_val);
233 CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
234 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
238 CB_FMTS("\t\t<seasonal_gamma>%0.10e</seasonal_gamma>\n",
239 rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
241 CB_FMTS("\t\t<seasonal_smooth_idx>%lu</seasonal_smooth_idx>\n",
242 rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
244 if (atoi(rrd.stat_head->version) >= 4) {
245 CB_FMTS("\t\t<smoothing_window>%0.10e</smoothing_window>\n",
246 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val);
249 CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
250 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
253 CB_FMTS("\t\t<delta_pos>%0.10e</delta_pos>\n",
254 rrd.rra_def[i].par[RRA_delta_pos].u_val);
256 CB_FMTS("\t\t<delta_neg>%0.10e</delta_neg>\n",
257 rrd.rra_def[i].par[RRA_delta_neg].u_val);
259 CB_FMTS("\t\t<window_len>%lu</window_len>\n",
260 rrd.rra_def[i].par[RRA_window_len].u_cnt);
262 CB_FMTS("\t\t<failure_threshold>%lu</failure_threshold>\n",
263 rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
267 CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
268 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
275 CB_FMTS("\t\t<xff>%0.10e</xff>\n",
276 rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
280 CB_PUTS("\t\t</params>\n");
281 CB_PUTS("\t\t<cdp_prep>\n");
283 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
284 unsigned long ivalue;
286 CB_PUTS("\t\t\t<ds>\n");
287 /* support for exporting all CDP parameters */
288 /* parameters common to all CFs */
289 /* primary_val and secondary_val do not need to be saved between updates
290 * so strictly speaking they could be omitted.
291 * However, they can be useful for diagnostic purposes, so are included here. */
292 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
293 scratch[CDP_primary_val].u_val;
295 CB_PUTS("\t\t\t<primary_value>NaN</primary_value>\n");
297 CB_FMTS("\t\t\t<primary_value>%0.10e</primary_value>\n", value);
300 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
301 scratch[CDP_secondary_val].u_val;
303 CB_PUTS("\t\t\t<secondary_value>NaN</secondary_value>\n");
305 CB_FMTS("\t\t\t<secondary_value>%0.10e</secondary_value>\n", value);
308 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
311 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
312 scratch[CDP_hw_intercept].u_val;
314 CB_PUTS("\t\t\t<intercept>NaN</intercept>\n");
316 CB_FMTS("\t\t\t<intercept>%0.10e</intercept>\n", value);
319 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
320 scratch[CDP_hw_last_intercept].u_val;
322 CB_PUTS("\t\t\t<last_intercept>NaN</last_intercept>\n");
324 CB_FMTS("\t\t\t<last_intercept>%0.10e</last_intercept>\n", value);
327 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
328 scratch[CDP_hw_slope].u_val;
330 CB_PUTS("\t\t\t<slope>NaN</slope>\n");
332 CB_FMTS("\t\t\t<slope>%0.10e</slope>\n", value);
335 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
336 scratch[CDP_hw_last_slope].u_val;
338 CB_PUTS("\t\t\t<last_slope>NaN</last_slope>\n");
340 CB_FMTS("\t\t\t<last_slope>%0.10e</last_slope>\n", value);
343 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
344 scratch[CDP_null_count].u_cnt;
345 CB_FMTS("\t\t\t<nan_count>%lu</nan_count>\n", ivalue);
347 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
348 scratch[CDP_last_null_count].u_cnt;
349 CB_FMTS("\t\t\t<last_nan_count>%lu</last_nan_count>\n", ivalue);
353 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
354 scratch[CDP_hw_seasonal].u_val;
356 CB_PUTS("\t\t\t<seasonal>NaN</seasonal>\n");
358 CB_FMTS("\t\t\t<seasonal>%0.10e</seasonal>\n", value);
361 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
362 scratch[CDP_hw_last_seasonal].u_val;
364 CB_PUTS("\t\t\t<last_seasonal>NaN</last_seasonal>\n");
366 CB_FMTS("\t\t\t<last_seasonal>%0.10e</last_seasonal>\n", value);
369 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
370 scratch[CDP_init_seasonal].u_cnt;
371 CB_FMTS("\t\t\t<init_flag>%lu</init_flag>\n", ivalue);
378 char *violations_array = (char *) ((void *)
379 rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch);
380 CB_PUTS("\t\t\t<history>");
382 vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
384 CB_FMTS("%d", violations_array[vidx]);
386 CB_PUTS("</history>\n");
394 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch[CDP_val].u_val;
396 CB_PUTS("\t\t\t<value>NaN</value>\n");
398 CB_FMTS("\t\t\t<value>%0.10e</value>\n", value);
401 CB_FMTS("\t\t\t<unknown_datapoints>%lu</unknown_datapoints>\n",
402 rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
403 scratch[CDP_unkn_pdp_cnt].u_cnt);
406 CB_PUTS("\t\t\t</ds>\n");
408 CB_PUTS("\t\t</cdp_prep>\n");
410 CB_PUTS("\t\t<database>\n");
411 rrd_seek(rrd_file, (rra_start + (rrd.rra_ptr[i].cur_row + 1)
412 * rrd.stat_head->ds_cnt
413 * sizeof(rrd_value_t)), SEEK_SET);
414 timer = -(long)(rrd.rra_def[i].row_cnt - 1);
415 ii = rrd.rra_ptr[i].cur_row;
416 for (ix = 0; ix < rrd.rra_def[i].row_cnt; ix++) {
418 if (ii >= rrd.rra_def[i].row_cnt) {
419 rrd_seek(rrd_file, rra_start, SEEK_SET);
420 ii = 0; /* wrap if max row cnt is reached */
422 now = (rrd.live_head->last_up
423 - rrd.live_head->last_up
424 % (rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step))
425 + (timer * rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
429 localtime_r(&now, &tm);
430 strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
432 # error "Need strftime"
434 CB_FMTS("\t\t\t<!-- %s / %lld --> <row>", somestring, (long long int) now);
435 for (iii = 0; iii < rrd.stat_head->ds_cnt; iii++) {
436 rrd_read(rrd_file, &my_cdp, sizeof(rrd_value_t) * 1);
438 CB_PUTS("<v>NaN</v>");
440 CB_FMTS("<v>%0.10e</v>", my_cdp);
445 CB_PUTS("\t\t</database>\n\t</rra>\n");
452 setlocale(LC_NUMERIC, old_locale);
454 return rrd_close(rrd_file);
456 //Undefining the previously defined shortcuts
457 //See start of this function
460 //End of macro undefining
464 size_t rrd_dump_opt_cb_fileout(
469 return fwrite(data, 1, len, (FILE *)user);
473 const char *filename,
482 if (!(out_file = fopen(outname, "w"))) {
489 res = rrd_dump_cb_r(filename, opt_noheader, rrd_dump_opt_cb_fileout, (void *)out_file);
491 if (out_file != stdout) {
498 /* backward compatibility with 1.2.x */
500 const char *filename,
503 return rrd_dump_opt_r(filename, outname, 0);
517 char *opt_daemon = NULL;
522 opterr = 0; /* initialize getopt */
524 while (42) {/* ha ha */
526 int option_index = 0;
527 static struct option long_options[] = {
528 {"daemon", required_argument, 0, 'd'},
529 {"header", required_argument, 0, 'h'},
530 {"no-header", no_argument, 0, 'n'},
534 opt = getopt_long(argc, argv, "d:h:n", long_options, &option_index);
541 if (opt_daemon != NULL)
543 opt_daemon = strdup (optarg);
544 if (opt_daemon == NULL)
546 rrd_set_error ("strdup failed.");
556 if (strcmp(optarg, "dtd") == 0) {
558 } else if (strcmp(optarg, "xsd") == 0) {
560 } else if (strcmp(optarg, "none") == 0) {
566 rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}] [--no-header]"
567 "file.rrd [file.xml]", argv[0]);
573 if ((argc - optind) < 1 || (argc - optind) > 2) {
574 rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}] [--no-header]"
575 "file.rrd [file.xml]", argv[0]);
579 rc = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
580 if (opt_daemon) free(opt_daemon);
583 if ((argc - optind) == 2) {
584 rc = rrd_dump_opt_r(argv[optind], argv[optind + 1], opt_header);
586 rc = rrd_dump_opt_r(argv[optind], NULL, opt_header);