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