- rrd_open() calculates file size for new files and calls mmap once for
[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     int       maxlen = 1024 + strlen(fmt);
25     char     *str = NULL;
26     va_list   argp;
27     str = malloc(sizeof(char) * (maxlen + 1));
28     if (str != NULL) {
29         va_start(argp, fmt);
30 #ifdef HAVE_VSNPRINTF
31         vsnprintf(str, maxlen, fmt, argp);
32 #else
33         vsprintf(str, fmt, argp);
34 #endif
35     }
36     va_end(argp);
37     return str;
38 }
39
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 */
42 rrd_info_t
43     * rrd_info_push(rrd_info_t * info,
44                     char *key, rrd_info_type_t type, rrd_infoval_t value)
45 {
46     rrd_info_t *next;
47
48     next = malloc(sizeof(*next));
49     next->next = (rrd_info_t *) 0;
50     if (info)
51         info->next = next;
52     next->type = type;
53     next->key = key;
54     switch (type) {
55     case RD_I_VAL:
56         next->value.u_val = value.u_val;
57         break;
58     case RD_I_CNT:
59         next->value.u_cnt = value.u_cnt;
60         break;
61     case RD_I_INT:
62         next->value.u_int = value.u_int;
63         break;
64     case RD_I_STR:
65         next->value.u_str = malloc(sizeof(char) * (strlen(value.u_str) + 1));
66         strcpy(next->value.u_str, value.u_str);
67         break;
68     case RD_I_BLO:
69         next->value.u_blo.size = value.u_blo.size;
70         next->value.u_blo.ptr =
71             malloc(sizeof(unsigned char) * value.u_blo.size);
72         memcpy(next->value.u_blo.ptr, value.u_blo.ptr, value.u_blo.size);
73         break;
74     }
75     return (next);
76 }
77
78
79 rrd_info_t *rrd_info(
80     int argc,
81     char **argv)
82 {
83     rrd_info_t *info;
84     char *opt_daemon = NULL;
85     int status;
86
87     optind = 0;
88     opterr = 0;         /* initialize getopt */
89
90     while (42) {
91         int       opt;
92         int       option_index = 0;
93         static struct option long_options[] = {
94             {"daemon", required_argument, 0, 'd'},
95             {0, 0, 0, 0}
96         };
97
98         opt = getopt_long(argc, argv, "d:", long_options, &option_index);
99
100         if (opt == EOF)
101             break;
102
103         switch (opt) {
104         case 'd':
105             if (opt_daemon != NULL)
106                     free (opt_daemon);
107             opt_daemon = strdup (optarg);
108             if (opt_daemon == NULL)
109             {
110                 rrd_set_error ("strdup failed.");
111                 return (NULL);
112             }
113             break;
114
115         default:
116             rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
117                     argv[0]);
118             return (NULL);
119             break;
120         }
121     }                   /* while (42) */
122
123     if ((argc - optind) != 1) {
124         rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
125                 argv[0]);
126         return (NULL);
127     }
128
129     status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
130     if (opt_daemon) free (opt_daemon);
131     if (status) return (NULL);
132
133     info = rrd_info_r(argv[optind]);
134
135     return (info);
136 } /* rrd_info_t *rrd_info */
137
138 rrd_info_t *rrd_info_r(
139     char *filename)
140 {
141     unsigned int i, ii = 0;
142     rrd_t     rrd;
143     rrd_info_t *data = NULL, *cd;
144     rrd_infoval_t info;
145     rrd_file_t *rrd_file;
146     enum cf_en current_cf;
147     enum dst_en current_ds;
148
149     rrd_init(&rrd);
150     rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
151     if (rrd_file == NULL)
152         goto err_free;
153
154     info.u_str = filename;
155     cd = rrd_info_push(NULL, sprintf_alloc("filename"), RD_I_STR, info);
156     data = cd;
157
158     info.u_str = rrd.stat_head->version;
159     cd = rrd_info_push(cd, sprintf_alloc("rrd_version"), RD_I_STR, info);
160
161     info.u_cnt = rrd.stat_head->pdp_step;
162     cd = rrd_info_push(cd, sprintf_alloc("step"), RD_I_CNT, info);
163
164     info.u_cnt = rrd.live_head->last_up;
165     cd = rrd_info_push(cd, sprintf_alloc("last_update"), RD_I_CNT, info);
166
167     for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
168
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),
172                            RD_I_STR, info);
173
174         current_ds = dst_conv(rrd.ds_def[i].dst);
175         switch (current_ds) {
176         case DST_CDEF:
177         {
178             char     *buffer = NULL;
179
180             rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
181                             rrd.ds_def, &buffer);
182             info.u_str = buffer;
183             cd = rrd_info_push(cd,
184                                sprintf_alloc("ds[%s].cdef",
185                                              rrd.ds_def[i].ds_nam), RD_I_STR,
186                                info);
187             free(buffer);
188         }
189             break;
190         default:
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,
195                                info);
196
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,
201                                info);
202
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,
207                                info);
208             break;
209         }
210
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,
215                            info);
216
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,
221                            info);
222
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,
227                            info);
228     }
229
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,
233                            info);
234         current_cf = cf_conv(rrd.rra_def[i].cf_nam);
235
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,
238                            info);
239
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,
242                            info);
243
244         info.u_cnt = rrd.rra_def[i].pdp_cnt;
245         cd = rrd_info_push(cd, sprintf_alloc("rra[%d].pdp_per_row", i),
246                            RD_I_CNT, info);
247
248         switch (current_cf) {
249         case CF_HWPREDICT:
250         case CF_MHWPREDICT:
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),
253                                RD_I_VAL, info);
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,
256                                info);
257             break;
258         case CF_SEASONAL:
259         case CF_DEVSEASONAL:
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),
262                                RD_I_VAL, info);
263             if (atoi(rrd.stat_head->version) >= 4) {
264                 info.u_val =
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",
268                                                  i), RD_I_VAL, info);
269             }
270             break;
271         case CF_FAILURES:
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),
274                                RD_I_VAL, info);
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),
277                                RD_I_VAL, info);
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),
281                                RD_I_CNT, info);
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),
284                                RD_I_CNT, info);
285             break;
286         case CF_DEVPREDICT:
287             break;
288         default:
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,
291                                info);
292             break;
293         }
294
295         for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
296             switch (current_cf) {
297             case CF_HWPREDICT:
298             case CF_MHWPREDICT:
299                 info.u_val =
300                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
301                                  ii].scratch[CDP_hw_intercept].u_val;
302                 cd = rrd_info_push(cd,
303                                    sprintf_alloc
304                                    ("rra[%d].cdp_prep[%d].intercept", i, ii),
305                                    RD_I_VAL, info);
306                 info.u_val =
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);
312                 info.u_cnt =
313                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
314                                  ii].scratch[CDP_null_count].u_cnt;
315                 cd = rrd_info_push(cd,
316                                    sprintf_alloc
317                                    ("rra[%d].cdp_prep[%d].NaN_count", i, ii),
318                                    RD_I_CNT, info);
319                 break;
320             case CF_SEASONAL:
321                 info.u_val =
322                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
323                                  ii].scratch[CDP_hw_seasonal].u_val;
324                 cd = rrd_info_push(cd,
325                                    sprintf_alloc
326                                    ("rra[%d].cdp_prep[%d].seasonal", i, ii),
327                                    RD_I_VAL, info);
328                 break;
329             case CF_DEVSEASONAL:
330                 info.u_val =
331                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
332                                  ii].scratch[CDP_seasonal_deviation].u_val;
333                 cd = rrd_info_push(cd,
334                                    sprintf_alloc
335                                    ("rra[%d].cdp_prep[%d].deviation", i, ii),
336                                    RD_I_VAL, info);
337                 break;
338             case CF_DEVPREDICT:
339                 break;
340             case CF_FAILURES:
341             {
342                 unsigned short j;
343                 char     *violations_array;
344                 char      history[MAX_FAILURES_WINDOW_LEN + 1];
345
346                 violations_array =
347                     (char *) rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
348                                           ii].scratch;
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';
351                 history[j] = '\0';
352                 info.u_str = history;
353                 cd = rrd_info_push(cd,
354                                    sprintf_alloc
355                                    ("rra[%d].cdp_prep[%d].history", i, ii),
356                                    RD_I_STR, info);
357             }
358                 break;
359             default:
360                 info.u_val =
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);
366                 info.u_cnt =
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,
370                                    sprintf_alloc
371                                    ("rra[%d].cdp_prep[%d].unknown_datapoints",
372                                     i, ii), RD_I_CNT, info);
373                 break;
374             }
375         }
376     }
377
378     rrd_close(rrd_file);
379   err_free:
380     rrd_free(&rrd);
381     return (data);
382 }
383
384
385 void rrd_info_print(
386     rrd_info_t * data)
387 {
388     while (data) {
389         printf("%s = ", data->key);
390
391         switch (data->type) {
392         case RD_I_VAL:
393             if (isnan(data->value.u_val))
394                 printf("NaN\n");
395             else
396                 printf("%0.10e\n", data->value.u_val);
397             break;
398         case RD_I_CNT:
399             printf("%lu\n", data->value.u_cnt);
400             break;
401         case RD_I_INT:
402             printf("%d\n", data->value.u_int);
403             break;
404         case RD_I_STR:
405             printf("\"%s\"\n", data->value.u_str);
406             break;
407         case RD_I_BLO:
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);
410             break;
411         }
412         data = data->next;
413     }
414 }
415
416 void rrd_info_free(
417     rrd_info_t * data)
418 {
419     rrd_info_t *save;
420
421     while (data) {
422         save = data;
423         if (data->key) {
424             if (data->type == RD_I_STR) {
425                 free(data->value.u_str);
426             }
427             if (data->type == RD_I_BLO) {
428                 free(data->value.u_blo.ptr);
429             }
430             free(data->key);
431         }
432         data = data->next;
433         free(save);
434     }
435 }