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 *****************************************************************************/
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;
115 xml_lc(buf); /* lets lowercase all active parts of the xml */
119 /* start with an RRD tag */
122 /* allocate static header */
123 if((rrd->stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
124 rrd_set_error("allocating rrd.stat_head");
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) )
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);
138 if (atoi(rrd -> stat_head -> version) < 2)
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);
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));
149 /* allocate live head */
150 if((rrd->live_head = calloc(1,sizeof(live_head_t)))==NULL){
151 rrd_set_error("allocating rrd.live_head");
154 read_tag(&ptr,"lastupdate","%lu",&(rrd->live_head->last_up));
156 /* Data Source Definition Part */
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");
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");
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));
174 read_tag(&ptr2,"name",DS_NAM_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].ds_nam);
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;
180 if (dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt-1].dst) != DST_CDEF)
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 */
188 read_tag(&ptr2,"cdef","%s",buffer);
189 parseCDEF_DS(buffer,rrd,rrd -> stat_head -> ds_cnt - 1);
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");
200 while (eat_tag(&ptr2,"rra") == 1){
201 rrd->stat_head->rra_cnt++;
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));
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; }
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));
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;
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");
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);
231 switch(cf_conv(rrd -> rra_def[rra_index].cf_nam)) {
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));
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));
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));
260 read_tag(&ptr2, "dependent_rra_idx", "%lu",
261 &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
268 read_tag(&ptr2, "xff","%lf",
269 &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].u_val));
272 eat_tag(&ptr2, "/params");
273 eat_tag(&ptr2,"cdp_prep");
274 for(i=0;i< (int)rrd->stat_head->ds_cnt;i++)
277 /* support to read CDP parameters */
278 rra_index = rrd->stat_head->rra_cnt-1;
280 if (strncmp(ptr2, "<value>",7) == 0) {
281 parse_patch1028_CDP_params(&ptr2,rrd,rra_index,i);
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)) {
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));
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));
325 parse_FAILURES_history(&ptr2,rrd,rra_index,i);
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));
339 eat_tag(&ptr2,"/ds");
341 eat_tag(&ptr2,"/cdp_prep");
342 rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt=0;
343 eat_tag(&ptr2,"database");
345 while (eat_tag(&ptr3,"row") == 1){
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; }
356 rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt++;
357 for(i=0;i< (int)rrd->stat_head->ds_cnt;i++){
359 rrd_value_t * value = &(rrd->rrd_value[(rows-1)*rrd->stat_head->ds_cnt+i]);
361 read_tag(&ptr3,"v","%lf", value);
364 (rc == 1) /* do we have to check for the ranges */
366 (!isnan(*value)) /* not a NAN value */
368 (dst_conv(rrd->ds_def[i].dst) != DST_CDEF)
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))
378 fprintf (stderr, "out of range found [ds: %lu], [value : %0.10e]\n", i, *value);
382 eat_tag(&ptr3,"/row");
385 eat_tag(&ptr2,"/database");
386 eat_tag(&ptr2,"/rra");
389 eat_tag(&ptr,"/rrd");
391 if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t)*rrd->stat_head->rra_cnt)) == NULL) {
392 rrd_set_error("allocating rra_ptr");
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;
411 /* create and empty rrd file according to the specs given */
414 rrd_write(char *file_name, rrd_t *rrd, char force_overwrite)
416 unsigned long i,ii,val_cnt;
421 if (strcmp("-",file_name)==0){
424 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
425 fdflags = O_RDWR|O_BINARY|O_CREAT;
427 fdflags = O_WRONLY|O_CREAT;
429 if (force_overwrite == 0) {
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));
440 fwrite(rrd->stat_head,
441 sizeof(stat_head_t), 1, rrd_file);
444 sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
447 sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
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);
453 fwrite(rrd->live_head, sizeof(live_head_t),1, rrd_file);
455 fwrite( rrd->pdp_prep, sizeof(pdp_prep_t),rrd->stat_head->ds_cnt,rrd_file);
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);
463 /* calculate the number of rrd_values to dump */
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++)
468 fwrite( rrd->rrd_value, sizeof(rrd_value_t),val_cnt,rrd_file);
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);
483 rrd_restore(int argc, char **argv)
488 char force_overwrite = 0;
491 optind = 0; opterr = 0; /* initialize getopt */
494 rrd_set_error("usage rrdtool %s [--range-check/-r] [--force-overwrite/-f] file.xml file.rrd",argv[0]);
499 static struct option long_options[] =
501 {"range-check", required_argument, 0, 'r'},
502 {"force-overwrite", required_argument, 0, 'f'},
505 int option_index = 0;
509 opt = getopt_long(argc, argv, "r:f", long_options, &option_index);
522 rrd_set_error("usage rrdtool %s [--range-check|-r] [--force-overwrite/-f] file.xml file.rrd",argv[0]);
528 if (readfile(argv[optind],&buf,0)==-1){
531 if (xml2rrd(buf,&rrd,rc)==-1) {
537 if(rrd_write(argv[optind+1],&rrd,force_overwrite)==-1){
545 /* a backwards compatibility routine that will parse the RRA params section
546 * generated by the aberrant patch to 1.0.28. */
549 parse_patch1028_RRA_params(char **buf, rrd_t *rrd, int rra_index)
552 for (i = 0; i < MAX_RRA_PAR_EN; i++)
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));
560 read_tag(buf, "value","%lf",
561 &(rrd->rra_def[rra_index].par[i].u_val));
565 /* a backwards compatibility routine that will parse the CDP params section
566 * generated by the aberrant patch to 1.0.28. */
568 parse_patch1028_CDP_params(char **buf, rrd_t *rrd, int rra_index, int ds_index)
571 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
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)
578 read_tag(buf,"value","%lu",
579 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index) + ds_index].scratch[ii].u_cnt));
581 read_tag(buf,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt*
582 (rra_index) + ds_index].scratch[ii].u_val));
588 parse_FAILURES_history(char **buf, rrd_t *rrd, int rra_index, int ds_index)
590 char history[MAX_FAILURES_WINDOW_LEN + 1];
591 char *violations_array;
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)
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;