cf90e0e2b8cbe1115f0589f0029b95cb65c0b9fc
1 /*****************************************************************************
2 * RRDtool 1.3rc6 Copyright by Tobi Oetiker, 1997-2008
3 *****************************************************************************
4 * rrd_info Get Information about the configuration of an RRD
5 *****************************************************************************/
7 #include "rrd_tool.h"
8 #include "rrd_rpncalc.h"
9 #include <stdarg.h>
11 /* proto */
12 info_t *rrd_info(
13 int,
14 char **);
15 info_t *rrd_info_r(
16 char *filename);
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 }
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;
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 case RD_I_BLO:
75 next->value.u_blo.size = value.u_blo.size;
76 next->value.u_blo.ptr =
77 malloc(sizeof(unsigned char) * value.u_blo.size);
78 memcpy(next->value.u_blo.ptr, value.u_blo.ptr, value.u_blo.size);
79 break;
80 }
81 return (next);
82 }
85 info_t *rrd_info(
86 int argc,
87 char **argv)
88 {
89 info_t *info;
91 if (argc < 2) {
92 rrd_set_error("please specify an rrd");
93 return NULL;
94 }
96 info = rrd_info_r(argv[1]);
98 return (info);
99 }
103 info_t *rrd_info_r(
104 char *filename)
105 {
106 unsigned int i, ii = 0;
107 rrd_t rrd;
108 info_t *data = NULL, *cd;
109 infoval info;
110 rrd_file_t *rrd_file;
111 enum cf_en current_cf;
112 enum dst_en current_ds;
114 rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
115 if (rrd_file == NULL)
116 goto err_free;
118 info.u_str = filename;
119 cd = info_push(NULL, sprintf_alloc("filename"), RD_I_STR, info);
120 data = cd;
122 info.u_str = rrd.stat_head->version;
123 cd = info_push(cd, sprintf_alloc("rrd_version"), RD_I_STR, info);
125 info.u_cnt = rrd.stat_head->pdp_step;
126 cd = info_push(cd, sprintf_alloc("step"), RD_I_CNT, info);
128 info.u_cnt = rrd.live_head->last_up;
129 cd = info_push(cd, sprintf_alloc("last_update"), RD_I_CNT, info);
131 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
133 info.u_str = rrd.ds_def[i].dst;
134 cd = info_push(cd, sprintf_alloc("ds[%s].type", rrd.ds_def[i].ds_nam),
135 RD_I_STR, info);
137 current_ds = dst_conv(rrd.ds_def[i].dst);
138 switch (current_ds) {
139 case DST_CDEF:
140 {
141 char *buffer = NULL;
143 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
144 rrd.ds_def, &buffer);
145 info.u_str = buffer;
146 cd = info_push(cd,
147 sprintf_alloc("ds[%s].cdef", rrd.ds_def[i].ds_nam),
148 RD_I_STR, info);
149 free(buffer);
150 }
151 break;
152 default:
153 info.u_cnt = rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt;
154 cd = info_push(cd,
155 sprintf_alloc("ds[%s].minimal_heartbeat",
156 rrd.ds_def[i].ds_nam), RD_I_CNT,
157 info);
159 info.u_val = rrd.ds_def[i].par[DS_min_val].u_val;
160 cd = info_push(cd,
161 sprintf_alloc("ds[%s].min", rrd.ds_def[i].ds_nam),
162 RD_I_VAL, info);
164 info.u_val = rrd.ds_def[i].par[DS_max_val].u_val;
165 cd = info_push(cd,
166 sprintf_alloc("ds[%s].max", rrd.ds_def[i].ds_nam),
167 RD_I_VAL, info);
168 break;
169 }
171 info.u_str = rrd.pdp_prep[i].last_ds;
172 cd = info_push(cd,
173 sprintf_alloc("ds[%s].last_ds", rrd.ds_def[i].ds_nam),
174 RD_I_STR, info);
176 info.u_val = rrd.pdp_prep[i].scratch[PDP_val].u_val;
177 cd = info_push(cd,
178 sprintf_alloc("ds[%s].value", rrd.ds_def[i].ds_nam),
179 RD_I_VAL, info);
181 info.u_cnt = rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt;
182 cd = info_push(cd,
183 sprintf_alloc("ds[%s].unknown_sec",
184 rrd.ds_def[i].ds_nam), RD_I_CNT, info);
185 }
187 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
188 info.u_str = rrd.rra_def[i].cf_nam;
189 cd = info_push(cd, sprintf_alloc("rra[%d].cf", i), RD_I_STR, info);
190 current_cf = cf_conv(rrd.rra_def[i].cf_nam);
192 info.u_cnt = rrd.rra_def[i].row_cnt;
193 cd = info_push(cd, sprintf_alloc("rra[%d].rows", i), RD_I_CNT, info);
195 info.u_cnt = rrd.rra_ptr[i].cur_row;
196 cd = info_push(cd, sprintf_alloc("rra[%d].cur_row", i), RD_I_CNT,
197 info);
199 info.u_cnt = rrd.rra_def[i].pdp_cnt;
200 cd = info_push(cd, sprintf_alloc("rra[%d].pdp_per_row", i), RD_I_CNT,
201 info);
203 switch (current_cf) {
204 case CF_HWPREDICT:
205 case CF_MHWPREDICT:
206 info.u_val = rrd.rra_def[i].par[RRA_hw_alpha].u_val;
207 cd = info_push(cd, sprintf_alloc("rra[%d].alpha", i), RD_I_VAL,
208 info);
209 info.u_val = rrd.rra_def[i].par[RRA_hw_beta].u_val;
210 cd = info_push(cd, sprintf_alloc("rra[%d].beta", i), RD_I_VAL,
211 info);
212 break;
213 case CF_SEASONAL:
214 case CF_DEVSEASONAL:
215 info.u_val = rrd.rra_def[i].par[RRA_seasonal_gamma].u_val;
216 cd = info_push(cd, sprintf_alloc("rra[%d].gamma", i), RD_I_VAL,
217 info);
218 if (atoi(rrd.stat_head->version) >= 4) {
219 info.u_val =
220 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val;
221 cd = info_push(cd,
222 sprintf_alloc("rra[%d].smoothing_window", i),
223 RD_I_VAL, info);
224 }
225 break;
226 case CF_FAILURES:
227 info.u_val = rrd.rra_def[i].par[RRA_delta_pos].u_val;
228 cd = info_push(cd, sprintf_alloc("rra[%d].delta_pos", i),
229 RD_I_VAL, info);
230 info.u_val = rrd.rra_def[i].par[RRA_delta_neg].u_val;
231 cd = info_push(cd, sprintf_alloc("rra[%d].delta_neg", i),
232 RD_I_VAL, info);
233 info.u_cnt = rrd.rra_def[i].par[RRA_failure_threshold].u_cnt;
234 cd = info_push(cd, sprintf_alloc("rra[%d].failure_threshold", i),
235 RD_I_CNT, info);
236 info.u_cnt = rrd.rra_def[i].par[RRA_window_len].u_cnt;
237 cd = info_push(cd, sprintf_alloc("rra[%d].window_length", i),
238 RD_I_CNT, info);
239 break;
240 case CF_DEVPREDICT:
241 break;
242 default:
243 info.u_val = rrd.rra_def[i].par[RRA_cdp_xff_val].u_val;
244 cd = info_push(cd, sprintf_alloc("rra[%d].xff", i), RD_I_VAL,
245 info);
246 break;
247 }
249 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
250 switch (current_cf) {
251 case CF_HWPREDICT:
252 case CF_MHWPREDICT:
253 info.u_val =
254 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
255 ii].scratch[CDP_hw_intercept].u_val;
256 cd = info_push(cd,
257 sprintf_alloc("rra[%d].cdp_prep[%d].intercept",
258 i, ii), RD_I_VAL, info);
259 info.u_val =
260 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
261 ii].scratch[CDP_hw_slope].u_val;
262 cd = info_push(cd,
263 sprintf_alloc("rra[%d].cdp_prep[%d].slope", i,
264 ii), RD_I_VAL, info);
265 info.u_cnt =
266 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
267 ii].scratch[CDP_null_count].u_cnt;
268 cd = info_push(cd,
269 sprintf_alloc("rra[%d].cdp_prep[%d].NaN_count",
270 i, ii), RD_I_CNT, info);
271 break;
272 case CF_SEASONAL:
273 info.u_val =
274 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
275 ii].scratch[CDP_hw_seasonal].u_val;
276 cd = info_push(cd,
277 sprintf_alloc("rra[%d].cdp_prep[%d].seasonal",
278 i, ii), RD_I_VAL, info);
279 break;
280 case CF_DEVSEASONAL:
281 info.u_val =
282 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
283 ii].scratch[CDP_seasonal_deviation].u_val;
284 cd = info_push(cd,
285 sprintf_alloc("rra[%d].cdp_prep[%d].deviation",
286 i, ii), RD_I_VAL, info);
287 break;
288 case CF_DEVPREDICT:
289 break;
290 case CF_FAILURES:
291 {
292 unsigned short j;
293 char *violations_array;
294 char history[MAX_FAILURES_WINDOW_LEN + 1];
296 violations_array =
297 (char *) rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
298 ii].scratch;
299 for (j = 0; j < rrd.rra_def[i].par[RRA_window_len].u_cnt; ++j)
300 history[j] = (violations_array[j] == 1) ? '1' : '0';
301 history[j] = '\0';
302 info.u_str = history;
303 cd = info_push(cd,
304 sprintf_alloc("rra[%d].cdp_prep[%d].history",
305 i, ii), RD_I_STR, info);
306 }
307 break;
308 default:
309 info.u_val =
310 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
311 ii].scratch[CDP_val].u_val;
312 cd = info_push(cd,
313 sprintf_alloc("rra[%d].cdp_prep[%d].value", i,
314 ii), RD_I_VAL, info);
315 info.u_cnt =
316 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
317 ii].scratch[CDP_unkn_pdp_cnt].u_cnt;
318 cd = info_push(cd,
319 sprintf_alloc
320 ("rra[%d].cdp_prep[%d].unknown_datapoints", i,
321 ii), RD_I_CNT, info);
322 break;
323 }
324 }
325 }
327 rrd_close(rrd_file);
328 err_free:
329 rrd_free(&rrd);
330 return (data);
331 }
334 void info_print(
335 info_t *data)
336 {
337 while (data) {
338 printf("%s = ", data->key);
340 switch (data->type) {
341 case RD_I_VAL:
342 if (isnan(data->value.u_val))
343 printf("NaN\n");
344 else
345 printf("%0.10e\n", data->value.u_val);
346 break;
347 case RD_I_CNT:
348 printf("%lu\n", data->value.u_cnt);
349 break;
350 case RD_I_INT:
351 printf("%d\n", data->value.u_int);
352 break;
353 case RD_I_STR:
354 printf("\"%s\"\n", data->value.u_str);
355 break;
356 case RD_I_BLO:
357 printf("BLOB_SIZE:%lu\n", data->value.u_blo.size);
358 fwrite(data->value.u_blo.ptr, data->value.u_blo.size, 1, stdout);
359 break;
360 }
361 data = data->next;
362 }
363 }
365 void info_free(
366 info_t *data)
367 {
368 info_t *save;
370 while (data) {
371 save = data;
372 if (data->key) {
373 if (data->type == RD_I_STR) {
374 free(data->value.u_str);
375 }
376 if (data->type == RD_I_BLO) {
377 free(data->value.u_blo.ptr);
378 }
379 free(data->key);
380 }
381 data = data->next;
382 free(save);
383 }
384 }