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