Generate a random cur_row for each RRA during
[rrdtool.git] / src / rrd_info.c
1 /*****************************************************************************
2  * RRDtool 1.2.99907080300  Copyright by Tobi Oetiker, 1997-2007
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(
13     int,
14     char **);
15 info_t   *rrd_info_r(
16     char *filename);
17
18 /* allocate memory for string */
19 char     *sprintf_alloc(
20     char *fmt,
21     ...)
22 {
23 #ifdef HAVE_VSNPRINTF
24     int       maxlen = 50;
25 #else
26     int       maxlen = 1000;
27 #endif
28     char     *str = NULL;
29     va_list   argp;
30     str = malloc(sizeof(char) * (strlen(fmt) + maxlen));
31     if (str != NULL) {
32         va_start(argp, fmt);
33 #ifdef HAVE_VSNPRINTF
34         vsnprintf(str, maxlen - 1, fmt, argp);
35 #else
36         vsprintf(str, fmt, argp);
37 #endif
38     }
39     va_end(argp);
40     return str;
41 }
42
43 /* the function formerly known as push was renamed info_push because
44  * it is now used outside the scope of this file */
45 info_t
46          *info_push(
47     info_t *info,
48     char *key,
49     enum info_type type,
50     infoval value)
51 {
52     info_t   *next;
53
54     next = malloc(sizeof(*next));
55     next->next = (info_t *) 0;
56     if (info)
57         info->next = next;
58     next->type = type;
59     next->key = key;
60     switch (type) {
61     case RD_I_VAL:
62         next->value.u_val = value.u_val;
63         break;
64     case RD_I_CNT:
65         next->value.u_cnt = value.u_cnt;
66         break;
67     case RD_I_INT:
68         next->value.u_int = value.u_int;
69         break;
70     case RD_I_STR:
71         next->value.u_str = malloc(sizeof(char) * (strlen(value.u_str) + 1));
72         strcpy(next->value.u_str, value.u_str);
73         break;
74     }
75     return (next);
76 }
77
78
79 info_t   *rrd_info(
80     int argc,
81     char **argv)
82 {
83     info_t   *info;
84
85     if (argc < 2) {
86         rrd_set_error("please specify an rrd");
87         return NULL;
88     }
89
90     info = rrd_info_r(argv[1]);
91
92     return (info);
93 }
94
95
96
97 info_t   *rrd_info_r(
98     char *filename)
99 {
100     unsigned int i, ii = 0;
101     rrd_t     rrd;
102     info_t   *data = NULL, *cd;
103     infoval   info;
104     rrd_file_t *rrd_file;
105     enum cf_en current_cf;
106     enum dst_en current_ds;
107
108     rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
109     if (rrd_file == NULL)
110         goto err_free;
111
112     info.u_str = filename;
113     cd = info_push(NULL, sprintf_alloc("filename"), RD_I_STR, info);
114     data = cd;
115
116     info.u_str = rrd.stat_head->version;
117     cd = info_push(cd, sprintf_alloc("rrd_version"), RD_I_STR, info);
118
119     info.u_cnt = rrd.stat_head->pdp_step;
120     cd = info_push(cd, sprintf_alloc("step"), RD_I_CNT, info);
121
122     info.u_cnt = rrd.live_head->last_up;
123     cd = info_push(cd, sprintf_alloc("last_update"), RD_I_CNT, info);
124
125     for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
126
127         info.u_str = rrd.ds_def[i].dst;
128         cd = info_push(cd, sprintf_alloc("ds[%s].type", rrd.ds_def[i].ds_nam),
129                        RD_I_STR, info);
130
131         current_ds = dst_conv(rrd.ds_def[i].dst);
132         switch (current_ds) {
133         case DST_CDEF:
134         {
135             char     *buffer = NULL;
136
137             rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
138                             rrd.ds_def, &buffer);
139             info.u_str = buffer;
140             cd = info_push(cd,
141                            sprintf_alloc("ds[%s].cdef", rrd.ds_def[i].ds_nam),
142                            RD_I_STR, info);
143             free(buffer);
144         }
145             break;
146         default:
147             info.u_cnt = rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt;
148             cd = info_push(cd,
149                            sprintf_alloc("ds[%s].minimal_heartbeat",
150                                          rrd.ds_def[i].ds_nam), RD_I_CNT,
151                            info);
152
153             info.u_val = rrd.ds_def[i].par[DS_min_val].u_val;
154             cd = info_push(cd,
155                            sprintf_alloc("ds[%s].min", rrd.ds_def[i].ds_nam),
156                            RD_I_VAL, info);
157
158             info.u_val = rrd.ds_def[i].par[DS_max_val].u_val;
159             cd = info_push(cd,
160                            sprintf_alloc("ds[%s].max", rrd.ds_def[i].ds_nam),
161                            RD_I_VAL, info);
162             break;
163         }
164
165         info.u_str = rrd.pdp_prep[i].last_ds;
166         cd = info_push(cd,
167                        sprintf_alloc("ds[%s].last_ds", rrd.ds_def[i].ds_nam),
168                        RD_I_STR, info);
169
170         info.u_val = rrd.pdp_prep[i].scratch[PDP_val].u_val;
171         cd = info_push(cd,
172                        sprintf_alloc("ds[%s].value", rrd.ds_def[i].ds_nam),
173                        RD_I_VAL, info);
174
175         info.u_cnt = rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt;
176         cd = info_push(cd,
177                        sprintf_alloc("ds[%s].unknown_sec",
178                                      rrd.ds_def[i].ds_nam), RD_I_CNT, info);
179     }
180
181     for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
182         info.u_str = rrd.rra_def[i].cf_nam;
183         cd = info_push(cd, sprintf_alloc("rra[%d].cf", i), RD_I_STR, info);
184         current_cf = cf_conv(rrd.rra_def[i].cf_nam);
185
186         info.u_cnt = rrd.rra_def[i].row_cnt;
187         cd = info_push(cd, sprintf_alloc("rra[%d].rows", i), RD_I_CNT, info);
188
189         info.u_cnt=rrd.rra_ptr[i].cur_row;
190         cd=info_push(cd,sprintf_alloc("rra[%d].cur_row",i),  RD_I_CNT,   info);
191
192         info.u_cnt = rrd.rra_def[i].pdp_cnt;
193         cd = info_push(cd, sprintf_alloc("rra[%d].pdp_per_row", i), RD_I_CNT,
194                        info);
195
196         switch (current_cf) {
197         case CF_HWPREDICT:
198         case CF_MHWPREDICT:
199             info.u_val = rrd.rra_def[i].par[RRA_hw_alpha].u_val;
200             cd = info_push(cd, sprintf_alloc("rra[%d].alpha", i), RD_I_VAL,
201                            info);
202             info.u_val = rrd.rra_def[i].par[RRA_hw_beta].u_val;
203             cd = info_push(cd, sprintf_alloc("rra[%d].beta", i), RD_I_VAL,
204                            info);
205             break;
206         case CF_SEASONAL:
207         case CF_DEVSEASONAL:
208             info.u_val = rrd.rra_def[i].par[RRA_seasonal_gamma].u_val;
209             cd = info_push(cd, sprintf_alloc("rra[%d].gamma", i), RD_I_VAL,
210                            info);
211             if (atoi(rrd.stat_head->version) >= 4) {
212                 info.u_val =
213                     rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val;
214                 cd = info_push(cd,
215                                sprintf_alloc("rra[%d].smoothing_window", i),
216                                RD_I_VAL, info);
217             }
218             break;
219         case CF_FAILURES:
220             info.u_val = rrd.rra_def[i].par[RRA_delta_pos].u_val;
221             cd = info_push(cd, sprintf_alloc("rra[%d].delta_pos", i),
222                            RD_I_VAL, info);
223             info.u_val = rrd.rra_def[i].par[RRA_delta_neg].u_val;
224             cd = info_push(cd, sprintf_alloc("rra[%d].delta_neg", i),
225                            RD_I_VAL, info);
226             info.u_cnt = rrd.rra_def[i].par[RRA_failure_threshold].u_cnt;
227             cd = info_push(cd, sprintf_alloc("rra[%d].failure_threshold", i),
228                            RD_I_CNT, info);
229             info.u_cnt = rrd.rra_def[i].par[RRA_window_len].u_cnt;
230             cd = info_push(cd, sprintf_alloc("rra[%d].window_length", i),
231                            RD_I_CNT, info);
232             break;
233         case CF_DEVPREDICT:
234             break;
235         default:
236             info.u_val = rrd.rra_def[i].par[RRA_cdp_xff_val].u_val;
237             cd = info_push(cd, sprintf_alloc("rra[%d].xff", i), RD_I_VAL,
238                            info);
239             break;
240         }
241
242         for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
243             switch (current_cf) {
244             case CF_HWPREDICT:
245             case CF_MHWPREDICT:
246                 info.u_val =
247                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
248                                  ii].scratch[CDP_hw_intercept].u_val;
249                 cd = info_push(cd,
250                                sprintf_alloc("rra[%d].cdp_prep[%d].intercept",
251                                              i, ii), RD_I_VAL, info);
252                 info.u_val =
253                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
254                                  ii].scratch[CDP_hw_slope].u_val;
255                 cd = info_push(cd,
256                                sprintf_alloc("rra[%d].cdp_prep[%d].slope", i,
257                                              ii), RD_I_VAL, info);
258                 info.u_cnt =
259                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
260                                  ii].scratch[CDP_null_count].u_cnt;
261                 cd = info_push(cd,
262                                sprintf_alloc("rra[%d].cdp_prep[%d].NaN_count",
263                                              i, ii), RD_I_CNT, info);
264                 break;
265             case CF_SEASONAL:
266                 info.u_val =
267                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
268                                  ii].scratch[CDP_hw_seasonal].u_val;
269                 cd = info_push(cd,
270                                sprintf_alloc("rra[%d].cdp_prep[%d].seasonal",
271                                              i, ii), RD_I_VAL, info);
272                 break;
273             case CF_DEVSEASONAL:
274                 info.u_val =
275                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
276                                  ii].scratch[CDP_seasonal_deviation].u_val;
277                 cd = info_push(cd,
278                                sprintf_alloc("rra[%d].cdp_prep[%d].deviation",
279                                              i, ii), RD_I_VAL, info);
280                 break;
281             case CF_DEVPREDICT:
282                 break;
283             case CF_FAILURES:
284             {
285                 unsigned short j;
286                 char     *violations_array;
287                 char      history[MAX_FAILURES_WINDOW_LEN + 1];
288
289                 violations_array =
290                     (char *) rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
291                                           ii].scratch;
292                 for (j = 0; j < rrd.rra_def[i].par[RRA_window_len].u_cnt; ++j)
293                     history[j] = (violations_array[j] == 1) ? '1' : '0';
294                 history[j] = '\0';
295                 info.u_str = history;
296                 cd = info_push(cd,
297                                sprintf_alloc("rra[%d].cdp_prep[%d].history",
298                                              i, ii), RD_I_STR, info);
299             }
300                 break;
301             default:
302                 info.u_val =
303                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
304                                  ii].scratch[CDP_val].u_val;
305                 cd = info_push(cd,
306                                sprintf_alloc("rra[%d].cdp_prep[%d].value", i,
307                                              ii), RD_I_VAL, info);
308                 info.u_cnt =
309                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
310                                  ii].scratch[CDP_unkn_pdp_cnt].u_cnt;
311                 cd = info_push(cd,
312                                sprintf_alloc
313                                ("rra[%d].cdp_prep[%d].unknown_datapoints", i,
314                                 ii), RD_I_CNT, info);
315                 break;
316             }
317         }
318     }
319
320     rrd_close(rrd_file);
321   err_free:
322     rrd_free(&rrd);
323     return (data);
324 }