Initial revision
[rrdtool.git] / src / rrd_info.c
1 /*****************************************************************************
2  * RRDtool 1.0.33  Copyright Tobias Oetiker, 1997 - 2000
3  *****************************************************************************
4  * rrd_info  Get Information about the configuration of an RRD
5  *****************************************************************************/
6
7 #include "rrd_tool.h"
8 #include <stdarg.h>
9
10 /* proto */
11 static char * sprintf_alloc(char *, ...);
12 static info_t *push(info_t *, char *, enum info_type, infoval);
13 info_t *rrd_info(int, char **);
14
15 /* allocate memory for string */
16 static char *
17 sprintf_alloc(char *fmt, ...) {
18 #ifdef HAVE_VSNPRINTF    
19     int maxlen = 50;
20 #else
21     int maxlen = 1000;
22 #endif
23     char *str = NULL;
24     va_list argp;
25     str = malloc(sizeof(char)*(strlen(fmt)+maxlen));
26     if (str != NULL) {
27         va_start(argp, fmt);
28 #ifdef HAVE_VSNPRINTF
29         vsnprintf(str, maxlen-1, fmt, argp);
30 #else
31         vsprintf(str, fmt, argp);
32 #endif
33     }
34     va_end(argp);
35     return str;
36 }
37
38 static info_t 
39 *push(info_t *info, char *key, enum info_type type, infoval value){
40     info_t *next;
41     next = malloc(sizeof(*next));
42     next->next = (info_t *) 0;
43     if( info )
44         info->next = next;
45     next->type = type;
46     next->key  = key;
47     switch (type) {
48     case RD_I_VAL:
49         next->value.u_val = value.u_val;
50         break;
51     case RD_I_CNT:
52         next->value.u_cnt = value.u_cnt;
53         break;
54     case RD_I_STR:
55         next->value.u_str = malloc(sizeof(char)*(strlen(value.u_str)+1));
56         strcpy(next->value.u_str,value.u_str);
57         break;
58     }
59     return(next);
60 }
61
62   
63 info_t *
64 rrd_info(int argc, char **argv) {   
65     int          i,ii=0;
66     FILE         *in_file;
67     rrd_t        rrd;
68     info_t       *data,*cd;
69     infoval      info;
70
71     if(rrd_open(argv[1],&in_file,&rrd, RRD_READONLY)==-1){
72         return(NULL);
73     }
74     fclose(in_file);
75
76     info.u_str=argv[1];
77     cd=push(NULL,sprintf_alloc("filename"),    RD_I_STR, info);
78     data=cd;
79
80     info.u_str=rrd.stat_head->version;
81     cd=push(cd,sprintf_alloc("rrd_version"),    RD_I_STR, info);
82
83     info.u_cnt=rrd.stat_head->pdp_step;
84     cd=push(cd,sprintf_alloc("step"),       RD_I_CNT, info);
85
86     info.u_cnt=rrd.live_head->last_up;
87     cd=push(cd,sprintf_alloc("last_update"), RD_I_CNT, info);
88
89     for(i=0;i<rrd.stat_head->ds_cnt;i++){
90
91         info.u_str=rrd.ds_def[i].dst;
92         cd=push(cd,sprintf_alloc("ds[%s].type",             rrd.ds_def[i].ds_nam), RD_I_STR, info);
93
94         info.u_cnt=rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt;
95         cd=push(cd,sprintf_alloc("ds[%s].minimal_heartbeat",rrd.ds_def[i].ds_nam), RD_I_CNT, info);
96
97         info.u_val=rrd.ds_def[i].par[DS_min_val].u_val;
98         cd=push(cd,sprintf_alloc("ds[%s].min",              rrd.ds_def[i].ds_nam), RD_I_VAL, info);
99         
100         info.u_val=rrd.ds_def[i].par[DS_max_val].u_val;
101         cd=push(cd,sprintf_alloc("ds[%s].max",              rrd.ds_def[i].ds_nam), RD_I_VAL, info);
102         
103         info.u_str=rrd.pdp_prep[i].last_ds;
104         cd=push(cd,sprintf_alloc("ds[%s].last_ds",          rrd.ds_def[i].ds_nam), RD_I_STR, info);
105
106         info.u_val=rrd.pdp_prep[i].scratch[PDP_val].u_val;
107         cd=push(cd,sprintf_alloc("ds[%s].value",            rrd.ds_def[i].ds_nam), RD_I_VAL, info);
108
109         info.u_cnt=rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt;
110         cd=push(cd,sprintf_alloc("ds[%s].unknown_sec",      rrd.ds_def[i].ds_nam), RD_I_CNT, info);
111     }
112
113     for(i=0;i<rrd.stat_head->rra_cnt;i++){
114         info.u_str=rrd.rra_def[i].cf_nam;
115         cd=push(cd,sprintf_alloc("rra[%d].cf",         i),  RD_I_STR,   info);
116
117         info.u_cnt=rrd.rra_def[i].row_cnt;
118         cd=push(cd,sprintf_alloc("rra[%d].rows",i),  RD_I_CNT,   info);
119
120         info.u_cnt=rrd.rra_def[i].pdp_cnt;
121         cd=push(cd,sprintf_alloc("rra[%d].pdp_per_row",i),  RD_I_CNT,   info);
122
123         info.u_val=rrd.rra_def[i].par[RRA_cdp_xff_val].u_val;
124         cd=push(cd,sprintf_alloc("rra[%d].xff",i),  RD_I_VAL,   info);
125         
126         for(ii=0;ii<rrd.stat_head->ds_cnt;ii++){
127             info.u_val=rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_val].u_val;
128             cd=push(cd,sprintf_alloc("rra[%d].cdp_prep[%d].value",i,ii), RD_I_VAL, info);
129
130             info.u_cnt=rrd.cdp_prep[i*rrd.stat_head->ds_cnt+ii].scratch[CDP_unkn_pdp_cnt].u_cnt;
131             cd=push(cd,sprintf_alloc("rra[%d].cdp_prep[%d].unknown_datapoints",i,ii), RD_I_CNT, info);
132         }
133     }
134     rrd_free(&rrd);
135     return(data);
136
137 }
138
139
140
141
142