1 /*****************************************************************************
2 * RRDtool 1.3rc2 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>
36 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
41 #include <libxml/parser.h>
43 #include "rrd_rpncalc.h"
44 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof ((a)[0]))
45 static int opt_range_check = 0;
46 static int opt_force_overwrite = 0;
51 static int get_string_from_node(
61 temp0 = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
63 rrd_set_error("get_string_from_node: xmlNodeListGetString failed.");
67 begin_ptr = (char *) temp0;
68 while ((begin_ptr[0] != 0) && (isspace(begin_ptr[0])))
71 if (begin_ptr[0] == 0) {
78 while ((end_ptr[0] != 0) && (!isspace(end_ptr[0])))
82 strncpy(buffer, begin_ptr, buffer_size);
83 buffer[buffer_size - 1] = 0;
88 } /* int get_string_from_node */
90 static int get_int_from_node(
99 str_ptr = (char *) xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
100 if (str_ptr == NULL) {
101 rrd_set_error("get_int_from_node: xmlNodeListGetString failed.");
106 temp = strtol(str_ptr, &end_ptr, 0);
109 if (str_ptr == end_ptr) {
110 rrd_set_error("get_int_from_node: Cannot parse buffer as int: %s",
118 } /* int get_int_from_node */
120 static int get_double_from_node(
129 str_ptr = (char *) xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
130 if (str_ptr == NULL) {
131 rrd_set_error("get_double_from_node: xmlNodeListGetString failed.");
136 temp = strtod(str_ptr, &end_ptr);
139 if (str_ptr == end_ptr) {
141 ("get_double_from_node: Cannot parse buffer as double: %s",
149 } /* int get_double_from_node */
151 static int value_check_range(
152 rrd_value_t *rrd_value,
153 const ds_def_t *ds_def)
158 if (opt_range_check == 0)
161 min = ds_def->par[DS_min_val].u_val;
162 max = ds_def->par[DS_max_val].u_val;
164 if (((!isnan(min)) && (*rrd_value < min))
165 || ((!isnan(max)) && (*rrd_value > max)))
169 } /* int value_check_range */
172 * Parse the <database> block within an RRA definition
174 static int parse_tag_rra_database_row(
178 rrd_value_t *rrd_value)
180 unsigned int values_count = 0;
185 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
186 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
187 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
189 else if (xmlStrcmp(child->name, (const xmlChar *) "v") == 0) {
190 if (values_count < rrd->stat_head->ds_cnt) {
192 get_double_from_node(doc, child,
193 rrd_value + values_count);
195 value_check_range(rrd_value + values_count,
196 rrd->ds_def + values_count);
201 rrd_set_error("parse_tag_rra_database_row: Unknown tag: %s",
208 } /* for (child = node->xmlChildrenNode) */
210 if (values_count != rrd->stat_head->ds_cnt) {
211 rrd_set_error("parse_tag_rra_database_row: Row has %u values "
212 "and RRD has %lu data sources.",
213 values_count, rrd->stat_head->ds_cnt);
218 } /* int parse_tag_rra_database_row */
220 static int parse_tag_rra_database(
225 rra_def_t *cur_rra_def;
226 unsigned int total_row_cnt;
232 for (i = 0; i < (((int) rrd->stat_head->rra_cnt) - 1); i++)
233 total_row_cnt += rrd->rra_def[i].row_cnt;
235 cur_rra_def = rrd->rra_def + i;
238 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
239 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
240 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
242 else if (xmlStrcmp(child->name, (const xmlChar *) "row") == 0) {
244 rrd_value_t *cur_rrd_value;
245 unsigned int total_values_count = rrd->stat_head->ds_cnt
246 * (total_row_cnt + 1);
248 /* Allocate space for the new values.. */
249 temp = (rrd_value_t *) realloc(rrd->rrd_value,
250 sizeof(rrd_value_t) *
253 rrd_set_error("parse_tag_rra_database: realloc failed.");
257 rrd->rrd_value = temp;
258 cur_rrd_value = rrd->rrd_value
259 + (rrd->stat_head->ds_cnt * total_row_cnt);
260 memset(cur_rrd_value, '\0',
261 sizeof(rrd_value_t) * rrd->stat_head->ds_cnt);
263 cur_rra_def->row_cnt++;
266 parse_tag_rra_database_row(doc, child, rrd, cur_rrd_value);
267 } /* if (xmlStrcmp (child->name, (const xmlChar *) "row") == 0) */
269 rrd_set_error("parse_tag_rra_database: Unknown tag: %s",
276 } /* for (child = node->xmlChildrenNode) */
279 } /* int parse_tag_rra_database */
282 * Parse the <cdp_prep> block within an RRA definition
284 static int parse_tag_rra_cdp_prep_ds_history(
287 cdp_prep_t *cdp_prep)
289 /* Make `history_buffer' the same size as the scratch area, plus the
290 * terminating NULL byte. */
291 char history_buffer[sizeof(((cdp_prep_t *)0)->scratch) + 1];
296 status = get_string_from_node(doc, node,
297 history_buffer, sizeof(history_buffer));
301 history_ptr = (char *) (&cdp_prep->scratch[0]);
302 for (i = 0; history_buffer[i] != '\0'; i++)
303 history_ptr[i] = (history_buffer[i] == '1') ? 1 : 0;
306 } /* int parse_tag_rra_cdp_prep_ds_history */
308 static int parse_tag_rra_cdp_prep_ds(
312 cdp_prep_t *cdp_prep)
317 memset(cdp_prep, '\0', sizeof(cdp_prep_t));
320 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
321 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
322 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
324 else if (xmlStrcmp(child->name, (const xmlChar *) "primary_value") ==
327 get_double_from_node(doc, child,
328 &cdp_prep->scratch[CDP_primary_val].
330 else if (xmlStrcmp(child->name, (const xmlChar *) "secondary_value")
333 get_double_from_node(doc, child,
334 &cdp_prep->scratch[CDP_secondary_val].
336 else if (xmlStrcmp(child->name, (const xmlChar *) "intercept") == 0)
337 status = get_double_from_node(doc, child,
339 scratch[CDP_hw_intercept].u_val);
340 else if (xmlStrcmp(child->name, (const xmlChar *) "last_intercept") ==
343 get_double_from_node(doc, child,
345 scratch[CDP_hw_last_intercept].u_val);
346 else if (xmlStrcmp(child->name, (const xmlChar *) "slope") == 0)
347 status = get_double_from_node(doc, child,
348 &cdp_prep->scratch[CDP_hw_slope].
350 else if (xmlStrcmp(child->name, (const xmlChar *) "last_slope") == 0)
351 status = get_double_from_node(doc, child,
353 scratch[CDP_hw_last_slope].u_val);
354 else if (xmlStrcmp(child->name, (const xmlChar *) "nan_count") == 0)
355 status = get_int_from_node(doc, child,
357 scratch[CDP_null_count].u_cnt);
358 else if (xmlStrcmp(child->name, (const xmlChar *) "last_nan_count") ==
361 get_int_from_node(doc, child,
363 scratch[CDP_last_null_count].u_cnt);
364 else if (xmlStrcmp(child->name, (const xmlChar *) "seasonal") == 0)
365 status = get_double_from_node(doc, child,
366 &cdp_prep->scratch[CDP_hw_seasonal].
368 else if (xmlStrcmp(child->name, (const xmlChar *) "last_seasonal") ==
371 get_double_from_node(doc, child,
372 &cdp_prep->scratch[CDP_hw_last_seasonal].
374 else if (xmlStrcmp(child->name, (const xmlChar *) "init_flag") == 0)
375 status = get_int_from_node(doc, child,
377 scratch[CDP_init_seasonal].u_cnt);
378 else if (xmlStrcmp(child->name, (const xmlChar *) "history") == 0)
379 status = parse_tag_rra_cdp_prep_ds_history(doc, child, cdp_prep);
380 else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0)
381 status = get_double_from_node(doc, child,
382 &cdp_prep->scratch[CDP_val].u_val);
383 else if (xmlStrcmp(child->name,
384 (const xmlChar *) "unknown_datapoints") == 0)
385 status = get_int_from_node(doc, child,
387 scratch[CDP_unkn_pdp_cnt].u_cnt);
389 * Compatibility code for 1.0.49
391 else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0) { /* {{{ */
393 rra_def_t *rra_def = rrd->rra_def + (rrd->stat_head->rra_cnt - 1);
396 if (i >= ARRAY_LENGTH(cdp_prep->scratch)) {
401 if ((cf_conv(rra_def->cf_nam) == CF_FAILURES)
402 || (i == CDP_unkn_pdp_cnt)
403 || (i == CDP_null_count)
404 || (i == CDP_last_null_count))
405 status = get_int_from_node(doc, child,
406 (int *) &cdp_prep->scratch[i].
409 status = get_double_from_node(doc, child,
410 &cdp_prep->scratch[i].
416 /* When this loops exits (sucessfully) `child' points to the last
417 * `value' tag in the list. */
418 if ((child->next == NULL)
419 || (xmlStrcmp(child->name, (const xmlChar *) "value") !=
428 rrd_set_error("parse_tag_rra_cdp_prep: Unknown tag: %s",
438 } /* int parse_tag_rra_cdp_prep_ds */
440 static int parse_tag_rra_cdp_prep(
444 cdp_prep_t *cdp_prep)
449 unsigned int ds_count = 0;
452 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
453 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
454 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
456 else if (xmlStrcmp(child->name, (const xmlChar *) "ds") == 0) {
457 if (ds_count >= rrd->stat_head->ds_cnt)
460 status = parse_tag_rra_cdp_prep_ds(doc, child, rrd,
461 cdp_prep + ds_count);
465 rrd_set_error("parse_tag_rra_cdp_prep: Unknown tag: %s",
474 if (ds_count != rrd->stat_head->ds_cnt) {
475 rrd_set_error("parse_tag_rra_cdp_prep: There are %i data sources in "
476 "the RRD file, but %i in this cdp_prep block!",
477 (int) rrd->stat_head->ds_cnt, ds_count);
482 } /* int parse_tag_rra_cdp_prep */
485 * Parse the <params> block within an RRA definition
487 static int parse_tag_rra_params(
496 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
497 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
498 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
501 * Parameters for CF_HWPREDICT
503 else if (xmlStrcmp(child->name, (const xmlChar *) "hw_alpha") == 0)
504 status = get_double_from_node(doc, child,
505 &rra_def->par[RRA_hw_alpha].u_val);
506 else if (xmlStrcmp(child->name, (const xmlChar *) "hw_beta") == 0)
507 status = get_double_from_node(doc, child,
508 &rra_def->par[RRA_hw_beta].u_val);
509 else if (xmlStrcmp(child->name,
510 (const xmlChar *) "dependent_rra_idx") == 0)
511 status = get_int_from_node(doc, child,
513 par[RRA_dependent_rra_idx].u_cnt);
515 * Parameters for CF_SEASONAL and CF_DEVSEASONAL
517 else if (xmlStrcmp(child->name, (const xmlChar *) "seasonal_gamma") ==
520 get_double_from_node(doc, child,
521 &rra_def->par[RRA_seasonal_gamma].u_val);
523 (child->name, (const xmlChar *) "seasonal_smooth_idx") == 0)
525 get_int_from_node(doc, child,
527 par[RRA_seasonal_smooth_idx].u_cnt);
528 else if (xmlStrcmp(child->name, (const xmlChar *) "smoothing_window")
531 get_double_from_node(doc, child,
533 par[RRA_seasonal_smoothing_window].
535 /* else if (dependent_rra_idx) ...; */
537 * Parameters for CF_FAILURES
539 else if (xmlStrcmp(child->name, (const xmlChar *) "delta_pos") == 0)
540 status = get_double_from_node(doc, child,
541 &rra_def->par[RRA_delta_pos].u_val);
542 else if (xmlStrcmp(child->name, (const xmlChar *) "delta_neg") == 0)
543 status = get_double_from_node(doc, child,
544 &rra_def->par[RRA_delta_neg].u_val);
545 else if (xmlStrcmp(child->name, (const xmlChar *) "window_len") == 0)
546 status = get_int_from_node(doc, child,
547 (int *) &rra_def->par[RRA_window_len].
549 else if (xmlStrcmp(child->name, (const xmlChar *) "failure_threshold")
552 get_int_from_node(doc, child,
554 par[RRA_failure_threshold].u_cnt);
556 * Parameters for CF_AVERAGE, CF_MAXIMUM, CF_MINIMUM, and CF_LAST
558 else if (xmlStrcmp(child->name, (const xmlChar *) "xff") == 0)
559 status = get_double_from_node(doc, child,
560 &rra_def->par[RRA_cdp_xff_val].
563 * Compatibility code for 1.0.49
565 else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0) { /* {{{ */
569 if (i >= ARRAY_LENGTH(rra_def->par)) {
574 if ((i == RRA_dependent_rra_idx)
575 || (i == RRA_seasonal_smooth_idx)
576 || (i == RRA_failure_threshold))
577 status = get_int_from_node(doc, child,
578 (int *) &rra_def->par[i].
581 status = get_double_from_node(doc, child,
582 &rra_def->par[i].u_val);
587 /* When this loops exits (sucessfully) `child' points to the last
588 * `value' tag in the list. */
589 if ((child->next == NULL)
590 || (xmlStrcmp(child->name, (const xmlChar *) "value") !=
599 rrd_set_error("parse_tag_rra_params: Unknown tag: %s",
609 } /* int parse_tag_rra_params */
612 * Parse an RRA definition
614 static int parse_tag_rra_cf(
621 status = get_string_from_node(doc, node,
622 rra_def->cf_nam, sizeof(rra_def->cf_nam));
626 status = cf_conv(rra_def->cf_nam);
628 rrd_set_error("parse_tag_rra_cf: Unknown consolidation function: %s",
634 } /* int parse_tag_rra_cf */
636 static int parse_tag_rra(
644 rra_def_t *cur_rra_def;
645 cdp_prep_t *cur_cdp_prep;
646 rra_ptr_t *cur_rra_ptr;
648 /* Allocate more rra_def space for this RRA */
652 temp = (rra_def_t *) realloc(rrd->rra_def,
654 (rrd->stat_head->rra_cnt + 1));
656 rrd_set_error("parse_tag_rra: realloc failed.");
660 cur_rra_def = rrd->rra_def + rrd->stat_head->rra_cnt;
661 memset(cur_rra_def, '\0', sizeof(rra_def_t));
664 /* allocate cdp_prep_t */
668 temp = (cdp_prep_t *) realloc(rrd->cdp_prep, sizeof(cdp_prep_t)
669 * rrd->stat_head->ds_cnt
670 * (rrd->stat_head->rra_cnt + 1));
672 rrd_set_error("parse_tag_rra: realloc failed.");
675 rrd->cdp_prep = temp;
676 cur_cdp_prep = rrd->cdp_prep
677 + (rrd->stat_head->ds_cnt * rrd->stat_head->rra_cnt);
678 memset(cur_cdp_prep, '\0',
679 sizeof(cdp_prep_t) * rrd->stat_head->ds_cnt);
682 /* allocate rra_ptr_t */
686 temp = (rra_ptr_t *) realloc(rrd->rra_ptr,
688 (rrd->stat_head->rra_cnt + 1));
690 rrd_set_error("parse_tag_rra: realloc failed.");
694 cur_rra_ptr = rrd->rra_ptr + rrd->stat_head->rra_cnt;
695 memset(cur_rra_ptr, '\0', sizeof(rra_ptr_t));
698 /* All space successfully allocated, increment number of RRAs. */
699 rrd->stat_head->rra_cnt++;
702 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
703 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
704 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
706 else if (xmlStrcmp(child->name, (const xmlChar *) "cf") == 0)
707 status = parse_tag_rra_cf(doc, child, cur_rra_def);
708 else if (xmlStrcmp(child->name, (const xmlChar *) "pdp_per_row") == 0)
709 status = get_int_from_node(doc, child,
710 (int *) &cur_rra_def->pdp_cnt);
711 else if (xmlStrcmp(child->name, (const xmlChar *) "params") == 0)
712 status = parse_tag_rra_params(doc, child, cur_rra_def);
713 else if (xmlStrcmp(child->name, (const xmlChar *) "cdp_prep") == 0)
714 status = parse_tag_rra_cdp_prep(doc, child, rrd, cur_cdp_prep);
715 else if (xmlStrcmp(child->name, (const xmlChar *) "database") == 0)
716 status = parse_tag_rra_database(doc, child, rrd);
718 rrd_set_error("parse_tag_rra: Unknown tag: %s", child->name);
726 /* Set the RRA pointer to the last value in the archive */
727 cur_rra_ptr->cur_row = cur_rra_def->row_cnt - 1;
730 } /* int parse_tag_rra */
733 * Parse a DS definition
735 static int parse_tag_ds_cdef(
743 status = get_string_from_node(doc, node, buffer, sizeof(buffer));
747 /* We're always working on the last DS that has been added to the structure
748 * when we get here */
749 parseCDEF_DS(buffer, rrd, rrd->stat_head->ds_cnt - 1);
752 } /* int parse_tag_ds_cdef */
754 static int parse_tag_ds_type(
761 status = get_string_from_node(doc, node,
762 ds_def->dst, sizeof(ds_def->dst));
766 status = dst_conv(ds_def->dst);
768 rrd_set_error("parse_tag_ds_type: Unknown data source type: %s",
774 } /* int parse_tag_ds_type */
776 static int parse_tag_ds(
784 ds_def_t *cur_ds_def;
785 pdp_prep_t *cur_pdp_prep;
788 * If there are DS definitions after RRA definitions the number of values,
789 * cdp_prep areas and so on will be calculated wrong. Thus, enforce a
790 * specific order in this case.
792 if (rrd->stat_head->rra_cnt > 0) {
793 rrd_set_error("parse_tag_ds: All data source definitions MUST "
794 "precede the RRA definitions!");
798 /* Allocate space for the new DS definition */
802 temp = (ds_def_t *) realloc(rrd->ds_def,
804 (rrd->stat_head->ds_cnt + 1));
806 rrd_set_error("parse_tag_ds: malloc failed.");
810 cur_ds_def = rrd->ds_def + rrd->stat_head->ds_cnt;
811 memset(cur_ds_def, '\0', sizeof(ds_def_t));
814 /* Allocate pdp_prep space for the new DS definition */
818 temp = (pdp_prep_t *) realloc(rrd->pdp_prep,
820 (rrd->stat_head->ds_cnt + 1));
822 rrd_set_error("parse_tag_ds: malloc failed.");
825 rrd->pdp_prep = temp;
826 cur_pdp_prep = rrd->pdp_prep + rrd->stat_head->ds_cnt;
827 memset(cur_pdp_prep, '\0', sizeof(pdp_prep_t));
830 /* All allocations successful, let's increment the number of DSes. */
831 rrd->stat_head->ds_cnt++;
834 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
835 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
836 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
838 else if (xmlStrcmp(child->name, (const xmlChar *) "name") == 0)
839 status = get_string_from_node(doc, child,
841 sizeof(cur_ds_def->ds_nam));
842 else if (xmlStrcmp(child->name, (const xmlChar *) "type") == 0)
843 status = parse_tag_ds_type(doc, child, cur_ds_def);
844 else if (xmlStrcmp(child->name,
845 (const xmlChar *) "minimal_heartbeat") == 0)
846 status = get_int_from_node(doc, child,
847 (int *) &cur_ds_def->par[DS_mrhb_cnt].
849 else if (xmlStrcmp(child->name, (const xmlChar *) "min") == 0)
850 status = get_double_from_node(doc, child,
851 &cur_ds_def->par[DS_min_val].u_val);
852 else if (xmlStrcmp(child->name, (const xmlChar *) "max") == 0)
853 status = get_double_from_node(doc, child,
854 &cur_ds_def->par[DS_max_val].u_val);
855 else if (xmlStrcmp(child->name, (const xmlChar *) "cdef") == 0)
856 status = parse_tag_ds_cdef(doc, child, rrd);
857 else if (xmlStrcmp(child->name, (const xmlChar *) "last_ds") == 0)
858 status = get_string_from_node(doc, child,
859 cur_pdp_prep->last_ds,
860 sizeof(cur_pdp_prep->last_ds));
861 else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0)
862 status = get_double_from_node(doc, child,
863 &cur_pdp_prep->scratch[PDP_val].
865 else if (xmlStrcmp(child->name, (const xmlChar *) "unknown_sec") == 0)
866 status = get_int_from_node(doc, child,
867 (int *) &cur_pdp_prep->
868 scratch[PDP_unkn_sec_cnt].u_cnt);
870 rrd_set_error("parse_tag_ds: Unknown tag: %s", child->name);
879 } /* int parse_tag_ds */
884 static int parse_tag_rrd(
893 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
894 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
895 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
897 else if (xmlStrcmp(child->name, (const xmlChar *) "version") == 0)
898 status = get_string_from_node(doc, child,
899 rrd->stat_head->version,
900 sizeof(rrd->stat_head->version));
901 else if (xmlStrcmp(child->name, (const xmlChar *) "step") == 0)
902 status = get_int_from_node(doc, child,
903 (int *) &rrd->stat_head->pdp_step);
904 else if (xmlStrcmp(child->name, (const xmlChar *) "lastupdate") == 0)
905 status = get_int_from_node(doc, child,
906 (int *) &rrd->live_head->last_up);
907 else if (xmlStrcmp(child->name, (const xmlChar *) "ds") == 0)
908 status = parse_tag_ds(doc, child, rrd);
909 else if (xmlStrcmp(child->name, (const xmlChar *) "rra") == 0)
910 status = parse_tag_rra(doc, child, rrd);
912 rrd_set_error("parse_tag_rrd: Unknown tag: %s", child->name);
921 } /* int parse_tag_rrd */
923 static rrd_t *parse_file(
924 const char *filename)
932 doc = xmlParseFile(filename);
934 rrd_set_error("Document not parsed successfully.");
938 cur = xmlDocGetRootElement(doc);
940 rrd_set_error("Document is empty.");
945 if (xmlStrcmp(cur->name, (const xmlChar *) "rrd") != 0) {
947 ("Document of the wrong type, root node is not \"rrd\".");
952 rrd = (rrd_t *) malloc(sizeof(rrd_t));
954 rrd_set_error("parse_file: malloc failed.");
958 memset(rrd, '\0', sizeof(rrd_t));
960 rrd->stat_head = (stat_head_t *) malloc(sizeof(stat_head_t));
961 if (rrd->stat_head == NULL) {
962 rrd_set_error("parse_tag_rrd: malloc failed.");
967 memset(rrd->stat_head, '\0', sizeof(stat_head_t));
969 strncpy(rrd->stat_head->cookie, "RRD", sizeof(rrd->stat_head->cookie));
970 rrd->stat_head->float_cookie = FLOAT_COOKIE;
972 rrd->live_head = (live_head_t *) malloc(sizeof(live_head_t));
973 if (rrd->live_head == NULL) {
974 rrd_set_error("parse_tag_rrd: malloc failed.");
976 free(rrd->stat_head);
980 memset(rrd->live_head, '\0', sizeof(live_head_t));
982 status = parse_tag_rrd(doc, cur, rrd);
991 } /* rrd_t *parse_file */
993 static int write_file(
994 const char *file_name,
999 unsigned int value_count;
1001 if (strcmp("-", file_name) == 0)
1004 int fd_flags = O_WRONLY | O_CREAT;
1007 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
1008 fd_flags |= O_BINARY;
1011 if (opt_force_overwrite == 0)
1014 fd = open(file_name, fd_flags, 0666);
1016 rrd_set_error("creating '%s': %s", file_name,
1017 rrd_strerror(errno));
1021 fh = fdopen(fd, "wb");
1023 rrd_set_error("fdopen failed: %s", rrd_strerror(errno));
1029 fwrite(rrd->stat_head, sizeof(stat_head_t), 1, fh);
1030 fwrite(rrd->ds_def, sizeof(ds_def_t), rrd->stat_head->ds_cnt, fh);
1031 fwrite(rrd->rra_def, sizeof(rra_def_t), rrd->stat_head->rra_cnt, fh);
1032 fwrite(rrd->live_head, sizeof(live_head_t), 1, fh);
1033 fwrite(rrd->pdp_prep, sizeof(pdp_prep_t), rrd->stat_head->ds_cnt, fh);
1034 fwrite(rrd->cdp_prep, sizeof(cdp_prep_t),
1035 rrd->stat_head->rra_cnt * rrd->stat_head->ds_cnt, fh);
1036 fwrite(rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt, fh);
1038 /* calculate the number of rrd_values to dump */
1040 for (i = 0; i < rrd->stat_head->rra_cnt; i++)
1041 value_count += (rrd->rra_def[i].row_cnt * rrd->stat_head->ds_cnt);
1043 fwrite(rrd->rrd_value, sizeof(rrd_value_t), value_count, fh);
1045 /* lets see if we had an error */
1047 rrd_set_error("a file error occurred while creating '%s'", file_name);
1054 } /* int write_file */
1062 /* init rrd clean */
1064 opterr = 0; /* initialize getopt */
1067 int option_index = 0;
1068 static struct option long_options[] = {
1069 {"range-check", no_argument, 0, 'r'},
1070 {"force-overwrite", no_argument, 0, 'f'},
1074 opt = getopt_long(argc, argv, "rf", long_options, &option_index);
1081 opt_range_check = 1;
1085 opt_force_overwrite = 1;
1089 rrd_set_error("usage rrdtool %s [--range-check|-r] "
1090 "[--force-overwrite/-f] file.xml file.rrd",
1097 if ((argc - optind) != 2) {
1098 rrd_set_error("usage rrdtool %s [--range-check/-r] "
1099 "[--force-overwrite/-f] file.xml file.rrd", argv[0]);
1103 rrd = parse_file(argv[optind]);
1107 if (write_file(argv[optind + 1], rrd) != 0) {
1114 } /* int rrd_restore */
1116 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */