1 /*****************************************************************************
2 * RRDtool 1.2.21 Copyright by Tobi Oetiker, 1997-2007
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__)
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);
30 /* convert all occurrences of <BlaBlaBla> to <blablabla> */
32 void xml_lc(char* buf){
35 if (intag ==0 && (*buf) == '<') {
38 else if (intag ==1 && (*buf) == '>') {
41 } else if (intag ==1) {
48 int skipxml(char **buf){
53 while((*(ptr+1)) && ((*ptr)==' ' || (*ptr)=='\r' || (*ptr)=='\n' || (*ptr)=='\t')) ptr++;
54 if (strncmp(ptr,"<?xml",4) == 0) {
55 ptr= strstr(ptr,"?>");
56 if (ptr) ptr+=2; else {
57 rrd_set_error("Dangling XML header");
62 } while ((*buf)!=ptr);
68 if ((buf == NULL) || (*buf == NULL))
73 while((*(ptr+1)) && ((*ptr)==' ' || (*ptr)=='\r' || (*ptr)=='\n' || (*ptr)=='\t')) ptr++;
74 if (strncmp(ptr,"<!--",4) == 0) {
75 ptr= strstr(ptr,"-->");
76 if (ptr) ptr+=3; else {
77 rrd_set_error("Dangling Comment");
82 } while ((*buf)!=ptr);
86 int eat_tag(char **buf, char *tag){
87 if ((*buf)==NULL) return -1; /* fall though clause */
92 && strncmp((*buf)+1,tag,strlen(tag)) == 0
93 && *((*buf)+strlen(tag)+1)=='>') {
94 (*buf) += strlen(tag)+2;
97 rrd_set_error("No <%s> tag found",tag);
105 int read_tag(char **buf, char *tag, char *format, void *value){
108 if ((*buf)==NULL) return -1; /* fall though clause */
110 if (eat_tag(buf,tag)==1){
113 while(*((*buf)+1) && (*(*buf) != '<')) (*buf)++; /*find start of endtag*/
115 matches =sscanf(temp,format,value);
117 end_tag = malloc((strlen(tag)+2)*sizeof(char));
118 sprintf(end_tag,"/%s",tag);
119 eat_tag(buf,end_tag);
121 if (matches == 0 && strcmp(format,"%lf") == 0)
122 (*((double* )(value))) = DNAN;
123 if (matches != 1) return 0;
130 /* parse the data stored in buf and return a filled rrd structure */
131 int xml2rrd(char* buf, rrd_t* rrd, char rc){
132 /* pass 1 identify number of RRAs */
133 char *ptr,*ptr2,*ptr3; /* walks thought the buffer */
134 long rows=0,mempool=0,i=0;
137 xml_lc(buf); /* lets lowercase all active parts of the xml */
141 /* start with an RRD tag */
146 /* allocate static header */
147 if((rrd->stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
148 rrd_set_error("allocating rrd.stat_head");
152 strcpy(rrd->stat_head->cookie,RRD_COOKIE);
153 read_tag(&ptr,"version","%4[0-9]",rrd->stat_head->version);
154 input_version = atoi(rrd->stat_head->version);
155 /* added primitive version checking */
156 if (input_version > atoi(RRD_VERSION) || input_version < 1)
158 rrd_set_error("Incompatible file version, detected version %s. This is not supported by the version %s restore tool.\n",
159 rrd -> stat_head -> version, RRD_VERSION );
160 free(rrd -> stat_head);
161 rrd->stat_head = NULL;
164 /* make sure we output the right version */
165 strcpy(rrd->stat_head->version,RRD_VERSION);
167 /* if (atoi(rrd -> stat_head -> version) < 2)
169 rrd_set_error("Can only restore version >= 2 (Not %s). Dump your old rrd using a current rrdtool dump.", rrd -> stat_head -> version );
173 rrd->stat_head->float_cookie = FLOAT_COOKIE;
174 rrd->stat_head->ds_cnt = 0;
175 rrd->stat_head->rra_cnt = 0;
176 read_tag(&ptr,"step","%lu",&(rrd->stat_head->pdp_step));
178 /* allocate live head */
179 if((rrd->live_head = calloc(1,sizeof(live_head_t)))==NULL){
180 rrd_set_error("allocating rrd.live_head");
183 read_tag(&ptr,"lastupdate","%lu",&(rrd->live_head->last_up));
185 /* Data Source Definition Part */
187 while (eat_tag(&ptr2,"ds") == 1){
188 rrd->stat_head->ds_cnt++;
189 if((rrd->ds_def = rrd_realloc(rrd->ds_def,rrd->stat_head->ds_cnt*sizeof(ds_def_t)))==NULL){
190 rrd_set_error("allocating rrd.ds_def");
193 /* clean out memory to make sure no data gets stored from previous tasks */
194 memset(&(rrd->ds_def[rrd->stat_head->ds_cnt-1]), 0, sizeof(ds_def_t));
195 if((rrd->pdp_prep = rrd_realloc(rrd->pdp_prep,rrd->stat_head->ds_cnt
196 *sizeof(pdp_prep_t)))==NULL){
197 rrd_set_error("allocating pdp_prep");
200 /* clean out memory to make sure no data gets stored from previous tasks */
201 memset(&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1]), 0, sizeof(pdp_prep_t));
203 read_tag(&ptr2,"name",DS_NAM_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].ds_nam);
205 read_tag(&ptr2,"type",DST_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].dst);
206 /* test for valid type */
207 if( (int)dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt-1].dst) == -1) return -1;
209 if (dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt-1].dst) != DST_CDEF)
211 read_tag(&ptr2,"minimal_heartbeat","%lu",
212 &(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_mrhb_cnt].u_cnt));
213 read_tag(&ptr2,"min","%lf",&(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_min_val].u_val));
214 read_tag(&ptr2,"max","%lf",&(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_max_val].u_val));
215 } else { /* DST_CDEF */
217 read_tag(&ptr2,"cdef","%1000s",buffer);
218 parseCDEF_DS(buffer,rrd,rrd -> stat_head -> ds_cnt - 1);
219 if (rrd_test_error()) return -1;
222 read_tag(&ptr2,"last_ds","%30s",rrd->pdp_prep[rrd->stat_head->ds_cnt-1].last_ds);
223 read_tag(&ptr2,"value","%lf",&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1].scratch[PDP_val].u_val));
224 read_tag(&ptr2,"unknown_sec","%lu",&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1].scratch[PDP_unkn_sec_cnt].u_cnt));
225 eat_tag(&ptr2,"/ds");
230 while (eat_tag(&ptr2,"rra") == 1){
231 rrd->stat_head->rra_cnt++;
233 /* allocate and reset rra definition areas */
234 if((rrd->rra_def = rrd_realloc(rrd->rra_def,rrd->stat_head->rra_cnt*sizeof(rra_def_t)))==NULL){
235 rrd_set_error("allocating rra_def"); return -1; }
236 memset(&(rrd->rra_def[rrd->stat_head->rra_cnt-1]), 0, sizeof(rra_def_t));
238 /* allocate and reset consolidation point areas */
239 if((rrd->cdp_prep = rrd_realloc(rrd->cdp_prep,
240 rrd->stat_head->rra_cnt
241 *rrd->stat_head->ds_cnt*sizeof(cdp_prep_t)))==NULL){
242 rrd_set_error("allocating cdp_prep"); return -1; }
244 memset(&(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rrd->stat_head->rra_cnt-1)]),
245 0, rrd->stat_head->ds_cnt*sizeof(cdp_prep_t));
248 read_tag(&ptr2,"cf",CF_NAM_FMT,rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam);
249 /* test for valid type */
250 if( (int)cf_conv(rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam) == -1) return -1;
252 read_tag(&ptr2,"pdp_per_row","%lu",&(rrd->rra_def[rrd->stat_head->rra_cnt-1].pdp_cnt));
253 /* support to read RRA parameters */
254 rra_index = rrd->stat_head->rra_cnt - 1;
255 if ( input_version < 2 ){
256 read_tag(&ptr2, "xff","%lf",
257 &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].u_val));
259 if (eat_tag(&ptr2, "params") != 1) {
260 rrd_set_error("could not find params tag to eat and skip");
264 /* backwards compatibility w/ old patch */
265 if (strncmp(ptr2, "<value>",7) == 0) {
266 parse_patch1028_RRA_params(&ptr2,rrd,rra_index);
268 switch(cf_conv(rrd -> rra_def[rra_index].cf_nam)) {
270 read_tag(&ptr2, "hw_alpha", "%lf",
271 &(rrd->rra_def[rra_index].par[RRA_hw_alpha].u_val));
272 read_tag(&ptr2, "hw_beta", "%lf",
273 &(rrd->rra_def[rra_index].par[RRA_hw_beta].u_val));
274 read_tag(&ptr2, "dependent_rra_idx", "%lu",
275 &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
279 read_tag(&ptr2, "seasonal_gamma", "%lf",
280 &(rrd->rra_def[rra_index].par[RRA_seasonal_gamma].u_val));
281 read_tag(&ptr2, "seasonal_smooth_idx", "%lu",
282 &(rrd->rra_def[rra_index].par[RRA_seasonal_smooth_idx].u_cnt));
283 read_tag(&ptr2, "dependent_rra_idx", "%lu",
284 &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
287 read_tag(&ptr2, "delta_pos", "%lf",
288 &(rrd->rra_def[rra_index].par[RRA_delta_pos].u_val));
289 read_tag(&ptr2, "delta_neg", "%lf",
290 &(rrd->rra_def[rra_index].par[RRA_delta_neg].u_val));
291 read_tag(&ptr2, "window_len", "%lu",
292 &(rrd->rra_def[rra_index].par[RRA_window_len].u_cnt));
293 read_tag(&ptr2, "failure_threshold", "%lu",
294 &(rrd->rra_def[rra_index].par[RRA_failure_threshold].u_cnt));
297 read_tag(&ptr2, "dependent_rra_idx", "%lu",
298 &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
305 read_tag(&ptr2, "xff","%lf",
306 &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].u_val));
309 eat_tag(&ptr2, "/params");
313 eat_tag(&ptr2,"cdp_prep");
314 for(i=0;i< (int)rrd->stat_head->ds_cnt;i++)
317 /* support to read CDP parameters */
318 rra_index = rrd->stat_head->rra_cnt-1;
320 if ( input_version < 2 ){
321 rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)+i].scratch[CDP_primary_val].u_val = 0.0;
322 rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)+i].scratch[CDP_secondary_val].u_val = 0.0;
323 read_tag(&ptr2,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
324 *(rra_index) +i].scratch[CDP_val].u_val));
325 read_tag(&ptr2,"unknown_datapoints","%lu",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
326 *(rra_index) +i].scratch[CDP_unkn_pdp_cnt].u_cnt));
329 if (strncmp(ptr2, "<value>",7) == 0) {
330 parse_patch1028_CDP_params(&ptr2,rrd,rra_index,i);
332 read_tag(&ptr2, "primary_value","%lf",
333 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
334 +i].scratch[CDP_primary_val].u_val));
335 read_tag(&ptr2, "secondary_value","%lf",
336 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
337 +i].scratch[CDP_secondary_val].u_val));
338 switch(cf_conv(rrd->rra_def[rra_index].cf_nam)) {
340 read_tag(&ptr2,"intercept","%lf",
341 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
342 +i].scratch[CDP_hw_intercept].u_val));
343 read_tag(&ptr2,"last_intercept","%lf",
344 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
345 +i].scratch[CDP_hw_last_intercept].u_val));
346 read_tag(&ptr2,"slope","%lf",
347 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
348 +i].scratch[CDP_hw_slope].u_val));
349 read_tag(&ptr2,"last_slope","%lf",
350 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
351 +i].scratch[CDP_hw_last_slope].u_val));
352 read_tag(&ptr2,"nan_count","%lu",
353 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
354 +i].scratch[CDP_null_count].u_cnt));
355 read_tag(&ptr2,"last_nan_count","%lu",
356 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
357 +i].scratch[CDP_last_null_count].u_cnt));
361 read_tag(&ptr2,"seasonal","%lf",
362 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
363 +i].scratch[CDP_hw_seasonal].u_val));
364 read_tag(&ptr2,"last_seasonal","%lf",
365 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
366 +i].scratch[CDP_hw_last_seasonal].u_val));
367 read_tag(&ptr2,"init_flag","%lu",
368 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
369 +i].scratch[CDP_init_seasonal].u_cnt));
374 parse_FAILURES_history(&ptr2,rrd,rra_index,i);
381 read_tag(&ptr2,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
382 *(rra_index) +i].scratch[CDP_val].u_val));
383 read_tag(&ptr2,"unknown_datapoints","%lu",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
384 *(rra_index) +i].scratch[CDP_unkn_pdp_cnt].u_cnt));
389 eat_tag(&ptr2,"/ds");
391 eat_tag(&ptr2,"/cdp_prep");
392 rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt=0;
393 eat_tag(&ptr2,"database");
395 while (eat_tag(&ptr3,"row") == 1){
399 if((rrd->rrd_value = rrd_realloc(rrd->rrd_value,
400 (rows+mempool)*(rrd->stat_head->ds_cnt)
401 *sizeof(rrd_value_t)))==NULL) {
402 rrd_set_error("allocating rrd_values"); return -1; }
406 rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt++;
407 for(i=0;i< (int)rrd->stat_head->ds_cnt;i++){
409 rrd_value_t * value = &(rrd->rrd_value[(rows-1)*rrd->stat_head->ds_cnt+i]);
411 read_tag(&ptr3,"v","%lf", value);
414 (rc == 1) /* do we have to check for the ranges */
416 (!isnan(*value)) /* not a NAN value */
418 (dst_conv(rrd->ds_def[i].dst) != DST_CDEF)
420 ( /* min defined and in the range ? */
421 (!isnan(rrd->ds_def[i].par[DS_min_val].u_val)
422 && (*value < rrd->ds_def[i].par[DS_min_val].u_val))
423 || /* max defined and in the range ? */
424 (!isnan(rrd->ds_def[i].par[DS_max_val].u_val)
425 && (*value > rrd->ds_def[i].par[DS_max_val].u_val))
428 fprintf (stderr, "out of range found [ds: %lu], [value : %0.10e]\n", i, *value);
432 eat_tag(&ptr3,"/row");
435 eat_tag(&ptr2,"/database");
436 eat_tag(&ptr2,"/rra");
439 eat_tag(&ptr,"/rrd");
441 if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t)*rrd->stat_head->rra_cnt)) == NULL) {
442 rrd_set_error("allocating rra_ptr");
446 for(i=0; i < (int)rrd->stat_head->rra_cnt; i++) {
447 /* last row in the xml file is the most recent; as
448 * rrd_update increments the current row pointer, set cur_row
449 * here to the last row. */
450 rrd->rra_ptr[i].cur_row = rrd->rra_def[i].row_cnt-1;
461 /* create and empty rrd file according to the specs given */
464 rrd_write(char *file_name, rrd_t *rrd, char force_overwrite)
466 unsigned long i,ii,val_cnt;
471 if (strcmp("-",file_name)==0){
474 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
475 fdflags = O_RDWR|O_BINARY|O_CREAT;
477 fdflags = O_WRONLY|O_CREAT;
479 if (force_overwrite == 0) {
482 fd = open(file_name,fdflags,0666);
483 if (fd == -1 || (rrd_file = fdopen(fd,"wb")) == NULL) {
484 rrd_set_error("creating '%s': %s",file_name,rrd_strerror(errno));
490 fwrite(rrd->stat_head,
491 sizeof(stat_head_t), 1, rrd_file);
494 sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
497 sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
499 fwrite(rrd->live_head, sizeof(live_head_t),1, rrd_file);
501 fwrite( rrd->pdp_prep, sizeof(pdp_prep_t),rrd->stat_head->ds_cnt,rrd_file);
503 fwrite( rrd->cdp_prep, sizeof(cdp_prep_t),rrd->stat_head->rra_cnt*
504 rrd->stat_head->ds_cnt,rrd_file);
505 fwrite( rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt,rrd_file);
509 /* calculate the number of rrd_values to dump */
511 for(i=0; i < rrd->stat_head->rra_cnt; i++)
512 for(ii=0; ii < rrd->rra_def[i].row_cnt * rrd->stat_head->ds_cnt;ii++)
514 fwrite( rrd->rrd_value, sizeof(rrd_value_t),val_cnt,rrd_file);
516 /* lets see if we had an error */
517 if(ferror(rrd_file)){
518 rrd_set_error("a file error occurred while creating '%s'",file_name);
529 rrd_restore(int argc, char **argv)
534 char force_overwrite = 0;
537 optind = 0; opterr = 0; /* initialize getopt */
539 static struct option long_options[] =
541 {"range-check", no_argument, 0, 'r'},
542 {"force-overwrite", no_argument, 0, 'f'},
545 int option_index = 0;
549 opt = getopt_long(argc, argv, "rf", long_options, &option_index);
562 rrd_set_error("usage rrdtool %s [--range-check|-r] [--force-overwrite/-f] file.xml file.rrd",argv[0]);
568 if (argc-optind != 2) {
569 rrd_set_error("usage rrdtool %s [--range-check/-r] [--force-overwrite/-f] file.xml file.rrd",argv[0]);
573 if (readfile(argv[optind],&buf,0)==-1){
579 if (xml2rrd(buf,&rrd,rc)==-1) {
587 if(rrd_write(argv[optind+1],&rrd,force_overwrite)==-1){
595 /* a backwards compatibility routine that will parse the RRA params section
596 * generated by the aberrant patch to 1.0.28. */
599 parse_patch1028_RRA_params(char **buf, rrd_t *rrd, int rra_index)
602 for (i = 0; i < MAX_RRA_PAR_EN; i++)
604 if (i == RRA_dependent_rra_idx ||
605 i == RRA_seasonal_smooth_idx ||
606 i == RRA_failure_threshold)
607 read_tag(buf, "value","%lu",
608 &(rrd->rra_def[rra_index].par[i].u_cnt));
610 read_tag(buf, "value","%lf",
611 &(rrd->rra_def[rra_index].par[i].u_val));
615 /* a backwards compatibility routine that will parse the CDP params section
616 * generated by the aberrant patch to 1.0.28. */
618 parse_patch1028_CDP_params(char **buf, rrd_t *rrd, int rra_index, int ds_index)
621 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
623 if (cf_conv(rrd->rra_def[rra_index].cf_nam) == CF_FAILURES ||
624 ii == CDP_unkn_pdp_cnt ||
625 ii == CDP_null_count ||
626 ii == CDP_last_null_count)
628 read_tag(buf,"value","%lu",
629 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index) + ds_index].scratch[ii].u_cnt));
631 read_tag(buf,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt*
632 (rra_index) + ds_index].scratch[ii].u_val));
638 parse_FAILURES_history(char **buf, rrd_t *rrd, int rra_index, int ds_index)
640 char history[MAX_FAILURES_WINDOW_LEN + 1];
641 char *violations_array;
644 /* 28 = MAX_FAILURES_WINDOW_LEN */
645 read_tag(buf, "history", "%28[0-1]", history);
646 violations_array = (char*) rrd -> cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
649 for (i = 0; i < rrd -> rra_def[rra_index].par[RRA_window_len].u_cnt; ++i)
650 violations_array[i] = (history[i] == '1') ? 1 : 0;