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