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