1 /*****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
3 *****************************************************************************
4 * rrd_info Get Information about the configuration of an RRD
5 *****************************************************************************/
8 #include "rrd_rpncalc.h"
9 #include "rrd_client.h"
16 rrd_info_t *rrd_info_r(
19 /* allocate memory for string */
27 va_start( argp, fmt );
28 if (vasprintf( &str, fmt, argp ) == -1){
30 rrd_set_error ("vasprintf failed.");
34 int maxlen = 1024 + strlen(fmt);
35 str = (char*)malloc(sizeof(char) * (maxlen + 1));
39 vsnprintf(str, maxlen, fmt, argp);
41 vsprintf(str, fmt, argp);
44 #endif /* HAVE_VASPRINTF */
49 /* the function formerly known as push was renamed to info_push and later
50 * rrd_info_push because it is now used outside the scope of this file */
52 * rrd_info_push(rrd_info_t * info,
53 char *key, rrd_info_type_t type, rrd_infoval_t value)
57 next = (rrd_info_t*)malloc(sizeof(*next));
58 next->next = (rrd_info_t *) 0;
65 next->value.u_val = value.u_val;
68 next->value.u_cnt = value.u_cnt;
71 next->value.u_int = value.u_int;
74 next->value.u_str = (char*)malloc(sizeof(char) * (strlen(value.u_str) + 1));
75 strcpy(next->value.u_str, value.u_str);
78 next->value.u_blo.size = value.u_blo.size;
79 next->value.u_blo.ptr =
80 (unsigned char *)malloc(sizeof(unsigned char) * value.u_blo.size);
81 memcpy(next->value.u_blo.ptr, value.u_blo.ptr, value.u_blo.size);
93 char *opt_daemon = NULL;
98 opterr = 0; /* initialize getopt */
102 int option_index = 0;
103 static struct option long_options[] = {
104 {"daemon", required_argument, 0, 'd'},
105 {"noflush", no_argument, 0, 'F'},
109 opt = getopt_long(argc, argv, "d:F", long_options, &option_index);
116 if (opt_daemon != NULL)
118 opt_daemon = strdup (optarg);
119 if (opt_daemon == NULL)
121 rrd_set_error ("strdup failed.");
131 rrd_set_error ("Usage: rrdtool %s [--daemon <addr> [--noflush]] <file>",
138 if ((argc - optind) != 1) {
139 rrd_set_error ("Usage: rrdtool %s [--daemon <addr> [--noflush]] <file>",
145 status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
146 if (status) return (NULL);
149 rrdc_connect (opt_daemon);
150 if (rrdc_is_connected (opt_daemon))
151 info = rrdc_info (argv[optind]);
153 info = rrd_info_r(argv[optind]);
156 } /* rrd_info_t *rrd_info */
158 rrd_info_t *rrd_info_r(
161 unsigned int i, ii = 0;
163 rrd_info_t *data = NULL, *cd;
165 rrd_file_t *rrd_file;
166 enum cf_en current_cf;
167 enum dst_en current_ds;
170 rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
171 if (rrd_file == NULL)
174 info.u_str = filename;
175 cd = rrd_info_push(NULL, sprintf_alloc("filename"), RD_I_STR, info);
178 info.u_str = rrd.stat_head->version;
179 cd = rrd_info_push(cd, sprintf_alloc("rrd_version"), RD_I_STR, info);
181 info.u_cnt = rrd.stat_head->pdp_step;
182 cd = rrd_info_push(cd, sprintf_alloc("step"), RD_I_CNT, info);
184 info.u_cnt = rrd.live_head->last_up;
185 cd = rrd_info_push(cd, sprintf_alloc("last_update"), RD_I_CNT, info);
187 info.u_cnt = rrd_get_header_size(&rrd);
188 cd = rrd_info_push(cd, sprintf_alloc("header_size"), RD_I_CNT, info);
190 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
193 cd= rrd_info_push(cd,sprintf_alloc("ds[%s].index",
194 rrd.ds_def[i].ds_nam),
197 info.u_str = rrd.ds_def[i].dst;
198 cd = rrd_info_push(cd, sprintf_alloc("ds[%s].type",
199 rrd.ds_def[i].ds_nam),
202 current_ds = dst_conv(rrd.ds_def[i].dst);
203 switch (current_ds) {
208 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
209 rrd.ds_def, &buffer);
211 cd = rrd_info_push(cd,
212 sprintf_alloc("ds[%s].cdef",
213 rrd.ds_def[i].ds_nam), RD_I_STR,
219 info.u_cnt = rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt;
220 cd = rrd_info_push(cd,
221 sprintf_alloc("ds[%s].minimal_heartbeat",
222 rrd.ds_def[i].ds_nam), RD_I_CNT,
225 info.u_val = rrd.ds_def[i].par[DS_min_val].u_val;
226 cd = rrd_info_push(cd,
227 sprintf_alloc("ds[%s].min",
228 rrd.ds_def[i].ds_nam), RD_I_VAL,
231 info.u_val = rrd.ds_def[i].par[DS_max_val].u_val;
232 cd = rrd_info_push(cd,
233 sprintf_alloc("ds[%s].max",
234 rrd.ds_def[i].ds_nam), RD_I_VAL,
239 info.u_str = rrd.pdp_prep[i].last_ds;
240 cd = rrd_info_push(cd,
241 sprintf_alloc("ds[%s].last_ds",
242 rrd.ds_def[i].ds_nam), RD_I_STR,
245 info.u_val = rrd.pdp_prep[i].scratch[PDP_val].u_val;
246 cd = rrd_info_push(cd,
247 sprintf_alloc("ds[%s].value",
248 rrd.ds_def[i].ds_nam), RD_I_VAL,
251 info.u_cnt = rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt;
252 cd = rrd_info_push(cd,
253 sprintf_alloc("ds[%s].unknown_sec",
254 rrd.ds_def[i].ds_nam), RD_I_CNT,
258 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
259 info.u_str = rrd.rra_def[i].cf_nam;
260 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].cf", i), RD_I_STR,
262 current_cf = cf_conv(rrd.rra_def[i].cf_nam);
264 info.u_cnt = rrd.rra_def[i].row_cnt;
265 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].rows", i), RD_I_CNT,
268 info.u_cnt = rrd.rra_ptr[i].cur_row;
269 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].cur_row", i), RD_I_CNT,
272 info.u_cnt = rrd.rra_def[i].pdp_cnt;
273 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].pdp_per_row", i),
276 switch (current_cf) {
279 info.u_val = rrd.rra_def[i].par[RRA_hw_alpha].u_val;
280 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].alpha", i),
282 info.u_val = rrd.rra_def[i].par[RRA_hw_beta].u_val;
283 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].beta", i), RD_I_VAL,
288 info.u_val = rrd.rra_def[i].par[RRA_seasonal_gamma].u_val;
289 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].gamma", i),
291 if (atoi(rrd.stat_head->version) >= 4) {
293 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val;
294 cd = rrd_info_push(cd,
295 sprintf_alloc("rra[%d].smoothing_window",
300 info.u_val = rrd.rra_def[i].par[RRA_delta_pos].u_val;
301 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].delta_pos", i),
303 info.u_val = rrd.rra_def[i].par[RRA_delta_neg].u_val;
304 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].delta_neg", i),
306 info.u_cnt = rrd.rra_def[i].par[RRA_failure_threshold].u_cnt;
307 cd = rrd_info_push(cd,
308 sprintf_alloc("rra[%d].failure_threshold", i),
310 info.u_cnt = rrd.rra_def[i].par[RRA_window_len].u_cnt;
311 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].window_length", i),
317 info.u_val = rrd.rra_def[i].par[RRA_cdp_xff_val].u_val;
318 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].xff", i), RD_I_VAL,
323 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
324 switch (current_cf) {
328 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
329 ii].scratch[CDP_hw_intercept].u_val;
330 cd = rrd_info_push(cd,
332 ("rra[%d].cdp_prep[%d].intercept", i, ii),
335 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
336 ii].scratch[CDP_hw_slope].u_val;
337 cd = rrd_info_push(cd,
338 sprintf_alloc("rra[%d].cdp_prep[%d].slope",
339 i, ii), RD_I_VAL, info);
341 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
342 ii].scratch[CDP_null_count].u_cnt;
343 cd = rrd_info_push(cd,
345 ("rra[%d].cdp_prep[%d].NaN_count", i, ii),
350 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
351 ii].scratch[CDP_hw_seasonal].u_val;
352 cd = rrd_info_push(cd,
354 ("rra[%d].cdp_prep[%d].seasonal", i, ii),
359 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
360 ii].scratch[CDP_seasonal_deviation].u_val;
361 cd = rrd_info_push(cd,
363 ("rra[%d].cdp_prep[%d].deviation", i, ii),
371 char *violations_array;
372 char history[MAX_FAILURES_WINDOW_LEN + 1];
375 (char *) rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
377 for (j = 0; j < rrd.rra_def[i].par[RRA_window_len].u_cnt; ++j)
378 history[j] = (violations_array[j] == 1) ? '1' : '0';
380 info.u_str = history;
381 cd = rrd_info_push(cd,
383 ("rra[%d].cdp_prep[%d].history", i, ii),
389 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
390 ii].scratch[CDP_val].u_val;
391 cd = rrd_info_push(cd,
392 sprintf_alloc("rra[%d].cdp_prep[%d].value",
393 i, ii), RD_I_VAL, info);
395 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
396 ii].scratch[CDP_unkn_pdp_cnt].u_cnt;
397 cd = rrd_info_push(cd,
399 ("rra[%d].cdp_prep[%d].unknown_datapoints",
400 i, ii), RD_I_CNT, info);
417 printf("%s = ", data->key);
419 switch (data->type) {
421 if (isnan(data->value.u_val))
424 printf("%0.10e\n", data->value.u_val);
427 printf("%lu\n", data->value.u_cnt);
430 printf("%d\n", data->value.u_int);
433 printf("\"%s\"\n", data->value.u_str);
436 printf("BLOB_SIZE:%lu\n", data->value.u_blo.size);
437 fwrite(data->value.u_blo.ptr, data->value.u_blo.size, 1, stdout);
452 if (data->type == RD_I_STR) {
453 free(data->value.u_str);
455 if (data->type == RD_I_BLO) {
456 free(data->value.u_blo.ptr);