1 /*****************************************************************************
2 * RRDtool 1.2.12 Copyright by Tobi Oetiker, 1997-2005
3 *****************************************************************************
4 * rrd_restore.c creates new rrd from data dumped by rrd_dump.c
5 *****************************************************************************/
8 #include "rrd_rpncalc.h"
11 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
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);
29 /* convert all occurrences of <BlaBlaBla> to <blablabla> */
31 void xml_lc(char* buf){
34 if (intag ==0 && (*buf) == '<') {
37 else if (intag ==1 && (*buf) == '>') {
40 } else if (intag ==1) {
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");
61 } while ((*buf)!=ptr);
65 int eat_tag(char **buf, char *tag){
66 if ((*buf)==NULL) return -1; /* fall though clause */
71 && strncmp((*buf)+1,tag,strlen(tag)) == 0
72 && *((*buf)+strlen(tag)+1)=='>') {
73 (*buf) += strlen(tag)+2;
76 rrd_set_error("No <%s> tag found",tag);
84 int read_tag(char **buf, char *tag, char *format, void *value){
87 if ((*buf)==NULL) return -1; /* fall though clause */
89 if (eat_tag(buf,tag)==1){
92 while(*((*buf)+1) && (*(*buf) != '<')) (*buf)++; /*find start of endtag*/
94 matches =sscanf(temp,format,value);
96 end_tag = malloc((strlen(tag)+2)*sizeof(char));
97 sprintf(end_tag,"/%s",tag);
100 if (matches == 0 && strcmp(format,"%lf") == 0)
101 (*((double* )(value))) = DNAN;
102 if (matches != 1) return 0;
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;
116 xml_lc(buf); /* lets lowercase all active parts of the xml */
120 /* start with an RRD tag */
123 /* allocate static header */
124 if((rrd->stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
125 rrd_set_error("allocating rrd.stat_head");
129 strcpy(rrd->stat_head->cookie,RRD_COOKIE);
130 read_tag(&ptr,"version","%4[0-9]",rrd->stat_head->version);
131 input_version = atoi(rrd->stat_head->version);
132 /* added primitive version checking */
133 if (input_version > atoi(RRD_VERSION) || input_version < 1)
135 rrd_set_error("Incompatible file version, detected version %s. This is not supported by the version %s restore tool.\n",
136 rrd -> stat_head -> version, RRD_VERSION );
137 free(rrd -> stat_head);
138 rrd->stat_head = NULL;
141 /* make sure we output the right version */
142 strcpy(rrd->stat_head->version,RRD_VERSION);
144 /* if (atoi(rrd -> stat_head -> version) < 2)
146 rrd_set_error("Can only restore version >= 2 (Not %s). Dump your old rrd using a current rrdtool dump.", rrd -> stat_head -> version );
150 rrd->stat_head->float_cookie = FLOAT_COOKIE;
151 rrd->stat_head->ds_cnt = 0;
152 rrd->stat_head->rra_cnt = 0;
153 read_tag(&ptr,"step","%lu",&(rrd->stat_head->pdp_step));
155 /* allocate live head */
156 if((rrd->live_head = calloc(1,sizeof(live_head_t)))==NULL){
157 rrd_set_error("allocating rrd.live_head");
160 read_tag(&ptr,"lastupdate","%lu",&(rrd->live_head->last_up));
162 /* Data Source Definition Part */
164 while (eat_tag(&ptr2,"ds") == 1){
165 rrd->stat_head->ds_cnt++;
166 if((rrd->ds_def = rrd_realloc(rrd->ds_def,rrd->stat_head->ds_cnt*sizeof(ds_def_t)))==NULL){
167 rrd_set_error("allocating rrd.ds_def");
170 /* clean out memory to make sure no data gets stored from previous tasks */
171 memset(&(rrd->ds_def[rrd->stat_head->ds_cnt-1]), 0, sizeof(ds_def_t));
172 if((rrd->pdp_prep = rrd_realloc(rrd->pdp_prep,rrd->stat_head->ds_cnt
173 *sizeof(pdp_prep_t)))==NULL){
174 rrd_set_error("allocating pdp_prep");
177 /* clean out memory to make sure no data gets stored from previous tasks */
178 memset(&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1]), 0, sizeof(pdp_prep_t));
180 read_tag(&ptr2,"name",DS_NAM_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].ds_nam);
182 read_tag(&ptr2,"type",DST_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].dst);
183 /* test for valid type */
184 if( (int)dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt-1].dst) == -1) return -1;
186 if (dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt-1].dst) != DST_CDEF)
188 read_tag(&ptr2,"minimal_heartbeat","%lu",
189 &(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_mrhb_cnt].u_cnt));
190 read_tag(&ptr2,"min","%lf",&(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_min_val].u_val));
191 read_tag(&ptr2,"max","%lf",&(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_max_val].u_val));
192 } else { /* DST_CDEF */
194 read_tag(&ptr2,"cdef","%s",buffer);
195 parseCDEF_DS(buffer,rrd,rrd -> stat_head -> ds_cnt - 1);
198 read_tag(&ptr2,"last_ds","%30s",rrd->pdp_prep[rrd->stat_head->ds_cnt-1].last_ds);
199 read_tag(&ptr2,"value","%lf",&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1].scratch[PDP_val].u_val));
200 read_tag(&ptr2,"unknown_sec","%lu",&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1].scratch[PDP_unkn_sec_cnt].u_cnt));
201 eat_tag(&ptr2,"/ds");
206 while (eat_tag(&ptr2,"rra") == 1){
207 rrd->stat_head->rra_cnt++;
209 /* allocate and reset rra definition areas */
210 if((rrd->rra_def = rrd_realloc(rrd->rra_def,rrd->stat_head->rra_cnt*sizeof(rra_def_t)))==NULL){
211 rrd_set_error("allocating rra_def"); return -1; }
212 memset(&(rrd->rra_def[rrd->stat_head->rra_cnt-1]), 0, sizeof(rra_def_t));
214 /* allocate and reset consolidation point areas */
215 if((rrd->cdp_prep = rrd_realloc(rrd->cdp_prep,
216 rrd->stat_head->rra_cnt
217 *rrd->stat_head->ds_cnt*sizeof(cdp_prep_t)))==NULL){
218 rrd_set_error("allocating cdp_prep"); return -1; }
220 memset(&(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rrd->stat_head->rra_cnt-1)]),
221 0, rrd->stat_head->ds_cnt*sizeof(cdp_prep_t));
224 read_tag(&ptr2,"cf",CF_NAM_FMT,rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam);
225 /* test for valid type */
226 if( (int)cf_conv(rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam) == -1) return -1;
228 read_tag(&ptr2,"pdp_per_row","%lu",&(rrd->rra_def[rrd->stat_head->rra_cnt-1].pdp_cnt));
229 /* support to read RRA parameters */
230 rra_index = rrd->stat_head->rra_cnt - 1;
231 if ( input_version < 2 ){
232 read_tag(&ptr2, "xff","%lf",
233 &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].u_val));
235 eat_tag(&ptr2, "params");
237 /* backwards compatibility w/ old patch */
238 if (strncmp(ptr2, "<value>",7) == 0) {
239 parse_patch1028_RRA_params(&ptr2,rrd,rra_index);
241 switch(cf_conv(rrd -> rra_def[rra_index].cf_nam)) {
243 read_tag(&ptr2, "hw_alpha", "%lf",
244 &(rrd->rra_def[rra_index].par[RRA_hw_alpha].u_val));
245 read_tag(&ptr2, "hw_beta", "%lf",
246 &(rrd->rra_def[rra_index].par[RRA_hw_beta].u_val));
247 read_tag(&ptr2, "dependent_rra_idx", "%lu",
248 &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
252 read_tag(&ptr2, "seasonal_gamma", "%lf",
253 &(rrd->rra_def[rra_index].par[RRA_seasonal_gamma].u_val));
254 read_tag(&ptr2, "seasonal_smooth_idx", "%lu",
255 &(rrd->rra_def[rra_index].par[RRA_seasonal_smooth_idx].u_cnt));
256 read_tag(&ptr2, "dependent_rra_idx", "%lu",
257 &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
260 read_tag(&ptr2, "delta_pos", "%lf",
261 &(rrd->rra_def[rra_index].par[RRA_delta_pos].u_val));
262 read_tag(&ptr2, "delta_neg", "%lf",
263 &(rrd->rra_def[rra_index].par[RRA_delta_neg].u_val));
264 read_tag(&ptr2, "window_len", "%lu",
265 &(rrd->rra_def[rra_index].par[RRA_window_len].u_cnt));
266 read_tag(&ptr2, "failure_threshold", "%lu",
267 &(rrd->rra_def[rra_index].par[RRA_failure_threshold].u_cnt));
270 read_tag(&ptr2, "dependent_rra_idx", "%lu",
271 &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
278 read_tag(&ptr2, "xff","%lf",
279 &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].u_val));
282 eat_tag(&ptr2, "/params");
286 eat_tag(&ptr2,"cdp_prep");
287 for(i=0;i< (int)rrd->stat_head->ds_cnt;i++)
290 /* support to read CDP parameters */
291 rra_index = rrd->stat_head->rra_cnt-1;
293 if ( input_version < 2 ){
294 rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)+i].scratch[CDP_primary_val].u_val = 0.0;
295 rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)+i].scratch[CDP_secondary_val].u_val = 0.0;
296 read_tag(&ptr2,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
297 *(rra_index) +i].scratch[CDP_val].u_val));
298 read_tag(&ptr2,"unknown_datapoints","%lu",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
299 *(rra_index) +i].scratch[CDP_unkn_pdp_cnt].u_cnt));
302 if (strncmp(ptr2, "<value>",7) == 0) {
303 parse_patch1028_CDP_params(&ptr2,rrd,rra_index,i);
305 read_tag(&ptr2, "primary_value","%lf",
306 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
307 +i].scratch[CDP_primary_val].u_val));
308 read_tag(&ptr2, "secondary_value","%lf",
309 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
310 +i].scratch[CDP_secondary_val].u_val));
311 switch(cf_conv(rrd->rra_def[rra_index].cf_nam)) {
313 read_tag(&ptr2,"intercept","%lf",
314 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
315 +i].scratch[CDP_hw_intercept].u_val));
316 read_tag(&ptr2,"last_intercept","%lf",
317 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
318 +i].scratch[CDP_hw_last_intercept].u_val));
319 read_tag(&ptr2,"slope","%lf",
320 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
321 +i].scratch[CDP_hw_slope].u_val));
322 read_tag(&ptr2,"last_slope","%lf",
323 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
324 +i].scratch[CDP_hw_last_slope].u_val));
325 read_tag(&ptr2,"nan_count","%lu",
326 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
327 +i].scratch[CDP_null_count].u_cnt));
328 read_tag(&ptr2,"last_nan_count","%lu",
329 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
330 +i].scratch[CDP_last_null_count].u_cnt));
334 read_tag(&ptr2,"seasonal","%lf",
335 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
336 +i].scratch[CDP_hw_seasonal].u_val));
337 read_tag(&ptr2,"last_seasonal","%lf",
338 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
339 +i].scratch[CDP_hw_last_seasonal].u_val));
340 read_tag(&ptr2,"init_flag","%lu",
341 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
342 +i].scratch[CDP_init_seasonal].u_cnt));
347 parse_FAILURES_history(&ptr2,rrd,rra_index,i);
354 read_tag(&ptr2,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
355 *(rra_index) +i].scratch[CDP_val].u_val));
356 read_tag(&ptr2,"unknown_datapoints","%lu",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
357 *(rra_index) +i].scratch[CDP_unkn_pdp_cnt].u_cnt));
362 eat_tag(&ptr2,"/ds");
364 eat_tag(&ptr2,"/cdp_prep");
365 rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt=0;
366 eat_tag(&ptr2,"database");
368 while (eat_tag(&ptr3,"row") == 1){
372 if((rrd->rrd_value = rrd_realloc(rrd->rrd_value,
373 (rows+mempool)*(rrd->stat_head->ds_cnt)
374 *sizeof(rrd_value_t)))==NULL) {
375 rrd_set_error("allocating rrd_values"); return -1; }
379 rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt++;
380 for(i=0;i< (int)rrd->stat_head->ds_cnt;i++){
382 rrd_value_t * value = &(rrd->rrd_value[(rows-1)*rrd->stat_head->ds_cnt+i]);
384 read_tag(&ptr3,"v","%lf", value);
387 (rc == 1) /* do we have to check for the ranges */
389 (!isnan(*value)) /* not a NAN value */
391 (dst_conv(rrd->ds_def[i].dst) != DST_CDEF)
393 ( /* min defined and in the range ? */
394 (!isnan(rrd->ds_def[i].par[DS_min_val].u_val)
395 && (*value < rrd->ds_def[i].par[DS_min_val].u_val))
396 || /* max defined and in the range ? */
397 (!isnan(rrd->ds_def[i].par[DS_max_val].u_val)
398 && (*value > rrd->ds_def[i].par[DS_max_val].u_val))
401 fprintf (stderr, "out of range found [ds: %lu], [value : %0.10e]\n", i, *value);
405 eat_tag(&ptr3,"/row");
408 eat_tag(&ptr2,"/database");
409 eat_tag(&ptr2,"/rra");
412 eat_tag(&ptr,"/rrd");
414 if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t)*rrd->stat_head->rra_cnt)) == NULL) {
415 rrd_set_error("allocating rra_ptr");
419 for(i=0; i < (int)rrd->stat_head->rra_cnt; i++) {
420 /* last row in the xml file is the most recent; as
421 * rrd_update increments the current row pointer, set cur_row
422 * here to the last row. */
423 rrd->rra_ptr[i].cur_row = rrd->rra_def[i].row_cnt-1;
434 /* create and empty rrd file according to the specs given */
437 rrd_write(char *file_name, rrd_t *rrd, char force_overwrite)
439 unsigned long i,ii,val_cnt;
444 if (strcmp("-",file_name)==0){
447 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
448 fdflags = O_RDWR|O_BINARY|O_CREAT;
450 fdflags = O_WRONLY|O_CREAT;
452 if (force_overwrite == 0) {
455 fd = open(file_name,fdflags,0666);
456 if (fd == -1 || (rrd_file = fdopen(fd,"wb")) == NULL) {
457 rrd_set_error("creating '%s': %s",file_name,rrd_strerror(errno));
463 fwrite(rrd->stat_head,
464 sizeof(stat_head_t), 1, rrd_file);
467 sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
470 sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
472 fwrite(rrd->live_head, sizeof(live_head_t),1, rrd_file);
474 fwrite( rrd->pdp_prep, sizeof(pdp_prep_t),rrd->stat_head->ds_cnt,rrd_file);
476 fwrite( rrd->cdp_prep, sizeof(cdp_prep_t),rrd->stat_head->rra_cnt*
477 rrd->stat_head->ds_cnt,rrd_file);
478 fwrite( rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt,rrd_file);
482 /* calculate the number of rrd_values to dump */
484 for(i=0; i < rrd->stat_head->rra_cnt; i++)
485 for(ii=0; ii < rrd->rra_def[i].row_cnt * rrd->stat_head->ds_cnt;ii++)
487 fwrite( rrd->rrd_value, sizeof(rrd_value_t),val_cnt,rrd_file);
489 /* lets see if we had an error */
490 if(ferror(rrd_file)){
491 rrd_set_error("a file error occurred while creating '%s'",file_name);
502 rrd_restore(int argc, char **argv)
507 char force_overwrite = 0;
510 optind = 0; opterr = 0; /* initialize getopt */
512 static struct option long_options[] =
514 {"range-check", no_argument, 0, 'r'},
515 {"force-overwrite", no_argument, 0, 'f'},
518 int option_index = 0;
522 opt = getopt_long(argc, argv, "rf", long_options, &option_index);
535 rrd_set_error("usage rrdtool %s [--range-check|-r] [--force-overwrite/-f] file.xml file.rrd",argv[0]);
541 if (argc-optind != 2) {
542 rrd_set_error("usage rrdtool %s [--range-check/-r] [--force-overwrite/-f] file.xml file.rrd",argv[0]);
546 if (readfile(argv[optind],&buf,0)==-1){
552 if (xml2rrd(buf,&rrd,rc)==-1) {
560 if(rrd_write(argv[optind+1],&rrd,force_overwrite)==-1){
568 /* a backwards compatibility routine that will parse the RRA params section
569 * generated by the aberrant patch to 1.0.28. */
572 parse_patch1028_RRA_params(char **buf, rrd_t *rrd, int rra_index)
575 for (i = 0; i < MAX_RRA_PAR_EN; i++)
577 if (i == RRA_dependent_rra_idx ||
578 i == RRA_seasonal_smooth_idx ||
579 i == RRA_failure_threshold)
580 read_tag(buf, "value","%lu",
581 &(rrd->rra_def[rra_index].par[i].u_cnt));
583 read_tag(buf, "value","%lf",
584 &(rrd->rra_def[rra_index].par[i].u_val));
588 /* a backwards compatibility routine that will parse the CDP params section
589 * generated by the aberrant patch to 1.0.28. */
591 parse_patch1028_CDP_params(char **buf, rrd_t *rrd, int rra_index, int ds_index)
594 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
596 if (cf_conv(rrd->rra_def[rra_index].cf_nam) == CF_FAILURES ||
597 ii == CDP_unkn_pdp_cnt ||
598 ii == CDP_null_count ||
599 ii == CDP_last_null_count)
601 read_tag(buf,"value","%lu",
602 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index) + ds_index].scratch[ii].u_cnt));
604 read_tag(buf,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt*
605 (rra_index) + ds_index].scratch[ii].u_val));
611 parse_FAILURES_history(char **buf, rrd_t *rrd, int rra_index, int ds_index)
613 char history[MAX_FAILURES_WINDOW_LEN + 1];
614 char *violations_array;
617 /* 28 = MAX_FAILURES_WINDOW_LEN */
618 read_tag(buf, "history", "%28[0-1]", history);
619 violations_array = (char*) rrd -> cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
622 for (i = 0; i < rrd -> rra_def[rra_index].par[RRA_window_len].u_cnt; ++i)
623 violations_array[i] = (history[i] == '1') ? 1 : 0;