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