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 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
169 info.u_str = rrd.ds_def[i].dst;
170 cd = rrd_info_push(cd, sprintf_alloc("ds[%s].type",
171 rrd.ds_def[i].ds_nam),
174 current_ds = dst_conv(rrd.ds_def[i].dst);
175 switch (current_ds) {
180 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
181 rrd.ds_def, &buffer);
183 cd = rrd_info_push(cd,
184 sprintf_alloc("ds[%s].cdef",
185 rrd.ds_def[i].ds_nam), RD_I_STR,
191 info.u_cnt = rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt;
192 cd = rrd_info_push(cd,
193 sprintf_alloc("ds[%s].minimal_heartbeat",
194 rrd.ds_def[i].ds_nam), RD_I_CNT,
197 info.u_val = rrd.ds_def[i].par[DS_min_val].u_val;
198 cd = rrd_info_push(cd,
199 sprintf_alloc("ds[%s].min",
200 rrd.ds_def[i].ds_nam), RD_I_VAL,
203 info.u_val = rrd.ds_def[i].par[DS_max_val].u_val;
204 cd = rrd_info_push(cd,
205 sprintf_alloc("ds[%s].max",
206 rrd.ds_def[i].ds_nam), RD_I_VAL,
211 info.u_str = rrd.pdp_prep[i].last_ds;
212 cd = rrd_info_push(cd,
213 sprintf_alloc("ds[%s].last_ds",
214 rrd.ds_def[i].ds_nam), RD_I_STR,
217 info.u_val = rrd.pdp_prep[i].scratch[PDP_val].u_val;
218 cd = rrd_info_push(cd,
219 sprintf_alloc("ds[%s].value",
220 rrd.ds_def[i].ds_nam), RD_I_VAL,
223 info.u_cnt = rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt;
224 cd = rrd_info_push(cd,
225 sprintf_alloc("ds[%s].unknown_sec",
226 rrd.ds_def[i].ds_nam), RD_I_CNT,
230 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
231 info.u_str = rrd.rra_def[i].cf_nam;
232 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].cf", i), RD_I_STR,
234 current_cf = cf_conv(rrd.rra_def[i].cf_nam);
236 info.u_cnt = rrd.rra_def[i].row_cnt;
237 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].rows", i), RD_I_CNT,
240 info.u_cnt = rrd.rra_ptr[i].cur_row;
241 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].cur_row", i), RD_I_CNT,
244 info.u_cnt = rrd.rra_def[i].pdp_cnt;
245 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].pdp_per_row", i),
248 switch (current_cf) {
251 info.u_val = rrd.rra_def[i].par[RRA_hw_alpha].u_val;
252 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].alpha", i),
254 info.u_val = rrd.rra_def[i].par[RRA_hw_beta].u_val;
255 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].beta", i), RD_I_VAL,
260 info.u_val = rrd.rra_def[i].par[RRA_seasonal_gamma].u_val;
261 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].gamma", i),
263 if (atoi(rrd.stat_head->version) >= 4) {
265 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val;
266 cd = rrd_info_push(cd,
267 sprintf_alloc("rra[%d].smoothing_window",
272 info.u_val = rrd.rra_def[i].par[RRA_delta_pos].u_val;
273 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].delta_pos", i),
275 info.u_val = rrd.rra_def[i].par[RRA_delta_neg].u_val;
276 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].delta_neg", i),
278 info.u_cnt = rrd.rra_def[i].par[RRA_failure_threshold].u_cnt;
279 cd = rrd_info_push(cd,
280 sprintf_alloc("rra[%d].failure_threshold", i),
282 info.u_cnt = rrd.rra_def[i].par[RRA_window_len].u_cnt;
283 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].window_length", i),
289 info.u_val = rrd.rra_def[i].par[RRA_cdp_xff_val].u_val;
290 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].xff", i), RD_I_VAL,
295 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
296 switch (current_cf) {
300 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
301 ii].scratch[CDP_hw_intercept].u_val;
302 cd = rrd_info_push(cd,
304 ("rra[%d].cdp_prep[%d].intercept", i, ii),
307 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
308 ii].scratch[CDP_hw_slope].u_val;
309 cd = rrd_info_push(cd,
310 sprintf_alloc("rra[%d].cdp_prep[%d].slope",
311 i, ii), RD_I_VAL, info);
313 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
314 ii].scratch[CDP_null_count].u_cnt;
315 cd = rrd_info_push(cd,
317 ("rra[%d].cdp_prep[%d].NaN_count", i, ii),
322 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
323 ii].scratch[CDP_hw_seasonal].u_val;
324 cd = rrd_info_push(cd,
326 ("rra[%d].cdp_prep[%d].seasonal", i, ii),
331 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
332 ii].scratch[CDP_seasonal_deviation].u_val;
333 cd = rrd_info_push(cd,
335 ("rra[%d].cdp_prep[%d].deviation", i, ii),
343 char *violations_array;
344 char history[MAX_FAILURES_WINDOW_LEN + 1];
347 (char *) rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
349 for (j = 0; j < rrd.rra_def[i].par[RRA_window_len].u_cnt; ++j)
350 history[j] = (violations_array[j] == 1) ? '1' : '0';
352 info.u_str = history;
353 cd = rrd_info_push(cd,
355 ("rra[%d].cdp_prep[%d].history", i, ii),
361 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
362 ii].scratch[CDP_val].u_val;
363 cd = rrd_info_push(cd,
364 sprintf_alloc("rra[%d].cdp_prep[%d].value",
365 i, ii), RD_I_VAL, info);
367 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
368 ii].scratch[CDP_unkn_pdp_cnt].u_cnt;
369 cd = rrd_info_push(cd,
371 ("rra[%d].cdp_prep[%d].unknown_datapoints",
372 i, ii), RD_I_CNT, info);
389 printf("%s = ", data->key);
391 switch (data->type) {
393 if (isnan(data->value.u_val))
396 printf("%0.10e\n", data->value.u_val);
399 printf("%lu\n", data->value.u_cnt);
402 printf("%d\n", data->value.u_int);
405 printf("\"%s\"\n", data->value.u_str);
408 printf("BLOB_SIZE:%lu\n", data->value.u_blo.size);
409 fwrite(data->value.u_blo.ptr, data->value.u_blo.size, 1, stdout);
424 if (data->type == RD_I_STR) {
425 free(data->value.u_str);
427 if (data->type == RD_I_BLO) {
428 free(data->value.u_blo.ptr);