d86a9074d90e631184d04e758652fa7788375c84
[rrdtool.git] / src / rrd_info.c
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  *****************************************************************************/
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     int flushfirst = 1;
96
97     optind = 0;
98     opterr = 0;         /* initialize getopt */
99
100     while (42) {
101         int       opt;
102         int       option_index = 0;
103         static struct option long_options[] = {
104             {"daemon", required_argument, 0, 'd'},
105             {"noflush", no_argument, 0, 'F'},
106             {0, 0, 0, 0}
107         };
108
109         opt = getopt_long(argc, argv, "d:F", long_options, &option_index);
110
111         if (opt == EOF)
112             break;
113
114         switch (opt) {
115         case 'd':
116             if (opt_daemon != NULL)
117                     free (opt_daemon);
118             opt_daemon = strdup (optarg);
119             if (opt_daemon == NULL)
120             {
121                 rrd_set_error ("strdup failed.");
122                 return (NULL);
123             }
124             break;
125
126         case 'F':
127             flushfirst = 0;
128             break;
129
130         default:
131             rrd_set_error ("Usage: rrdtool %s [--daemon <addr> [--noflush]] <file>",
132                     argv[0]);
133             return (NULL);
134             break;
135         }
136     }                   /* while (42) */
137
138     if ((argc - optind) != 1) {
139         rrd_set_error ("Usage: rrdtool %s [--daemon <addr> [--noflush]] <file>",
140                 argv[0]);
141         return (NULL);
142     }
143
144     if( flushfirst ) {
145     status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
146     if (status) return (NULL);
147     }
148
149     rrdc_connect (opt_daemon);
150     if (rrdc_is_connected (opt_daemon))
151         info = rrdc_info (argv[optind]);
152     else
153     info = rrd_info_r(argv[optind]);
154
155     return (info);
156 } /* rrd_info_t *rrd_info */
157
158 rrd_info_t *rrd_info_r(
159     char *filename)
160 {
161     unsigned int i, ii = 0;
162     rrd_t     rrd;
163     rrd_info_t *data = NULL, *cd;
164     rrd_infoval_t info;
165     rrd_file_t *rrd_file;
166     enum cf_en current_cf;
167     enum dst_en current_ds;
168
169     rrd_init(&rrd);
170     rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
171     if (rrd_file == NULL)
172         goto err_free;
173
174     info.u_str = filename;
175     cd = rrd_info_push(NULL, sprintf_alloc("filename"), RD_I_STR, info);
176     data = cd;
177
178     info.u_str = rrd.stat_head->version;
179     cd = rrd_info_push(cd, sprintf_alloc("rrd_version"), RD_I_STR, info);
180
181     info.u_cnt = rrd.stat_head->pdp_step;
182     cd = rrd_info_push(cd, sprintf_alloc("step"), RD_I_CNT, info);
183
184     info.u_cnt = rrd.live_head->last_up;
185     cd = rrd_info_push(cd, sprintf_alloc("last_update"), RD_I_CNT, info);
186
187     info.u_cnt = rrd_get_header_size(&rrd);
188     cd = rrd_info_push(cd, sprintf_alloc("header_size"), RD_I_CNT, info);
189
190     for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
191
192         info.u_cnt=i;
193         cd= rrd_info_push(cd,sprintf_alloc("ds[%s].index",
194                                      rrd.ds_def[i].ds_nam),
195                      RD_I_CNT, info);
196     
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),
200                            RD_I_STR, info);
201
202         current_ds = dst_conv(rrd.ds_def[i].dst);
203         switch (current_ds) {
204         case DST_CDEF:
205         {
206             char     *buffer = NULL;
207
208             rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
209                             rrd.ds_def, &buffer);
210             info.u_str = buffer;
211             cd = rrd_info_push(cd,
212                                sprintf_alloc("ds[%s].cdef",
213                                              rrd.ds_def[i].ds_nam), RD_I_STR,
214                                info);
215             free(buffer);
216         }
217             break;
218         default:
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,
223                                info);
224
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,
229                                info);
230
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,
235                                info);
236             break;
237         }
238
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,
243                            info);
244
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,
249                            info);
250
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,
255                            info);
256     }
257
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,
261                            info);
262         current_cf = cf_conv(rrd.rra_def[i].cf_nam);
263
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,
266                            info);
267
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,
270                            info);
271
272         info.u_cnt = rrd.rra_def[i].pdp_cnt;
273         cd = rrd_info_push(cd, sprintf_alloc("rra[%d].pdp_per_row", i),
274                            RD_I_CNT, info);
275
276         switch (current_cf) {
277         case CF_HWPREDICT:
278         case CF_MHWPREDICT:
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),
281                                RD_I_VAL, info);
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,
284                                info);
285             break;
286         case CF_SEASONAL:
287         case CF_DEVSEASONAL:
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),
290                                RD_I_VAL, info);
291             if (atoi(rrd.stat_head->version) >= 4) {
292                 info.u_val =
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",
296                                                  i), RD_I_VAL, info);
297             }
298             break;
299         case CF_FAILURES:
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),
302                                RD_I_VAL, info);
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),
305                                RD_I_VAL, info);
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),
309                                RD_I_CNT, info);
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),
312                                RD_I_CNT, info);
313             break;
314         case CF_DEVPREDICT:
315             break;
316         default:
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,
319                                info);
320             break;
321         }
322
323         for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
324             switch (current_cf) {
325             case CF_HWPREDICT:
326             case CF_MHWPREDICT:
327                 info.u_val =
328                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
329                                  ii].scratch[CDP_hw_intercept].u_val;
330                 cd = rrd_info_push(cd,
331                                    sprintf_alloc
332                                    ("rra[%d].cdp_prep[%d].intercept", i, ii),
333                                    RD_I_VAL, info);
334                 info.u_val =
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);
340                 info.u_cnt =
341                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
342                                  ii].scratch[CDP_null_count].u_cnt;
343                 cd = rrd_info_push(cd,
344                                    sprintf_alloc
345                                    ("rra[%d].cdp_prep[%d].NaN_count", i, ii),
346                                    RD_I_CNT, info);
347                 break;
348             case CF_SEASONAL:
349                 info.u_val =
350                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
351                                  ii].scratch[CDP_hw_seasonal].u_val;
352                 cd = rrd_info_push(cd,
353                                    sprintf_alloc
354                                    ("rra[%d].cdp_prep[%d].seasonal", i, ii),
355                                    RD_I_VAL, info);
356                 break;
357             case CF_DEVSEASONAL:
358                 info.u_val =
359                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
360                                  ii].scratch[CDP_seasonal_deviation].u_val;
361                 cd = rrd_info_push(cd,
362                                    sprintf_alloc
363                                    ("rra[%d].cdp_prep[%d].deviation", i, ii),
364                                    RD_I_VAL, info);
365                 break;
366             case CF_DEVPREDICT:
367                 break;
368             case CF_FAILURES:
369             {
370                 unsigned short j;
371                 char     *violations_array;
372                 char      history[MAX_FAILURES_WINDOW_LEN + 1];
373
374                 violations_array =
375                     (char *) rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
376                                           ii].scratch;
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';
379                 history[j] = '\0';
380                 info.u_str = history;
381                 cd = rrd_info_push(cd,
382                                    sprintf_alloc
383                                    ("rra[%d].cdp_prep[%d].history", i, ii),
384                                    RD_I_STR, info);
385             }
386                 break;
387             default:
388                 info.u_val =
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);
394                 info.u_cnt =
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,
398                                    sprintf_alloc
399                                    ("rra[%d].cdp_prep[%d].unknown_datapoints",
400                                     i, ii), RD_I_CNT, info);
401                 break;
402             }
403         }
404     }
405
406     rrd_close(rrd_file);
407   err_free:
408     rrd_free(&rrd);
409     return (data);
410 }
411
412
413 void rrd_info_print(
414     rrd_info_t * data)
415 {
416     while (data) {
417         printf("%s = ", data->key);
418
419         switch (data->type) {
420         case RD_I_VAL:
421             if (isnan(data->value.u_val))
422                 printf("NaN\n");
423             else
424                 printf("%0.10e\n", data->value.u_val);
425             break;
426         case RD_I_CNT:
427             printf("%lu\n", data->value.u_cnt);
428             break;
429         case RD_I_INT:
430             printf("%d\n", data->value.u_int);
431             break;
432         case RD_I_STR:
433             printf("\"%s\"\n", data->value.u_str);
434             break;
435         case RD_I_BLO:
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);
438             break;
439         }
440         data = data->next;
441     }
442 }
443
444 void rrd_info_free(
445     rrd_info_t * data)
446 {
447     rrd_info_t *save;
448
449     while (data) {
450         save = data;
451         if (data->key) {
452             if (data->type == RD_I_STR) {
453                 free(data->value.u_str);
454             }
455             if (data->type == RD_I_BLO) {
456                 free(data->value.u_blo.ptr);
457             }
458             free(data->key);
459         }
460         data = data->next;
461         free(save);
462     }
463 }