1 /*****************************************************************************
2 * RRDtool 1.3.2 Copyright by Tobi Oetiker, 1997-2008
3 * This file: Copyright 2008 Florian octo Forster
4 * Distributed under the GPL
5 *****************************************************************************
6 * rrd_restore.c Contains logic to parse XML input and create an RRD file
7 *****************************************************************************
9 *************************************************************************** */
12 * This program is free software; you can redistribute it and / or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (t your option)
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110 - 1301 USA
27 * Florian octo Forster <octo at verplant.org>
37 # include <unistd.h> /* for off_t */
39 # define random() rand()
40 # define srandom(x) srand(x)
42 typedef size_t ssize_t;
47 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
52 #include <libxml/parser.h>
54 #include "rrd_rpncalc.h"
55 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof ((a)[0]))
56 static int opt_range_check = 0;
57 static int opt_force_overwrite = 0;
62 static int get_string_from_node(
72 temp0 = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
74 rrd_set_error("get_string_from_node: xmlNodeListGetString failed.");
78 begin_ptr = (char *) temp0;
79 while ((begin_ptr[0] != 0) && (isspace(begin_ptr[0])))
82 if (begin_ptr[0] == 0) {
89 while ((end_ptr[0] != 0) && (!isspace(end_ptr[0])))
93 strncpy(buffer, begin_ptr, buffer_size);
94 buffer[buffer_size - 1] = 0;
99 } /* int get_string_from_node */
101 static int get_int_from_node(
110 str_ptr = (char *) xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
111 if (str_ptr == NULL) {
112 rrd_set_error("get_int_from_node: xmlNodeListGetString failed.");
117 temp = strtol(str_ptr, &end_ptr, 0);
120 if (str_ptr == end_ptr) {
121 rrd_set_error("get_int_from_node: Cannot parse buffer as int: %s",
129 } /* int get_int_from_node */
131 static int get_double_from_node(
140 str_ptr = (char *) xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
141 if (str_ptr == NULL) {
142 rrd_set_error("get_double_from_node: xmlNodeListGetString failed.");
147 temp = strtod(str_ptr, &end_ptr);
150 if (str_ptr == end_ptr) {
152 ("get_double_from_node: Cannot parse buffer as double: %s",
160 } /* int get_double_from_node */
162 static int value_check_range(
163 rrd_value_t *rrd_value,
164 const ds_def_t *ds_def)
169 if (opt_range_check == 0)
172 min = ds_def->par[DS_min_val].u_val;
173 max = ds_def->par[DS_max_val].u_val;
175 if (((!isnan(min)) && (*rrd_value < min))
176 || ((!isnan(max)) && (*rrd_value > max)))
180 } /* int value_check_range */
183 * Parse the <database> block within an RRA definition
185 static int parse_tag_rra_database_row(
189 rrd_value_t *rrd_value)
191 unsigned int values_count = 0;
196 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
197 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
198 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
200 else if (xmlStrcmp(child->name, (const xmlChar *) "v") == 0) {
201 if (values_count < rrd->stat_head->ds_cnt) {
203 get_double_from_node(doc, child,
204 rrd_value + values_count);
206 value_check_range(rrd_value + values_count,
207 rrd->ds_def + values_count);
212 rrd_set_error("parse_tag_rra_database_row: Unknown tag: %s",
219 } /* for (child = node->xmlChildrenNode) */
221 if (values_count != rrd->stat_head->ds_cnt) {
222 rrd_set_error("parse_tag_rra_database_row: Row has %u values "
223 "and RRD has %lu data sources.",
224 values_count, rrd->stat_head->ds_cnt);
229 } /* int parse_tag_rra_database_row */
231 static int parse_tag_rra_database(
236 rra_def_t *cur_rra_def;
237 unsigned int total_row_cnt;
243 for (i = 0; i < (((int) rrd->stat_head->rra_cnt) - 1); i++)
244 total_row_cnt += rrd->rra_def[i].row_cnt;
246 cur_rra_def = rrd->rra_def + i;
249 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
250 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
251 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
253 else if (xmlStrcmp(child->name, (const xmlChar *) "row") == 0) {
255 rrd_value_t *cur_rrd_value;
256 unsigned int total_values_count = rrd->stat_head->ds_cnt
257 * (total_row_cnt + 1);
259 /* Allocate space for the new values.. */
260 temp = (rrd_value_t *) realloc(rrd->rrd_value,
261 sizeof(rrd_value_t) *
264 rrd_set_error("parse_tag_rra_database: realloc failed.");
268 rrd->rrd_value = temp;
269 cur_rrd_value = rrd->rrd_value
270 + (rrd->stat_head->ds_cnt * total_row_cnt);
271 memset(cur_rrd_value, '\0',
272 sizeof(rrd_value_t) * rrd->stat_head->ds_cnt);
274 cur_rra_def->row_cnt++;
277 parse_tag_rra_database_row(doc, child, rrd, cur_rrd_value);
278 } /* if (xmlStrcmp (child->name, (const xmlChar *) "row") == 0) */
280 rrd_set_error("parse_tag_rra_database: Unknown tag: %s",
287 } /* for (child = node->xmlChildrenNode) */
290 } /* int parse_tag_rra_database */
293 * Parse the <cdp_prep> block within an RRA definition
295 static int parse_tag_rra_cdp_prep_ds_history(
298 cdp_prep_t *cdp_prep)
300 /* Make `history_buffer' the same size as the scratch area, plus the
301 * terminating NULL byte. */
302 char history_buffer[sizeof(((cdp_prep_t *)0)->scratch) + 1];
307 status = get_string_from_node(doc, node,
308 history_buffer, sizeof(history_buffer));
312 history_ptr = (char *) (&cdp_prep->scratch[0]);
313 for (i = 0; history_buffer[i] != '\0'; i++)
314 history_ptr[i] = (history_buffer[i] == '1') ? 1 : 0;
317 } /* int parse_tag_rra_cdp_prep_ds_history */
319 static int parse_tag_rra_cdp_prep_ds(
323 cdp_prep_t *cdp_prep)
328 memset(cdp_prep, '\0', sizeof(cdp_prep_t));
331 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
332 if (atoi(rrd->stat_head->version) == 1) {
333 cdp_prep->scratch[CDP_primary_val].u_val = 0.0;
334 cdp_prep->scratch[CDP_secondary_val].u_val = 0.0;
336 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
337 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
339 else if (xmlStrcmp(child->name, (const xmlChar *) "primary_value") ==
342 get_double_from_node(doc, child,
343 &cdp_prep->scratch[CDP_primary_val].
345 else if (xmlStrcmp(child->name, (const xmlChar *) "secondary_value")
348 get_double_from_node(doc, child,
349 &cdp_prep->scratch[CDP_secondary_val].
351 else if (xmlStrcmp(child->name, (const xmlChar *) "intercept") == 0)
352 status = get_double_from_node(doc, child,
354 scratch[CDP_hw_intercept].u_val);
355 else if (xmlStrcmp(child->name, (const xmlChar *) "last_intercept") ==
358 get_double_from_node(doc, child,
360 scratch[CDP_hw_last_intercept].u_val);
361 else if (xmlStrcmp(child->name, (const xmlChar *) "slope") == 0)
362 status = get_double_from_node(doc, child,
363 &cdp_prep->scratch[CDP_hw_slope].
365 else if (xmlStrcmp(child->name, (const xmlChar *) "last_slope") == 0)
366 status = get_double_from_node(doc, child,
368 scratch[CDP_hw_last_slope].u_val);
369 else if (xmlStrcmp(child->name, (const xmlChar *) "nan_count") == 0)
370 status = get_int_from_node(doc, child,
372 scratch[CDP_null_count].u_cnt);
373 else if (xmlStrcmp(child->name, (const xmlChar *) "last_nan_count") ==
376 get_int_from_node(doc, child,
378 scratch[CDP_last_null_count].u_cnt);
379 else if (xmlStrcmp(child->name, (const xmlChar *) "seasonal") == 0)
380 status = get_double_from_node(doc, child,
381 &cdp_prep->scratch[CDP_hw_seasonal].
383 else if (xmlStrcmp(child->name, (const xmlChar *) "last_seasonal") ==
386 get_double_from_node(doc, child,
387 &cdp_prep->scratch[CDP_hw_last_seasonal].
389 else if (xmlStrcmp(child->name, (const xmlChar *) "init_flag") == 0)
390 status = get_int_from_node(doc, child,
392 scratch[CDP_init_seasonal].u_cnt);
393 else if (xmlStrcmp(child->name, (const xmlChar *) "history") == 0)
394 status = parse_tag_rra_cdp_prep_ds_history(doc, child, cdp_prep);
395 else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0)
396 status = get_double_from_node(doc, child,
397 &cdp_prep->scratch[CDP_val].u_val);
398 else if (xmlStrcmp(child->name,
399 (const xmlChar *) "unknown_datapoints") == 0)
400 status = get_int_from_node(doc, child,
402 scratch[CDP_unkn_pdp_cnt].u_cnt);
404 rrd_set_error("parse_tag_rra_cdp_prep: Unknown tag: %s",
414 } /* int parse_tag_rra_cdp_prep_ds */
416 static int parse_tag_rra_cdp_prep(
420 cdp_prep_t *cdp_prep)
425 unsigned int ds_count = 0;
428 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
429 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
430 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
432 else if (xmlStrcmp(child->name, (const xmlChar *) "ds") == 0) {
433 if (ds_count >= rrd->stat_head->ds_cnt)
436 status = parse_tag_rra_cdp_prep_ds(doc, child, rrd,
437 cdp_prep + ds_count);
441 rrd_set_error("parse_tag_rra_cdp_prep: Unknown tag: %s",
450 if (ds_count != rrd->stat_head->ds_cnt) {
451 rrd_set_error("parse_tag_rra_cdp_prep: There are %i data sources in "
452 "the RRD file, but %i in this cdp_prep block!",
453 (int) rrd->stat_head->ds_cnt, ds_count);
458 } /* int parse_tag_rra_cdp_prep */
461 * Parse the <params> block within an RRA definition
463 static int parse_tag_rra_params(
472 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
473 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
474 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
477 * Parameters for CF_HWPREDICT
479 else if (xmlStrcmp(child->name, (const xmlChar *) "hw_alpha") == 0)
480 status = get_double_from_node(doc, child,
481 &rra_def->par[RRA_hw_alpha].u_val);
482 else if (xmlStrcmp(child->name, (const xmlChar *) "hw_beta") == 0)
483 status = get_double_from_node(doc, child,
484 &rra_def->par[RRA_hw_beta].u_val);
485 else if (xmlStrcmp(child->name,
486 (const xmlChar *) "dependent_rra_idx") == 0)
487 status = get_int_from_node(doc, child,
489 par[RRA_dependent_rra_idx].u_cnt);
491 * Parameters for CF_SEASONAL and CF_DEVSEASONAL
493 else if (xmlStrcmp(child->name, (const xmlChar *) "seasonal_gamma") ==
496 get_double_from_node(doc, child,
497 &rra_def->par[RRA_seasonal_gamma].u_val);
499 (child->name, (const xmlChar *) "seasonal_smooth_idx") == 0)
501 get_int_from_node(doc, child,
503 par[RRA_seasonal_smooth_idx].u_cnt);
504 else if (xmlStrcmp(child->name, (const xmlChar *) "smoothing_window")
507 get_double_from_node(doc, child,
509 par[RRA_seasonal_smoothing_window].
511 /* else if (dependent_rra_idx) ...; */
513 * Parameters for CF_FAILURES
515 else if (xmlStrcmp(child->name, (const xmlChar *) "delta_pos") == 0)
516 status = get_double_from_node(doc, child,
517 &rra_def->par[RRA_delta_pos].u_val);
518 else if (xmlStrcmp(child->name, (const xmlChar *) "delta_neg") == 0)
519 status = get_double_from_node(doc, child,
520 &rra_def->par[RRA_delta_neg].u_val);
521 else if (xmlStrcmp(child->name, (const xmlChar *) "window_len") == 0)
522 status = get_int_from_node(doc, child,
523 (int *) &rra_def->par[RRA_window_len].
525 else if (xmlStrcmp(child->name, (const xmlChar *) "failure_threshold")
528 get_int_from_node(doc, child,
530 par[RRA_failure_threshold].u_cnt);
532 * Parameters for CF_AVERAGE, CF_MAXIMUM, CF_MINIMUM, and CF_LAST
534 else if (xmlStrcmp(child->name, (const xmlChar *) "xff") == 0)
535 status = get_double_from_node(doc, child,
536 &rra_def->par[RRA_cdp_xff_val].
539 * Compatibility code for 1.0.49
541 else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0) { /* {{{ */
545 if (i >= ARRAY_LENGTH(rra_def->par)) {
550 if ((i == RRA_dependent_rra_idx)
551 || (i == RRA_seasonal_smooth_idx)
552 || (i == RRA_failure_threshold))
553 status = get_int_from_node(doc, child,
554 (int *) &rra_def->par[i].
557 status = get_double_from_node(doc, child,
558 &rra_def->par[i].u_val);
563 /* When this loops exits (sucessfully) `child' points to the last
564 * `value' tag in the list. */
565 if ((child->next == NULL)
566 || (xmlStrcmp(child->name, (const xmlChar *) "value") !=
575 rrd_set_error("parse_tag_rra_params: Unknown tag: %s",
585 } /* int parse_tag_rra_params */
588 * Parse an RRA definition
590 static int parse_tag_rra_cf(
597 status = get_string_from_node(doc, node,
598 rra_def->cf_nam, sizeof(rra_def->cf_nam));
602 status = cf_conv(rra_def->cf_nam);
604 rrd_set_error("parse_tag_rra_cf: Unknown consolidation function: %s",
610 } /* int parse_tag_rra_cf */
612 static int parse_tag_rra(
620 rra_def_t *cur_rra_def;
621 cdp_prep_t *cur_cdp_prep;
622 rra_ptr_t *cur_rra_ptr;
624 /* Allocate more rra_def space for this RRA */
628 temp = (rra_def_t *) realloc(rrd->rra_def,
630 (rrd->stat_head->rra_cnt + 1));
632 rrd_set_error("parse_tag_rra: realloc failed.");
636 cur_rra_def = rrd->rra_def + rrd->stat_head->rra_cnt;
637 memset(cur_rra_def, '\0', sizeof(rra_def_t));
640 /* allocate cdp_prep_t */
644 temp = (cdp_prep_t *) realloc(rrd->cdp_prep, sizeof(cdp_prep_t)
645 * rrd->stat_head->ds_cnt
646 * (rrd->stat_head->rra_cnt + 1));
648 rrd_set_error("parse_tag_rra: realloc failed.");
651 rrd->cdp_prep = temp;
652 cur_cdp_prep = rrd->cdp_prep
653 + (rrd->stat_head->ds_cnt * rrd->stat_head->rra_cnt);
654 memset(cur_cdp_prep, '\0',
655 sizeof(cdp_prep_t) * rrd->stat_head->ds_cnt);
658 /* allocate rra_ptr_t */
662 temp = (rra_ptr_t *) realloc(rrd->rra_ptr,
664 (rrd->stat_head->rra_cnt + 1));
666 rrd_set_error("parse_tag_rra: realloc failed.");
670 cur_rra_ptr = rrd->rra_ptr + rrd->stat_head->rra_cnt;
671 memset(cur_rra_ptr, '\0', sizeof(rra_ptr_t));
674 /* All space successfully allocated, increment number of RRAs. */
675 rrd->stat_head->rra_cnt++;
678 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
679 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
680 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
682 else if (xmlStrcmp(child->name, (const xmlChar *) "cf") == 0)
683 status = parse_tag_rra_cf(doc, child, cur_rra_def);
684 else if (xmlStrcmp(child->name, (const xmlChar *) "pdp_per_row") == 0)
685 status = get_int_from_node(doc, child,
686 (int *) &cur_rra_def->pdp_cnt);
687 else if (atoi(rrd->stat_head->version) == 1
688 && xmlStrcmp(child->name, (const xmlChar *) "xff") == 0)
689 status = get_double_from_node(doc, child,
690 (double *) &cur_rra_def->
691 par[RRA_cdp_xff_val].u_val);
692 else if (atoi(rrd->stat_head->version) >= 2
693 && xmlStrcmp(child->name, (const xmlChar *) "params") == 0)
694 status = parse_tag_rra_params(doc, child, cur_rra_def);
695 else if (xmlStrcmp(child->name, (const xmlChar *) "cdp_prep") == 0)
696 status = parse_tag_rra_cdp_prep(doc, child, rrd, cur_cdp_prep);
697 else if (xmlStrcmp(child->name, (const xmlChar *) "database") == 0)
698 status = parse_tag_rra_database(doc, child, rrd);
700 rrd_set_error("parse_tag_rra: Unknown tag: %s", child->name);
708 /* Set the RRA pointer to a random location */
709 cur_rra_ptr->cur_row = random() % cur_rra_def->row_cnt;
712 } /* int parse_tag_rra */
715 * Parse a DS definition
717 static int parse_tag_ds_cdef(
725 status = get_string_from_node(doc, node, buffer, sizeof(buffer));
729 /* We're always working on the last DS that has been added to the structure
730 * when we get here */
731 parseCDEF_DS(buffer, rrd, rrd->stat_head->ds_cnt - 1);
734 } /* int parse_tag_ds_cdef */
736 static int parse_tag_ds_type(
743 status = get_string_from_node(doc, node,
744 ds_def->dst, sizeof(ds_def->dst));
748 status = dst_conv(ds_def->dst);
750 rrd_set_error("parse_tag_ds_type: Unknown data source type: %s",
756 } /* int parse_tag_ds_type */
758 static int parse_tag_ds(
766 ds_def_t *cur_ds_def;
767 pdp_prep_t *cur_pdp_prep;
770 * If there are DS definitions after RRA definitions the number of values,
771 * cdp_prep areas and so on will be calculated wrong. Thus, enforce a
772 * specific order in this case.
774 if (rrd->stat_head->rra_cnt > 0) {
775 rrd_set_error("parse_tag_ds: All data source definitions MUST "
776 "precede the RRA definitions!");
780 /* Allocate space for the new DS definition */
784 temp = (ds_def_t *) realloc(rrd->ds_def,
786 (rrd->stat_head->ds_cnt + 1));
788 rrd_set_error("parse_tag_ds: malloc failed.");
792 cur_ds_def = rrd->ds_def + rrd->stat_head->ds_cnt;
793 memset(cur_ds_def, '\0', sizeof(ds_def_t));
796 /* Allocate pdp_prep space for the new DS definition */
800 temp = (pdp_prep_t *) realloc(rrd->pdp_prep,
802 (rrd->stat_head->ds_cnt + 1));
804 rrd_set_error("parse_tag_ds: malloc failed.");
807 rrd->pdp_prep = temp;
808 cur_pdp_prep = rrd->pdp_prep + rrd->stat_head->ds_cnt;
809 memset(cur_pdp_prep, '\0', sizeof(pdp_prep_t));
812 /* All allocations successful, let's increment the number of DSes. */
813 rrd->stat_head->ds_cnt++;
816 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
817 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
818 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
820 else if (xmlStrcmp(child->name, (const xmlChar *) "name") == 0)
821 status = get_string_from_node(doc, child,
823 sizeof(cur_ds_def->ds_nam));
824 else if (xmlStrcmp(child->name, (const xmlChar *) "type") == 0)
825 status = parse_tag_ds_type(doc, child, cur_ds_def);
826 else if (xmlStrcmp(child->name,
827 (const xmlChar *) "minimal_heartbeat") == 0)
828 status = get_int_from_node(doc, child,
829 (int *) &cur_ds_def->par[DS_mrhb_cnt].
831 else if (xmlStrcmp(child->name, (const xmlChar *) "min") == 0)
832 status = get_double_from_node(doc, child,
833 &cur_ds_def->par[DS_min_val].u_val);
834 else if (xmlStrcmp(child->name, (const xmlChar *) "max") == 0)
835 status = get_double_from_node(doc, child,
836 &cur_ds_def->par[DS_max_val].u_val);
837 else if (xmlStrcmp(child->name, (const xmlChar *) "cdef") == 0)
838 status = parse_tag_ds_cdef(doc, child, rrd);
839 else if (xmlStrcmp(child->name, (const xmlChar *) "last_ds") == 0)
840 status = get_string_from_node(doc, child,
841 cur_pdp_prep->last_ds,
842 sizeof(cur_pdp_prep->last_ds));
843 else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0)
844 status = get_double_from_node(doc, child,
845 &cur_pdp_prep->scratch[PDP_val].
847 else if (xmlStrcmp(child->name, (const xmlChar *) "unknown_sec") == 0)
848 status = get_int_from_node(doc, child,
849 (int *) &cur_pdp_prep->
850 scratch[PDP_unkn_sec_cnt].u_cnt);
852 rrd_set_error("parse_tag_ds: Unknown tag: %s", child->name);
861 } /* int parse_tag_ds */
866 static int parse_tag_rrd(
875 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
876 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
877 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
879 else if (xmlStrcmp(child->name, (const xmlChar *) "version") == 0)
880 status = get_string_from_node(doc, child,
881 rrd->stat_head->version,
882 sizeof(rrd->stat_head->version));
883 else if (xmlStrcmp(child->name, (const xmlChar *) "step") == 0)
884 status = get_int_from_node(doc, child,
885 (int *) &rrd->stat_head->pdp_step);
886 else if (xmlStrcmp(child->name, (const xmlChar *) "lastupdate") == 0)
887 status = get_int_from_node(doc, child,
888 (int *) &rrd->live_head->last_up);
889 else if (xmlStrcmp(child->name, (const xmlChar *) "ds") == 0)
890 status = parse_tag_ds(doc, child, rrd);
891 else if (xmlStrcmp(child->name, (const xmlChar *) "rra") == 0)
892 status = parse_tag_rra(doc, child, rrd);
894 rrd_set_error("parse_tag_rrd: Unknown tag: %s", child->name);
903 } /* int parse_tag_rrd */
905 static rrd_t *parse_file(
906 const char *filename)
914 doc = xmlParseFile(filename);
916 rrd_set_error("Document not parsed successfully.");
920 cur = xmlDocGetRootElement(doc);
922 rrd_set_error("Document is empty.");
927 if (xmlStrcmp(cur->name, (const xmlChar *) "rrd") != 0) {
929 ("Document of the wrong type, root node is not \"rrd\".");
934 rrd = (rrd_t *) malloc(sizeof(rrd_t));
936 rrd_set_error("parse_file: malloc failed.");
940 memset(rrd, '\0', sizeof(rrd_t));
942 rrd->stat_head = (stat_head_t *) malloc(sizeof(stat_head_t));
943 if (rrd->stat_head == NULL) {
944 rrd_set_error("parse_tag_rrd: malloc failed.");
949 memset(rrd->stat_head, '\0', sizeof(stat_head_t));
951 strncpy(rrd->stat_head->cookie, "RRD", sizeof(rrd->stat_head->cookie));
952 rrd->stat_head->float_cookie = FLOAT_COOKIE;
954 rrd->live_head = (live_head_t *) malloc(sizeof(live_head_t));
955 if (rrd->live_head == NULL) {
956 rrd_set_error("parse_tag_rrd: malloc failed.");
958 free(rrd->stat_head);
962 memset(rrd->live_head, '\0', sizeof(live_head_t));
964 status = parse_tag_rrd(doc, cur, rrd);
973 } /* rrd_t *parse_file */
975 static int write_file(
976 const char *file_name,
981 unsigned int rra_offset;
983 if (strcmp("-", file_name) == 0)
986 int fd_flags = O_WRONLY | O_CREAT;
989 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
990 fd_flags |= O_BINARY;
993 if (opt_force_overwrite == 0)
996 fd = open(file_name, fd_flags, 0666);
998 rrd_set_error("creating '%s': %s", file_name,
999 rrd_strerror(errno));
1003 fh = fdopen(fd, "wb");
1005 rrd_set_error("fdopen failed: %s", rrd_strerror(errno));
1010 if (atoi(rrd->stat_head->version) < 3) {
1011 /* we output 3 or higher */
1012 strcpy(rrd->stat_head->version, "0003");
1014 fwrite(rrd->stat_head, sizeof(stat_head_t), 1, fh);
1015 fwrite(rrd->ds_def, sizeof(ds_def_t), rrd->stat_head->ds_cnt, fh);
1016 fwrite(rrd->rra_def, sizeof(rra_def_t), rrd->stat_head->rra_cnt, fh);
1017 fwrite(rrd->live_head, sizeof(live_head_t), 1, fh);
1018 fwrite(rrd->pdp_prep, sizeof(pdp_prep_t), rrd->stat_head->ds_cnt, fh);
1019 fwrite(rrd->cdp_prep, sizeof(cdp_prep_t),
1020 rrd->stat_head->rra_cnt * rrd->stat_head->ds_cnt, fh);
1021 fwrite(rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt, fh);
1023 /* calculate the number of rrd_values to dump */
1025 for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
1026 unsigned long num_rows = rrd->rra_def[i].row_cnt;
1027 unsigned long cur_row = rrd->rra_ptr[i].cur_row;
1028 unsigned long ds_cnt = rrd->stat_head->ds_cnt;
1030 fwrite(rrd->rrd_value +
1031 (rra_offset + num_rows - 1 - cur_row) * ds_cnt,
1032 sizeof(rrd_value_t), (cur_row + 1) * ds_cnt, fh);
1034 fwrite(rrd->rrd_value + rra_offset * ds_cnt,
1035 sizeof(rrd_value_t), (num_rows - 1 - cur_row) * ds_cnt, fh);
1037 rra_offset += num_rows;
1040 /* lets see if we had an error */
1042 rrd_set_error("a file error occurred while creating '%s'", file_name);
1049 } /* int write_file */
1057 srandom((unsigned int) time(NULL) + (unsigned int) getpid());
1058 /* init rrd clean */
1060 opterr = 0; /* initialize getopt */
1063 int option_index = 0;
1064 static struct option long_options[] = {
1065 {"range-check", no_argument, 0, 'r'},
1066 {"force-overwrite", no_argument, 0, 'f'},
1070 opt = getopt_long(argc, argv, "rf", long_options, &option_index);
1077 opt_range_check = 1;
1081 opt_force_overwrite = 1;
1085 rrd_set_error("usage rrdtool %s [--range-check|-r] "
1086 "[--force-overwrite/-f] file.xml file.rrd",
1093 if ((argc - optind) != 2) {
1094 rrd_set_error("usage rrdtool %s [--range-check/-r] "
1095 "[--force-overwrite/-f] file.xml file.rrd", argv[0]);
1099 rrd = parse_file(argv[optind]);
1103 if (write_file(argv[optind + 1], rrd) != 0) {
1110 } /* int rrd_restore */
1112 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */