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