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