a patch to avoid a memory leak and a Makefile.am patch to
[rrdtool.git] / src / rrd_dump.c
1 /*****************************************************************************
2  * RRDtool 1.1.x  Copyright Tobias Oetiker, 1997 - 2002
3  *****************************************************************************
4  * rrd_dump  Display a RRD
5  *****************************************************************************
6  * $Id$
7  * $Log$
8  * Revision 1.5  2003/02/13 07:05:27  oetiker
9  * Find attached the patch I promised to send to you. Please note that there
10  * are three new source files (src/rrd_is_thread_safe.h, src/rrd_thread_safe.c
11  * and src/rrd_not_thread_safe.c) and the introduction of librrd_th. This
12  * library is identical to librrd, but it contains support code for per-thread
13  * global variables currently used for error information only. This is similar
14  * to how errno per-thread variables are implemented.  librrd_th must be linked
15  * alongside of libpthred
16  *
17  * There is also a new file "THREADS", holding some documentation.
18  *
19  * -- Peter Stamfest <peter@stamfest.at>
20  *
21  * Revision 1.4  2002/02/01 20:34:49  oetiker
22  * fixed version number and date/time
23  *
24  * Revision 1.3  2001/03/10 23:54:39  oetiker
25  * Support for COMPUTE data sources (CDEF data sources). Removes the RPN
26  * parser and calculator from rrd_graph and puts then in a new file,
27  * rrd_rpncalc.c. Changes to core files rrd_create and rrd_update. Some
28  * clean-up of aberrant behavior stuff, including a bug fix.
29  * Documentation update (rrdcreate.pod, rrdupdate.pod). Change xml format.
30  * -- Jake Brutlag <jakeb@corp.webtv.net>
31  *
32  * Revision 1.2  2001/03/04 13:01:55  oetiker
33  *
34  * Revision 1.1.1.1  2001/02/25 22:25:05  oetiker
35  * checkin
36  *
37  *****************************************************************************/
38
39 #include "rrd_tool.h"
40 #include "rrd_rpncalc.h"
41
42 extern char *tzname[2];
43
44 int
45 rrd_dump(int argc, char **argv) 
46 {
47     int                 rc;
48
49     if (argc < 2) {
50         rrd_set_error("Not enough arguments");
51         return -1;
52     }
53
54     rc = rrd_dump_r(argv[1]);
55     
56     return rc;
57 }
58
59 int
60 rrd_dump_r(char *filename)    
61 {   
62     unsigned int i,ii,ix,iii=0;
63     time_t       now;
64     char         somestring[255];
65     rrd_value_t  my_cdp;
66     long         rra_base, rra_start, rra_next;
67     FILE        *in_file;
68     rrd_t        rrd;
69     rrd_value_t  value;
70     struct tm    tm;
71     if(rrd_open(filename, &in_file,&rrd, RRD_READONLY)==-1){
72         return(-1);
73     }
74
75     puts("<!-- Round Robin Database Dump -->");
76     puts("<rrd>");
77     printf("\t<version> %s </version>\n",RRD_VERSION);
78     printf("\t<step> %lu </step> <!-- Seconds -->\n",rrd.stat_head->pdp_step);
79 #if HAVE_STRFTIME
80     localtime_r(&rrd.live_head->last_up, &tm);
81     strftime(somestring,200,"%Y-%m-%d %H:%M:%S %Z",
82              &tm);
83 #else
84 # error "Need strftime"
85 #endif
86     printf("\t<lastupdate> %ld </lastupdate> <!-- %s -->\n\n",
87            rrd.live_head->last_up,somestring);
88     for(i=0;i<rrd.stat_head->ds_cnt;i++){
89         printf("\t<ds>\n");
90         printf("\t\t<name> %s </name>\n",rrd.ds_def[i].ds_nam);
91         printf("\t\t<type> %s </type>\n",rrd.ds_def[i].dst);
92     if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
93         printf("\t\t<minimal_heartbeat> %lu </minimal_heartbeat>\n",rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
94         if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)){
95           printf("\t\t<min> NaN </min>\n");
96         } else {
97           printf("\t\t<min> %0.10e </min>\n",rrd.ds_def[i].par[DS_min_val].u_val);
98         }
99         if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)){
100           printf("\t\t<max> NaN </max>\n");
101         } else {
102           printf("\t\t<max> %0.10e </max>\n",rrd.ds_def[i].par[DS_max_val].u_val);
103         }
104     } else { /* DST_CDEF */
105           char *str;
106           rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),rrd.ds_def,&str);
107           printf("\t\t<cdef> %s </cdef>\n", str);
108           free(str);
109         }
110         printf("\n\t\t<!-- PDP Status -->\n");
111         printf("\t\t<last_ds> %s </last_ds>\n",rrd.pdp_prep[i].last_ds);
112         if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)){
113           printf("\t\t<value> NaN </value>\n");
114         } else {
115           printf("\t\t<value> %0.10e </value>\n",rrd.pdp_prep[i].scratch[PDP_val].u_val);
116         }
117         printf("\t\t<unknown_sec> %lu </unknown_sec>\n",
118                rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
119         
120         printf("\t</ds>\n\n");
121     }
122
123     puts("<!-- Round Robin Archives -->");
124
125     rra_base=ftell(in_file);    
126     rra_next = rra_base;
127
128     for(i=0;i<rrd.stat_head->rra_cnt;i++){
129         
130         long timer=0;
131         rra_start= rra_next;
132         rra_next +=  ( rrd.stat_head->ds_cnt
133                       * rrd.rra_def[i].row_cnt
134                       * sizeof(rrd_value_t));
135         printf("\t<rra>\n");
136         printf("\t\t<cf> %s </cf>\n",rrd.rra_def[i].cf_nam);
137         printf("\t\t<pdp_per_row> %lu </pdp_per_row> <!-- %lu seconds -->\n\n",
138                rrd.rra_def[i].pdp_cnt, rrd.rra_def[i].pdp_cnt
139                *rrd.stat_head->pdp_step);
140         /* support for RRA parameters */
141         printf("\t\t<params>\n");
142         switch(cf_conv(rrd.rra_def[i].cf_nam)) {
143         case CF_HWPREDICT:
144            printf("\t\t<hw_alpha> %0.10e </hw_alpha>\n", 
145                   rrd.rra_def[i].par[RRA_hw_alpha].u_val);
146            printf("\t\t<hw_beta> %0.10e </hw_beta>\n", 
147                   rrd.rra_def[i].par[RRA_hw_beta].u_val);
148            printf("\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
149                   rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
150            break;
151         case CF_SEASONAL:
152         case CF_DEVSEASONAL:
153            printf("\t\t<seasonal_gamma> %0.10e </seasonal_gamma>\n", 
154                   rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
155            printf("\t\t<seasonal_smooth_idx> %lu </seasonal_smooth_idx>\n",
156                   rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
157            printf("\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
158                   rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
159            break;
160         case CF_FAILURES:
161            printf("\t\t<delta_pos> %0.10e </delta_pos>\n", 
162                   rrd.rra_def[i].par[RRA_delta_pos].u_val);
163            printf("\t\t<delta_neg> %0.10e </delta_neg>\n", 
164                   rrd.rra_def[i].par[RRA_delta_neg].u_val);
165            printf("\t\t<window_len> %lu </window_len>\n",
166                   rrd.rra_def[i].par[RRA_window_len].u_cnt);
167            printf("\t\t<failure_threshold> %lu </failure_threshold>\n",
168                   rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
169                   /* fall thru */
170         case CF_DEVPREDICT:
171            printf("\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
172                   rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
173            break;
174         case CF_AVERAGE:
175         case CF_MAXIMUM:
176         case CF_MINIMUM:
177         case CF_LAST:
178         default:
179            printf("\t\t<xff> %0.10e </xff>\n", rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
180            break;
181         }
182         printf("\t\t</params>\n");
183         printf("\t\t<cdp_prep>\n");
184         for(ii=0;ii<rrd.stat_head->ds_cnt;ii++){
185                 unsigned long ivalue;
186                 printf("\t\t\t<ds>\n");
187                 /* support for exporting all CDP parameters */
188                 /* parameters common to all CFs */
189                     /* primary_val and secondary_val do not need to be saved between updates
190                          * so strictly speaking they could be omitted.
191                          * However, they can be useful for diagnostic purposes, so are included here. */
192                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt
193                            +ii].scratch[CDP_primary_val].u_val;
194                         if (isnan(value)) {
195                            printf("\t\t\t<primary_value> NaN </primary_value>\n");
196                         } else {
197                            printf("\t\t\t<primary_value> %0.10e </primary_value>\n", value);
198                         }
199                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_secondary_val].u_val;
200                         if (isnan(value)) {
201                            printf("\t\t\t<secondary_value> NaN </secondary_value>\n");
202                         } else {
203                            printf("\t\t\t<secondary_value> %0.10e </secondary_value>\n", value);
204                         }
205                 switch(cf_conv(rrd.rra_def[i].cf_nam)) {
206                 case CF_HWPREDICT:
207                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_intercept].u_val;
208                         if (isnan(value)) {
209                            printf("\t\t\t<intercept> NaN </intercept>\n");
210                         } else {
211                            printf("\t\t\t<intercept> %0.10e </intercept>\n", value);
212                         }
213                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_last_intercept].u_val;
214                         if (isnan(value)) {
215                            printf("\t\t\t<last_intercept> NaN </last_intercept>\n");
216                         } else {
217                            printf("\t\t\t<last_intercept> %0.10e </last_intercept>\n", value);
218                         }
219                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_slope].u_val;
220                         if (isnan(value)) {
221                            printf("\t\t\t<slope> NaN </slope>\n");
222                         } else {
223                            printf("\t\t\t<slope> %0.10e </slope>\n", value);
224                         }
225                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_last_slope].u_val;
226                         if (isnan(value)) {
227                            printf("\t\t\t<last_slope> NaN </last_slope>\n");
228                         } else {
229                            printf("\t\t\t<last_slope> %0.10e </last_slope>\n", value);
230                         }
231                         ivalue = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_null_count].u_cnt;
232                         printf("\t\t\t<nan_count> %lu </nan_count>\n", ivalue);
233                         ivalue = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_last_null_count].u_cnt;
234                         printf("\t\t\t<last_nan_count> %lu </last_nan_count>\n", ivalue);
235                         break;
236                 case CF_SEASONAL:
237                 case CF_DEVSEASONAL:
238                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_seasonal].u_val;
239                         if (isnan(value)) {
240                            printf("\t\t\t<seasonal> NaN </seasonal>\n");
241                         } else {
242                            printf("\t\t\t<seasonal> %0.10e </seasonal>\n", value);
243                         }
244                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_last_seasonal].u_val;
245                         if (isnan(value)) {
246                            printf("\t\t\t<last_seasonal> NaN </last_seasonal>\n");
247                         } else {
248                            printf("\t\t\t<last_seasonal> %0.10e </last_seasonal>\n", value);
249                         }
250                 ivalue = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_init_seasonal].u_cnt;
251                         printf("\t\t\t<init_flag> %lu </init_flag>\n", ivalue);
252                         break;
253                 case CF_DEVPREDICT:
254                         break;
255                 case CF_FAILURES:
256                     {
257             unsigned short vidx;
258                         char *violations_array = (char *) ((void*) 
259                            rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch);
260                         printf("\t\t\t<history> ");
261                         for (vidx = 0; vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt; ++vidx)
262                         {
263                                 printf("%d",violations_array[vidx]);
264                         }
265                         printf(" </history>\n");
266                         }
267                         break;
268                 case CF_AVERAGE:
269                 case CF_MAXIMUM:
270                 case CF_MINIMUM:
271                 case CF_LAST:
272                 default:
273                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_val].u_val;
274                         if (isnan(value)) {
275                            printf("\t\t\t<value> NaN </value>\n");
276                         } else {
277                            printf("\t\t\t<value> %0.10e </value>\n", value);
278                         }
279                     printf("\t\t\t<unknown_datapoints> %lu </unknown_datapoints>\n",
280                        rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_unkn_pdp_cnt].u_cnt);
281                         break;
282                 }
283         printf("\t\t\t</ds>\n");         
284     }
285         printf("\t\t</cdp_prep>\n");
286
287         printf("\t\t<database>\n");
288         fseek(in_file,(rra_start
289                        +(rrd.rra_ptr[i].cur_row+1)
290                        * rrd.stat_head->ds_cnt
291                        * sizeof(rrd_value_t)),SEEK_SET);
292         timer = - (rrd.rra_def[i].row_cnt-1);
293         ii=rrd.rra_ptr[i].cur_row;
294         for(ix=0;ix<rrd.rra_def[i].row_cnt;ix++){           
295             ii++;
296             if (ii>=rrd.rra_def[i].row_cnt) {
297                 fseek(in_file,rra_start,SEEK_SET);
298                 ii=0; /* wrap if max row cnt is reached */
299             }
300             now = (rrd.live_head->last_up 
301                    - rrd.live_head->last_up 
302                    % (rrd.rra_def[i].pdp_cnt*rrd.stat_head->pdp_step)) 
303                 + (timer*rrd.rra_def[i].pdp_cnt*rrd.stat_head->pdp_step);
304
305             timer++;
306 #if HAVE_STRFTIME
307             localtime_r(&now, &tm);
308             strftime(somestring,200,"%Y-%m-%d %H:%M:%S %Z", &tm);
309 #else
310 # error "Need strftime"
311 #endif
312             printf("\t\t\t<!-- %s / %d --> <row>",somestring,(int)now);
313             for(iii=0;iii<rrd.stat_head->ds_cnt;iii++){                  
314                 fread(&my_cdp,sizeof(rrd_value_t),1,in_file);           
315                 if (isnan(my_cdp)){
316                   printf("<v> NaN </v>");
317                 } else {
318                   printf("<v> %0.10e </v>",my_cdp);
319                 };
320             }
321             printf("</row>\n");
322         }
323         printf("\t\t</database>\n\t</rra>\n");
324         
325     }
326     printf("</rrd>\n");
327     rrd_free(&rrd);
328     fclose(in_file);
329     return(0);
330 }
331
332
333
334