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