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