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 *****************************************************************************/
7 #include "rrd_tool.h"
8 #include "rrd_rpncalc.h"
9 #include <fcntl.h>
11 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
12 #include <io.h>
13 #define open _open
14 #define close _close
15 #endif
17 /* Prototypes */
19 void xml_lc(
20 char *);
21 int skip(
22 char **);
23 int skipxml(
24 char **);
25 int eat_tag(
26 char **,
27 char *);
28 int read_tag(
29 char **,
30 char *,
31 char *,
32 void *);
33 int xml2rrd(
34 char *,
35 rrd_t *,
36 char);
37 int rrd_creat(
38 char *,
39 rrd_t *,
40 char);
41 void parse_patch1028_RRA_params(
42 char **buf,
43 rrd_t *rrd,
44 int rra_index);
45 void parse_patch1028_CDP_params(
46 char **buf,
47 rrd_t *rrd,
48 int rra_index,
49 int ds_index);
50 void parse_FAILURES_history(
51 char **buf,
52 rrd_t *rrd,
53 int rra_index,
54 int ds_index);
55 long int rra_random_row(
56 rra_def_t *);
58 /* convert all occurrences of <BlaBlaBla> to <blablabla> */
60 void xml_lc(
61 char *buf)
62 {
63 int intag = 0;
65 while ((*buf)) {
66 if (intag == 0 && (*buf) == '<') {
67 intag = 1;
68 } else if (intag == 1 && (*buf) == '>') {
69 intag = 0;
70 continue;
71 } else if (intag == 1) {
72 *buf = tolower(*buf);
73 }
74 buf++;
75 }
76 }
78 int skipxml(
79 char **buf)
80 {
81 char *ptr;
83 ptr = (*buf);
84 do {
85 (*buf) = ptr;
86 while ((*(ptr + 1))
87 && ((*ptr) == ' ' || (*ptr) == '\r' || (*ptr) == '\n'
88 || (*ptr) == '\t'))
89 ptr++;
90 if (strncmp(ptr, "<?xml", 4) == 0) {
91 ptr = strstr(ptr, "?>");
92 if (ptr)
93 ptr += 2;
94 else {
95 rrd_set_error("Dangling XML header");
96 (*buf) = NULL;
97 return -1;
98 }
99 }
100 } while ((*buf) != ptr);
101 return 1;
102 }
104 int skip(
105 char **buf)
106 {
107 char *ptr;
109 if ((buf == NULL) || (*buf == NULL))
110 return -1;
111 ptr = (*buf);
112 do {
113 (*buf) = ptr;
114 while ((*(ptr + 1))
115 && ((*ptr) == ' ' || (*ptr) == '\r' || (*ptr) == '\n'
116 || (*ptr) == '\t'))
117 ptr++;
118 if (strncmp(ptr, "<!--", 4) == 0) {
119 ptr = strstr(ptr, "-->");
120 if (ptr)
121 ptr += 3;
122 else {
123 rrd_set_error("Dangling Comment");
124 (*buf) = NULL;
125 return -1;
126 }
127 }
128 } while ((*buf) != ptr);
129 return 1;
130 }
132 int eat_tag(
133 char **buf,
134 char *tag)
135 {
136 if ((*buf) == NULL)
137 return -1; /* fall though clause */
139 rrd_clear_error();
140 skip(buf);
141 if ((**buf) == '<'
142 && strncmp((*buf) + 1, tag, strlen(tag)) == 0
143 && *((*buf) + strlen(tag) + 1) == '>') {
144 (*buf) += strlen(tag) + 2;
145 } else {
146 rrd_set_error("No <%s> tag found", tag);
147 (*buf) = NULL;
148 return -1;
149 }
150 skip(buf);
151 return 1;
152 }
154 int read_tag(
155 char **buf,
156 char *tag,
157 char *format,
158 void *value)
159 {
160 char *end_tag;
161 int matches;
163 if ((*buf) == NULL)
164 return -1; /* fall though clause */
165 rrd_clear_error();
166 if (eat_tag(buf, tag) == 1) {
167 char *temp;
169 temp = (*buf);
170 while (*((*buf) + 1) && (*(*buf) != '<'))
171 (*buf)++; /*find start of endtag */
172 *(*buf) = '\0';
173 matches = sscanf(temp, format, value);
174 *(*buf) = '<';
175 end_tag = malloc((strlen(tag) + 2) * sizeof(char));
176 sprintf(end_tag, "/%s", tag);
177 eat_tag(buf, end_tag);
178 free(end_tag);
179 if (matches == 0 && strcmp(format, "%lf") == 0)
180 (*((double *) (value))) = DNAN;
181 if (matches != 1)
182 return 0;
183 return 1;
184 }
185 return -1;
186 }
189 /* parse the data stored in buf and return a filled rrd structure */
190 int xml2rrd(
191 char *buf,
192 rrd_t *rrd,
193 char rc)
194 {
195 /* pass 1 identify number of RRAs */
196 char *ptr, *ptr2, *ptr3; /* walks thought the buffer */
197 long rows = 0, mempool = 0, i = 0;
198 int rra_index;
199 int input_version;
201 xml_lc(buf); /* lets lowercase all active parts of the xml */
202 ptr = buf;
203 ptr2 = buf;
204 ptr3 = buf;
205 /* start with an RRD tag */
207 skipxml(&ptr);
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");
213 return -1;
214 };
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) {
221 rrd_set_error
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;
226 return -1;
227 }
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);
231 } else {
232 strcpy(rrd->stat_head->version, RRD_VERSION3);
233 }
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");
243 return -1;
244 }
245 read_tag(&ptr, "lastupdate", "%lu", &(rrd->live_head->last_up));
247 /* Data Source Definition Part */
248 ptr2 = ptr;
249 while (eat_tag(&ptr2, "ds") == 1) {
250 rrd->stat_head->ds_cnt++;
251 if ((rrd->ds_def =
252 rrd_realloc(rrd->ds_def,
253 rrd->stat_head->ds_cnt * sizeof(ds_def_t))) ==
254 NULL) {
255 rrd_set_error("allocating rrd.ds_def");
256 return -1;
257 };
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,
260 sizeof(ds_def_t));
261 if ((rrd->pdp_prep =
262 rrd_realloc(rrd->pdp_prep,
263 rrd->stat_head->ds_cnt * sizeof(pdp_prep_t))) ==
264 NULL) {
265 rrd_set_error("allocating pdp_prep");
266 return (-1);
267 }
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,
270 sizeof(pdp_prep_t));
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)
279 return -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 */
292 char buffer[1024];
294 read_tag(&ptr2, "cdef", "%1000s", buffer);
295 parseCDEF_DS(buffer, rrd, rrd->stat_head->ds_cnt - 1);
296 if (rrd_test_error())
297 return -1;
298 }
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].
304 u_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");
309 ptr = ptr2;
310 }
312 ptr2 = ptr;
313 while (eat_tag(&ptr2, "rra") == 1) {
314 rrd->stat_head->rra_cnt++;
316 /* allocate and reset rra definition areas */
317 if ((rrd->rra_def =
318 rrd_realloc(rrd->rra_def,
319 rrd->stat_head->rra_cnt * sizeof(rra_def_t))) ==
320 NULL) {
321 rrd_set_error("allocating rra_def");
322 return -1;
323 }
324 memset(&(rrd->rra_def[rrd->stat_head->rra_cnt - 1]), 0,
325 sizeof(rra_def_t));
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");
333 return -1;
334 }
336 memset(&
337 (rrd->
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) ==
347 -1)
348 return -1;
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));
357 } else {
358 if (eat_tag(&ptr2, "params") != 1) {
359 rrd_set_error("could not find params tag to eat and skip");
360 return -1;
361 }
362 skip(&ptr2);
363 /* backwards compatibility w/ old patch */
364 if (strncmp(ptr2, "<value>", 7) == 0) {
365 parse_patch1028_RRA_params(&ptr2, rrd, rra_index);
366 } else {
367 switch (cf_conv(rrd->rra_def[rra_index].cf_nam)) {
368 case CF_HWPREDICT:
369 case CF_MHWPREDICT:
370 read_tag(&ptr2, "hw_alpha", "%lf",
371 &(rrd->rra_def[rra_index].par[RRA_hw_alpha].
372 u_val));
373 read_tag(&ptr2, "hw_beta", "%lf",
374 &(rrd->rra_def[rra_index].par[RRA_hw_beta].
375 u_val));
376 read_tag(&ptr2, "dependent_rra_idx", "%lu",
377 &(rrd->rra_def[rra_index].
378 par[RRA_dependent_rra_idx].u_cnt));
379 break;
380 case CF_SEASONAL:
381 case CF_DEVSEASONAL:
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));
392 }
393 read_tag(&ptr2, "dependent_rra_idx", "%lu",
394 &(rrd->rra_def[rra_index].
395 par[RRA_dependent_rra_idx].u_cnt));
396 break;
397 case CF_FAILURES:
398 read_tag(&ptr2, "delta_pos", "%lf",
399 &(rrd->rra_def[rra_index].par[RRA_delta_pos].
400 u_val));
401 read_tag(&ptr2, "delta_neg", "%lf",
402 &(rrd->rra_def[rra_index].par[RRA_delta_neg].
403 u_val));
404 read_tag(&ptr2, "window_len", "%lu",
405 &(rrd->rra_def[rra_index].par[RRA_window_len].
406 u_cnt));
407 read_tag(&ptr2, "failure_threshold", "%lu",
408 &(rrd->rra_def[rra_index].
409 par[RRA_failure_threshold].u_cnt));
410 /* fall thru */
411 case CF_DEVPREDICT:
412 read_tag(&ptr2, "dependent_rra_idx", "%lu",
413 &(rrd->rra_def[rra_index].
414 par[RRA_dependent_rra_idx].u_cnt));
415 break;
416 case CF_AVERAGE:
417 case CF_MAXIMUM:
418 case CF_MINIMUM:
419 case CF_LAST:
420 default:
421 read_tag(&ptr2, "xff", "%lf",
422 &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].
423 u_val));
424 }
425 }
426 eat_tag(&ptr2, "/params");
427 }
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);
434 return -1;
435 }
436 /* support to read CDP parameters */
437 rra_index = rrd->stat_head->rra_cnt - 1;
438 skip(&ptr2);
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",
445 &(rrd->
446 cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
447 i].scratch[CDP_val].u_val));
448 read_tag(&ptr2, "unknown_datapoints", "%lu",
449 &(rrd->
450 cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
451 i].scratch[CDP_unkn_pdp_cnt].u_cnt));
452 } else {
454 if (strncmp(ptr2, "<value>", 7) == 0) {
455 parse_patch1028_CDP_params(&ptr2, rrd, rra_index, i);
456 } else {
457 read_tag(&ptr2, "primary_value", "%lf",
458 &(rrd->
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",
462 &(rrd->
463 cdp_prep[rrd->stat_head->ds_cnt * (rra_index)
464 +
465 i].scratch[CDP_secondary_val].u_val));
466 switch (cf_conv(rrd->rra_def[rra_index].cf_nam)) {
467 case CF_HWPREDICT:
468 case CF_MHWPREDICT:
469 read_tag(&ptr2, "intercept", "%lf",
470 &(rrd->
471 cdp_prep[rrd->stat_head->ds_cnt *
472 (rra_index)
473 +
474 i].scratch[CDP_hw_intercept].
475 u_val));
476 read_tag(&ptr2, "last_intercept", "%lf",
477 &(rrd->
478 cdp_prep[rrd->stat_head->ds_cnt *
479 (rra_index)
480 +
481 i].scratch[CDP_hw_last_intercept].
482 u_val));
483 read_tag(&ptr2, "slope", "%lf",
484 &(rrd->
485 cdp_prep[rrd->stat_head->ds_cnt *
486 (rra_index)
487 +
488 i].scratch[CDP_hw_slope].u_val));
489 read_tag(&ptr2, "last_slope", "%lf",
490 &(rrd->
491 cdp_prep[rrd->stat_head->ds_cnt *
492 (rra_index)
493 +
494 i].scratch[CDP_hw_last_slope].
495 u_val));
496 read_tag(&ptr2, "nan_count", "%lu",
497 &(rrd->
498 cdp_prep[rrd->stat_head->ds_cnt *
499 (rra_index)
500 +
501 i].scratch[CDP_null_count].
502 u_cnt));
503 read_tag(&ptr2, "last_nan_count", "%lu",
504 &(rrd->
505 cdp_prep[rrd->stat_head->ds_cnt *
506 (rra_index)
507 +
508 i].scratch[CDP_last_null_count].
509 u_cnt));
510 break;
511 case CF_SEASONAL:
512 case CF_DEVSEASONAL:
513 read_tag(&ptr2, "seasonal", "%lf",
514 &(rrd->
515 cdp_prep[rrd->stat_head->ds_cnt *
516 (rra_index)
517 +
518 i].scratch[CDP_hw_seasonal].
519 u_val));
520 read_tag(&ptr2, "last_seasonal", "%lf",
521 &(rrd->
522 cdp_prep[rrd->stat_head->ds_cnt *
523 (rra_index)
524 +
525 i].scratch[CDP_hw_last_seasonal].
526 u_val));
527 read_tag(&ptr2, "init_flag", "%lu",
528 &(rrd->
529 cdp_prep[rrd->stat_head->ds_cnt *
530 (rra_index)
531 +
532 i].scratch[CDP_init_seasonal].
533 u_cnt));
534 break;
535 case CF_DEVPREDICT:
536 break;
537 case CF_FAILURES:
538 parse_FAILURES_history(&ptr2, rrd, rra_index, i);
539 break;
540 case CF_AVERAGE:
541 case CF_MAXIMUM:
542 case CF_MINIMUM:
543 case CF_LAST:
544 default:
545 read_tag(&ptr2, "value", "%lf",
546 &(rrd->
547 cdp_prep[rrd->stat_head->ds_cnt *
548 (rra_index) +
549 i].scratch[CDP_val].u_val));
550 read_tag(&ptr2, "unknown_datapoints", "%lu",
551 &(rrd->
552 cdp_prep[rrd->stat_head->ds_cnt *
553 (rra_index) +
554 i].scratch[CDP_unkn_pdp_cnt].
555 u_cnt));
556 break;
557 }
558 }
559 }
560 eat_tag(&ptr2, "/ds");
561 }
562 eat_tag(&ptr2, "/cdp_prep");
563 rrd->rra_def[rrd->stat_head->rra_cnt - 1].row_cnt = 0;
564 eat_tag(&ptr2, "database");
565 ptr3 = ptr2;
566 while (eat_tag(&ptr3, "row") == 1) {
568 if (mempool == 0) {
569 mempool = 1000;
570 if ((rrd->rrd_value = rrd_realloc(rrd->rrd_value,
571 (rows +
572 mempool) *
573 (rrd->stat_head->ds_cnt)
574 * sizeof(rrd_value_t))) ==
575 NULL) {
576 rrd_set_error("allocating rrd_values");
577 return -1;
578 }
579 }
580 rows++;
581 mempool--;
582 rrd->rra_def[rrd->stat_head->rra_cnt - 1].row_cnt++;
583 for (i = 0; i < (int) rrd->stat_head->ds_cnt; i++) {
585 rrd_value_t *value =
586 &(rrd->
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)
596 && (*value <
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)
600 && (*value >
601 rrd->ds_def[i].par[DS_max_val].u_val))
602 )
603 ) {
604 fprintf(stderr,
605 "out of range found [ds: %lu], [value : %0.10e]\n",
606 i, *value);
607 *value = DNAN;
608 }
609 }
610 eat_tag(&ptr3, "/row");
611 ptr2 = ptr3;
612 }
613 eat_tag(&ptr2, "/database");
614 eat_tag(&ptr2, "/rra");
615 ptr = ptr2;
616 }
617 eat_tag(&ptr, "/rrd");
619 if ((rrd->rra_ptr =
620 calloc(1, sizeof(rra_ptr_t) * rrd->stat_head->rra_cnt)) == NULL) {
621 rrd_set_error("allocating rra_ptr");
622 return (-1);
623 }
625 if (ptr == NULL)
626 return -1;
627 return 1;
628 }
634 /* create and empty rrd file according to the specs given */
636 int rrd_creat(
637 char *file_name,
638 rrd_t *rrd,
639 char force_overwrite)
640 {
641 unsigned long i, ii, rra_offset;
642 FILE *rrd_file = NULL;
643 int fdflags;
644 int fd;
646 if (strcmp("-", file_name) == 0) {
647 rrd_file = stdout;
648 } else {
649 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
650 fdflags = O_RDWR | O_BINARY | O_CREAT;
651 #else
652 fdflags = O_WRONLY | O_CREAT;
653 #endif
654 if (force_overwrite == 0) {
655 fdflags |= O_EXCL;
656 }
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));
661 if (fd != -1)
662 close(fd);
663 return (-1);
664 }
665 }
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);
670 fwrite(rrd->rra_def,
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,
676 rrd_file);
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,
685 rrd_file);
688 /* Dump RRD values */
689 rra_offset=0;
690 for(i=0; i < rrd->stat_head->rra_cnt; i++)
691 {
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;
703 }
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);
708 fclose(rrd_file);
709 return (-1);
710 }
712 fclose(rrd_file);
713 return 0;
714 }
717 int rrd_restore(
718 int argc,
719 char **argv)
720 {
721 rrd_t rrd;
722 char *buf;
723 char rc = 0;
724 char force_overwrite = 0;
725 struct option long_options[] = {
726 {"range-check", no_argument, 0, 'r'},
727 {"force-overwrite", no_argument, 0, 'f'},
728 {0, 0, 0, 0}
729 };
731 /* init rrd clean */
732 optind = 0;
733 opterr = 0; /* initialize getopt */
734 while (1) {
735 int option_index = 0;
736 int opt;
738 opt = getopt_long(argc, argv, "rf", long_options, &option_index);
740 if (opt == EOF)
741 break;
743 switch (opt) {
744 case 'r':
745 rc = 1;
746 break;
747 case 'f':
748 force_overwrite = 1;
749 break;
750 default:
751 rrd_set_error
752 ("usage rrdtool %s [--range-check|-r] [--force-overwrite/-f] file.xml file.rrd",
753 argv[0]);
754 return -1;
755 break;
756 }
757 }
759 if (argc - optind != 2) {
760 rrd_set_error
761 ("usage rrdtool %s [--range-check/-r] [--force-overwrite/-f] file.xml file.rrd",
762 argv[0]);
763 return -1;
764 }
766 if (readfile(argv[optind], &buf, 0) == -1) {
767 return -1;
768 }
770 rrd_init(&rrd);
772 if (xml2rrd(buf, &rrd, rc) == -1) {
773 rrd_free(&rrd);
774 free(buf);
775 return -1;
776 }
778 free(buf);
780 if (rrd_creat(argv[optind + 1], &rrd, force_overwrite) == -1) {
781 rrd_free(&rrd);
782 return -1;
783 };
784 rrd_free(&rrd);
785 return 0;
786 }
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(
792 char **buf,
793 rrd_t *rrd,
794 int rra_index)
795 {
796 int i;
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));
803 else
804 read_tag(buf, "value", "%lf",
805 &(rrd->rra_def[rra_index].par[i].u_val));
806 }
807 }
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(
812 char **buf,
813 rrd_t *rrd,
814 int rra_index,
815 int ds_index)
816 {
817 int ii;
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",
824 &(rrd->
825 cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
826 ds_index].scratch[ii].u_cnt));
827 } else {
828 read_tag(buf, "value", "%lf",
829 &(rrd->
830 cdp_prep[rrd->stat_head->ds_cnt * (rra_index) +
831 ds_index].scratch[ii].u_val));
832 }
833 }
834 }
836 void parse_FAILURES_history(
837 char **buf,
838 rrd_t *rrd,
839 int rra_index,
840 int ds_index)
841 {
842 char history[MAX_FAILURES_WINDOW_LEN + 1];
843 char *violations_array;
844 unsigned short i;
846 /* 28 = MAX_FAILURES_WINDOW_LEN */
847 read_tag(buf, "history", "%28[0-1]", history);
848 violations_array =
849 (char *) rrd->cdp_prep[rrd->stat_head->ds_cnt * (rra_index)
850 + ds_index].scratch;
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;
855 }