reindented everything according to .indent.pro
[rrdtool.git] / src / rrd_info.c
1 /*****************************************************************************
2  * RRDtool 1.2.23  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, *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         return (NULL);
111     }
112     close(rrd_file->fd);
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_def[i].pdp_cnt;
192         cd = info_push(cd, sprintf_alloc("rra[%d].pdp_per_row", i), RD_I_CNT,
193                        info);
194
195         switch (current_cf) {
196         case CF_HWPREDICT:
197             info.u_val = rrd.rra_def[i].par[RRA_hw_alpha].u_val;
198             cd = info_push(cd, sprintf_alloc("rra[%d].alpha", i), RD_I_VAL,
199                            info);
200             info.u_val = rrd.rra_def[i].par[RRA_hw_beta].u_val;
201             cd = info_push(cd, sprintf_alloc("rra[%d].beta", i), RD_I_VAL,
202                            info);
203             break;
204         case CF_SEASONAL:
205         case CF_DEVSEASONAL:
206             info.u_val = rrd.rra_def[i].par[RRA_seasonal_gamma].u_val;
207             cd = info_push(cd, sprintf_alloc("rra[%d].gamma", i), RD_I_VAL,
208                            info);
209             break;
210         case CF_FAILURES:
211             info.u_val = rrd.rra_def[i].par[RRA_delta_pos].u_val;
212             cd = info_push(cd, sprintf_alloc("rra[%d].delta_pos", i),
213                            RD_I_VAL, info);
214             info.u_val = rrd.rra_def[i].par[RRA_delta_neg].u_val;
215             cd = info_push(cd, sprintf_alloc("rra[%d].delta_neg", i),
216                            RD_I_VAL, info);
217             info.u_cnt = rrd.rra_def[i].par[RRA_failure_threshold].u_cnt;
218             cd = info_push(cd, sprintf_alloc("rra[%d].failure_threshold", i),
219                            RD_I_CNT, info);
220             info.u_cnt = rrd.rra_def[i].par[RRA_window_len].u_cnt;
221             cd = info_push(cd, sprintf_alloc("rra[%d].window_length", i),
222                            RD_I_CNT, info);
223             break;
224         case CF_DEVPREDICT:
225             break;
226         default:
227             info.u_val = rrd.rra_def[i].par[RRA_cdp_xff_val].u_val;
228             cd = info_push(cd, sprintf_alloc("rra[%d].xff", i), RD_I_VAL,
229                            info);
230             break;
231         }
232
233         for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
234             switch (current_cf) {
235             case CF_HWPREDICT:
236                 info.u_val =
237                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
238                                  ii].scratch[CDP_hw_intercept].u_val;
239                 cd = info_push(cd,
240                                sprintf_alloc("rra[%d].cdp_prep[%d].intercept",
241                                              i, ii), RD_I_VAL, info);
242                 info.u_val =
243                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
244                                  ii].scratch[CDP_hw_slope].u_val;
245                 cd = info_push(cd,
246                                sprintf_alloc("rra[%d].cdp_prep[%d].slope", i,
247                                              ii), RD_I_VAL, info);
248                 info.u_cnt =
249                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
250                                  ii].scratch[CDP_null_count].u_cnt;
251                 cd = info_push(cd,
252                                sprintf_alloc("rra[%d].cdp_prep[%d].NaN_count",
253                                              i, ii), RD_I_CNT, info);
254                 break;
255             case CF_SEASONAL:
256                 info.u_val =
257                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
258                                  ii].scratch[CDP_hw_seasonal].u_val;
259                 cd = info_push(cd,
260                                sprintf_alloc("rra[%d].cdp_prep[%d].seasonal",
261                                              i, ii), RD_I_VAL, info);
262                 break;
263             case CF_DEVSEASONAL:
264                 info.u_val =
265                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
266                                  ii].scratch[CDP_seasonal_deviation].u_val;
267                 cd = info_push(cd,
268                                sprintf_alloc("rra[%d].cdp_prep[%d].deviation",
269                                              i, ii), RD_I_VAL, info);
270                 break;
271             case CF_DEVPREDICT:
272                 break;
273             case CF_FAILURES:
274             {
275                 unsigned short j;
276                 char     *violations_array;
277                 char      history[MAX_FAILURES_WINDOW_LEN + 1];
278
279                 violations_array =
280                     (char *) rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
281                                           ii].scratch;
282                 for (j = 0; j < rrd.rra_def[i].par[RRA_window_len].u_cnt; ++j)
283                     history[j] = (violations_array[j] == 1) ? '1' : '0';
284                 history[j] = '\0';
285                 info.u_str = history;
286                 cd = info_push(cd,
287                                sprintf_alloc("rra[%d].cdp_prep[%d].history",
288                                              i, ii), RD_I_STR, info);
289             }
290                 break;
291             default:
292                 info.u_val =
293                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
294                                  ii].scratch[CDP_val].u_val;
295                 cd = info_push(cd,
296                                sprintf_alloc("rra[%d].cdp_prep[%d].value", i,
297                                              ii), RD_I_VAL, info);
298                 info.u_cnt =
299                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
300                                  ii].scratch[CDP_unkn_pdp_cnt].u_cnt;
301                 cd = info_push(cd,
302                                sprintf_alloc
303                                ("rra[%d].cdp_prep[%d].unknown_datapoints", i,
304                                 ii), RD_I_CNT, info);
305                 break;
306             }
307         }
308     }
309     rrd_free(&rrd);
310     rrd_close(rrd_file);
311     return (data);
312
313 }