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