Code

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