1 /*****************************************************************************
2 * RRDtool 1.3.2 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 "rrd_client.h"
10 #include <stdarg.h>
12 /* proto */
13 rrd_info_t *rrd_info(
14 int,
15 char **);
16 rrd_info_t *rrd_info_r(
17 char *filename);
19 /* allocate memory for string */
20 char *sprintf_alloc(
21 char *fmt,
22 ...)
23 {
24 char *str = NULL;
25 va_list argp;
26 #ifdef HAVE_VASPRINTF
27 va_start( argp, fmt );
28 vasprintf( &str, fmt, argp );
29 #else
30 int maxlen = 1024 + strlen(fmt);
31 str = (char*)malloc(sizeof(char) * (maxlen + 1));
32 if (str != NULL) {
33 va_start(argp, fmt);
34 #ifdef HAVE_VSNPRINTF
35 vsnprintf(str, maxlen, fmt, argp);
36 #else
37 vsprintf(str, fmt, argp);
38 #endif
39 }
40 #endif // HAVE_VASPRINTF
41 va_end(argp);
42 return str;
43 }
45 /* the function formerly known as push was renamed to info_push and later
46 * rrd_info_push because it is now used outside the scope of this file */
47 rrd_info_t
48 * rrd_info_push(rrd_info_t * info,
49 char *key, rrd_info_type_t type, rrd_infoval_t value)
50 {
51 rrd_info_t *next;
53 next = (rrd_info_t*)malloc(sizeof(*next));
54 next->next = (rrd_info_t *) 0;
55 if (info)
56 info->next = next;
57 next->type = type;
58 next->key = key;
59 switch (type) {
60 case RD_I_VAL:
61 next->value.u_val = value.u_val;
62 break;
63 case RD_I_CNT:
64 next->value.u_cnt = value.u_cnt;
65 break;
66 case RD_I_INT:
67 next->value.u_int = value.u_int;
68 break;
69 case RD_I_STR:
70 next->value.u_str = (char*)malloc(sizeof(char) * (strlen(value.u_str) + 1));
71 strcpy(next->value.u_str, value.u_str);
72 break;
73 case RD_I_BLO:
74 next->value.u_blo.size = value.u_blo.size;
75 next->value.u_blo.ptr =
76 (unsigned char *)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 rrd_info_t *rrd_info(
85 int argc,
86 char **argv)
87 {
88 rrd_info_t *info;
89 char *opt_daemon = NULL;
90 int status;
92 optind = 0;
93 opterr = 0; /* initialize getopt */
95 while (42) {
96 int opt;
97 int option_index = 0;
98 static struct option long_options[] = {
99 {"daemon", required_argument, 0, 'd'},
100 {0, 0, 0, 0}
101 };
103 opt = getopt_long(argc, argv, "d:", long_options, &option_index);
105 if (opt == EOF)
106 break;
108 switch (opt) {
109 case 'd':
110 if (opt_daemon != NULL)
111 free (opt_daemon);
112 opt_daemon = strdup (optarg);
113 if (opt_daemon == NULL)
114 {
115 rrd_set_error ("strdup failed.");
116 return (NULL);
117 }
118 break;
120 default:
121 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
122 argv[0]);
123 return (NULL);
124 break;
125 }
126 } /* while (42) */
128 if ((argc - optind) != 1) {
129 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
130 argv[0]);
131 return (NULL);
132 }
134 status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
135 if (opt_daemon) free (opt_daemon);
136 if (status) return (NULL);
138 info = rrd_info_r(argv[optind]);
140 return (info);
141 } /* rrd_info_t *rrd_info */
143 rrd_info_t *rrd_info_r(
144 char *filename)
145 {
146 unsigned int i, ii = 0;
147 rrd_t rrd;
148 rrd_info_t *data = NULL, *cd;
149 rrd_infoval_t info;
150 rrd_file_t *rrd_file;
151 enum cf_en current_cf;
152 enum dst_en current_ds;
154 rrd_init(&rrd);
155 rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
156 if (rrd_file == NULL)
157 goto err_free;
159 info.u_str = filename;
160 cd = rrd_info_push(NULL, sprintf_alloc("filename"), RD_I_STR, info);
161 data = cd;
163 info.u_str = rrd.stat_head->version;
164 cd = rrd_info_push(cd, sprintf_alloc("rrd_version"), RD_I_STR, info);
166 info.u_cnt = rrd.stat_head->pdp_step;
167 cd = rrd_info_push(cd, sprintf_alloc("step"), RD_I_CNT, info);
169 info.u_cnt = rrd.live_head->last_up;
170 cd = rrd_info_push(cd, sprintf_alloc("last_update"), RD_I_CNT, info);
172 info.u_cnt = rrd_get_header_size(&rrd);
173 cd = rrd_info_push(cd, sprintf_alloc("header_size"), RD_I_CNT, info);
175 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
177 info.u_str = rrd.ds_def[i].dst;
178 cd = rrd_info_push(cd, sprintf_alloc("ds[%s].type",
179 rrd.ds_def[i].ds_nam),
180 RD_I_STR, info);
182 current_ds = dst_conv(rrd.ds_def[i].dst);
183 switch (current_ds) {
184 case DST_CDEF:
185 {
186 char *buffer = NULL;
188 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
189 rrd.ds_def, &buffer);
190 info.u_str = buffer;
191 cd = rrd_info_push(cd,
192 sprintf_alloc("ds[%s].cdef",
193 rrd.ds_def[i].ds_nam), RD_I_STR,
194 info);
195 free(buffer);
196 }
197 break;
198 default:
199 info.u_cnt = rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt;
200 cd = rrd_info_push(cd,
201 sprintf_alloc("ds[%s].minimal_heartbeat",
202 rrd.ds_def[i].ds_nam), RD_I_CNT,
203 info);
205 info.u_val = rrd.ds_def[i].par[DS_min_val].u_val;
206 cd = rrd_info_push(cd,
207 sprintf_alloc("ds[%s].min",
208 rrd.ds_def[i].ds_nam), RD_I_VAL,
209 info);
211 info.u_val = rrd.ds_def[i].par[DS_max_val].u_val;
212 cd = rrd_info_push(cd,
213 sprintf_alloc("ds[%s].max",
214 rrd.ds_def[i].ds_nam), RD_I_VAL,
215 info);
216 break;
217 }
219 info.u_str = rrd.pdp_prep[i].last_ds;
220 cd = rrd_info_push(cd,
221 sprintf_alloc("ds[%s].last_ds",
222 rrd.ds_def[i].ds_nam), RD_I_STR,
223 info);
225 info.u_val = rrd.pdp_prep[i].scratch[PDP_val].u_val;
226 cd = rrd_info_push(cd,
227 sprintf_alloc("ds[%s].value",
228 rrd.ds_def[i].ds_nam), RD_I_VAL,
229 info);
231 info.u_cnt = rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt;
232 cd = rrd_info_push(cd,
233 sprintf_alloc("ds[%s].unknown_sec",
234 rrd.ds_def[i].ds_nam), RD_I_CNT,
235 info);
236 }
238 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
239 info.u_str = rrd.rra_def[i].cf_nam;
240 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].cf", i), RD_I_STR,
241 info);
242 current_cf = cf_conv(rrd.rra_def[i].cf_nam);
244 info.u_cnt = rrd.rra_def[i].row_cnt;
245 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].rows", i), RD_I_CNT,
246 info);
248 info.u_cnt = rrd.rra_ptr[i].cur_row;
249 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].cur_row", i), RD_I_CNT,
250 info);
252 info.u_cnt = rrd.rra_def[i].pdp_cnt;
253 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].pdp_per_row", i),
254 RD_I_CNT, info);
256 switch (current_cf) {
257 case CF_HWPREDICT:
258 case CF_MHWPREDICT:
259 info.u_val = rrd.rra_def[i].par[RRA_hw_alpha].u_val;
260 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].alpha", i),
261 RD_I_VAL, info);
262 info.u_val = rrd.rra_def[i].par[RRA_hw_beta].u_val;
263 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].beta", i), RD_I_VAL,
264 info);
265 break;
266 case CF_SEASONAL:
267 case CF_DEVSEASONAL:
268 info.u_val = rrd.rra_def[i].par[RRA_seasonal_gamma].u_val;
269 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].gamma", i),
270 RD_I_VAL, info);
271 if (atoi(rrd.stat_head->version) >= 4) {
272 info.u_val =
273 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val;
274 cd = rrd_info_push(cd,
275 sprintf_alloc("rra[%d].smoothing_window",
276 i), RD_I_VAL, info);
277 }
278 break;
279 case CF_FAILURES:
280 info.u_val = rrd.rra_def[i].par[RRA_delta_pos].u_val;
281 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].delta_pos", i),
282 RD_I_VAL, info);
283 info.u_val = rrd.rra_def[i].par[RRA_delta_neg].u_val;
284 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].delta_neg", i),
285 RD_I_VAL, info);
286 info.u_cnt = rrd.rra_def[i].par[RRA_failure_threshold].u_cnt;
287 cd = rrd_info_push(cd,
288 sprintf_alloc("rra[%d].failure_threshold", i),
289 RD_I_CNT, info);
290 info.u_cnt = rrd.rra_def[i].par[RRA_window_len].u_cnt;
291 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].window_length", i),
292 RD_I_CNT, info);
293 break;
294 case CF_DEVPREDICT:
295 break;
296 default:
297 info.u_val = rrd.rra_def[i].par[RRA_cdp_xff_val].u_val;
298 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].xff", i), RD_I_VAL,
299 info);
300 break;
301 }
303 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
304 switch (current_cf) {
305 case CF_HWPREDICT:
306 case CF_MHWPREDICT:
307 info.u_val =
308 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
309 ii].scratch[CDP_hw_intercept].u_val;
310 cd = rrd_info_push(cd,
311 sprintf_alloc
312 ("rra[%d].cdp_prep[%d].intercept", i, ii),
313 RD_I_VAL, info);
314 info.u_val =
315 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
316 ii].scratch[CDP_hw_slope].u_val;
317 cd = rrd_info_push(cd,
318 sprintf_alloc("rra[%d].cdp_prep[%d].slope",
319 i, ii), RD_I_VAL, info);
320 info.u_cnt =
321 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
322 ii].scratch[CDP_null_count].u_cnt;
323 cd = rrd_info_push(cd,
324 sprintf_alloc
325 ("rra[%d].cdp_prep[%d].NaN_count", i, ii),
326 RD_I_CNT, info);
327 break;
328 case CF_SEASONAL:
329 info.u_val =
330 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
331 ii].scratch[CDP_hw_seasonal].u_val;
332 cd = rrd_info_push(cd,
333 sprintf_alloc
334 ("rra[%d].cdp_prep[%d].seasonal", i, ii),
335 RD_I_VAL, info);
336 break;
337 case CF_DEVSEASONAL:
338 info.u_val =
339 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
340 ii].scratch[CDP_seasonal_deviation].u_val;
341 cd = rrd_info_push(cd,
342 sprintf_alloc
343 ("rra[%d].cdp_prep[%d].deviation", i, ii),
344 RD_I_VAL, info);
345 break;
346 case CF_DEVPREDICT:
347 break;
348 case CF_FAILURES:
349 {
350 unsigned short j;
351 char *violations_array;
352 char history[MAX_FAILURES_WINDOW_LEN + 1];
354 violations_array =
355 (char *) rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
356 ii].scratch;
357 for (j = 0; j < rrd.rra_def[i].par[RRA_window_len].u_cnt; ++j)
358 history[j] = (violations_array[j] == 1) ? '1' : '0';
359 history[j] = '\0';
360 info.u_str = history;
361 cd = rrd_info_push(cd,
362 sprintf_alloc
363 ("rra[%d].cdp_prep[%d].history", i, ii),
364 RD_I_STR, info);
365 }
366 break;
367 default:
368 info.u_val =
369 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
370 ii].scratch[CDP_val].u_val;
371 cd = rrd_info_push(cd,
372 sprintf_alloc("rra[%d].cdp_prep[%d].value",
373 i, ii), RD_I_VAL, info);
374 info.u_cnt =
375 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
376 ii].scratch[CDP_unkn_pdp_cnt].u_cnt;
377 cd = rrd_info_push(cd,
378 sprintf_alloc
379 ("rra[%d].cdp_prep[%d].unknown_datapoints",
380 i, ii), RD_I_CNT, info);
381 break;
382 }
383 }
384 }
386 rrd_close(rrd_file);
387 err_free:
388 rrd_free(&rrd);
389 return (data);
390 }
393 void rrd_info_print(
394 rrd_info_t * data)
395 {
396 while (data) {
397 printf("%s = ", data->key);
399 switch (data->type) {
400 case RD_I_VAL:
401 if (isnan(data->value.u_val))
402 printf("NaN\n");
403 else
404 printf("%0.10e\n", data->value.u_val);
405 break;
406 case RD_I_CNT:
407 printf("%lu\n", data->value.u_cnt);
408 break;
409 case RD_I_INT:
410 printf("%d\n", data->value.u_int);
411 break;
412 case RD_I_STR:
413 printf("\"%s\"\n", data->value.u_str);
414 break;
415 case RD_I_BLO:
416 printf("BLOB_SIZE:%lu\n", data->value.u_blo.size);
417 fwrite(data->value.u_blo.ptr, data->value.u_blo.size, 1, stdout);
418 break;
419 }
420 data = data->next;
421 }
422 }
424 void rrd_info_free(
425 rrd_info_t * data)
426 {
427 rrd_info_t *save;
429 while (data) {
430 save = data;
431 if (data->key) {
432 if (data->type == RD_I_STR) {
433 free(data->value.u_str);
434 }
435 if (data->type == RD_I_BLO) {
436 free(data->value.u_blo.ptr);
437 }
438 free(data->key);
439 }
440 data = data->next;
441 free(save);
442 }
443 }