Code

42c316c0f056d7b9d32e19b508dbec7c0d9e71b6
[rrdtool-all.git] / program / src / rrd_restore.c
1 /*****************************************************************************
2  * RRDtool 1.2.27  Copyright by Tobi Oetiker, 1997-2008
3  *****************************************************************************
4  * rrd_restore.c  creates new rrd from data dumped by rrd_dump.c
5  *****************************************************************************/
7 #include "rrd_tool.h"
8 #include "rrd_rpncalc.h"
9 #include <fcntl.h>
11 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
12 #include <io.h>
13 #define open _open
14 #define close _close
15 #endif
17 /* Prototypes */
19 void xml_lc(char*);
20 int skip(char **);
21 int skipxml(char **);
22 int eat_tag(char **, char *);
23 int read_tag(char **, char *, char *, void *);
24 int xml2rrd(char*, rrd_t*, char);
25 int rrd_write(char *, rrd_t *, char);
26 void parse_patch1028_RRA_params(char **buf, rrd_t *rrd, int rra_index);
27 void parse_patch1028_CDP_params(char **buf, rrd_t *rrd, int rra_index, int ds_index);
28 void parse_FAILURES_history(char **buf, rrd_t *rrd, int rra_index, int ds_index);
29 long int rra_random_row(rra_def_t *);
31 /* convert all occurrences of <BlaBlaBla> to <blablabla> */
33 void xml_lc(char* buf){
34   int intag=0;
35   while((*buf)){
36     if (intag ==0 && (*buf) == '<') {
37       intag = 1;
38     }
39     else if (intag ==1 && (*buf) == '>') {
40       intag = 0;
41       continue;
42     } else  if (intag ==1) {
43       *buf = tolower(*buf);
44     }
45     buf++;    
46   }
47 }
49 int skipxml(char **buf){
50   char *ptr;  
51   ptr=(*buf);
52   do {
53     (*buf)=ptr;
54     while((*(ptr+1)) && ((*ptr)==' ' ||  (*ptr)=='\r' || (*ptr)=='\n' || (*ptr)=='\t')) ptr++;
55     if (strncmp(ptr,"<?xml",4) == 0) {
56       ptr= strstr(ptr,"?>");
57       if (ptr) ptr+=2; else {
58         rrd_set_error("Dangling XML header");
59         (*buf) = NULL;
60         return -1;
61       }
62     }
63   } while ((*buf)!=ptr);  
64   return 1;
65 }
67 int skip(char **buf){
68   char *ptr;
69   if ((buf == NULL) || (*buf == NULL))
70     return -1;  
71   ptr=(*buf);
72   do {
73     (*buf)=ptr;
74     while((*(ptr+1)) && ((*ptr)==' ' ||  (*ptr)=='\r' || (*ptr)=='\n' || (*ptr)=='\t')) ptr++;
75     if (strncmp(ptr,"<!--",4) == 0) {
76       ptr= strstr(ptr,"-->");
77       if (ptr) ptr+=3; else {
78         rrd_set_error("Dangling Comment");
79         (*buf) = NULL;
80         return -1;
81       }
82     }
83   } while ((*buf)!=ptr);  
84   return 1;
85 }
87 int eat_tag(char **buf, char *tag){ 
88   if ((*buf)==NULL) return -1;   /* fall though clause */
90   rrd_clear_error();
91   skip(buf);
92   if ((**buf)=='<' 
93       && strncmp((*buf)+1,tag,strlen(tag)) == 0 
94       && *((*buf)+strlen(tag)+1)=='>') {
95     (*buf) += strlen(tag)+2;
96   }
97   else {
98     rrd_set_error("No <%s> tag found",tag);
99     (*buf) = NULL;
100     return -1;
101   }
102   skip(buf);
103   return 1;
106 int read_tag(char **buf, char *tag, char *format, void *value){
107     char *end_tag;
108     int matches;
109     if ((*buf)==NULL) return -1;   /* fall though clause */
110     rrd_clear_error();
111     if (eat_tag(buf,tag)==1){
112         char *temp;
113         temp = (*buf);
114         while(*((*buf)+1) && (*(*buf) != '<')) (*buf)++; /*find start of endtag*/
115         *(*buf) = '\0';
116         matches =sscanf(temp,format,value);
117         *(*buf) = '<';
118         end_tag = malloc((strlen(tag)+2)*sizeof(char));
119         sprintf(end_tag,"/%s",tag);
120         eat_tag(buf,end_tag);
121         free(end_tag);
122         if (matches == 0 && strcmp(format,"%lf") == 0)
123             (*((double* )(value))) = DNAN;
124         if (matches != 1)       return 0;       
125         return 1;
126     }
127     return -1;
131 /* parse the data stored in buf and return a filled rrd structure */
132 int xml2rrd(char* buf, rrd_t* rrd, char rc){
133   /* pass 1 identify number of RRAs  */
134   char *ptr,*ptr2,*ptr3; /* walks thought the buffer */
135   long rows=0,mempool=0,i=0;
136   int rra_index;
137   int input_version;
138   xml_lc(buf); /* lets lowercase all active parts of the xml */
139   ptr=buf;
140   ptr2=buf;
141   ptr3=buf;
142   /* start with an RRD tag */
143   
144   skipxml(&ptr);
146   eat_tag(&ptr,"rrd");
147   /* allocate static header */
148   if((rrd->stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
149     rrd_set_error("allocating rrd.stat_head");
150     return -1;    
151   };
153   strcpy(rrd->stat_head->cookie,RRD_COOKIE);
154   read_tag(&ptr,"version","%4[0-9]",rrd->stat_head->version);
155   input_version = atoi(rrd->stat_head->version);
156   /* added primitive version checking */
157   if (input_version > atoi(RRD_VERSION) || input_version < 1)
158   {
159     rrd_set_error("Incompatible file version, detected version %s. This is not supported by the version %s restore tool.\n",
160                   rrd -> stat_head -> version, RRD_VERSION );
161     free(rrd -> stat_head); 
162     rrd->stat_head = NULL; 
163     return -1;
164   }
165   /* make sure we output the right version */
166   strcpy(rrd->stat_head->version,RRD_VERSION);
168   /*  if (atoi(rrd -> stat_head -> version) < 2) 
169   {
170     rrd_set_error("Can only restore version >= 2 (Not %s). Dump your old rrd using a current rrdtool dump.",  rrd -> stat_head -> version );
171     return -1;
172   } */
174   rrd->stat_head->float_cookie = FLOAT_COOKIE;
175   rrd->stat_head->ds_cnt = 0;
176   rrd->stat_head->rra_cnt = 0;
177   read_tag(&ptr,"step","%lu",&(rrd->stat_head->pdp_step));
179   /* allocate live head */
180   if((rrd->live_head = calloc(1,sizeof(live_head_t)))==NULL){
181     rrd_set_error("allocating rrd.live_head");
182     return -1;    
183   }
184   read_tag(&ptr,"lastupdate","%lu",&(rrd->live_head->last_up));
186   /* Data Source Definition Part */
187   ptr2 = ptr;
188   while (eat_tag(&ptr2,"ds") == 1){
189       rrd->stat_head->ds_cnt++;
190       if((rrd->ds_def = rrd_realloc(rrd->ds_def,rrd->stat_head->ds_cnt*sizeof(ds_def_t)))==NULL){
191           rrd_set_error("allocating rrd.ds_def");
192           return -1;
193       };
194       /* clean out memory to make sure no data gets stored from previous tasks */
195       memset(&(rrd->ds_def[rrd->stat_head->ds_cnt-1]), 0, sizeof(ds_def_t));
196       if((rrd->pdp_prep = rrd_realloc(rrd->pdp_prep,rrd->stat_head->ds_cnt
197                                   *sizeof(pdp_prep_t)))==NULL){
198         rrd_set_error("allocating pdp_prep");
199         return(-1);
200       }
201       /* clean out memory to make sure no data gets stored from previous tasks */
202       memset(&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1]), 0, sizeof(pdp_prep_t));
204       read_tag(&ptr2,"name",DS_NAM_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].ds_nam);
206       read_tag(&ptr2,"type",DST_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].dst);
207       /* test for valid type */
208       if( (int)dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt-1].dst) == -1) return -1;      
210           if (dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt-1].dst) != DST_CDEF)
211           {
212       read_tag(&ptr2,"minimal_heartbeat","%lu",
213                &(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_mrhb_cnt].u_cnt));
214       read_tag(&ptr2,"min","%lf",&(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_min_val].u_val));
215       read_tag(&ptr2,"max","%lf",&(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_max_val].u_val));
216           } else { /* DST_CDEF */
217                  char buffer[1024];
218                  read_tag(&ptr2,"cdef","%1000s",buffer);
219                  parseCDEF_DS(buffer,rrd,rrd -> stat_head -> ds_cnt - 1);
220                  if (rrd_test_error()) return -1;
221           }
223       read_tag(&ptr2,"last_ds","%30s",rrd->pdp_prep[rrd->stat_head->ds_cnt-1].last_ds);
224       read_tag(&ptr2,"value","%lf",&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1].scratch[PDP_val].u_val));
225       read_tag(&ptr2,"unknown_sec","%lu",&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1].scratch[PDP_unkn_sec_cnt].u_cnt));      
226       eat_tag(&ptr2,"/ds");
227       ptr=ptr2;
228   }
229   
230   ptr2 = ptr;
231   while (eat_tag(&ptr2,"rra") == 1){
232       rrd->stat_head->rra_cnt++;
234       /* allocate and reset rra definition areas */
235       if((rrd->rra_def = rrd_realloc(rrd->rra_def,rrd->stat_head->rra_cnt*sizeof(rra_def_t)))==NULL){
236           rrd_set_error("allocating rra_def"); return -1; }      
237       memset(&(rrd->rra_def[rrd->stat_head->rra_cnt-1]), 0, sizeof(rra_def_t));
239       /* allocate and reset consolidation point areas */
240       if((rrd->cdp_prep = rrd_realloc(rrd->cdp_prep,
241                                   rrd->stat_head->rra_cnt
242                                   *rrd->stat_head->ds_cnt*sizeof(cdp_prep_t)))==NULL){
243          rrd_set_error("allocating cdp_prep"); return -1; }
245       memset(&(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rrd->stat_head->rra_cnt-1)]), 
246              0, rrd->stat_head->ds_cnt*sizeof(cdp_prep_t));
248       
249       read_tag(&ptr2,"cf",CF_NAM_FMT,rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam);
250       /* test for valid type */
251       if( (int)cf_conv(rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam) == -1) return -1;
253       read_tag(&ptr2,"pdp_per_row","%lu",&(rrd->rra_def[rrd->stat_head->rra_cnt-1].pdp_cnt));
254       /* support to read RRA parameters */
255       rra_index = rrd->stat_head->rra_cnt - 1;
256       if ( input_version < 2 ){
257          read_tag(&ptr2, "xff","%lf",
258             &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].u_val));
259       } else {
260         if (eat_tag(&ptr2, "params") != 1) {
261           rrd_set_error("could not find params tag to eat and skip");
262           return -1;
263         }
264         skip(&ptr2);
265         /* backwards compatibility w/ old patch */
266       if (strncmp(ptr2, "<value>",7) == 0) {
267           parse_patch1028_RRA_params(&ptr2,rrd,rra_index); 
268       } else {
269       switch(cf_conv(rrd -> rra_def[rra_index].cf_nam)) {
270       case CF_HWPREDICT:
271          read_tag(&ptr2, "hw_alpha", "%lf", 
272             &(rrd->rra_def[rra_index].par[RRA_hw_alpha].u_val));
273          read_tag(&ptr2, "hw_beta", "%lf", 
274             &(rrd->rra_def[rra_index].par[RRA_hw_beta].u_val));
275          read_tag(&ptr2, "dependent_rra_idx", "%lu", 
276             &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
277          break;
278       case CF_SEASONAL:
279       case CF_DEVSEASONAL:
280          read_tag(&ptr2, "seasonal_gamma", "%lf", 
281             &(rrd->rra_def[rra_index].par[RRA_seasonal_gamma].u_val));
282          read_tag(&ptr2, "seasonal_smooth_idx", "%lu", 
283             &(rrd->rra_def[rra_index].par[RRA_seasonal_smooth_idx].u_cnt));
284          read_tag(&ptr2, "dependent_rra_idx", "%lu", 
285             &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
286          break;
287       case CF_FAILURES:
288          read_tag(&ptr2, "delta_pos", "%lf", 
289             &(rrd->rra_def[rra_index].par[RRA_delta_pos].u_val));
290          read_tag(&ptr2, "delta_neg", "%lf", 
291             &(rrd->rra_def[rra_index].par[RRA_delta_neg].u_val));
292          read_tag(&ptr2, "window_len", "%lu", 
293             &(rrd->rra_def[rra_index].par[RRA_window_len].u_cnt));
294          read_tag(&ptr2, "failure_threshold", "%lu", 
295             &(rrd->rra_def[rra_index].par[RRA_failure_threshold].u_cnt));
296          /* fall thru */
297       case CF_DEVPREDICT:
298          read_tag(&ptr2, "dependent_rra_idx", "%lu", 
299             &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
300          break;
301       case CF_AVERAGE:
302       case CF_MAXIMUM:
303       case CF_MINIMUM:
304       case CF_LAST:
305       default:
306          read_tag(&ptr2, "xff","%lf",
307             &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].u_val));
308       }
309       }
310       eat_tag(&ptr2, "/params");
311    }
314       eat_tag(&ptr2,"cdp_prep");
315       for(i=0;i< (int)rrd->stat_head->ds_cnt;i++)
316       {
317       eat_tag(&ptr2,"ds");
318       /* support to read CDP parameters */
319       rra_index = rrd->stat_head->rra_cnt-1; 
320       skip(&ptr2);
321       if ( input_version < 2 ){
322           rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)+i].scratch[CDP_primary_val].u_val = 0.0;
323           rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)+i].scratch[CDP_secondary_val].u_val = 0.0;
324           read_tag(&ptr2,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
325                *(rra_index) +i].scratch[CDP_val].u_val));
326           read_tag(&ptr2,"unknown_datapoints","%lu",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
327               *(rra_index) +i].scratch[CDP_unkn_pdp_cnt].u_cnt));
328       } else {
330       if (strncmp(ptr2, "<value>",7) == 0) {
331          parse_patch1028_CDP_params(&ptr2,rrd,rra_index,i);
332       } else {
333          read_tag(&ptr2, "primary_value","%lf",
334                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
335                +i].scratch[CDP_primary_val].u_val));
336          read_tag(&ptr2, "secondary_value","%lf",
337                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
338                +i].scratch[CDP_secondary_val].u_val));
339          switch(cf_conv(rrd->rra_def[rra_index].cf_nam)) {
340          case CF_HWPREDICT:
341             read_tag(&ptr2,"intercept","%lf", 
342                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
343                +i].scratch[CDP_hw_intercept].u_val));
344             read_tag(&ptr2,"last_intercept","%lf", 
345                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
346                +i].scratch[CDP_hw_last_intercept].u_val));
347             read_tag(&ptr2,"slope","%lf", 
348                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
349                +i].scratch[CDP_hw_slope].u_val));
350             read_tag(&ptr2,"last_slope","%lf", 
351                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
352                +i].scratch[CDP_hw_last_slope].u_val));
353             read_tag(&ptr2,"nan_count","%lu", 
354                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
355                +i].scratch[CDP_null_count].u_cnt));
356             read_tag(&ptr2,"last_nan_count","%lu", 
357                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
358                +i].scratch[CDP_last_null_count].u_cnt));
359             break;
360          case CF_SEASONAL:
361          case CF_DEVSEASONAL:
362             read_tag(&ptr2,"seasonal","%lf", 
363                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
364                +i].scratch[CDP_hw_seasonal].u_val));
365             read_tag(&ptr2,"last_seasonal","%lf", 
366                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
367                +i].scratch[CDP_hw_last_seasonal].u_val));
368             read_tag(&ptr2,"init_flag","%lu", 
369                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
370                +i].scratch[CDP_init_seasonal].u_cnt));
371             break;
372          case CF_DEVPREDICT:
373             break;
374          case CF_FAILURES:
375             parse_FAILURES_history(&ptr2,rrd,rra_index,i); 
376             break;
377          case CF_AVERAGE:
378          case CF_MAXIMUM:
379          case CF_MINIMUM:
380          case CF_LAST:
381          default:
382             read_tag(&ptr2,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
383                *(rra_index) +i].scratch[CDP_val].u_val));
384             read_tag(&ptr2,"unknown_datapoints","%lu",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
385                *(rra_index) +i].scratch[CDP_unkn_pdp_cnt].u_cnt));
386             break;
387          }
388       }
389       }
390       eat_tag(&ptr2,"/ds");
391       }
392       eat_tag(&ptr2,"/cdp_prep");
393       rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt=0;
394       eat_tag(&ptr2,"database");
395       ptr3 = ptr2;      
396       while (eat_tag(&ptr3,"row") == 1){
397         
398           if(mempool==0){
399             mempool = 1000;
400             if((rrd->rrd_value = rrd_realloc(rrd->rrd_value,
401                                          (rows+mempool)*(rrd->stat_head->ds_cnt)
402                                          *sizeof(rrd_value_t)))==NULL) {
403               rrd_set_error("allocating rrd_values"); return -1; }
404           }
405           rows++;
406           mempool--;
407           rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt++;
408           for(i=0;i< (int)rrd->stat_head->ds_cnt;i++){
410                   rrd_value_t  * value = &(rrd->rrd_value[(rows-1)*rrd->stat_head->ds_cnt+i]);
412                   read_tag(&ptr3,"v","%lf", value);
413                   
414                   if (
415                           (rc == 1)                     /* do we have to check for the ranges */
416                           &&
417                       (!isnan(*value))  /* not a NAN value */
418                       &&
419                           (dst_conv(rrd->ds_def[i].dst) != DST_CDEF)
420                           &&
421                       (                                 /* min defined and in the range ? */
422                           (!isnan(rrd->ds_def[i].par[DS_min_val].u_val) 
423                                 && (*value < rrd->ds_def[i].par[DS_min_val].u_val)) 
424                           ||                            /* max defined and in the range ? */
425                           (!isnan(rrd->ds_def[i].par[DS_max_val].u_val) 
426                                 && (*value > rrd->ds_def[i].par[DS_max_val].u_val))
427                       )
428                   ) {
429                       fprintf (stderr, "out of range found [ds: %lu], [value : %0.10e]\n", i, *value);
430                       *value = DNAN;
431                   }
432           }
433           eat_tag(&ptr3,"/row");                  
434           ptr2=ptr3;
435       }
436       eat_tag(&ptr2,"/database");
437       eat_tag(&ptr2,"/rra");                  
438       ptr=ptr2;
439   }  
440   eat_tag(&ptr,"/rrd");
442   if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t)*rrd->stat_head->rra_cnt)) == NULL) {
443       rrd_set_error("allocating rra_ptr");
444       return(-1);
445   }
447   if (ptr==NULL)
448       return -1;
449   return 1;
451   
452     
456 /* create and empty rrd file according to the specs given */
458 int
459 rrd_write(char *file_name, rrd_t *rrd, char force_overwrite)
461     unsigned long    i,ii,rra_offset;
462     FILE             *rrd_file=NULL;
463     int                 fdflags;
464     int                 fd;
466     if (strcmp("-",file_name)==0){
467       rrd_file= stdout;
468     } else {
469 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
470       fdflags = O_RDWR|O_BINARY|O_CREAT;
471 #else
472       fdflags = O_WRONLY|O_CREAT;
473 #endif            
474       if (force_overwrite == 0) {
475         fdflags |= O_EXCL;
476       }
477       fd = open(file_name,fdflags,0666);
478       if (fd == -1 || (rrd_file = fdopen(fd,"wb")) == NULL) {
479         rrd_set_error("creating '%s': %s",file_name,rrd_strerror(errno));
480         if (fd != -1)
481           close(fd);
482         return(-1);
483       }
484     }
485     fwrite(rrd->stat_head,
486            sizeof(stat_head_t), 1, rrd_file);
488     fwrite(rrd->ds_def,
489            sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
491     fwrite(rrd->rra_def,
492            sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
494     fwrite(rrd->live_head, sizeof(live_head_t),1, rrd_file);
496     fwrite( rrd->pdp_prep, sizeof(pdp_prep_t),rrd->stat_head->ds_cnt,rrd_file);
497     
498     fwrite( rrd->cdp_prep, sizeof(cdp_prep_t),rrd->stat_head->rra_cnt*
499             rrd->stat_head->ds_cnt,rrd_file);
501     for(i=0; i < rrd->stat_head->rra_cnt; i++)
502       rrd->rra_ptr[i].cur_row = rra_random_row(&rrd->rra_def[i]);
504     fwrite( rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt,rrd_file);
508     /* Dump RRD values */
509     rra_offset=0;
510     for(i=0; i <  rrd->stat_head->rra_cnt; i++)
511     {
512         unsigned long num_rows = rrd->rra_def[i].row_cnt;
513         unsigned long cur_row = rrd->rra_ptr[i].cur_row;
514         unsigned long ds_cnt = rrd->stat_head->ds_cnt;
516         fwrite(rrd->rrd_value + (rra_offset + num_rows-1 - cur_row) * ds_cnt,
517                sizeof(rrd_value_t), (cur_row+1)*ds_cnt, rrd_file);
519         fwrite(rrd->rrd_value + rra_offset * ds_cnt,
520                sizeof(rrd_value_t), (num_rows-1 - cur_row)*ds_cnt, rrd_file);
522         rra_offset += num_rows;
523     }
525     /* lets see if we had an error */
526     if(ferror(rrd_file)){
527         rrd_set_error("a file error occurred while creating '%s'",file_name);
528         fclose(rrd_file);       
529         return(-1);
530     }
531     
532     fclose(rrd_file);    
533     return 0;
537 int
538 rrd_restore(int argc, char **argv) 
540     rrd_t          rrd;
541     char          *buf;
542         char                    rc = 0;
543         char                    force_overwrite = 0;    
545     /* init rrd clean */
546     optind = 0; opterr = 0;  /* initialize getopt */
547         while (1) {
548                 static struct option long_options[] =
549                 {
550                         {"range-check",      no_argument, 0,  'r'},
551                         {"force-overwrite",  no_argument, 0,  'f'},
552                         {0,0,0,0}
553                 };
554                 int option_index = 0;
555                 int opt;
556                 
557                 
558                 opt = getopt_long(argc, argv, "rf", long_options, &option_index);
559                 
560                 if (opt == EOF)
561                         break;
562                 
563                 switch(opt) {
564                 case 'r':
565                         rc=1;
566                         break;
567                 case 'f':
568                         force_overwrite=1;
569                         break;
570                 default:
571                         rrd_set_error("usage rrdtool %s [--range-check|-r] [--force-overwrite/-f]  file.xml file.rrd",argv[0]);
572                         return -1;
573                         break;
574                 }
575     }
577     if (argc-optind != 2) {
578                 rrd_set_error("usage rrdtool %s [--range-check/-r] [--force-overwrite/-f] file.xml file.rrd",argv[0]);
579                 return -1;
580     }
581         
582     if (readfile(argv[optind],&buf,0)==-1){
583       return -1;
584     }
586     rrd_init(&rrd);
588     if (xml2rrd(buf,&rrd,rc)==-1) {
589         rrd_free(&rrd);
590         free(buf);
591         return -1;
592     }
594     free(buf);
596     if(rrd_write(argv[optind+1],&rrd,force_overwrite)==-1){
597         rrd_free(&rrd); 
598         return -1;      
599     };
600     rrd_free(&rrd);    
601     return 0;
604 /* a backwards compatibility routine that will parse the RRA params section
605  * generated by the aberrant patch to 1.0.28. */
607 void
608 parse_patch1028_RRA_params(char **buf, rrd_t *rrd, int rra_index)
610    int i;
611    for (i = 0; i < MAX_RRA_PAR_EN; i++)
612    {
613    if (i == RRA_dependent_rra_idx ||
614        i == RRA_seasonal_smooth_idx ||
615        i == RRA_failure_threshold)
616       read_tag(buf, "value","%lu",
617          &(rrd->rra_def[rra_index].par[i].u_cnt));
618    else
619       read_tag(buf, "value","%lf",
620          &(rrd->rra_def[rra_index].par[i].u_val));
621    }
624 /* a backwards compatibility routine that will parse the CDP params section
625  * generated by the aberrant patch to 1.0.28. */
626 void
627 parse_patch1028_CDP_params(char **buf, rrd_t *rrd, int rra_index, int ds_index)
629    int ii;
630    for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
631    {
632    if (cf_conv(rrd->rra_def[rra_index].cf_nam) == CF_FAILURES ||
633        ii == CDP_unkn_pdp_cnt ||
634        ii == CDP_null_count ||
635        ii == CDP_last_null_count)
636    {
637       read_tag(buf,"value","%lu",
638        &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index) + ds_index].scratch[ii].u_cnt));
639    } else {
640       read_tag(buf,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt*
641        (rra_index) + ds_index].scratch[ii].u_val));
642    }
643    }
646 void
647 parse_FAILURES_history(char **buf, rrd_t *rrd, int rra_index, int ds_index)
649    char history[MAX_FAILURES_WINDOW_LEN + 1];
650    char *violations_array;
651    unsigned short i;
653    /* 28 = MAX_FAILURES_WINDOW_LEN */ 
654    read_tag(buf, "history", "%28[0-1]", history);
655    violations_array = (char*) rrd -> cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
656       + ds_index].scratch;
657    
658    for (i = 0; i < rrd -> rra_def[rra_index].par[RRA_window_len].u_cnt; ++i)
659       violations_array[i] = (history[i] == '1') ? 1 : 0;