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