a206230a1b6a5fdda28eacae4f63ed8bb1d50e6c
[rrdtool.git] / src / rrd_dump.c
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"
47
48 #ifdef HAVE_LOCALE_H
49 #include <locale.h>
50 #endif
51
52 #if !(defined(NETWARE) || defined(WIN32))
53 extern char *tzname[2];
54 #endif
55
56 //Local prototypes
57 size_t rrd_dump_opt_cb_fileout(
58     const void *data,
59     size_t len,
60     void *user);
61
62
63 int rrd_dump_cb_r(
64     const char *filename,
65     int opt_header,
66     rrd_output_callback_t cb,
67     void *user)
68 {
69     unsigned int i, ii, ix, iii = 0;
70     time_t    now;
71     char      somestring[255];
72     rrd_value_t my_cdp;
73     off_t     rra_base, rra_start, rra_next;
74     rrd_file_t *rrd_file;
75     rrd_t     rrd;
76     rrd_value_t value;
77     struct tm tm;
78     char *old_locale = "";
79
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 {                                       \
85     char buffer[256];                                           \
86     snprintf (buffer, sizeof(buffer), __VA_ARGS__);             \
87     CB_PUTS (buffer);                                           \
88     } while (0)
89 //These macros are to be undefined at the end of this function
90
91     //Check if we got a (valid) callback method
92     if (!cb) {
93         return (-1);
94     }
95
96     rrd_init(&rrd);
97
98     rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_READAHEAD);
99     if (rrd_file == NULL) {
100         rrd_free(&rrd);
101         return (-1);
102     }
103
104 #ifdef HAVE_SETLOCALE
105     old_locale = setlocale(LC_NUMERIC, "C");
106 #endif
107
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");
112         CB_PUTS("<rrd>\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");
120     } else {
121         CB_PUTS("<!-- Round Robin Database Dump -->\n");
122         CB_PUTS("<rrd>\n");
123     }
124
125     if (atoi(rrd.stat_head->version) <= 3) {
126         CB_FMTS("\t<version>%s</version>\n", RRD_VERSION3);
127     } else {
128         CB_FMTS("\t<version>%s</version>\n", RRD_VERSION);
129     }
130     
131     CB_FMTS("\t<step>%lu</step> <!-- Seconds -->\n",
132         rrd.stat_head->pdp_step);
133
134 #ifdef HAVE_STRFTIME
135     localtime_r(&rrd.live_head->last_up, &tm);
136     strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
137 #else
138 # error "Need strftime"
139 #endif
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++) {
143         CB_PUTS("\t<ds>\n");
144
145         CB_FMTS("\t\t<name> %s </name>\n", rrd.ds_def[i].ds_nam);
146
147         CB_FMTS("\t\t<type> %s </type>\n", rrd.ds_def[i].dst);
148
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);
152
153             if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)) {
154                 CB_PUTS("\t\t<min>NaN</min>\n");
155             } else {
156                 CB_FMTS("\t\t<min>%0.10e</min>\n",
157                     rrd.ds_def[i].par[DS_min_val].u_val);
158             }
159             
160             if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)) {
161                 CB_PUTS("\t\t<max>NaN</max>\n");
162             } else {
163                 CB_FMTS("\t\t<max>%0.10e</max>\n",
164                     rrd.ds_def[i].par[DS_max_val].u_val);
165             }
166         } else {        /* DST_CDEF */
167             char     *str = NULL;
168
169             rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
170                 rrd.ds_def, &str);
171
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> ");
175             CB_PUTS(str);
176             CB_PUTS(" </cdef>\n");
177
178             free(str);
179         }
180
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);
184
185         if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)) {
186             CB_PUTS("\t\t<value>NaN</value>\n");
187         } else {
188             CB_FMTS("\t\t<value>%0.10e</value>\n",
189                 rrd.pdp_prep[i].scratch[PDP_val].u_val);
190         }
191
192         CB_FMTS("\t\t<unknown_sec> %lu </unknown_sec>\n",
193             rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
194
195         CB_PUTS("\t</ds>\n\n");
196     }
197
198     CB_PUTS("\t<!-- Round Robin Archives -->\n");
199
200     rra_base = rrd_file->header_len;
201     rra_next = rra_base;
202
203     for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
204
205         long      timer = 0;
206
207         rra_start = rra_next;
208         rra_next += (rrd.stat_head->ds_cnt
209                      * rrd.rra_def[i].row_cnt * sizeof(rrd_value_t));
210
211         CB_PUTS("\t<rra>\n");
212
213         CB_FMTS("\t\t<cf>%s</cf>\n", rrd.rra_def[i].cf_nam);
214
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);
218
219         /* support for RRA parameters */
220         CB_PUTS("\t\t<params>\n");
221
222         switch (cf_conv(rrd.rra_def[i].cf_nam)) {
223         case CF_HWPREDICT:
224         case CF_MHWPREDICT:
225             CB_FMTS("\t\t<hw_alpha>%0.10e</hw_alpha>\n",
226                 rrd.rra_def[i].par[RRA_hw_alpha].u_val);
227
228             CB_FMTS("\t\t<hw_beta>%0.10e</hw_beta>\n",
229                 rrd.rra_def[i].par[RRA_hw_beta].u_val);
230
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);
233             break;
234         case CF_SEASONAL:
235         case CF_DEVSEASONAL:
236             CB_FMTS("\t\t<seasonal_gamma>%0.10e</seasonal_gamma>\n",
237                 rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
238
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);
241
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);
245             }
246
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);
249             break;
250         case CF_FAILURES:
251             CB_FMTS("\t\t<delta_pos>%0.10e</delta_pos>\n",
252                 rrd.rra_def[i].par[RRA_delta_pos].u_val);
253
254             CB_FMTS("\t\t<delta_neg>%0.10e</delta_neg>\n",
255                 rrd.rra_def[i].par[RRA_delta_neg].u_val);
256
257             CB_FMTS("\t\t<window_len>%lu</window_len>\n",
258                 rrd.rra_def[i].par[RRA_window_len].u_cnt);
259
260             CB_FMTS("\t\t<failure_threshold>%lu</failure_threshold>\n",
261                 rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
262
263             /* fall thru */
264         case CF_DEVPREDICT:
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);
267             break;
268         case CF_AVERAGE:
269         case CF_MAXIMUM:
270         case CF_MINIMUM:
271         case CF_LAST:
272         default:
273             CB_FMTS("\t\t<xff>%0.10e</xff>\n",
274                 rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
275             break;
276         }
277
278         CB_PUTS("\t\t</params>\n");
279         CB_PUTS("\t\t<cdp_prep>\n");
280
281         for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
282             unsigned long ivalue;
283
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;
292             if (isnan(value)) {
293                 CB_PUTS("\t\t\t<primary_value>NaN</primary_value>\n");
294             } else {
295                 CB_FMTS("\t\t\t<primary_value>%0.10e</primary_value>\n", value);
296             }
297
298             value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
299                 scratch[CDP_secondary_val].u_val;
300             if (isnan(value)) {
301                 CB_PUTS("\t\t\t<secondary_value>NaN</secondary_value>\n");
302             } else {
303                 CB_FMTS("\t\t\t<secondary_value>%0.10e</secondary_value>\n", value);
304             }
305
306             switch (cf_conv(rrd.rra_def[i].cf_nam)) {
307             case CF_HWPREDICT:
308             case CF_MHWPREDICT:
309                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
310                     scratch[CDP_hw_intercept].u_val;
311                 if (isnan(value)) {
312                     CB_PUTS("\t\t\t<intercept>NaN</intercept>\n");
313                 } else {
314                     CB_FMTS("\t\t\t<intercept>%0.10e</intercept>\n", value);
315                 }
316
317                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
318                     scratch[CDP_hw_last_intercept].u_val;
319                 if (isnan(value)) {
320                     CB_PUTS("\t\t\t<last_intercept>NaN</last_intercept>\n");
321                 } else {
322                     CB_FMTS("\t\t\t<last_intercept>%0.10e</last_intercept>\n", value);
323                 }
324
325                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
326                     scratch[CDP_hw_slope].u_val;
327                 if (isnan(value)) {
328                     CB_PUTS("\t\t\t<slope>NaN</slope>\n");
329                 } else {
330                     CB_FMTS("\t\t\t<slope>%0.10e</slope>\n", value);
331                 }
332
333                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
334                     scratch[CDP_hw_last_slope].u_val;
335                 if (isnan(value)) {
336                     CB_PUTS("\t\t\t<last_slope>NaN</last_slope>\n");
337                 } else {
338                     CB_FMTS("\t\t\t<last_slope>%0.10e</last_slope>\n", value);
339                 }
340
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);
344
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);
348                 break;
349             case CF_SEASONAL:
350             case CF_DEVSEASONAL:
351                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
352                     scratch[CDP_hw_seasonal].u_val;
353                 if (isnan(value)) {
354                     CB_PUTS("\t\t\t<seasonal>NaN</seasonal>\n");
355                 } else {
356                     CB_FMTS("\t\t\t<seasonal>%0.10e</seasonal>\n", value);
357                 }
358
359                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
360                     scratch[CDP_hw_last_seasonal].u_val;
361                 if (isnan(value)) {
362                     CB_PUTS("\t\t\t<last_seasonal>NaN</last_seasonal>\n");
363                 } else {
364                     CB_FMTS("\t\t\t<last_seasonal>%0.10e</last_seasonal>\n", value);
365                 }
366
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);
370                 break;
371             case CF_DEVPREDICT:
372                 break;
373             case CF_FAILURES:
374             {
375                 unsigned short vidx;
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>");
379                 for (vidx = 0;
380                     vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
381                     ++vidx) {
382                     CB_FMTS("%d", violations_array[vidx]);
383                 }
384                 CB_PUTS("</history>\n");
385             }
386                 break;
387             case CF_AVERAGE:
388             case CF_MAXIMUM:
389             case CF_MINIMUM:
390             case CF_LAST:
391             default:
392                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch[CDP_val].u_val;
393                 if (isnan(value)) {
394                     CB_PUTS("\t\t\t<value>NaN</value>\n");
395                 } else {
396                     CB_FMTS("\t\t\t<value>%0.10e</value>\n", value);
397                 }
398
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);
402                 break;
403             }
404             CB_PUTS("\t\t\t</ds>\n");
405         }
406         CB_PUTS("\t\t</cdp_prep>\n");
407
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++) {
415             ii++;
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 */
419             }
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);
424
425             timer++;
426 #if HAVE_STRFTIME
427             localtime_r(&now, &tm);
428             strftime(somestring, 255, "%Y-%m-%d %H:%M:%S %Z", &tm);
429 #else
430 # error "Need strftime"
431 #endif
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);
435                 if (isnan(my_cdp)) {
436                     CB_PUTS("<v>NaN</v>");
437                 } else {
438                     CB_FMTS("<v>%0.10e</v>", my_cdp);
439                 }
440             }
441             CB_PUTS("</row>\n");
442         }
443         CB_PUTS("\t\t</database>\n\t</rra>\n");
444     }
445
446     CB_PUTS("</rrd>\n");
447
448     rrd_free(&rrd);
449
450 #ifdef HAVE_SETLOCALE
451     setlocale(LC_NUMERIC, old_locale);
452 #endif
453
454     return rrd_close(rrd_file);
455
456 //Undefining the previously defined shortcuts
457 //See start of this function
458 #undef CB_PUTS
459 #undef CB_FMTS
460 //End of macro undefining
461
462 }
463
464 size_t rrd_dump_opt_cb_fileout(
465     const void *data,
466     size_t len,
467     void *user)
468 {
469     return fwrite(data, 1, len, (FILE *)user);
470 }
471
472 int rrd_dump_opt_r(
473     const char *filename,
474     char *outname,
475     int opt_noheader)
476 {
477     FILE     *out_file;
478     int       res;
479
480     out_file = NULL;
481     if (outname) {
482         if (!(out_file = fopen(outname, "w"))) {
483             return (-1);
484         }
485     } else {
486         out_file = stdout;
487     }
488
489     res = rrd_dump_cb_r(filename, opt_noheader, rrd_dump_opt_cb_fileout, (void *)out_file);
490
491     if (out_file != stdout) {
492         fclose(out_file);
493     }
494
495     return res;
496 }
497
498 /* backward compatibility with 1.2.x */
499 int rrd_dump_r(
500     const char *filename,
501     char *outname)
502 {
503     return rrd_dump_opt_r(filename, outname, 0);
504 }
505
506 int rrd_dump(
507     int argc,
508     char **argv)
509 {
510     int       rc;
511     /** 
512      * 0 = no header
513      * 1 = dtd header
514      * 2 = xsd header
515      */
516     int       opt_header = 1;
517     char     *opt_daemon = NULL;
518
519     /* init rrd clean */
520
521     optind = 0;
522     opterr = 0;         /* initialize getopt */
523
524     while (42) {/* ha ha */
525         int       opt;
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'},
531             {0, 0, 0, 0}
532         };
533
534         opt = getopt_long(argc, argv, "d:h:n", long_options, &option_index);
535
536         if (opt == EOF)
537             break;
538
539         switch (opt) {
540         case 'd':
541             if (opt_daemon != NULL)
542                     free (opt_daemon);
543             opt_daemon = strdup (optarg);
544             if (opt_daemon == NULL)
545             {
546                 rrd_set_error ("strdup failed.");
547                 return (-1);
548             }
549             break;
550
551         case 'n':
552            opt_header = 0;
553            break;
554
555         case 'h':
556            if (strcmp(optarg, "dtd") == 0) {
557                 opt_header = 1;
558            } else if (strcmp(optarg, "xsd") == 0) {
559                 opt_header = 2;
560            } else if (strcmp(optarg, "none") == 0) {
561                 opt_header = 0;
562            }
563            break;
564
565         default:
566             rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}] [--no-header]"
567                           "file.rrd [file.xml]", argv[0]);
568             return (-1);
569             break;
570         }
571     }                   /* while (42) */
572
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]);
576         return (-1);
577     }
578
579     rc = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
580     if (opt_daemon) free(opt_daemon);
581     if (rc) return (rc);
582
583     if ((argc - optind) == 2) {
584         rc = rrd_dump_opt_r(argv[optind], argv[optind + 1], opt_header);
585     } else {
586         rc = rrd_dump_opt_r(argv[optind], NULL, opt_header);
587     }
588
589     return rc;
590 }