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 */
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;
97 opterr = 0; /* initialize getopt */
101 int option_index = 0;
102 static struct option long_options[] = {
103 {"daemon", required_argument, 0, 'd'},
107 opt = getopt_long(argc, argv, "d:", long_options, &option_index);
114 if (opt_daemon != NULL)
116 opt_daemon = strdup (optarg);
117 if (opt_daemon == NULL)
119 rrd_set_error ("strdup failed.");
125 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
132 if ((argc - optind) != 1) {
133 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
138 status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
139 if (opt_daemon) free (opt_daemon);
140 if (status) return (NULL);
142 info = rrd_info_r(argv[optind]);
145 } /* rrd_info_t *rrd_info */
147 rrd_info_t *rrd_info_r(
150 unsigned int i, ii = 0;
152 rrd_info_t *data = NULL, *cd;
154 rrd_file_t *rrd_file;
155 enum cf_en current_cf;
156 enum dst_en current_ds;
159 rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
160 if (rrd_file == NULL)
163 info.u_str = filename;
164 cd = rrd_info_push(NULL, sprintf_alloc("filename"), RD_I_STR, info);
167 info.u_str = rrd.stat_head->version;
168 cd = rrd_info_push(cd, sprintf_alloc("rrd_version"), RD_I_STR, info);
170 info.u_cnt = rrd.stat_head->pdp_step;
171 cd = rrd_info_push(cd, sprintf_alloc("step"), RD_I_CNT, info);
173 info.u_cnt = rrd.live_head->last_up;
174 cd = rrd_info_push(cd, sprintf_alloc("last_update"), RD_I_CNT, info);
176 info.u_cnt = rrd_get_header_size(&rrd);
177 cd = rrd_info_push(cd, sprintf_alloc("header_size"), RD_I_CNT, info);
179 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
181 info.u_str = rrd.ds_def[i].dst;
182 cd = rrd_info_push(cd, sprintf_alloc("ds[%s].type",
183 rrd.ds_def[i].ds_nam),
186 current_ds = dst_conv(rrd.ds_def[i].dst);
187 switch (current_ds) {
192 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
193 rrd.ds_def, &buffer);
195 cd = rrd_info_push(cd,
196 sprintf_alloc("ds[%s].cdef",
197 rrd.ds_def[i].ds_nam), RD_I_STR,
203 info.u_cnt = rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt;
204 cd = rrd_info_push(cd,
205 sprintf_alloc("ds[%s].minimal_heartbeat",
206 rrd.ds_def[i].ds_nam), RD_I_CNT,
209 info.u_val = rrd.ds_def[i].par[DS_min_val].u_val;
210 cd = rrd_info_push(cd,
211 sprintf_alloc("ds[%s].min",
212 rrd.ds_def[i].ds_nam), RD_I_VAL,
215 info.u_val = rrd.ds_def[i].par[DS_max_val].u_val;
216 cd = rrd_info_push(cd,
217 sprintf_alloc("ds[%s].max",
218 rrd.ds_def[i].ds_nam), RD_I_VAL,
223 info.u_str = rrd.pdp_prep[i].last_ds;
224 cd = rrd_info_push(cd,
225 sprintf_alloc("ds[%s].last_ds",
226 rrd.ds_def[i].ds_nam), RD_I_STR,
229 info.u_val = rrd.pdp_prep[i].scratch[PDP_val].u_val;
230 cd = rrd_info_push(cd,
231 sprintf_alloc("ds[%s].value",
232 rrd.ds_def[i].ds_nam), RD_I_VAL,
235 info.u_cnt = rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt;
236 cd = rrd_info_push(cd,
237 sprintf_alloc("ds[%s].unknown_sec",
238 rrd.ds_def[i].ds_nam), RD_I_CNT,
242 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
243 info.u_str = rrd.rra_def[i].cf_nam;
244 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].cf", i), RD_I_STR,
246 current_cf = cf_conv(rrd.rra_def[i].cf_nam);
248 info.u_cnt = rrd.rra_def[i].row_cnt;
249 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].rows", i), RD_I_CNT,
252 info.u_cnt = rrd.rra_ptr[i].cur_row;
253 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].cur_row", i), RD_I_CNT,
256 info.u_cnt = rrd.rra_def[i].pdp_cnt;
257 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].pdp_per_row", i),
260 switch (current_cf) {
263 info.u_val = rrd.rra_def[i].par[RRA_hw_alpha].u_val;
264 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].alpha", i),
266 info.u_val = rrd.rra_def[i].par[RRA_hw_beta].u_val;
267 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].beta", i), RD_I_VAL,
272 info.u_val = rrd.rra_def[i].par[RRA_seasonal_gamma].u_val;
273 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].gamma", i),
275 if (atoi(rrd.stat_head->version) >= 4) {
277 rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val;
278 cd = rrd_info_push(cd,
279 sprintf_alloc("rra[%d].smoothing_window",
284 info.u_val = rrd.rra_def[i].par[RRA_delta_pos].u_val;
285 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].delta_pos", i),
287 info.u_val = rrd.rra_def[i].par[RRA_delta_neg].u_val;
288 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].delta_neg", i),
290 info.u_cnt = rrd.rra_def[i].par[RRA_failure_threshold].u_cnt;
291 cd = rrd_info_push(cd,
292 sprintf_alloc("rra[%d].failure_threshold", i),
294 info.u_cnt = rrd.rra_def[i].par[RRA_window_len].u_cnt;
295 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].window_length", i),
301 info.u_val = rrd.rra_def[i].par[RRA_cdp_xff_val].u_val;
302 cd = rrd_info_push(cd, sprintf_alloc("rra[%d].xff", i), RD_I_VAL,
307 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
308 switch (current_cf) {
312 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
313 ii].scratch[CDP_hw_intercept].u_val;
314 cd = rrd_info_push(cd,
316 ("rra[%d].cdp_prep[%d].intercept", i, ii),
319 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
320 ii].scratch[CDP_hw_slope].u_val;
321 cd = rrd_info_push(cd,
322 sprintf_alloc("rra[%d].cdp_prep[%d].slope",
323 i, ii), RD_I_VAL, info);
325 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
326 ii].scratch[CDP_null_count].u_cnt;
327 cd = rrd_info_push(cd,
329 ("rra[%d].cdp_prep[%d].NaN_count", i, ii),
334 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
335 ii].scratch[CDP_hw_seasonal].u_val;
336 cd = rrd_info_push(cd,
338 ("rra[%d].cdp_prep[%d].seasonal", i, ii),
343 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
344 ii].scratch[CDP_seasonal_deviation].u_val;
345 cd = rrd_info_push(cd,
347 ("rra[%d].cdp_prep[%d].deviation", i, ii),
355 char *violations_array;
356 char history[MAX_FAILURES_WINDOW_LEN + 1];
359 (char *) rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
361 for (j = 0; j < rrd.rra_def[i].par[RRA_window_len].u_cnt; ++j)
362 history[j] = (violations_array[j] == 1) ? '1' : '0';
364 info.u_str = history;
365 cd = rrd_info_push(cd,
367 ("rra[%d].cdp_prep[%d].history", i, ii),
373 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
374 ii].scratch[CDP_val].u_val;
375 cd = rrd_info_push(cd,
376 sprintf_alloc("rra[%d].cdp_prep[%d].value",
377 i, ii), RD_I_VAL, info);
379 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
380 ii].scratch[CDP_unkn_pdp_cnt].u_cnt;
381 cd = rrd_info_push(cd,
383 ("rra[%d].cdp_prep[%d].unknown_datapoints",
384 i, ii), RD_I_CNT, info);
401 printf("%s = ", data->key);
403 switch (data->type) {
405 if (isnan(data->value.u_val))
408 printf("%0.10e\n", data->value.u_val);
411 printf("%lu\n", data->value.u_cnt);
414 printf("%d\n", data->value.u_int);
417 printf("\"%s\"\n", data->value.u_str);
420 printf("BLOB_SIZE:%lu\n", data->value.u_blo.size);
421 fwrite(data->value.u_blo.ptr, data->value.u_blo.size, 1, stdout);
436 if (data->type == RD_I_STR) {
437 free(data->value.u_str);
439 if (data->type == RD_I_BLO) {
440 free(data->value.u_blo.ptr);