prepare for the release of rrdtool-1.2.18
[rrdtool.git] / src / rrd_info.c
1 /*****************************************************************************
2  * RRDtool 1.2.18  Copyright by Tobi Oetiker, 1997-2006
3  *****************************************************************************
4  * rrd_info  Get Information about the configuration of an RRD
5  *****************************************************************************/
6
7 #include "rrd_tool.h"
8 #include "rrd_rpncalc.h"
9 #include <stdarg.h>
10
11 /* proto */
12 info_t *rrd_info(int, char **);
13 info_t *rrd_info_r(char *filename);
14
15 /* allocate memory for string */
16 char *
17 sprintf_alloc(char *fmt, ...) {
18 #ifdef HAVE_VSNPRINTF    
19     int maxlen = 50;
20 #else
21     int maxlen = 1000;
22 #endif
23     char *str = NULL;
24     va_list argp;
25     str = malloc(sizeof(char)*(strlen(fmt)+maxlen));
26     if (str != NULL) {
27         va_start(argp, fmt);
28 #ifdef HAVE_VSNPRINTF
29         vsnprintf(str, maxlen-1, fmt, argp);
30 #else
31         vsprintf(str, fmt, argp);
32 #endif
33     }
34     va_end(argp);
35     return str;
36 }
37 /* the function formerly known as push was renamed info_push because
38  * it is now used outside the scope of this file */
39 info_t 
40 *info_push(info_t *info, char *key, enum info_type type, infoval value){
41     info_t *next;
42     next = malloc(sizeof(*next));
43     next->next = (info_t *) 0;
44     if( info )
45         info->next = next;
46     next->type = type;
47     next->key  = key;
48     switch (type) {
49     case RD_I_VAL:
50         next->value.u_val = value.u_val;
51         break;
52     case RD_I_CNT:
53         next->value.u_cnt = value.u_cnt;
54         break;
55     case RD_I_INT:
56         next->value.u_int = value.u_int;
57         break;
58     case RD_I_STR:
59         next->value.u_str = malloc(sizeof(char)*(strlen(value.u_str)+1));
60         strcpy(next->value.u_str,value.u_str);
61         break;
62     }
63     return(next);
64 }
65
66
67 info_t *
68 rrd_info(int argc, char **argv) {   
69     info_t             *info;
70
71     if(argc < 2){
72         rrd_set_error("please specify an rrd");
73         return NULL;
74     }
75
76     info = rrd_info_r(argv[1]);
77
78     return(info);
79 }
80
81
82   
83 info_t *
84 rrd_info_r(char *filename) {   
85     unsigned int i,ii=0;
86     FILE         *in_file;
87     rrd_t        rrd;
88     info_t       *data,*cd;
89     infoval      info;
90         enum cf_en   current_cf;
91         enum dst_en  current_ds;
92
93     if(rrd_open(filename,&in_file,&rrd, RRD_READONLY)==-1){
94         return(NULL);
95     }
96     fclose(in_file);
97
98     info.u_str=filename;
99     cd=info_push(NULL,sprintf_alloc("filename"),    RD_I_STR, info);
100     data=cd;
101
102     info.u_str=rrd.stat_head->version;
103     cd=info_push(cd,sprintf_alloc("rrd_version"),    RD_I_STR, info);
104
105     info.u_cnt=rrd.stat_head->pdp_step;
106     cd=info_push(cd,sprintf_alloc("step"),       RD_I_CNT, info);
107
108     info.u_cnt=rrd.live_head->last_up;
109     cd=info_push(cd,sprintf_alloc("last_update"), RD_I_CNT, info);
110
111     for(i=0;i<rrd.stat_head->ds_cnt;i++){
112
113         info.u_str=rrd.ds_def[i].dst;
114         cd=info_push(cd,sprintf_alloc("ds[%s].type",             rrd.ds_def[i].ds_nam), RD_I_STR, info);
115   
116         current_ds = dst_conv(rrd.ds_def[i].dst);
117     switch (current_ds) {
118            case DST_CDEF:
119                   {
120                   char *buffer = 0;
121                   rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
122                          rrd.ds_def, &buffer);
123                   info.u_str = buffer;
124                   cd=info_push(cd,sprintf_alloc("ds[%s].cdef",rrd.ds_def[i].ds_nam),RD_I_STR,info);
125                   free(buffer);
126                   }
127                   break;
128            default:
129            info.u_cnt=rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt;
130            cd=info_push(cd,sprintf_alloc("ds[%s].minimal_heartbeat",rrd.ds_def[i].ds_nam), RD_I_CNT, info);
131
132            info.u_val=rrd.ds_def[i].par[DS_min_val].u_val;
133            cd=info_push(cd,sprintf_alloc("ds[%s].min",rrd.ds_def[i].ds_nam), RD_I_VAL, info);
134         
135            info.u_val=rrd.ds_def[i].par[DS_max_val].u_val;
136            cd=info_push(cd,sprintf_alloc("ds[%s].max",rrd.ds_def[i].ds_nam), RD_I_VAL, info);
137            break;
138         }
139         
140         info.u_str=rrd.pdp_prep[i].last_ds;
141         cd=info_push(cd,sprintf_alloc("ds[%s].last_ds",          rrd.ds_def[i].ds_nam), RD_I_STR, info);
142
143         info.u_val=rrd.pdp_prep[i].scratch[PDP_val].u_val;
144         cd=info_push(cd,sprintf_alloc("ds[%s].value",            rrd.ds_def[i].ds_nam), RD_I_VAL, info);
145
146         info.u_cnt=rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt;
147         cd=info_push(cd,sprintf_alloc("ds[%s].unknown_sec",      rrd.ds_def[i].ds_nam), RD_I_CNT, info);
148     }
149
150     for(i=0;i<rrd.stat_head->rra_cnt;i++){
151         info.u_str=rrd.rra_def[i].cf_nam;
152         cd=info_push(cd,sprintf_alloc("rra[%d].cf",         i),  RD_I_STR,   info);
153         current_cf = cf_conv(rrd.rra_def[i].cf_nam);
154
155         info.u_cnt=rrd.rra_def[i].row_cnt;
156         cd=info_push(cd,sprintf_alloc("rra[%d].rows",i),  RD_I_CNT,   info);
157
158         info.u_cnt=rrd.rra_def[i].pdp_cnt;
159         cd=info_push(cd,sprintf_alloc("rra[%d].pdp_per_row",i),  RD_I_CNT,   info);
160
161         switch(current_cf)
162         {
163            case CF_HWPREDICT:
164                   info.u_val=rrd.rra_def[i].par[RRA_hw_alpha].u_val;
165                   cd=info_push(cd,sprintf_alloc("rra[%d].alpha",i),RD_I_VAL,info);
166                   info.u_val=rrd.rra_def[i].par[RRA_hw_beta].u_val;
167                   cd=info_push(cd,sprintf_alloc("rra[%d].beta",i),RD_I_VAL,info);
168                   break;
169            case CF_SEASONAL:
170            case CF_DEVSEASONAL:
171                   info.u_val=rrd.rra_def[i].par[RRA_seasonal_gamma].u_val;
172                   cd=info_push(cd,sprintf_alloc("rra[%d].gamma",i),RD_I_VAL,info);
173                   break;
174            case CF_FAILURES:
175                   info.u_val=rrd.rra_def[i].par[RRA_delta_pos].u_val;
176                   cd=info_push(cd,sprintf_alloc("rra[%d].delta_pos",i),RD_I_VAL,info);
177                   info.u_val=rrd.rra_def[i].par[RRA_delta_neg].u_val;
178                   cd=info_push(cd,sprintf_alloc("rra[%d].delta_neg",i),RD_I_VAL,info);
179                   info.u_cnt=rrd.rra_def[i].par[RRA_failure_threshold].u_cnt;
180                   cd=info_push(cd,sprintf_alloc("rra[%d].failure_threshold",i),RD_I_CNT,info);
181                   info.u_cnt=rrd.rra_def[i].par[RRA_window_len].u_cnt;
182                   cd=info_push(cd,sprintf_alloc("rra[%d].window_length",i),RD_I_CNT,info);
183                   break;
184            case CF_DEVPREDICT:
185                   break;
186            default:
187                   info.u_val=rrd.rra_def[i].par[RRA_cdp_xff_val].u_val;
188                   cd=info_push(cd,sprintf_alloc("rra[%d].xff",i),RD_I_VAL,info);
189                   break;
190         }
191
192         for(ii=0;ii<rrd.stat_head->ds_cnt;ii++){
193         switch(current_cf)
194                 {
195                 case CF_HWPREDICT:
196             info.u_val=rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_intercept].u_val;
197             cd=info_push(cd,sprintf_alloc("rra[%d].cdp_prep[%d].intercept",i,ii), RD_I_VAL, info);
198             info.u_val=rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_slope].u_val;
199             cd=info_push(cd,sprintf_alloc("rra[%d].cdp_prep[%d].slope",i,ii), RD_I_VAL, info);
200             info.u_cnt=rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_null_count].u_cnt;
201             cd=info_push(cd,sprintf_alloc("rra[%d].cdp_prep[%d].NaN_count",i,ii), RD_I_CNT, info);
202                    break;
203                 case CF_SEASONAL:
204             info.u_val=rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_hw_seasonal].u_val;
205             cd=info_push(cd,sprintf_alloc("rra[%d].cdp_prep[%d].seasonal",i,ii), RD_I_VAL, info);
206                    break;
207                 case CF_DEVSEASONAL:
208             info.u_val=rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_seasonal_deviation].u_val;
209             cd=info_push(cd,sprintf_alloc("rra[%d].cdp_prep[%d].deviation",i,ii), RD_I_VAL, info);
210                    break;
211                 case CF_DEVPREDICT:
212                    break;
213                 case CF_FAILURES:
214                    {
215                           unsigned short j;
216                           char *violations_array;
217                           char history[MAX_FAILURES_WINDOW_LEN+1];
218                           violations_array = (char*) rrd.cdp_prep[i*rrd.stat_head->ds_cnt +ii].scratch;
219                           for (j = 0; j < rrd.rra_def[i].par[RRA_window_len].u_cnt; ++j)
220                                  history[j] = (violations_array[j] == 1) ? '1' : '0';
221                       history[j] = '\0';
222                       info.u_str = history;
223                           cd=info_push(cd,sprintf_alloc("rra[%d].cdp_prep[%d].history",i,ii), RD_I_STR, info);
224                    }
225                    break;
226                 default:
227             info.u_val=rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_val].u_val;
228             cd=info_push(cd,sprintf_alloc("rra[%d].cdp_prep[%d].value",i,ii), RD_I_VAL, info);
229             info.u_cnt=rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_unkn_pdp_cnt].u_cnt;
230             cd=info_push(cd,sprintf_alloc("rra[%d].cdp_prep[%d].unknown_datapoints",i,ii), RD_I_CNT, info);
231                    break;
232         }
233     }
234         }
235         rrd_free(&rrd);
236     return(data);
237
238 }