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 *****************************************************************************/
8 #include "rrd_rpncalc.h"
9 #include "rrd_client.h"
16 rrd_info_t *rrd_info_r(
19 /* allocate memory for string */
24 int maxlen = 1024 + strlen(fmt);
27 str = (char*)malloc(sizeof(char) * (maxlen + 1));
31 vsnprintf(str, maxlen, fmt, argp);
33 vsprintf(str, fmt, argp);
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 */
43 * rrd_info_push(rrd_info_t * info,
44 char *key, rrd_info_type_t type, rrd_infoval_t value)
48 next = (rrd_info_t*)malloc(sizeof(*next));
49 next->next = (rrd_info_t *) 0;
56 next->value.u_val = value.u_val;
59 next->value.u_cnt = value.u_cnt;
62 next->value.u_int = value.u_int;
65 next->value.u_str = (char*)malloc(sizeof(char) * (strlen(value.u_str) + 1));
66 strcpy(next->value.u_str, value.u_str);
69 next->value.u_blo.size = value.u_blo.size;
70 next->value.u_blo.ptr =
71 (unsigned char *)malloc(sizeof(unsigned char) * value.u_blo.size);
72 memcpy(next->value.u_blo.ptr, value.u_blo.ptr, value.u_blo.size);
84 char *opt_daemon = NULL;
88 opterr = 0; /* initialize getopt */
93 static struct option long_options[] = {
94 {"daemon", required_argument, 0, 'd'},
98 opt = getopt_long(argc, argv, "d:", long_options, &option_index);
105 if (opt_daemon != NULL)
107 opt_daemon = strdup (optarg);
108 if (opt_daemon == NULL)
110 rrd_set_error ("strdup failed.");
116 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
123 if ((argc - optind) != 1) {
124 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
129 status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
130 if (opt_daemon) free (opt_daemon);
131 if (status) return (NULL);
133 info = rrd_info_r(argv[optind]);
136 } /* rrd_info_t *rrd_info */
138 rrd_info_t *rrd_info_r(
141 unsigned int i, ii = 0;
143 rrd_info_t *data = NULL, *cd;
145 rrd_file_t *rrd_file;
146 enum cf_en current_cf;
147 enum dst_en current_ds;
150 rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
151 if (rrd_file == NULL)
154 info.u_str = filename;
155 cd = rrd_info_push(NULL, sprintf_alloc("filename"), RD_I_STR, info);
158 info.u_str = rrd.stat_head->version;
159 cd = rrd_info_push(cd, sprintf_alloc("rrd_version"), RD_I_STR, info);
161 info.u_cnt = rrd.stat_head->pdp_step;
162 cd = rrd_info_push(cd, sprintf_alloc("step"), RD_I_CNT, info);
164 info.u_cnt = rrd.live_head->last_up;
165 cd = rrd_info_push(cd, sprintf_alloc("last_update"), RD_I_CNT, info);
167 info.u_cnt = rrd_get_header_size(&rrd);
168 cd = rrd_info_push(cd, sprintf_alloc("header_size"), RD_I_CNT, info);
170 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
172 info.u_str = rrd.ds_def[i].dst;
173 cd = rrd_info_push(cd, sprintf_alloc("ds[%s].type",
174 rrd.ds_def[i].ds_nam),
177 current_ds = dst_conv(rrd.ds_def[i].dst);
178 switch (current_ds) {
183 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
184 rrd.ds_def, &buffer);
186 cd = rrd_info_push(cd,
187 sprintf_alloc("ds[%s].cdef",
188 rrd.ds_def[i].ds_nam), RD_I_STR,
194 info.u_cnt = rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt;
195 cd = rrd_info_push(cd,
196 sprintf_alloc("ds[%s].minimal_heartbeat",
197 rrd.ds_def[i].ds_nam), RD_I_CNT,
200 info.u_val = rrd.ds_def[i].par[DS_min_val].u_val;
201 cd = rrd_info_push(cd,
202 sprintf_alloc("ds[%s].min",
203 rrd.ds_def[i].ds_nam), RD_I_VAL,
206 info.u_val = rrd.ds_def[i].par[DS_max_val].u_val;
207 cd = rrd_info_push(cd,
208 sprintf_alloc("ds[%s].max",
209 rrd.ds_def[i].ds_nam), RD_I_VAL,
214 info.u_str = rrd.pdp_prep[i].last_ds;
215 cd = rrd_info_push(cd,
216 sprintf_alloc("ds[%s].last_ds",
217 rrd.ds_def[i].ds_nam), RD_I_STR,
220 info.u_val = rrd.pdp_prep[i].scratch[PDP_val].u_val;
221 cd = rrd_info_push(cd,
222 sprintf_alloc("ds[%s].value",
223 rrd.ds_def[i].ds_nam), RD_I_VAL,
226 info.u_cnt = rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt;
227 cd = rrd_info_push(cd,
228 sprintf_alloc("ds[%s].unknown_sec",
229 rrd.ds_def[i].ds_nam), RD_I_CNT,
233 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
234 info.u_str = rrd.rra_def[i].cf_nam;
235 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].cf", i), RD_I_STR,
237 current_cf = cf_conv(rrd.rra_def[i].cf_nam);
239 info.u_cnt = rrd.rra_def[i].row_cnt;
240 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].rows", i), RD_I_CNT,
243 info.u_cnt = rrd.rra_ptr[i].cur_row;
244 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].cur_row", i), RD_I_CNT,
247 info.u_cnt = rrd.rra_def[i].pdp_cnt;
248 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].pdp_per_row", i),
251 switch (current_cf) {
254 info.u_val = rrd.rra_def[i].par[RRA_hw_alpha].u_val;
255 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].alpha", i),
257 info.u_val = rrd.rra_def[i].par[RRA_hw_beta].u_val;
258 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].beta", i), RD_I_VAL,
263 info.u_val = rrd.rra_def[i].par[RRA_seasonal_gamma].u_val;
264 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].gamma", i),
266 if (atoi(rrd.stat_head->version) >= 4) {
268 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val;
269 cd = rrd_info_push(cd,
270 sprintf_alloc("rra[%d].smoothing_window",
275 info.u_val = rrd.rra_def[i].par[RRA_delta_pos].u_val;
276 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].delta_pos", i),
278 info.u_val = rrd.rra_def[i].par[RRA_delta_neg].u_val;
279 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].delta_neg", i),
281 info.u_cnt = rrd.rra_def[i].par[RRA_failure_threshold].u_cnt;
282 cd = rrd_info_push(cd,
283 sprintf_alloc("rra[%d].failure_threshold", i),
285 info.u_cnt = rrd.rra_def[i].par[RRA_window_len].u_cnt;
286 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].window_length", i),
292 info.u_val = rrd.rra_def[i].par[RRA_cdp_xff_val].u_val;
293 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].xff", i), RD_I_VAL,
298 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
299 switch (current_cf) {
303 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
304 ii].scratch[CDP_hw_intercept].u_val;
305 cd = rrd_info_push(cd,
307 ("rra[%d].cdp_prep[%d].intercept", i, ii),
310 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
311 ii].scratch[CDP_hw_slope].u_val;
312 cd = rrd_info_push(cd,
313 sprintf_alloc("rra[%d].cdp_prep[%d].slope",
314 i, ii), RD_I_VAL, info);
316 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
317 ii].scratch[CDP_null_count].u_cnt;
318 cd = rrd_info_push(cd,
320 ("rra[%d].cdp_prep[%d].NaN_count", i, ii),
325 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
326 ii].scratch[CDP_hw_seasonal].u_val;
327 cd = rrd_info_push(cd,
329 ("rra[%d].cdp_prep[%d].seasonal", i, ii),
334 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
335 ii].scratch[CDP_seasonal_deviation].u_val;
336 cd = rrd_info_push(cd,
338 ("rra[%d].cdp_prep[%d].deviation", i, ii),
346 char *violations_array;
347 char history[MAX_FAILURES_WINDOW_LEN + 1];
350 (char *) rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
352 for (j = 0; j < rrd.rra_def[i].par[RRA_window_len].u_cnt; ++j)
353 history[j] = (violations_array[j] == 1) ? '1' : '0';
355 info.u_str = history;
356 cd = rrd_info_push(cd,
358 ("rra[%d].cdp_prep[%d].history", i, ii),
364 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
365 ii].scratch[CDP_val].u_val;
366 cd = rrd_info_push(cd,
367 sprintf_alloc("rra[%d].cdp_prep[%d].value",
368 i, ii), RD_I_VAL, info);
370 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
371 ii].scratch[CDP_unkn_pdp_cnt].u_cnt;
372 cd = rrd_info_push(cd,
374 ("rra[%d].cdp_prep[%d].unknown_datapoints",
375 i, ii), RD_I_CNT, info);
392 printf("%s = ", data->key);
394 switch (data->type) {
396 if (isnan(data->value.u_val))
399 printf("%0.10e\n", data->value.u_val);
402 printf("%lu\n", data->value.u_cnt);
405 printf("%d\n", data->value.u_int);
408 printf("\"%s\"\n", data->value.u_str);
411 printf("BLOB_SIZE:%lu\n", data->value.u_blo.size);
412 fwrite(data->value.u_blo.ptr, data->value.u_blo.size, 1, stdout);
427 if (data->type == RD_I_STR) {
428 free(data->value.u_str);
430 if (data->type == RD_I_BLO) {
431 free(data->value.u_blo.ptr);