1 /*****************************************************************************
2 * RRDtool 1.2.23 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(
56 /* convert all occurrences of <BlaBlaBla> to <blablabla> */
64 if (intag == 0 && (*buf) == '<') {
66 } else if (intag == 1 && (*buf) == '>') {
69 } else if (intag == 1) {
85 && ((*ptr) == ' ' || (*ptr) == '\r' || (*ptr) == '\n'
88 if (strncmp(ptr, "<?xml", 4) == 0) {
89 ptr = strstr(ptr, "?>");
93 rrd_set_error("Dangling XML header");
98 } while ((*buf) != ptr);
107 if ((buf == NULL) || (*buf == NULL))
113 && ((*ptr) == ' ' || (*ptr) == '\r' || (*ptr) == '\n'
116 if (strncmp(ptr, "<!--", 4) == 0) {
117 ptr = strstr(ptr, "-->");
121 rrd_set_error("Dangling Comment");
126 } while ((*buf) != ptr);
135 return -1; /* fall though clause */
140 && strncmp((*buf) + 1, tag, strlen(tag)) == 0
141 && *((*buf) + strlen(tag) + 1) == '>') {
142 (*buf) += strlen(tag) + 2;
144 rrd_set_error("No <%s> tag found", tag);
162 return -1; /* fall though clause */
164 if (eat_tag(buf, tag) == 1) {
168 while (*((*buf) + 1) && (*(*buf) != '<'))
169 (*buf)++; /*find start of endtag */
171 matches = sscanf(temp, format, value);
173 end_tag = malloc((strlen(tag) + 2) * sizeof(char));
174 sprintf(end_tag, "/%s", tag);
175 eat_tag(buf, end_tag);
177 if (matches == 0 && strcmp(format, "%lf") == 0)
178 (*((double *) (value))) = DNAN;
187 /* parse the data stored in buf and return a filled rrd structure */
193 /* pass 1 identify number of RRAs */
194 char *ptr, *ptr2, *ptr3; /* walks thought the buffer */
195 long rows = 0, mempool = 0, i = 0;
199 xml_lc(buf); /* lets lowercase all active parts of the xml */
203 /* start with an RRD tag */
207 eat_tag(&ptr, "rrd");
208 /* allocate static header */
209 if ((rrd->stat_head = calloc(1, sizeof(stat_head_t))) == NULL) {
210 rrd_set_error("allocating rrd.stat_head");
214 strcpy(rrd->stat_head->cookie, RRD_COOKIE);
215 read_tag(&ptr, "version", "%4[0-9]", rrd->stat_head->version);
216 input_version = atoi(rrd->stat_head->version);
217 /* added primitive version checking */
218 if (input_version > atoi(RRD_VERSION) || input_version < 1) {
220 ("Incompatible file version, detected version %s. This is not supported by the version %s restore tool.\n",
221 rrd->stat_head->version, RRD_VERSION);
222 free(rrd->stat_head);
223 rrd->stat_head = NULL;
226 /* make sure we output the right version */
227 strcpy(rrd->stat_head->version, RRD_VERSION);
229 /* if (atoi(rrd -> stat_head -> version) < 2)
231 rrd_set_error("Can only restore version >= 2 (Not %s). Dump your old rrd using a current rrdtool dump.", rrd -> stat_head -> version );
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 read_tag(&ptr2, "dependent_rra_idx", "%lu",
389 &(rrd->rra_def[rra_index].
390 par[RRA_dependent_rra_idx].u_cnt));
393 read_tag(&ptr2, "delta_pos", "%lf",
394 &(rrd->rra_def[rra_index].par[RRA_delta_pos].
396 read_tag(&ptr2, "delta_neg", "%lf",
397 &(rrd->rra_def[rra_index].par[RRA_delta_neg].
399 read_tag(&ptr2, "window_len", "%lu",
400 &(rrd->rra_def[rra_index].par[RRA_window_len].
402 read_tag(&ptr2, "failure_threshold", "%lu",
403 &(rrd->rra_def[rra_index].
404 par[RRA_failure_threshold].u_cnt));
407 read_tag(&ptr2, "dependent_rra_idx", "%lu",
408 &(rrd->rra_def[rra_index].
409 par[RRA_dependent_rra_idx].u_cnt));
416 read_tag(&ptr2, "xff", "%lf",
417 &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].
421 eat_tag(&ptr2, "/params");
425 eat_tag(&ptr2, "cdp_prep");
426 for (i = 0; i < (int) rrd->stat_head->ds_cnt; i++) {
427 eat_tag(&ptr2, "ds");
428 /* support to read CDP parameters */
429 rra_index = rrd->stat_head->rra_cnt - 1;
431 if (input_version < 2) {
432 rrd->cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
433 i].scratch[CDP_primary_val].u_val = 0.0;
434 rrd->cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
435 i].scratch[CDP_secondary_val].u_val = 0.0;
436 read_tag(&ptr2, "value", "%lf",
438 cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
439 i].scratch[CDP_val].u_val));
440 read_tag(&ptr2, "unknown_datapoints", "%lu",
442 cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
443 i].scratch[CDP_unkn_pdp_cnt].u_cnt));
446 if (strncmp(ptr2, "<value>", 7) == 0) {
447 parse_patch1028_CDP_params(&ptr2, rrd, rra_index, i);
449 read_tag(&ptr2, "primary_value", "%lf",
451 cdp_prep[rrd->stat_head->ds_cnt * (rra_index)
452 + i].scratch[CDP_primary_val].u_val));
453 read_tag(&ptr2, "secondary_value", "%lf",
455 cdp_prep[rrd->stat_head->ds_cnt * (rra_index)
457 i].scratch[CDP_secondary_val].u_val));
458 switch (cf_conv(rrd->rra_def[rra_index].cf_nam)) {
461 read_tag(&ptr2, "intercept", "%lf",
463 cdp_prep[rrd->stat_head->ds_cnt *
466 i].scratch[CDP_hw_intercept].
468 read_tag(&ptr2, "last_intercept", "%lf",
470 cdp_prep[rrd->stat_head->ds_cnt *
473 i].scratch[CDP_hw_last_intercept].
475 read_tag(&ptr2, "slope", "%lf",
477 cdp_prep[rrd->stat_head->ds_cnt *
480 i].scratch[CDP_hw_slope].u_val));
481 read_tag(&ptr2, "last_slope", "%lf",
483 cdp_prep[rrd->stat_head->ds_cnt *
486 i].scratch[CDP_hw_last_slope].
488 read_tag(&ptr2, "nan_count", "%lu",
490 cdp_prep[rrd->stat_head->ds_cnt *
493 i].scratch[CDP_null_count].
495 read_tag(&ptr2, "last_nan_count", "%lu",
497 cdp_prep[rrd->stat_head->ds_cnt *
500 i].scratch[CDP_last_null_count].
505 read_tag(&ptr2, "seasonal", "%lf",
507 cdp_prep[rrd->stat_head->ds_cnt *
510 i].scratch[CDP_hw_seasonal].
512 read_tag(&ptr2, "last_seasonal", "%lf",
514 cdp_prep[rrd->stat_head->ds_cnt *
517 i].scratch[CDP_hw_last_seasonal].
519 read_tag(&ptr2, "init_flag", "%lu",
521 cdp_prep[rrd->stat_head->ds_cnt *
524 i].scratch[CDP_init_seasonal].
530 parse_FAILURES_history(&ptr2, rrd, rra_index, i);
537 read_tag(&ptr2, "value", "%lf",
539 cdp_prep[rrd->stat_head->ds_cnt *
541 i].scratch[CDP_val].u_val));
542 read_tag(&ptr2, "unknown_datapoints", "%lu",
544 cdp_prep[rrd->stat_head->ds_cnt *
546 i].scratch[CDP_unkn_pdp_cnt].
552 eat_tag(&ptr2, "/ds");
554 eat_tag(&ptr2, "/cdp_prep");
555 rrd->rra_def[rrd->stat_head->rra_cnt - 1].row_cnt = 0;
556 eat_tag(&ptr2, "database");
558 while (eat_tag(&ptr3, "row") == 1) {
562 if ((rrd->rrd_value = rrd_realloc(rrd->rrd_value,
565 (rrd->stat_head->ds_cnt)
566 * sizeof(rrd_value_t))) ==
568 rrd_set_error("allocating rrd_values");
574 rrd->rra_def[rrd->stat_head->rra_cnt - 1].row_cnt++;
575 for (i = 0; i < (int) rrd->stat_head->ds_cnt; i++) {
579 rrd_value[(rows - 1) * rrd->stat_head->ds_cnt + i]);
581 read_tag(&ptr3, "v", "%lf", value);
583 if ((rc == 1) /* do we have to check for the ranges */
584 &&(!isnan(*value)) /* not a NAN value */
585 &&(dst_conv(rrd->ds_def[i].dst) != DST_CDEF)
586 && ( /* min defined and in the range ? */
587 (!isnan(rrd->ds_def[i].par[DS_min_val].u_val)
589 rrd->ds_def[i].par[DS_min_val].u_val))
590 || /* max defined and in the range ? */
591 (!isnan(rrd->ds_def[i].par[DS_max_val].u_val)
593 rrd->ds_def[i].par[DS_max_val].u_val))
597 "out of range found [ds: %lu], [value : %0.10e]\n",
602 eat_tag(&ptr3, "/row");
605 eat_tag(&ptr2, "/database");
606 eat_tag(&ptr2, "/rra");
609 eat_tag(&ptr, "/rrd");
612 calloc(1, sizeof(rra_ptr_t) * rrd->stat_head->rra_cnt)) == NULL) {
613 rrd_set_error("allocating rra_ptr");
617 for (i = 0; i < (int) rrd->stat_head->rra_cnt; i++) {
618 /* last row in the xml file is the most recent; as
619 * rrd_update increments the current row pointer, set cur_row
620 * here to the last row. */
621 rrd->rra_ptr[i].cur_row = rrd->rra_def[i].row_cnt - 1;
632 /* create and empty rrd file according to the specs given */
637 char force_overwrite)
639 unsigned long i, ii, val_cnt;
640 FILE *rrd_file = NULL;
644 if (strcmp("-", file_name) == 0) {
647 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
648 fdflags = O_RDWR | O_BINARY | O_CREAT;
650 fdflags = O_WRONLY | O_CREAT;
652 if (force_overwrite == 0) {
655 fd = open(file_name, fdflags, 0666);
656 if (fd == -1 || (rrd_file = fdopen(fd, "wb")) == NULL) {
657 rrd_set_error("creating '%s': %s", file_name,
658 rrd_strerror(errno));
664 fwrite(rrd->stat_head, sizeof(stat_head_t), 1, rrd_file);
666 fwrite(rrd->ds_def, sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
669 sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
671 fwrite(rrd->live_head, sizeof(live_head_t), 1, rrd_file);
673 fwrite(rrd->pdp_prep, sizeof(pdp_prep_t), rrd->stat_head->ds_cnt,
676 fwrite(rrd->cdp_prep, sizeof(cdp_prep_t), rrd->stat_head->rra_cnt *
677 rrd->stat_head->ds_cnt, rrd_file);
678 fwrite(rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt,
683 /* calculate the number of rrd_values to dump */
685 for (i = 0; i < rrd->stat_head->rra_cnt; i++)
686 for (ii = 0; ii < rrd->rra_def[i].row_cnt * rrd->stat_head->ds_cnt;
689 fwrite(rrd->rrd_value, sizeof(rrd_value_t), val_cnt, rrd_file);
691 /* lets see if we had an error */
692 if (ferror(rrd_file)) {
693 rrd_set_error("a file error occurred while creating '%s'", file_name);
710 char force_overwrite = 0;
714 opterr = 0; /* initialize getopt */
716 static struct option long_options[] = {
717 {"range-check", no_argument, 0, 'r'},
718 {"force-overwrite", no_argument, 0, 'f'},
721 int option_index = 0;
725 opt = getopt_long(argc, argv, "rf", long_options, &option_index);
739 ("usage rrdtool %s [--range-check|-r] [--force-overwrite/-f] file.xml file.rrd",
746 if (argc - optind != 2) {
748 ("usage rrdtool %s [--range-check/-r] [--force-overwrite/-f] file.xml file.rrd",
753 if (readfile(argv[optind], &buf, 0) == -1) {
759 if (xml2rrd(buf, &rrd, rc) == -1) {
767 if (rrd_creat(argv[optind + 1], &rrd, force_overwrite) == -1) {
775 /* a backwards compatibility routine that will parse the RRA params section
776 * generated by the aberrant patch to 1.0.28. */
778 void parse_patch1028_RRA_params(
785 for (i = 0; i < MAX_RRA_PAR_EN; i++) {
786 if (i == RRA_dependent_rra_idx ||
787 i == RRA_seasonal_smooth_idx || i == RRA_failure_threshold)
788 read_tag(buf, "value", "%lu",
789 &(rrd->rra_def[rra_index].par[i].u_cnt));
791 read_tag(buf, "value", "%lf",
792 &(rrd->rra_def[rra_index].par[i].u_val));
796 /* a backwards compatibility routine that will parse the CDP params section
797 * generated by the aberrant patch to 1.0.28. */
798 void parse_patch1028_CDP_params(
806 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++) {
807 if (cf_conv(rrd->rra_def[rra_index].cf_nam) == CF_FAILURES ||
808 ii == CDP_unkn_pdp_cnt ||
809 ii == CDP_null_count || ii == CDP_last_null_count) {
810 read_tag(buf, "value", "%lu",
812 cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
813 ds_index].scratch[ii].u_cnt));
815 read_tag(buf, "value", "%lf",
817 cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
818 ds_index].scratch[ii].u_val));
823 void parse_FAILURES_history(
829 char history[MAX_FAILURES_WINDOW_LEN + 1];
830 char *violations_array;
833 /* 28 = MAX_FAILURES_WINDOW_LEN */
834 read_tag(buf, "history", "%28[0-1]", history);
836 (char *) rrd->cdp_prep[rrd->stat_head->ds_cnt * (rra_index)
839 for (i = 0; i < rrd->rra_def[rra_index].par[RRA_window_len].u_cnt; ++i)
840 violations_array[i] = (history[i] == '1') ? 1 : 0;