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(
66 rrd_output_callback_t cb,
69 unsigned int i, ii, ix, iii = 0;
73 off_t rra_base, rra_start, rra_next;
78 char *old_locale = "";
80 //These two macros are local defines to clean up visible code from its redndancy
81 //and make it easier to read.
82 #define CB_PUTS(str) \
83 cb((str), strlen((str)), user)
84 #define CB_FMTS(...) do { \
86 snprintf (buffer, sizeof(buffer), __VA_ARGS__); \
89 //These macros are to be undefined at the end of this function
91 //Check if we got a (valid) callback method
98 rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_READAHEAD);
99 if (rrd_file == NULL) {
104 #ifdef HAVE_SETLOCALE
105 old_locale = setlocale(LC_NUMERIC, "C");
108 if (opt_header == 1) {
109 CB_PUTS("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
110 CB_PUTS("<!DOCTYPE rrd SYSTEM \"http://oss.oetiker.ch/rrdtool/rrdtool.dtd\">\n");
111 CB_PUTS("<!-- Round Robin Database Dump -->\n");
113 } else if (opt_header == 2) {
114 CB_PUTS("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
115 CB_PUTS("<!-- Round Robin Database Dump -->\n");
116 CB_PUTS("<rrd xmlns=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml\" "
117 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
118 CB_PUTS("\txsi:schemaLocation=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml "
119 "http://oss.oetiker.ch/rrdtool/rrdtool-dump.xsd\">\n");
121 CB_PUTS("<!-- Round Robin Database Dump -->\n");
125 if (atoi(rrd.stat_head->version) <= 3) {
126 CB_FMTS("\t<version>%s</version>\n", RRD_VERSION3);
128 CB_FMTS("\t<version>%s</version>\n", RRD_VERSION);
131 CB_FMTS("\t<step>%lu</step> <!-- Seconds -->\n",
132 rrd.stat_head->pdp_step);
135 localtime_r(&rrd.live_head->last_up, &tm);
136 strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
138 # error "Need strftime"
140 CB_FMTS("\t<lastupdate>%lld</lastupdate> <!-- %s -->\n\n",
141 (long long) rrd.live_head->last_up, somestring);
142 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
145 CB_FMTS("\t\t<name> %s </name>\n", rrd.ds_def[i].ds_nam);
147 CB_FMTS("\t\t<type> %s </type>\n", rrd.ds_def[i].dst);
149 if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
150 CB_FMTS("\t\t<minimal_heartbeat>%lu</minimal_heartbeat>\n",
151 rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
153 if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)) {
154 CB_PUTS("\t\t<min>NaN</min>\n");
156 CB_FMTS("\t\t<min>%0.10e</min>\n",
157 rrd.ds_def[i].par[DS_min_val].u_val);
160 if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)) {
161 CB_PUTS("\t\t<max>NaN</max>\n");
163 CB_FMTS("\t\t<max>%0.10e</max>\n",
164 rrd.ds_def[i].par[DS_max_val].u_val);
166 } else { /* DST_CDEF */
169 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
172 //Splitting into 3 writes to avoid allocating memory
173 //This is better compared to snprintf as str may be of arbitrary size
174 CB_PUTS("\t\t<cdef> ");
176 CB_PUTS(" </cdef>\n");
181 CB_PUTS("\n\t\t<!-- PDP Status -->\n");
182 CB_FMTS("\t\t<last_ds>%s</last_ds>\n",
183 rrd.pdp_prep[i].last_ds);
185 if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)) {
186 CB_PUTS("\t\t<value>NaN</value>\n");
188 CB_FMTS("\t\t<value>%0.10e</value>\n",
189 rrd.pdp_prep[i].scratch[PDP_val].u_val);
192 CB_FMTS("\t\t<unknown_sec> %lu </unknown_sec>\n",
193 rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
195 CB_PUTS("\t</ds>\n\n");
198 CB_PUTS("\t<!-- Round Robin Archives -->\n");
200 rra_base = rrd_file->header_len;
203 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
207 rra_start = rra_next;
208 rra_next += (rrd.stat_head->ds_cnt
209 * rrd.rra_def[i].row_cnt * sizeof(rrd_value_t));
211 CB_PUTS("\t<rra>\n");
213 CB_FMTS("\t\t<cf>%s</cf>\n", rrd.rra_def[i].cf_nam);
215 CB_FMTS("\t\t<pdp_per_row>%lu</pdp_per_row> <!-- %lu seconds -->\n\n",
216 rrd.rra_def[i].pdp_cnt,
217 rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
219 /* support for RRA parameters */
220 CB_PUTS("\t\t<params>\n");
222 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
225 CB_FMTS("\t\t<hw_alpha>%0.10e</hw_alpha>\n",
226 rrd.rra_def[i].par[RRA_hw_alpha].u_val);
228 CB_FMTS("\t\t<hw_beta>%0.10e</hw_beta>\n",
229 rrd.rra_def[i].par[RRA_hw_beta].u_val);
231 CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
232 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
236 CB_FMTS("\t\t<seasonal_gamma>%0.10e</seasonal_gamma>\n",
237 rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
239 CB_FMTS("\t\t<seasonal_smooth_idx>%lu</seasonal_smooth_idx>\n",
240 rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
242 if (atoi(rrd.stat_head->version) >= 4) {
243 CB_FMTS("\t\t<smoothing_window>%0.10e</smoothing_window>\n",
244 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val);
247 CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
248 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
251 CB_FMTS("\t\t<delta_pos>%0.10e</delta_pos>\n",
252 rrd.rra_def[i].par[RRA_delta_pos].u_val);
254 CB_FMTS("\t\t<delta_neg>%0.10e</delta_neg>\n",
255 rrd.rra_def[i].par[RRA_delta_neg].u_val);
257 CB_FMTS("\t\t<window_len>%lu</window_len>\n",
258 rrd.rra_def[i].par[RRA_window_len].u_cnt);
260 CB_FMTS("\t\t<failure_threshold>%lu</failure_threshold>\n",
261 rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
265 CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
266 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
273 CB_FMTS("\t\t<xff>%0.10e</xff>\n",
274 rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
278 CB_PUTS("\t\t</params>\n");
279 CB_PUTS("\t\t<cdp_prep>\n");
281 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
282 unsigned long ivalue;
284 CB_PUTS("\t\t\t<ds>\n");
285 /* support for exporting all CDP parameters */
286 /* parameters common to all CFs */
287 /* primary_val and secondary_val do not need to be saved between updates
288 * so strictly speaking they could be omitted.
289 * However, they can be useful for diagnostic purposes, so are included here. */
290 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
291 scratch[CDP_primary_val].u_val;
293 CB_PUTS("\t\t\t<primary_value>NaN</primary_value>\n");
295 CB_FMTS("\t\t\t<primary_value>%0.10e</primary_value>\n", value);
298 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
299 scratch[CDP_secondary_val].u_val;
301 CB_PUTS("\t\t\t<secondary_value>NaN</secondary_value>\n");
303 CB_FMTS("\t\t\t<secondary_value>%0.10e</secondary_value>\n", value);
306 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
309 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
310 scratch[CDP_hw_intercept].u_val;
312 CB_PUTS("\t\t\t<intercept>NaN</intercept>\n");
314 CB_FMTS("\t\t\t<intercept>%0.10e</intercept>\n", value);
317 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
318 scratch[CDP_hw_last_intercept].u_val;
320 CB_PUTS("\t\t\t<last_intercept>NaN</last_intercept>\n");
322 CB_FMTS("\t\t\t<last_intercept>%0.10e</last_intercept>\n", value);
325 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
326 scratch[CDP_hw_slope].u_val;
328 CB_PUTS("\t\t\t<slope>NaN</slope>\n");
330 CB_FMTS("\t\t\t<slope>%0.10e</slope>\n", value);
333 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
334 scratch[CDP_hw_last_slope].u_val;
336 CB_PUTS("\t\t\t<last_slope>NaN</last_slope>\n");
338 CB_FMTS("\t\t\t<last_slope>%0.10e</last_slope>\n", value);
341 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
342 scratch[CDP_null_count].u_cnt;
343 CB_FMTS("\t\t\t<nan_count>%lu</nan_count>\n", ivalue);
345 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
346 scratch[CDP_last_null_count].u_cnt;
347 CB_FMTS("\t\t\t<last_nan_count>%lu</last_nan_count>\n", ivalue);
351 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
352 scratch[CDP_hw_seasonal].u_val;
354 CB_PUTS("\t\t\t<seasonal>NaN</seasonal>\n");
356 CB_FMTS("\t\t\t<seasonal>%0.10e</seasonal>\n", value);
359 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
360 scratch[CDP_hw_last_seasonal].u_val;
362 CB_PUTS("\t\t\t<last_seasonal>NaN</last_seasonal>\n");
364 CB_FMTS("\t\t\t<last_seasonal>%0.10e</last_seasonal>\n", value);
367 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
368 scratch[CDP_init_seasonal].u_cnt;
369 CB_FMTS("\t\t\t<init_flag>%lu</init_flag>\n", ivalue);
376 char *violations_array = (char *) ((void *)
377 rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch);
378 CB_PUTS("\t\t\t<history>");
380 vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
382 CB_FMTS("%d", violations_array[vidx]);
384 CB_PUTS("</history>\n");
392 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch[CDP_val].u_val;
394 CB_PUTS("\t\t\t<value>NaN</value>\n");
396 CB_FMTS("\t\t\t<value>%0.10e</value>\n", value);
399 CB_FMTS("\t\t\t<unknown_datapoints>%lu</unknown_datapoints>\n",
400 rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
401 scratch[CDP_unkn_pdp_cnt].u_cnt);
404 CB_PUTS("\t\t\t</ds>\n");
406 CB_PUTS("\t\t</cdp_prep>\n");
408 CB_PUTS("\t\t<database>\n");
409 rrd_seek(rrd_file, (rra_start + (rrd.rra_ptr[i].cur_row + 1)
410 * rrd.stat_head->ds_cnt
411 * sizeof(rrd_value_t)), SEEK_SET);
412 timer = -(long)(rrd.rra_def[i].row_cnt - 1);
413 ii = rrd.rra_ptr[i].cur_row;
414 for (ix = 0; ix < rrd.rra_def[i].row_cnt; ix++) {
416 if (ii >= rrd.rra_def[i].row_cnt) {
417 rrd_seek(rrd_file, rra_start, SEEK_SET);
418 ii = 0; /* wrap if max row cnt is reached */
420 now = (rrd.live_head->last_up
421 - rrd.live_head->last_up
422 % (rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step))
423 + (timer * rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
427 localtime_r(&now, &tm);
428 strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
430 # error "Need strftime"
432 CB_FMTS("\t\t\t<!-- %s / %lld --> <row>", somestring, (long long) now);
433 for (iii = 0; iii < rrd.stat_head->ds_cnt; iii++) {
434 rrd_read(rrd_file, &my_cdp, sizeof(rrd_value_t) * 1);
436 CB_PUTS("<v>NaN</v>");
438 CB_FMTS("<v>%0.10e</v>", my_cdp);
443 CB_PUTS("\t\t</database>\n\t</rra>\n");
450 #ifdef HAVE_SETLOCALE
451 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);