1 /*****************************************************************************
2 * RRDtool 1.0.33 Copyright Tobias Oetiker, 1997 - 2000
3 *****************************************************************************
4 * rrd_restore.c creates new rrd from data dumped by rrd_dump.c
5 *****************************************************************************/
13 int eat_tag(char **, char *);
14 int read_tag(char **, char *, char *, void *);
15 int xml2rrd(char*, rrd_t*, char);
16 int rrd_write(char *, rrd_t *);
18 /* convert all ocurances of <BlaBlaBla> to <blablabla> */
20 void xml_lc(char* buf){
23 if (intag ==0 && (*buf) == '<') {
26 else if (intag ==1 && (*buf) == '>') {
29 } else if (intag ==1) {
41 while((*(ptr+1)) && ((*ptr)==' ' || (*ptr)=='\r' || (*ptr)=='\n' || (*ptr)=='\t')) ptr++;
42 if (strncmp(ptr,"<!--",4) == 0) {
43 ptr= strstr(ptr,"-->");
44 if (ptr) ptr+=3; else {
45 rrd_set_error("Dangling Comment");
50 } while ((*buf)!=ptr);
54 int eat_tag(char **buf, char *tag){
55 if ((*buf)==NULL) return -1; /* fall though clause */
60 && strncmp((*buf)+1,tag,strlen(tag)) == 0
61 && *((*buf)+strlen(tag)+1)=='>') {
62 (*buf) += strlen(tag)+2;
65 rrd_set_error("No <%s> tag found",tag);
73 int read_tag(char **buf, char *tag, char *format, void *value){
76 if ((*buf)==NULL) return -1; /* fall though clause */
78 if (eat_tag(buf,tag)==1){
81 while(*((*buf)+1) && (*(*buf) != '<')) (*buf)++; /*find start of endtag*/
83 matches =sscanf(temp,format,value);
85 end_tag = malloc((strlen(tag)+2)*sizeof(char));
86 sprintf(end_tag,"/%s",tag);
89 if (matches == 0 && strcmp(format,"%lf") == 0)
90 (*((double* )(value))) = DNAN;
91 if (matches != 1) return 0;
98 /* parse the data stored in buf and return a filled rrd structure */
99 int xml2rrd(char* buf, rrd_t* rrd, char rc){
100 /* pass 1 identify number of RRAs */
101 char *ptr,*ptr2,*ptr3; /* walks thought the buffer */
102 long rows=0,mempool=0,i=0;
103 xml_lc(buf); /* lets lowercase all active parts of the xml */
107 /* start with an RRD tag */
110 /* allocate static header */
111 if((rrd->stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
112 rrd_set_error("allocating rrd.stat_head");
116 strcpy(rrd->stat_head->cookie,RRD_COOKIE);
117 read_tag(&ptr,"version","%4[0-9]",rrd->stat_head->version);
118 rrd->stat_head->float_cookie = FLOAT_COOKIE;
119 rrd->stat_head->ds_cnt = 0;
120 rrd->stat_head->rra_cnt = 0;
121 read_tag(&ptr,"step","%lu",&(rrd->stat_head->pdp_step));
123 /* allocate live head */
124 if((rrd->live_head = calloc(1,sizeof(live_head_t)))==NULL){
125 rrd_set_error("allocating rrd.live_head");
128 read_tag(&ptr,"lastupdate","%lu",&(rrd->live_head->last_up));
130 /* Data Source Definition Part */
132 while (eat_tag(&ptr2,"ds") == 1){
133 rrd->stat_head->ds_cnt++;
134 if((rrd->ds_def = rrd_realloc(rrd->ds_def,rrd->stat_head->ds_cnt*sizeof(ds_def_t)))==NULL){
135 rrd_set_error("allocating rrd.ds_def");
138 /* clean out memory to make sure no data gets stored from previous tasks */
139 memset(&(rrd->ds_def[rrd->stat_head->ds_cnt-1]), 0, sizeof(ds_def_t));
140 if((rrd->pdp_prep = rrd_realloc(rrd->pdp_prep,rrd->stat_head->ds_cnt
141 *sizeof(pdp_prep_t)))==NULL){
142 rrd_set_error("allocating pdp_prep");
145 /* clean out memory to make sure no data gets stored from previous tasks */
146 memset(&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1]), 0, sizeof(pdp_prep_t));
148 read_tag(&ptr2,"name",DS_NAM_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].ds_nam);
150 read_tag(&ptr2,"type",DST_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].dst);
151 /* test for valid type */
152 if(dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt-1].dst) == -1) return -1;
154 read_tag(&ptr2,"minimal_heartbeat","%lu",
155 &(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_mrhb_cnt].u_cnt));
156 read_tag(&ptr2,"min","%lf",&(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_min_val].u_val));
157 read_tag(&ptr2,"max","%lf",&(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_max_val].u_val));
159 read_tag(&ptr2,"last_ds","%30s",rrd->pdp_prep[rrd->stat_head->ds_cnt-1].last_ds);
160 read_tag(&ptr2,"value","%lf",&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1].scratch[PDP_val].u_val));
161 read_tag(&ptr2,"unknown_sec","%lu",&(rrd->pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt));
162 eat_tag(&ptr2,"/ds");
167 while (eat_tag(&ptr2,"rra") == 1){
168 rrd->stat_head->rra_cnt++;
170 /* alocate and reset rra definition areas */
171 if((rrd->rra_def = rrd_realloc(rrd->rra_def,rrd->stat_head->rra_cnt*sizeof(rra_def_t)))==NULL){
172 rrd_set_error("allocating rra_def"); return -1; }
173 memset(&(rrd->rra_def[rrd->stat_head->rra_cnt-1]), 0, sizeof(rra_def_t));
175 /* alocate and reset consolidation point areas */
176 if((rrd->cdp_prep = rrd_realloc(rrd->cdp_prep,
177 rrd->stat_head->rra_cnt
178 *rrd->stat_head->ds_cnt*sizeof(cdp_prep_t)))==NULL){
179 rrd_set_error("allocating cdp_prep"); return -1; }
181 memset(&(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rrd->stat_head->rra_cnt-1)]),
182 0, rrd->stat_head->ds_cnt*sizeof(cdp_prep_t));
185 read_tag(&ptr2,"cf",CF_NAM_FMT,rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam);
186 /* test for valid type */
187 if(cf_conv(rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam) == -1) return -1;
189 read_tag(&ptr2,"pdp_per_row","%lu",&(rrd->rra_def[rrd->stat_head->rra_cnt-1].pdp_cnt));
190 read_tag(&ptr2,"xff","%lf",&(rrd->rra_def[rrd->stat_head->rra_cnt-1].par[RRA_cdp_xff_val].u_val));
191 if(rrd->rra_def[rrd->stat_head->rra_cnt-1].par[RRA_cdp_xff_val].u_val > 1 ||
192 rrd->rra_def[rrd->stat_head->rra_cnt-1].par[RRA_cdp_xff_val].u_val < 0)
195 eat_tag(&ptr2,"cdp_prep");
196 for(i=0;i<rrd->stat_head->ds_cnt;i++){
198 read_tag(&ptr2,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt*
199 (rrd->stat_head->rra_cnt-1)
200 +i].scratch[CDP_val].u_val));
201 read_tag(&ptr2,"unknown_datapoints","%lu",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
202 *(rrd->stat_head->rra_cnt-1)
203 +i].scratch[CDP_unkn_pdp_cnt].u_cnt));
204 eat_tag(&ptr2,"/ds");
206 eat_tag(&ptr2,"/cdp_prep");
207 rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt=0;
208 eat_tag(&ptr2,"database");
210 while (eat_tag(&ptr3,"row") == 1){
214 if((rrd->rrd_value = rrd_realloc(rrd->rrd_value,
215 (rows+mempool)*(rrd->stat_head->ds_cnt)
216 *sizeof(rrd_value_t)))==NULL) {
217 rrd_set_error("allocating rrd_values"); return -1; }
221 rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt++;
222 for(i=0;i<rrd->stat_head->ds_cnt;i++){
224 rrd_value_t * value = &(rrd->rrd_value[(rows-1)*rrd->stat_head->ds_cnt+i]);
226 read_tag(&ptr3,"v","%lf", value);
229 (rc == 1) /* do we have to check for the ranges */
231 (!isnan(*value)) /* not a NAN value */
233 ( /* min defined and in the range ? */
234 (!isnan(rrd->ds_def[i].par[DS_min_val].u_val)
235 && (*value < rrd->ds_def[i].par[DS_min_val].u_val))
236 || /* max defined and in the range ? */
237 (!isnan(rrd->ds_def[i].par[DS_max_val].u_val)
238 && (*value > rrd->ds_def[i].par[DS_max_val].u_val))
241 fprintf (stderr, "out of range found [ds: %lu], [value : %0.10e]\n", i, *value);
245 eat_tag(&ptr3,"/row");
248 eat_tag(&ptr2,"/database");
249 eat_tag(&ptr2,"/rra");
252 eat_tag(&ptr,"/rrd");
254 if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t)*rrd->stat_head->rra_cnt)) == NULL) {
255 rrd_set_error("allocating rra_ptr");
259 for(i=0; i <rrd->stat_head->rra_cnt; i++) {
260 rrd->rra_ptr[i].cur_row = rrd->rra_def[i].row_cnt-1;
271 /* create and empty rrd file according to the specs given */
274 rrd_write(char *file_name, rrd_t *rrd)
276 unsigned long i,ii,val_cnt;
279 if (strcmp("-",file_name)==0){
282 if ((rrd_file = fopen(file_name,"wb")) == NULL ) {
283 rrd_set_error("creating '%s': %s",file_name,strerror(errno));
288 fwrite(rrd->stat_head,
289 sizeof(stat_head_t), 1, rrd_file);
292 sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
295 sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
297 fwrite(rrd->live_head, sizeof(live_head_t),1, rrd_file);
299 fwrite( rrd->pdp_prep, sizeof(pdp_prep_t),rrd->stat_head->ds_cnt,rrd_file);
301 fwrite( rrd->cdp_prep, sizeof(cdp_prep_t),rrd->stat_head->rra_cnt*
302 rrd->stat_head->ds_cnt,rrd_file);
303 fwrite( rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt,rrd_file);
307 /* calculate the number of rrd_values to dump */
309 for(i=0; i < rrd->stat_head->rra_cnt; i++)
310 for(ii=0; ii < rrd->rra_def[i].row_cnt * rrd->stat_head->ds_cnt;ii++)
312 fwrite( rrd->rrd_value, sizeof(rrd_value_t),val_cnt,rrd_file);
314 /* lets see if we had an error */
315 if(ferror(rrd_file)){
316 rrd_set_error("a file error occurred while creating '%s'",file_name);
327 rrd_restore(int argc, char **argv)
336 rrd_set_error("usage rrdtool %s [--range-check/-r] file.xml file.rrd",argv[0]);
341 static struct option long_options[] =
343 {"range-check", required_argument, 0, 'r'},
346 int option_index = 0;
350 opt = getopt_long(argc, argv, "r", long_options, &option_index);
360 rrd_set_error("usage rrdtool %s [--range-check|-r] file.xml file.rrd",argv[0]);
366 if (readfile(argv[optind],&buf,0)==-1){
369 if (xml2rrd(buf,&rrd,rc)==-1) {
375 if(rrd_write(argv[optind+1],&rrd)==-1){