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