fixes for compiling rrdtool with win32 mingw and netware -- Guenter Knauf <gk with...
[rrdtool.git] / src / rrd_dump.c
1 /*****************************************************************************
2  * RRDtool 1.2.11  Copyright by Tobi Oetiker, 1997-2005
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
45 #include "rrd_tool.h"
46 #include "rrd_rpncalc.h"
47
48 #ifndef NETWARE
49 extern char *tzname[2];
50 #endif
51
52 int
53 rrd_dump(int argc, char **argv) 
54 {
55     int                 rc;
56
57     if (argc < 2) {
58         rrd_set_error("Not enough arguments");
59         return -1;
60     }
61
62     rc = rrd_dump_r(argv[1]);
63     
64     return rc;
65 }
66
67 int
68 rrd_dump_r(char *filename)    
69 {   
70     unsigned int i,ii,ix,iii=0;
71     time_t       now;
72     char         somestring[255];
73     rrd_value_t  my_cdp;
74     long         rra_base, rra_start, rra_next;
75     FILE        *in_file;
76     rrd_t        rrd;
77     rrd_value_t  value;
78     struct tm    tm;
79     if(rrd_open(filename, &in_file,&rrd, RRD_READONLY)==-1){
80         rrd_free(&rrd);
81         return(-1);
82     }
83
84     puts("<!-- Round Robin Database Dump -->");
85     puts("<rrd>");
86     printf("\t<version> %s </version>\n",RRD_VERSION);
87     printf("\t<step> %lu </step> <!-- Seconds -->\n",rrd.stat_head->pdp_step);
88 #if HAVE_STRFTIME
89     localtime_r(&rrd.live_head->last_up, &tm);
90     strftime(somestring,200,"%Y-%m-%d %H:%M:%S %Z",
91              &tm);
92 #else
93 # error "Need strftime"
94 #endif
95     printf("\t<lastupdate> %ld </lastupdate> <!-- %s -->\n\n",
96            rrd.live_head->last_up,somestring);
97     for(i=0;i<rrd.stat_head->ds_cnt;i++){
98         printf("\t<ds>\n");
99         printf("\t\t<name> %s </name>\n",rrd.ds_def[i].ds_nam);
100         printf("\t\t<type> %s </type>\n",rrd.ds_def[i].dst);
101     if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
102         printf("\t\t<minimal_heartbeat> %lu </minimal_heartbeat>\n",rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
103         if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)){
104           printf("\t\t<min> NaN </min>\n");
105         } else {
106           printf("\t\t<min> %0.10e </min>\n",rrd.ds_def[i].par[DS_min_val].u_val);
107         }
108         if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)){
109           printf("\t\t<max> NaN </max>\n");
110         } else {
111           printf("\t\t<max> %0.10e </max>\n",rrd.ds_def[i].par[DS_max_val].u_val);
112         }
113     } else { /* DST_CDEF */
114           char *str;
115           rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),rrd.ds_def,&str);
116           printf("\t\t<cdef> %s </cdef>\n", str);
117           free(str);
118         }
119         printf("\n\t\t<!-- PDP Status -->\n");
120         printf("\t\t<last_ds> %s </last_ds>\n",rrd.pdp_prep[i].last_ds);
121         if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)){
122           printf("\t\t<value> NaN </value>\n");
123         } else {
124           printf("\t\t<value> %0.10e </value>\n",rrd.pdp_prep[i].scratch[PDP_val].u_val);
125         }
126         printf("\t\t<unknown_sec> %lu </unknown_sec>\n",
127                rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
128         
129         printf("\t</ds>\n\n");
130     }
131
132     puts("<!-- Round Robin Archives -->");
133
134     rra_base=ftell(in_file);    
135     rra_next = rra_base;
136
137     for(i=0;i<rrd.stat_head->rra_cnt;i++){
138         
139         long timer=0;
140         rra_start= rra_next;
141         rra_next +=  ( rrd.stat_head->ds_cnt
142                       * rrd.rra_def[i].row_cnt
143                       * sizeof(rrd_value_t));
144         printf("\t<rra>\n");
145         printf("\t\t<cf> %s </cf>\n",rrd.rra_def[i].cf_nam);
146         printf("\t\t<pdp_per_row> %lu </pdp_per_row> <!-- %lu seconds -->\n\n",
147                rrd.rra_def[i].pdp_cnt, rrd.rra_def[i].pdp_cnt
148                *rrd.stat_head->pdp_step);
149         /* support for RRA parameters */
150         printf("\t\t<params>\n");
151         switch(cf_conv(rrd.rra_def[i].cf_nam)) {
152         case CF_HWPREDICT:
153            printf("\t\t<hw_alpha> %0.10e </hw_alpha>\n", 
154                   rrd.rra_def[i].par[RRA_hw_alpha].u_val);
155            printf("\t\t<hw_beta> %0.10e </hw_beta>\n", 
156                   rrd.rra_def[i].par[RRA_hw_beta].u_val);
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_SEASONAL:
161         case CF_DEVSEASONAL:
162            printf("\t\t<seasonal_gamma> %0.10e </seasonal_gamma>\n", 
163                   rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
164            printf("\t\t<seasonal_smooth_idx> %lu </seasonal_smooth_idx>\n",
165                   rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
166            printf("\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
167                   rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
168            break;
169         case CF_FAILURES:
170            printf("\t\t<delta_pos> %0.10e </delta_pos>\n", 
171                   rrd.rra_def[i].par[RRA_delta_pos].u_val);
172            printf("\t\t<delta_neg> %0.10e </delta_neg>\n", 
173                   rrd.rra_def[i].par[RRA_delta_neg].u_val);
174            printf("\t\t<window_len> %lu </window_len>\n",
175                   rrd.rra_def[i].par[RRA_window_len].u_cnt);
176            printf("\t\t<failure_threshold> %lu </failure_threshold>\n",
177                   rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
178                   /* fall thru */
179         case CF_DEVPREDICT:
180            printf("\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
181                   rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
182            break;
183         case CF_AVERAGE:
184         case CF_MAXIMUM:
185         case CF_MINIMUM:
186         case CF_LAST:
187         default:
188            printf("\t\t<xff> %0.10e </xff>\n", rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
189            break;
190         }
191         printf("\t\t</params>\n");
192         printf("\t\t<cdp_prep>\n");
193         for(ii=0;ii<rrd.stat_head->ds_cnt;ii++){
194                 unsigned long ivalue;
195                 printf("\t\t\t<ds>\n");
196                 /* support for exporting all CDP parameters */
197                 /* parameters common to all CFs */
198                     /* primary_val and secondary_val do not need to be saved between updates
199                          * so strictly speaking they could be omitted.
200                          * However, they can be useful for diagnostic purposes, so are included here. */
201                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt
202                            +ii].scratch[CDP_primary_val].u_val;
203                         if (isnan(value)) {
204                            printf("\t\t\t<primary_value> NaN </primary_value>\n");
205                         } else {
206                            printf("\t\t\t<primary_value> %0.10e </primary_value>\n", value);
207                         }
208                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_secondary_val].u_val;
209                         if (isnan(value)) {
210                            printf("\t\t\t<secondary_value> NaN </secondary_value>\n");
211                         } else {
212                            printf("\t\t\t<secondary_value> %0.10e </secondary_value>\n", value);
213                         }
214                 switch(cf_conv(rrd.rra_def[i].cf_nam)) {
215                 case CF_HWPREDICT:
216                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_intercept].u_val;
217                         if (isnan(value)) {
218                            printf("\t\t\t<intercept> NaN </intercept>\n");
219                         } else {
220                            printf("\t\t\t<intercept> %0.10e </intercept>\n", value);
221                         }
222                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_last_intercept].u_val;
223                         if (isnan(value)) {
224                            printf("\t\t\t<last_intercept> NaN </last_intercept>\n");
225                         } else {
226                            printf("\t\t\t<last_intercept> %0.10e </last_intercept>\n", value);
227                         }
228                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_slope].u_val;
229                         if (isnan(value)) {
230                            printf("\t\t\t<slope> NaN </slope>\n");
231                         } else {
232                            printf("\t\t\t<slope> %0.10e </slope>\n", value);
233                         }
234                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_last_slope].u_val;
235                         if (isnan(value)) {
236                            printf("\t\t\t<last_slope> NaN </last_slope>\n");
237                         } else {
238                            printf("\t\t\t<last_slope> %0.10e </last_slope>\n", value);
239                         }
240                         ivalue = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_null_count].u_cnt;
241                         printf("\t\t\t<nan_count> %lu </nan_count>\n", ivalue);
242                         ivalue = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_last_null_count].u_cnt;
243                         printf("\t\t\t<last_nan_count> %lu </last_nan_count>\n", ivalue);
244                         break;
245                 case CF_SEASONAL:
246                 case CF_DEVSEASONAL:
247                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_seasonal].u_val;
248                         if (isnan(value)) {
249                            printf("\t\t\t<seasonal> NaN </seasonal>\n");
250                         } else {
251                            printf("\t\t\t<seasonal> %0.10e </seasonal>\n", value);
252                         }
253                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_last_seasonal].u_val;
254                         if (isnan(value)) {
255                            printf("\t\t\t<last_seasonal> NaN </last_seasonal>\n");
256                         } else {
257                            printf("\t\t\t<last_seasonal> %0.10e </last_seasonal>\n", value);
258                         }
259                 ivalue = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_init_seasonal].u_cnt;
260                         printf("\t\t\t<init_flag> %lu </init_flag>\n", ivalue);
261                         break;
262                 case CF_DEVPREDICT:
263                         break;
264                 case CF_FAILURES:
265                     {
266             unsigned short vidx;
267                         char *violations_array = (char *) ((void*) 
268                            rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch);
269                         printf("\t\t\t<history> ");
270                         for (vidx = 0; vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt; ++vidx)
271                         {
272                                 printf("%d",violations_array[vidx]);
273                         }
274                         printf(" </history>\n");
275                         }
276                         break;
277                 case CF_AVERAGE:
278                 case CF_MAXIMUM:
279                 case CF_MINIMUM:
280                 case CF_LAST:
281                 default:
282                 value = rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_val].u_val;
283                         if (isnan(value)) {
284                            printf("\t\t\t<value> NaN </value>\n");
285                         } else {
286                            printf("\t\t\t<value> %0.10e </value>\n", value);
287                         }
288                     printf("\t\t\t<unknown_datapoints> %lu </unknown_datapoints>\n",
289                        rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_unkn_pdp_cnt].u_cnt);
290                         break;
291                 }
292         printf("\t\t\t</ds>\n");         
293     }
294         printf("\t\t</cdp_prep>\n");
295
296         printf("\t\t<database>\n");
297         fseek(in_file,(rra_start
298                        +(rrd.rra_ptr[i].cur_row+1)
299                        * rrd.stat_head->ds_cnt
300                        * sizeof(rrd_value_t)),SEEK_SET);
301         timer = - (rrd.rra_def[i].row_cnt-1);
302         ii=rrd.rra_ptr[i].cur_row;
303         for(ix=0;ix<rrd.rra_def[i].row_cnt;ix++){           
304             ii++;
305             if (ii>=rrd.rra_def[i].row_cnt) {
306                 fseek(in_file,rra_start,SEEK_SET);
307                 ii=0; /* wrap if max row cnt is reached */
308             }
309             now = (rrd.live_head->last_up 
310                    - rrd.live_head->last_up 
311                    % (rrd.rra_def[i].pdp_cnt*rrd.stat_head->pdp_step)) 
312                 + (timer*rrd.rra_def[i].pdp_cnt*rrd.stat_head->pdp_step);
313
314             timer++;
315 #if HAVE_STRFTIME
316             localtime_r(&now, &tm);
317             strftime(somestring,200,"%Y-%m-%d %H:%M:%S %Z", &tm);
318 #else
319 # error "Need strftime"
320 #endif
321             printf("\t\t\t<!-- %s / %d --> <row>",somestring,(int)now);
322             for(iii=0;iii<rrd.stat_head->ds_cnt;iii++){                  
323                 fread(&my_cdp,sizeof(rrd_value_t),1,in_file);           
324                 if (isnan(my_cdp)){
325                   printf("<v> NaN </v>");
326                 } else {
327                   printf("<v> %0.10e </v>",my_cdp);
328                 };
329             }
330             printf("</row>\n");
331         }
332         printf("\t\t</database>\n\t</rra>\n");
333         
334     }
335     printf("</rrd>\n");
336     rrd_free(&rrd);
337     fclose(in_file);
338     return(0);
339 }
340
341
342
343