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