* introduce a new rrd_create_r2 call to fix the no-overwrite api mess
[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     if (opt_daemon) free(opt_daemon);
156     return (info);
157 } /* rrd_info_t *rrd_info */
158
159 rrd_info_t *rrd_info_r(
160     char *filename)
161 {
162     unsigned int i, ii = 0;
163     rrd_t     rrd;
164     rrd_info_t *data = NULL, *cd;
165     rrd_infoval_t info;
166     rrd_file_t *rrd_file;
167     enum cf_en current_cf;
168     enum dst_en current_ds;
169
170     rrd_init(&rrd);
171     rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
172     if (rrd_file == NULL)
173         goto err_free;
174
175     info.u_str = filename;
176     cd = rrd_info_push(NULL, sprintf_alloc("filename"), RD_I_STR, info);
177     data = cd;
178
179     info.u_str = rrd.stat_head->version;
180     cd = rrd_info_push(cd, sprintf_alloc("rrd_version"), RD_I_STR, info);
181
182     info.u_cnt = rrd.stat_head->pdp_step;
183     cd = rrd_info_push(cd, sprintf_alloc("step"), RD_I_CNT, info);
184
185     info.u_cnt = rrd.live_head->last_up;
186     cd = rrd_info_push(cd, sprintf_alloc("last_update"), RD_I_CNT, info);
187
188     info.u_cnt = rrd_get_header_size(&rrd);
189     cd = rrd_info_push(cd, sprintf_alloc("header_size"), RD_I_CNT, info);
190
191     for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
192
193         info.u_cnt=i;
194         cd= rrd_info_push(cd,sprintf_alloc("ds[%s].index",
195                                      rrd.ds_def[i].ds_nam),
196                      RD_I_CNT, info);
197     
198         info.u_str = rrd.ds_def[i].dst;
199         cd = rrd_info_push(cd, sprintf_alloc("ds[%s].type",
200                                              rrd.ds_def[i].ds_nam),
201                            RD_I_STR, info);
202
203         current_ds = dst_conv(rrd.ds_def[i].dst);
204         switch (current_ds) {
205         case DST_CDEF:
206         {
207             char     *buffer = NULL;
208
209             rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
210                             rrd.ds_def, &buffer);
211             info.u_str = buffer;
212             cd = rrd_info_push(cd,
213                                sprintf_alloc("ds[%s].cdef",
214                                              rrd.ds_def[i].ds_nam), RD_I_STR,
215                                info);
216             free(buffer);
217         }
218             break;
219         default:
220             info.u_cnt = rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt;
221             cd = rrd_info_push(cd,
222                                sprintf_alloc("ds[%s].minimal_heartbeat",
223                                              rrd.ds_def[i].ds_nam), RD_I_CNT,
224                                info);
225
226             info.u_val = rrd.ds_def[i].par[DS_min_val].u_val;
227             cd = rrd_info_push(cd,
228                                sprintf_alloc("ds[%s].min",
229                                              rrd.ds_def[i].ds_nam), RD_I_VAL,
230                                info);
231
232             info.u_val = rrd.ds_def[i].par[DS_max_val].u_val;
233             cd = rrd_info_push(cd,
234                                sprintf_alloc("ds[%s].max",
235                                              rrd.ds_def[i].ds_nam), RD_I_VAL,
236                                info);
237             break;
238         }
239
240         info.u_str = rrd.pdp_prep[i].last_ds;
241         cd = rrd_info_push(cd,
242                            sprintf_alloc("ds[%s].last_ds",
243                                          rrd.ds_def[i].ds_nam), RD_I_STR,
244                            info);
245
246         info.u_val = rrd.pdp_prep[i].scratch[PDP_val].u_val;
247         cd = rrd_info_push(cd,
248                            sprintf_alloc("ds[%s].value",
249                                          rrd.ds_def[i].ds_nam), RD_I_VAL,
250                            info);
251
252         info.u_cnt = rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt;
253         cd = rrd_info_push(cd,
254                            sprintf_alloc("ds[%s].unknown_sec",
255                                          rrd.ds_def[i].ds_nam), RD_I_CNT,
256                            info);
257     }
258
259     for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
260         info.u_str = rrd.rra_def[i].cf_nam;
261         cd = rrd_info_push(cd, sprintf_alloc("rra[%d].cf", i), RD_I_STR,
262                            info);
263         current_cf = cf_conv(rrd.rra_def[i].cf_nam);
264
265         info.u_cnt = rrd.rra_def[i].row_cnt;
266         cd = rrd_info_push(cd, sprintf_alloc("rra[%d].rows", i), RD_I_CNT,
267                            info);
268
269         info.u_cnt = rrd.rra_ptr[i].cur_row;
270         cd = rrd_info_push(cd, sprintf_alloc("rra[%d].cur_row", i), RD_I_CNT,
271                            info);
272
273         info.u_cnt = rrd.rra_def[i].pdp_cnt;
274         cd = rrd_info_push(cd, sprintf_alloc("rra[%d].pdp_per_row", i),
275                            RD_I_CNT, info);
276
277         switch (current_cf) {
278         case CF_HWPREDICT:
279         case CF_MHWPREDICT:
280             info.u_val = rrd.rra_def[i].par[RRA_hw_alpha].u_val;
281             cd = rrd_info_push(cd, sprintf_alloc("rra[%d].alpha", i),
282                                RD_I_VAL, info);
283             info.u_val = rrd.rra_def[i].par[RRA_hw_beta].u_val;
284             cd = rrd_info_push(cd, sprintf_alloc("rra[%d].beta", i), RD_I_VAL,
285                                info);
286             break;
287         case CF_SEASONAL:
288         case CF_DEVSEASONAL:
289             info.u_val = rrd.rra_def[i].par[RRA_seasonal_gamma].u_val;
290             cd = rrd_info_push(cd, sprintf_alloc("rra[%d].gamma", i),
291                                RD_I_VAL, info);
292             if (atoi(rrd.stat_head->version) >= 4) {
293                 info.u_val =
294                     rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val;
295                 cd = rrd_info_push(cd,
296                                    sprintf_alloc("rra[%d].smoothing_window",
297                                                  i), RD_I_VAL, info);
298             }
299             break;
300         case CF_FAILURES:
301             info.u_val = rrd.rra_def[i].par[RRA_delta_pos].u_val;
302             cd = rrd_info_push(cd, sprintf_alloc("rra[%d].delta_pos", i),
303                                RD_I_VAL, info);
304             info.u_val = rrd.rra_def[i].par[RRA_delta_neg].u_val;
305             cd = rrd_info_push(cd, sprintf_alloc("rra[%d].delta_neg", i),
306                                RD_I_VAL, info);
307             info.u_cnt = rrd.rra_def[i].par[RRA_failure_threshold].u_cnt;
308             cd = rrd_info_push(cd,
309                                sprintf_alloc("rra[%d].failure_threshold", i),
310                                RD_I_CNT, info);
311             info.u_cnt = rrd.rra_def[i].par[RRA_window_len].u_cnt;
312             cd = rrd_info_push(cd, sprintf_alloc("rra[%d].window_length", i),
313                                RD_I_CNT, info);
314             break;
315         case CF_DEVPREDICT:
316             break;
317         default:
318             info.u_val = rrd.rra_def[i].par[RRA_cdp_xff_val].u_val;
319             cd = rrd_info_push(cd, sprintf_alloc("rra[%d].xff", i), RD_I_VAL,
320                                info);
321             break;
322         }
323
324         for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
325             switch (current_cf) {
326             case CF_HWPREDICT:
327             case CF_MHWPREDICT:
328                 info.u_val =
329                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
330                                  ii].scratch[CDP_hw_intercept].u_val;
331                 cd = rrd_info_push(cd,
332                                    sprintf_alloc
333                                    ("rra[%d].cdp_prep[%d].intercept", i, ii),
334                                    RD_I_VAL, info);
335                 info.u_val =
336                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
337                                  ii].scratch[CDP_hw_slope].u_val;
338                 cd = rrd_info_push(cd,
339                                    sprintf_alloc("rra[%d].cdp_prep[%d].slope",
340                                                  i, ii), RD_I_VAL, info);
341                 info.u_cnt =
342                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
343                                  ii].scratch[CDP_null_count].u_cnt;
344                 cd = rrd_info_push(cd,
345                                    sprintf_alloc
346                                    ("rra[%d].cdp_prep[%d].NaN_count", i, ii),
347                                    RD_I_CNT, info);
348                 break;
349             case CF_SEASONAL:
350                 info.u_val =
351                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
352                                  ii].scratch[CDP_hw_seasonal].u_val;
353                 cd = rrd_info_push(cd,
354                                    sprintf_alloc
355                                    ("rra[%d].cdp_prep[%d].seasonal", i, ii),
356                                    RD_I_VAL, info);
357                 break;
358             case CF_DEVSEASONAL:
359                 info.u_val =
360                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
361                                  ii].scratch[CDP_seasonal_deviation].u_val;
362                 cd = rrd_info_push(cd,
363                                    sprintf_alloc
364                                    ("rra[%d].cdp_prep[%d].deviation", i, ii),
365                                    RD_I_VAL, info);
366                 break;
367             case CF_DEVPREDICT:
368                 break;
369             case CF_FAILURES:
370             {
371                 unsigned short j;
372                 char     *violations_array;
373                 char      history[MAX_FAILURES_WINDOW_LEN + 1];
374
375                 violations_array =
376                     (char *) rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
377                                           ii].scratch;
378                 for (j = 0; j < rrd.rra_def[i].par[RRA_window_len].u_cnt; ++j)
379                     history[j] = (violations_array[j] == 1) ? '1' : '0';
380                 history[j] = '\0';
381                 info.u_str = history;
382                 cd = rrd_info_push(cd,
383                                    sprintf_alloc
384                                    ("rra[%d].cdp_prep[%d].history", i, ii),
385                                    RD_I_STR, info);
386             }
387                 break;
388             default:
389                 info.u_val =
390                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
391                                  ii].scratch[CDP_val].u_val;
392                 cd = rrd_info_push(cd,
393                                    sprintf_alloc("rra[%d].cdp_prep[%d].value",
394                                                  i, ii), RD_I_VAL, info);
395                 info.u_cnt =
396                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
397                                  ii].scratch[CDP_unkn_pdp_cnt].u_cnt;
398                 cd = rrd_info_push(cd,
399                                    sprintf_alloc
400                                    ("rra[%d].cdp_prep[%d].unknown_datapoints",
401                                     i, ii), RD_I_CNT, info);
402                 break;
403             }
404         }
405     }
406
407     rrd_close(rrd_file);
408   err_free:
409     rrd_free(&rrd);
410     return (data);
411 }
412
413
414 void rrd_info_print(
415     rrd_info_t * data)
416 {
417     while (data) {
418         printf("%s = ", data->key);
419
420         switch (data->type) {
421         case RD_I_VAL:
422             if (isnan(data->value.u_val))
423                 printf("NaN\n");
424             else
425                 printf("%0.10e\n", data->value.u_val);
426             break;
427         case RD_I_CNT:
428             printf("%lu\n", data->value.u_cnt);
429             break;
430         case RD_I_INT:
431             printf("%d\n", data->value.u_int);
432             break;
433         case RD_I_STR:
434             printf("\"%s\"\n", data->value.u_str);
435             break;
436         case RD_I_BLO:
437             printf("BLOB_SIZE:%lu\n", data->value.u_blo.size);
438             fwrite(data->value.u_blo.ptr, data->value.u_blo.size, 1, stdout);
439             break;
440         }
441         data = data->next;
442     }
443 }
444
445 void rrd_info_free(
446     rrd_info_t * data)
447 {
448     rrd_info_t *save;
449
450     while (data) {
451         save = data;
452         if (data->key) {
453             if (data->type == RD_I_STR) {
454                 free(data->value.u_str);
455             }
456             if (data->type == RD_I_BLO) {
457                 free(data->value.u_blo.ptr);
458             }
459             free(data->key);
460         }
461         data = data->next;
462         free(save);
463     }
464 }