1 /*****************************************************************************
2 * RRDtool 1.2.99907080300 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__)
41 void parse_patch1028_RRA_params(
45 void parse_patch1028_CDP_params(
50 void parse_FAILURES_history(
55 long int rra_random_row(
58 /* convert all occurrences of <BlaBlaBla> to <blablabla> */
66 if (intag == 0 && (*buf) == '<') {
68 } else if (intag == 1 && (*buf) == '>') {
71 } else if (intag == 1) {
87 && ((*ptr) == ' ' || (*ptr) == '\r' || (*ptr) == '\n'
90 if (strncmp(ptr, "<?xml", 4) == 0) {
91 ptr = strstr(ptr, "?>");
95 rrd_set_error("Dangling XML header");
100 } while ((*buf) != ptr);
109 if ((buf == NULL) || (*buf == NULL))
115 && ((*ptr) == ' ' || (*ptr) == '\r' || (*ptr) == '\n'
118 if (strncmp(ptr, "<!--", 4) == 0) {
119 ptr = strstr(ptr, "-->");
123 rrd_set_error("Dangling Comment");
128 } while ((*buf) != ptr);
137 return -1; /* fall though clause */
142 && strncmp((*buf) + 1, tag, strlen(tag)) == 0
143 && *((*buf) + strlen(tag) + 1) == '>') {
144 (*buf) += strlen(tag) + 2;
146 rrd_set_error("No <%s> tag found", tag);
164 return -1; /* fall though clause */
166 if (eat_tag(buf, tag) == 1) {
170 while (*((*buf) + 1) && (*(*buf) != '<'))
171 (*buf)++; /*find start of endtag */
173 matches = sscanf(temp, format, value);
175 end_tag = malloc((strlen(tag) + 2) * sizeof(char));
176 sprintf(end_tag, "/%s", tag);
177 eat_tag(buf, end_tag);
179 if (matches == 0 && strcmp(format, "%lf") == 0)
180 (*((double *) (value))) = DNAN;
189 /* parse the data stored in buf and return a filled rrd structure */
195 /* pass 1 identify number of RRAs */
196 char *ptr, *ptr2, *ptr3; /* walks thought the buffer */
197 long rows = 0, mempool = 0, i = 0;
201 xml_lc(buf); /* lets lowercase all active parts of the xml */
205 /* start with an RRD tag */
209 eat_tag(&ptr, "rrd");
210 /* allocate static header */
211 if ((rrd->stat_head = calloc(1, sizeof(stat_head_t))) == NULL) {
212 rrd_set_error("allocating rrd.stat_head");
216 strcpy(rrd->stat_head->cookie, RRD_COOKIE);
217 read_tag(&ptr, "version", "%4[0-9]", rrd->stat_head->version);
218 input_version = atoi(rrd->stat_head->version);
219 /* added primitive version checking */
220 if (input_version > atoi(RRD_VERSION) || input_version < 1) {
222 ("Incompatible file version, detected version %s. This is not supported by the version %s restore tool.\n",
223 rrd->stat_head->version, RRD_VERSION);
224 free(rrd->stat_head);
225 rrd->stat_head = NULL;
228 /* make sure we output the right version only go over 3 if input is over 3 too */
229 if (input_version > 3) {
230 strcpy(rrd->stat_head->version, RRD_VERSION);
232 strcpy(rrd->stat_head->version, RRD_VERSION3);
235 rrd->stat_head->float_cookie = FLOAT_COOKIE;
236 rrd->stat_head->ds_cnt = 0;
237 rrd->stat_head->rra_cnt = 0;
238 read_tag(&ptr, "step", "%lu", &(rrd->stat_head->pdp_step));
240 /* allocate live head */
241 if ((rrd->live_head = calloc(1, sizeof(live_head_t))) == NULL) {
242 rrd_set_error("allocating rrd.live_head");
245 read_tag(&ptr, "lastupdate", "%lu", &(rrd->live_head->last_up));
247 /* Data Source Definition Part */
249 while (eat_tag(&ptr2, "ds") == 1) {
250 rrd->stat_head->ds_cnt++;
252 rrd_realloc(rrd->ds_def,
253 rrd->stat_head->ds_cnt * sizeof(ds_def_t))) ==
255 rrd_set_error("allocating rrd.ds_def");
258 /* clean out memory to make sure no data gets stored from previous tasks */
259 memset(&(rrd->ds_def[rrd->stat_head->ds_cnt - 1]), 0,
262 rrd_realloc(rrd->pdp_prep,
263 rrd->stat_head->ds_cnt * sizeof(pdp_prep_t))) ==
265 rrd_set_error("allocating pdp_prep");
268 /* clean out memory to make sure no data gets stored from previous tasks */
269 memset(&(rrd->pdp_prep[rrd->stat_head->ds_cnt - 1]), 0,
272 read_tag(&ptr2, "name", DS_NAM_FMT,
273 rrd->ds_def[rrd->stat_head->ds_cnt - 1].ds_nam);
275 read_tag(&ptr2, "type", DST_FMT,
276 rrd->ds_def[rrd->stat_head->ds_cnt - 1].dst);
277 /* test for valid type */
278 if ((int) dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt - 1].dst) == -1)
281 if (dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt - 1].dst) != DST_CDEF) {
282 read_tag(&ptr2, "minimal_heartbeat", "%lu",
283 &(rrd->ds_def[rrd->stat_head->ds_cnt - 1].
284 par[DS_mrhb_cnt].u_cnt));
285 read_tag(&ptr2, "min", "%lf",
286 &(rrd->ds_def[rrd->stat_head->ds_cnt - 1].
287 par[DS_min_val].u_val));
288 read_tag(&ptr2, "max", "%lf",
289 &(rrd->ds_def[rrd->stat_head->ds_cnt - 1].
290 par[DS_max_val].u_val));
291 } else { /* DST_CDEF */
294 read_tag(&ptr2, "cdef", "%1000s", buffer);
295 parseCDEF_DS(buffer, rrd, rrd->stat_head->ds_cnt - 1);
296 if (rrd_test_error())
300 read_tag(&ptr2, "last_ds", "%30s",
301 rrd->pdp_prep[rrd->stat_head->ds_cnt - 1].last_ds);
302 read_tag(&ptr2, "value", "%lf",
303 &(rrd->pdp_prep[rrd->stat_head->ds_cnt - 1].scratch[PDP_val].
305 read_tag(&ptr2, "unknown_sec", "%lu",
306 &(rrd->pdp_prep[rrd->stat_head->ds_cnt - 1].
307 scratch[PDP_unkn_sec_cnt].u_cnt));
308 eat_tag(&ptr2, "/ds");
313 while (eat_tag(&ptr2, "rra") == 1) {
314 rrd->stat_head->rra_cnt++;
316 /* allocate and reset rra definition areas */
318 rrd_realloc(rrd->rra_def,
319 rrd->stat_head->rra_cnt * sizeof(rra_def_t))) ==
321 rrd_set_error("allocating rra_def");
324 memset(&(rrd->rra_def[rrd->stat_head->rra_cnt - 1]), 0,
327 /* allocate and reset consolidation point areas */
328 if ((rrd->cdp_prep = rrd_realloc(rrd->cdp_prep,
329 rrd->stat_head->rra_cnt
330 * rrd->stat_head->ds_cnt *
331 sizeof(cdp_prep_t))) == NULL) {
332 rrd_set_error("allocating cdp_prep");
338 cdp_prep[rrd->stat_head->ds_cnt *
339 (rrd->stat_head->rra_cnt - 1)]), 0,
340 rrd->stat_head->ds_cnt * sizeof(cdp_prep_t));
343 read_tag(&ptr2, "cf", CF_NAM_FMT,
344 rrd->rra_def[rrd->stat_head->rra_cnt - 1].cf_nam);
345 /* test for valid type */
346 if ((int) cf_conv(rrd->rra_def[rrd->stat_head->rra_cnt - 1].cf_nam) ==
350 read_tag(&ptr2, "pdp_per_row", "%lu",
351 &(rrd->rra_def[rrd->stat_head->rra_cnt - 1].pdp_cnt));
352 /* support to read RRA parameters */
353 rra_index = rrd->stat_head->rra_cnt - 1;
354 if (input_version < 2) {
355 read_tag(&ptr2, "xff", "%lf",
356 &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].u_val));
358 if (eat_tag(&ptr2, "params") != 1) {
359 rrd_set_error("could not find params tag to eat and skip");
363 /* backwards compatibility w/ old patch */
364 if (strncmp(ptr2, "<value>", 7) == 0) {
365 parse_patch1028_RRA_params(&ptr2, rrd, rra_index);
367 switch (cf_conv(rrd->rra_def[rra_index].cf_nam)) {
370 read_tag(&ptr2, "hw_alpha", "%lf",
371 &(rrd->rra_def[rra_index].par[RRA_hw_alpha].
373 read_tag(&ptr2, "hw_beta", "%lf",
374 &(rrd->rra_def[rra_index].par[RRA_hw_beta].
376 read_tag(&ptr2, "dependent_rra_idx", "%lu",
377 &(rrd->rra_def[rra_index].
378 par[RRA_dependent_rra_idx].u_cnt));
382 read_tag(&ptr2, "seasonal_gamma", "%lf",
383 &(rrd->rra_def[rra_index].
384 par[RRA_seasonal_gamma].u_val));
385 read_tag(&ptr2, "seasonal_smooth_idx", "%lu",
386 &(rrd->rra_def[rra_index].
387 par[RRA_seasonal_smooth_idx].u_cnt));
388 if (atoi(rrd->stat_head->version) >= 4) {
389 read_tag(&ptr2, "smoothing_window", "%lf",
390 &(rrd->rra_def[rra_index].
391 par[RRA_seasonal_smoothing_window].u_val));
393 read_tag(&ptr2, "dependent_rra_idx", "%lu",
394 &(rrd->rra_def[rra_index].
395 par[RRA_dependent_rra_idx].u_cnt));
398 read_tag(&ptr2, "delta_pos", "%lf",
399 &(rrd->rra_def[rra_index].par[RRA_delta_pos].
401 read_tag(&ptr2, "delta_neg", "%lf",
402 &(rrd->rra_def[rra_index].par[RRA_delta_neg].
404 read_tag(&ptr2, "window_len", "%lu",
405 &(rrd->rra_def[rra_index].par[RRA_window_len].
407 read_tag(&ptr2, "failure_threshold", "%lu",
408 &(rrd->rra_def[rra_index].
409 par[RRA_failure_threshold].u_cnt));
412 read_tag(&ptr2, "dependent_rra_idx", "%lu",
413 &(rrd->rra_def[rra_index].
414 par[RRA_dependent_rra_idx].u_cnt));
421 read_tag(&ptr2, "xff", "%lf",
422 &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].
426 eat_tag(&ptr2, "/params");
430 eat_tag(&ptr2, "cdp_prep");
431 for (i = 0; i < (int) rrd->stat_head->ds_cnt; i++) {
432 if (eat_tag(&ptr2, "ds") != 1){
433 rrd_set_error("expected to find %lu <ds> entries in <cdp_prep>",rrd->stat_head->ds_cnt);
436 /* support to read CDP parameters */
437 rra_index = rrd->stat_head->rra_cnt - 1;
439 if (input_version < 2) {
440 rrd->cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
441 i].scratch[CDP_primary_val].u_val = 0.0;
442 rrd->cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
443 i].scratch[CDP_secondary_val].u_val = 0.0;
444 read_tag(&ptr2, "value", "%lf",
446 cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
447 i].scratch[CDP_val].u_val));
448 read_tag(&ptr2, "unknown_datapoints", "%lu",
450 cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
451 i].scratch[CDP_unkn_pdp_cnt].u_cnt));
454 if (strncmp(ptr2, "<value>", 7) == 0) {
455 parse_patch1028_CDP_params(&ptr2, rrd, rra_index, i);
457 read_tag(&ptr2, "primary_value", "%lf",
459 cdp_prep[rrd->stat_head->ds_cnt * (rra_index)
460 + i].scratch[CDP_primary_val].u_val));
461 read_tag(&ptr2, "secondary_value", "%lf",
463 cdp_prep[rrd->stat_head->ds_cnt * (rra_index)
465 i].scratch[CDP_secondary_val].u_val));
466 switch (cf_conv(rrd->rra_def[rra_index].cf_nam)) {
469 read_tag(&ptr2, "intercept", "%lf",
471 cdp_prep[rrd->stat_head->ds_cnt *
474 i].scratch[CDP_hw_intercept].
476 read_tag(&ptr2, "last_intercept", "%lf",
478 cdp_prep[rrd->stat_head->ds_cnt *
481 i].scratch[CDP_hw_last_intercept].
483 read_tag(&ptr2, "slope", "%lf",
485 cdp_prep[rrd->stat_head->ds_cnt *
488 i].scratch[CDP_hw_slope].u_val));
489 read_tag(&ptr2, "last_slope", "%lf",
491 cdp_prep[rrd->stat_head->ds_cnt *
494 i].scratch[CDP_hw_last_slope].
496 read_tag(&ptr2, "nan_count", "%lu",
498 cdp_prep[rrd->stat_head->ds_cnt *
501 i].scratch[CDP_null_count].
503 read_tag(&ptr2, "last_nan_count", "%lu",
505 cdp_prep[rrd->stat_head->ds_cnt *
508 i].scratch[CDP_last_null_count].
513 read_tag(&ptr2, "seasonal", "%lf",
515 cdp_prep[rrd->stat_head->ds_cnt *
518 i].scratch[CDP_hw_seasonal].
520 read_tag(&ptr2, "last_seasonal", "%lf",
522 cdp_prep[rrd->stat_head->ds_cnt *
525 i].scratch[CDP_hw_last_seasonal].
527 read_tag(&ptr2, "init_flag", "%lu",
529 cdp_prep[rrd->stat_head->ds_cnt *
532 i].scratch[CDP_init_seasonal].
538 parse_FAILURES_history(&ptr2, rrd, rra_index, i);
545 read_tag(&ptr2, "value", "%lf",
547 cdp_prep[rrd->stat_head->ds_cnt *
549 i].scratch[CDP_val].u_val));
550 read_tag(&ptr2, "unknown_datapoints", "%lu",
552 cdp_prep[rrd->stat_head->ds_cnt *
554 i].scratch[CDP_unkn_pdp_cnt].
560 eat_tag(&ptr2, "/ds");
562 eat_tag(&ptr2, "/cdp_prep");
563 rrd->rra_def[rrd->stat_head->rra_cnt - 1].row_cnt = 0;
564 eat_tag(&ptr2, "database");
566 while (eat_tag(&ptr3, "row") == 1) {
570 if ((rrd->rrd_value = rrd_realloc(rrd->rrd_value,
573 (rrd->stat_head->ds_cnt)
574 * sizeof(rrd_value_t))) ==
576 rrd_set_error("allocating rrd_values");
582 rrd->rra_def[rrd->stat_head->rra_cnt - 1].row_cnt++;
583 for (i = 0; i < (int) rrd->stat_head->ds_cnt; i++) {
587 rrd_value[(rows - 1) * rrd->stat_head->ds_cnt + i]);
589 read_tag(&ptr3, "v", "%lf", value);
591 if ((rc == 1) /* do we have to check for the ranges */
592 &&(!isnan(*value)) /* not a NAN value */
593 &&(dst_conv(rrd->ds_def[i].dst) != DST_CDEF)
594 && ( /* min defined and in the range ? */
595 (!isnan(rrd->ds_def[i].par[DS_min_val].u_val)
597 rrd->ds_def[i].par[DS_min_val].u_val))
598 || /* max defined and in the range ? */
599 (!isnan(rrd->ds_def[i].par[DS_max_val].u_val)
601 rrd->ds_def[i].par[DS_max_val].u_val))
605 "out of range found [ds: %lu], [value : %0.10e]\n",
610 eat_tag(&ptr3, "/row");
613 eat_tag(&ptr2, "/database");
614 eat_tag(&ptr2, "/rra");
617 eat_tag(&ptr, "/rrd");
620 calloc(1, sizeof(rra_ptr_t) * rrd->stat_head->rra_cnt)) == NULL) {
621 rrd_set_error("allocating rra_ptr");
634 /* create and empty rrd file according to the specs given */
639 char force_overwrite)
641 unsigned long i, ii, rra_offset;
642 FILE *rrd_file = NULL;
646 if (strcmp("-", file_name) == 0) {
649 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
650 fdflags = O_RDWR | O_BINARY | O_CREAT;
652 fdflags = O_WRONLY | O_CREAT;
654 if (force_overwrite == 0) {
657 fd = open(file_name, fdflags, 0666);
658 if (fd == -1 || (rrd_file = fdopen(fd, "wb")) == NULL) {
659 rrd_set_error("creating '%s': %s", file_name,
660 rrd_strerror(errno));
666 fwrite(rrd->stat_head, sizeof(stat_head_t), 1, rrd_file);
668 fwrite(rrd->ds_def, sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
671 sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
673 fwrite(rrd->live_head, sizeof(live_head_t), 1, rrd_file);
675 fwrite(rrd->pdp_prep, sizeof(pdp_prep_t), rrd->stat_head->ds_cnt,
678 fwrite(rrd->cdp_prep, sizeof(cdp_prep_t), rrd->stat_head->rra_cnt *
679 rrd->stat_head->ds_cnt, rrd_file);
681 for(i=0; i < rrd->stat_head->rra_cnt; i++)
682 rrd->rra_ptr[i].cur_row = rra_random_row(&rrd->rra_def[i]);
684 fwrite(rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt,
688 /* Dump RRD values */
690 for(i=0; i < rrd->stat_head->rra_cnt; i++)
692 unsigned long num_rows = rrd->rra_def[i].row_cnt;
693 unsigned long cur_row = rrd->rra_ptr[i].cur_row;
694 unsigned long ds_cnt = rrd->stat_head->ds_cnt;
696 fwrite(rrd->rrd_value + (rra_offset + num_rows-1 - cur_row) * ds_cnt,
697 sizeof(rrd_value_t), (cur_row+1)*ds_cnt, rrd_file);
699 fwrite(rrd->rrd_value + rra_offset * ds_cnt,
700 sizeof(rrd_value_t), (num_rows-1 - cur_row)*ds_cnt, rrd_file);
702 rra_offset += num_rows;
705 /* lets see if we had an error */
706 if (ferror(rrd_file)) {
707 rrd_set_error("a file error occurred while creating '%s'", file_name);
724 char force_overwrite = 0;
725 struct option long_options[] = {
726 {"range-check", no_argument, 0, 'r'},
727 {"force-overwrite", no_argument, 0, 'f'},
733 opterr = 0; /* initialize getopt */
735 int option_index = 0;
738 opt = getopt_long(argc, argv, "rf", long_options, &option_index);
752 ("usage rrdtool %s [--range-check|-r] [--force-overwrite/-f] file.xml file.rrd",
759 if (argc - optind != 2) {
761 ("usage rrdtool %s [--range-check/-r] [--force-overwrite/-f] file.xml file.rrd",
766 if (readfile(argv[optind], &buf, 0) == -1) {
772 if (xml2rrd(buf, &rrd, rc) == -1) {
780 if (rrd_creat(argv[optind + 1], &rrd, force_overwrite) == -1) {
788 /* a backwards compatibility routine that will parse the RRA params section
789 * generated by the aberrant patch to 1.0.28. */
791 void parse_patch1028_RRA_params(
798 for (i = 0; i < MAX_RRA_PAR_EN; i++) {
799 if (i == RRA_dependent_rra_idx ||
800 i == RRA_seasonal_smooth_idx || i == RRA_failure_threshold)
801 read_tag(buf, "value", "%lu",
802 &(rrd->rra_def[rra_index].par[i].u_cnt));
804 read_tag(buf, "value", "%lf",
805 &(rrd->rra_def[rra_index].par[i].u_val));
809 /* a backwards compatibility routine that will parse the CDP params section
810 * generated by the aberrant patch to 1.0.28. */
811 void parse_patch1028_CDP_params(
819 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++) {
820 if (cf_conv(rrd->rra_def[rra_index].cf_nam) == CF_FAILURES ||
821 ii == CDP_unkn_pdp_cnt ||
822 ii == CDP_null_count || ii == CDP_last_null_count) {
823 read_tag(buf, "value", "%lu",
825 cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
826 ds_index].scratch[ii].u_cnt));
828 read_tag(buf, "value", "%lf",
830 cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
831 ds_index].scratch[ii].u_val));
836 void parse_FAILURES_history(
842 char history[MAX_FAILURES_WINDOW_LEN + 1];
843 char *violations_array;
846 /* 28 = MAX_FAILURES_WINDOW_LEN */
847 read_tag(buf, "history", "%28[0-1]", history);
849 (char *) rrd->cdp_prep[rrd->stat_head->ds_cnt * (rra_index)
852 for (i = 0; i < rrd->rra_def[rra_index].par[RRA_window_len].u_cnt; ++i)
853 violations_array[i] = (history[i] == '1') ? 1 : 0;