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