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