4e2b6c311a7d02a11f25b776a012064a6a8ece04
[rrdtool.git] / src / rrd_restore.c
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  *****************************************************************************/
6
7 #include "rrd_tool.h"
8
9 /* Prototypes */
10
11 void xml_lc(char*);
12 int skip(char **);
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 *);
17
18 /* convert all ocurances of <BlaBlaBla> to <blablabla> */
19
20 void xml_lc(char* buf){
21   int intag=0;
22   while((*buf)){
23     if (intag ==0 && (*buf) == '<') {
24       intag = 1;
25     }
26     else if (intag ==1 && (*buf) == '>') {
27       intag = 0;
28       continue;
29     } else  if (intag ==1) {
30       *buf = tolower(*buf);
31     }
32     buf++;    
33   }
34 }
35
36 int skip(char **buf){
37   char *ptr;  
38   ptr=(*buf);
39   do {
40     (*buf)=ptr;
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");
46         (*buf) = NULL;
47         return -1;
48       }
49     }
50   } while ((*buf)!=ptr);  
51   return 1;
52 }
53
54 int eat_tag(char **buf, char *tag){ 
55   if ((*buf)==NULL) return -1;   /* fall though clause */
56
57   rrd_clear_error();
58   skip(buf);
59   if ((**buf)=='<' 
60       && strncmp((*buf)+1,tag,strlen(tag)) == 0 
61       && *((*buf)+strlen(tag)+1)=='>') {
62     (*buf) += strlen(tag)+2;
63   }
64   else {
65     rrd_set_error("No <%s> tag found",tag);
66     (*buf) = NULL;
67     return -1;
68   }
69   skip(buf);
70   return 1;
71 }
72
73 int read_tag(char **buf, char *tag, char *format, void *value){
74     char *end_tag;
75     int matches;
76     if ((*buf)==NULL) return -1;   /* fall though clause */
77     rrd_clear_error();
78     if (eat_tag(buf,tag)==1){
79         char *temp;
80         temp = (*buf);
81         while(*((*buf)+1) && (*(*buf) != '<')) (*buf)++; /*find start of endtag*/
82         *(*buf) = '\0';
83         matches =sscanf(temp,format,value);
84         *(*buf) = '<';
85         end_tag = malloc((strlen(tag)+2)*sizeof(char));
86         sprintf(end_tag,"/%s",tag);
87         eat_tag(buf,end_tag);
88         free(end_tag);
89         if (matches == 0 && strcmp(format,"%lf") == 0)
90             (*((double* )(value))) = DNAN;
91         if (matches != 1)       return 0;       
92         return 1;
93     }
94     return -1;
95 }
96
97
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 */
104   ptr=buf;
105   ptr2=buf;
106   ptr3=buf;
107   /* start with an RRD tag */
108
109   eat_tag(&ptr,"rrd");
110   /* allocate static header */
111   if((rrd->stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
112     rrd_set_error("allocating rrd.stat_head");
113     return -1;    
114   };
115
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));
122
123   /* allocate live head */
124   if((rrd->live_head = calloc(1,sizeof(live_head_t)))==NULL){
125     rrd_set_error("allocating rrd.live_head");
126     return -1;    
127   }
128   read_tag(&ptr,"lastupdate","%lu",&(rrd->live_head->last_up));
129
130   /* Data Source Definition Part */
131   ptr2 = ptr;
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");
136           return -1;
137       };
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");
143         return(-1);
144       }
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));
147
148       read_tag(&ptr2,"name",DS_NAM_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].ds_nam);
149
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;      
153
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));
158
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");
163       ptr=ptr2;
164   }
165   
166   ptr2 = ptr;
167   while (eat_tag(&ptr2,"rra") == 1){
168       rrd->stat_head->rra_cnt++;
169
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));
174
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; }
180
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));
183
184       
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;
188
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)
193           return -1;
194       
195       eat_tag(&ptr2,"cdp_prep");
196       for(i=0;i<rrd->stat_head->ds_cnt;i++){
197            eat_tag(&ptr2,"ds");
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");
205       }
206       eat_tag(&ptr2,"/cdp_prep");
207       rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt=0;
208       eat_tag(&ptr2,"database");
209       ptr3 = ptr2;      
210       while (eat_tag(&ptr3,"row") == 1){
211         
212           if(mempool==0){
213             mempool = 1000;
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; }
218           }
219           rows++;
220           mempool--;
221           rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt++;
222           for(i=0;i<rrd->stat_head->ds_cnt;i++){
223
224                   rrd_value_t  * value = &(rrd->rrd_value[(rows-1)*rrd->stat_head->ds_cnt+i]);
225
226                   read_tag(&ptr3,"v","%lf", value);
227                   
228                   if (
229                           (rc == 1)                     /* do we have to check for the ranges */
230                           &&
231                       (!isnan(*value))  /* not a NAN value */
232                       &&
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))
239                       )
240                   ) {
241                       fprintf (stderr, "out of range found [ds: %lu], [value : %0.10e]\n", i, *value);
242                       *value = DNAN;
243                   }
244           }
245           eat_tag(&ptr3,"/row");                  
246           ptr2=ptr3;
247       }
248       eat_tag(&ptr2,"/database");
249       eat_tag(&ptr2,"/rra");                  
250       ptr=ptr2;
251   }  
252   eat_tag(&ptr,"/rrd");
253
254   if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t)*rrd->stat_head->rra_cnt)) == NULL) {
255       rrd_set_error("allocating rra_ptr");
256       return(-1);
257   }
258
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;
261   }
262   if (ptr==NULL)
263       return -1;
264   return 1;
265 }
266   
267     
268
269
270
271 /* create and empty rrd file according to the specs given */
272
273 int
274 rrd_write(char *file_name, rrd_t *rrd)
275 {
276     unsigned long    i,ii,val_cnt;
277     FILE             *rrd_file=NULL;
278
279     if (strcmp("-",file_name)==0){
280       *rrd_file= *stdout;
281     } else {
282       if ((rrd_file = fopen(file_name,"wb")) == NULL ) {
283         rrd_set_error("creating '%s': %s",file_name,strerror(errno));
284         rrd_free(rrd);
285         return(-1);
286       }
287     }
288     fwrite(rrd->stat_head,
289            sizeof(stat_head_t), 1, rrd_file);
290
291     fwrite(rrd->ds_def,
292            sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
293
294     fwrite(rrd->rra_def,
295            sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
296     
297     fwrite(rrd->live_head, sizeof(live_head_t),1, rrd_file);
298
299     fwrite( rrd->pdp_prep, sizeof(pdp_prep_t),rrd->stat_head->ds_cnt,rrd_file);
300     
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);
304
305
306
307     /* calculate the number of rrd_values to dump */
308     val_cnt=0;
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++)
311             val_cnt++;
312     fwrite( rrd->rrd_value, sizeof(rrd_value_t),val_cnt,rrd_file);
313
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);
317         fclose(rrd_file);       
318         return(-1);
319     }
320     
321     fclose(rrd_file);    
322     return 0;
323 }
324
325
326 int
327 rrd_restore(int argc, char **argv) 
328 {
329     rrd_t          rrd;
330     char          *buf;
331         char                    rc = 0;
332
333     /* init rrd clean */
334     rrd_init(&rrd);
335     if (argc<3) {
336                 rrd_set_error("usage rrdtool %s [--range-check/-r] file.xml file.rrd",argv[0]);
337                 return -1;
338     }
339         
340         while (1) {
341                 static struct option long_options[] =
342                 {
343                         {"range-check",      required_argument, 0,  'r'},
344                         {0,0,0,0}
345                 };
346                 int option_index = 0;
347                 int opt;
348                 
349                 
350                 opt = getopt_long(argc, argv, "r", long_options, &option_index);
351                 
352                 if (opt == EOF)
353                         break;
354                 
355                 switch(opt) {
356                 case 'r':
357                         rc=1;
358                         break;
359                 default:
360                         rrd_set_error("usage rrdtool %s [--range-check|-r] file.xml file.rrd",argv[0]);
361         return -1;
362                         break;
363                 }
364     }
365         
366     if (readfile(argv[optind],&buf,0)==-1){
367       return -1;
368     }
369     if (xml2rrd(buf,&rrd,rc)==-1) {
370         rrd_free(&rrd);
371         free(buf);
372         return -1;
373     }
374     free(buf);
375     if(rrd_write(argv[optind+1],&rrd)==-1){
376         rrd_free(&rrd); 
377         return -1;      
378     };
379     rrd_free(&rrd);    
380     return 0;
381 }