1 /*****************************************************************************
2 * RRDtool 1.2.99907080300 Copyright by Tobi Oetiker, 1997-2007
3 * This file: Copyright 2008 Florian octo Forster
4 * Distributed under the GPL
5 *****************************************************************************
6 * rrd_thread_safe.c Contains routines used when thread safety is required
7 *****************************************************************************
9 *************************************************************************** */
10 * *This program is free software;
11 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;
15 either version 2 of the License, or(
18 * *This program is distributed in the hope that it will be useful,
19 but WITHOUT * ANY WARRANTY;
20 without even the implied warranty of MERCHANTABILITY or
22 FITNESS FOR A PARTICULAR PURPOSE.
23 See the GNU General Public License for *more details. *
24 *You should have received a copy of the GNU General Public License
25 along * with this program; if not
26 , write to the Free Software Foundation, Inc., *51 Franklin St, Fifth Floor, Boston, MA 02110 - 1301 USA * *Authors:
27 *Florian octo Forster < octo at verplant.org > **/
34 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
39 #include <libxml/parser.h>
41 #include "rrd_rpncalc.h"
42 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof ((a)[0]))
43 static int opt_range_check = 0;
44 static int opt_force_overwrite = 0;
49 static int get_string_from_node(
59 temp0 = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
61 rrd_set_error("get_string_from_node: xmlNodeListGetString failed.");
65 begin_ptr = (char *) temp0;
66 while ((begin_ptr[0] != 0) && (isspace(begin_ptr[0])))
69 if (begin_ptr[0] == 0) {
76 while ((end_ptr[0] != 0) && (!isspace(end_ptr[0])))
80 strncpy(buffer, begin_ptr, buffer_size);
81 buffer[buffer_size - 1] = 0;
86 } /* int get_string_from_node */
88 static int get_int_from_node(
97 str_ptr = (char *) xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
98 if (str_ptr == NULL) {
99 rrd_set_error("get_int_from_node: xmlNodeListGetString failed.");
104 temp = strtol(str_ptr, &end_ptr, 0);
107 if (str_ptr == end_ptr) {
108 rrd_set_error("get_int_from_node: Cannot parse buffer as int: %s",
116 } /* int get_int_from_node */
118 static int get_double_from_node(
127 str_ptr = (char *) xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
128 if (str_ptr == NULL) {
129 rrd_set_error("get_double_from_node: xmlNodeListGetString failed.");
134 temp = strtod(str_ptr, &end_ptr);
137 if (str_ptr == end_ptr) {
139 ("get_double_from_node: Cannot parse buffer as double: %s",
147 } /* int get_double_from_node */
149 static int value_check_range(
150 rrd_value_t *rrd_value,
151 const ds_def_t *ds_def)
156 if (opt_range_check == 0)
159 min = ds_def->par[DS_min_val].u_val;
160 max = ds_def->par[DS_max_val].u_val;
162 if (((!isnan(min)) && (*rrd_value < min))
163 || ((!isnan(max)) && (*rrd_value > max)))
167 } /* int value_check_range */
170 * Parse the <database> block within an RRA definition
172 static int parse_tag_rra_database_row(
176 rrd_value_t *rrd_value)
178 unsigned int values_count = 0;
183 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
184 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
185 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
187 else if (xmlStrcmp(child->name, (const xmlChar *) "v") == 0) {
188 if (values_count < rrd->stat_head->ds_cnt) {
190 get_double_from_node(doc, child,
191 rrd_value + values_count);
193 value_check_range(rrd_value + values_count,
194 rrd->ds_def + values_count);
199 rrd_set_error("parse_tag_rra_database_row: Unknown tag: %s",
206 } /* for (child = node->xmlChildrenNode) */
208 if (values_count != rrd->stat_head->ds_cnt) {
209 rrd_set_error("parse_tag_rra_database_row: Row has %u values "
210 "and RRD has %lu data sources.",
211 values_count, rrd->stat_head->ds_cnt);
216 } /* int parse_tag_rra_database_row */
218 static int parse_tag_rra_database(
223 rra_def_t *cur_rra_def;
224 unsigned int total_row_cnt;
230 for (i = 0; i < (((int) rrd->stat_head->rra_cnt) - 1); i++)
231 total_row_cnt += rrd->rra_def[i].row_cnt;
233 cur_rra_def = rrd->rra_def + i;
236 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
237 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
238 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
240 else if (xmlStrcmp(child->name, (const xmlChar *) "row") == 0) {
242 rrd_value_t *cur_rrd_value;
243 unsigned int total_values_count = rrd->stat_head->ds_cnt
244 * (total_row_cnt + 1);
246 /* Allocate space for the new values.. */
247 temp = (rrd_value_t *) realloc(rrd->rrd_value,
248 sizeof(rrd_value_t) *
251 rrd_set_error("parse_tag_rra_database: realloc failed.");
255 rrd->rrd_value = temp;
256 cur_rrd_value = rrd->rrd_value
257 + (rrd->stat_head->ds_cnt * total_row_cnt);
258 memset(cur_rrd_value, '\0',
259 sizeof(rrd_value_t) * rrd->stat_head->ds_cnt);
261 cur_rra_def->row_cnt++;
264 parse_tag_rra_database_row(doc, child, rrd, cur_rrd_value);
265 } /* if (xmlStrcmp (child->name, (const xmlChar *) "row") == 0) */
267 rrd_set_error("parse_tag_rra_database: Unknown tag: %s",
274 } /* for (child = node->xmlChildrenNode) */
277 } /* int parse_tag_rra_database */
280 * Parse the <cdp_prep> block within an RRA definition
282 static int parse_tag_rra_cdp_prep_ds_history(
285 cdp_prep_t *cdp_prep)
287 /* Make `history_buffer' the same size as the scratch area, plus the
288 * terminating NULL byte. */
289 char history_buffer[sizeof(((cdp_prep_t *)0)->scratch) + 1];
294 status = get_string_from_node(doc, node,
295 history_buffer, sizeof(history_buffer));
299 history_ptr = (char *) (&cdp_prep->scratch[0]);
300 for (i = 0; history_buffer[i] != '\0'; i++)
301 history_ptr[i] = (history_buffer[i] == '1') ? 1 : 0;
304 } /* int parse_tag_rra_cdp_prep_ds_history */
306 static int parse_tag_rra_cdp_prep_ds(
310 cdp_prep_t *cdp_prep)
315 memset(cdp_prep, '\0', sizeof(cdp_prep_t));
318 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
319 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
320 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
322 else if (xmlStrcmp(child->name, (const xmlChar *) "primary_value") ==
325 get_double_from_node(doc, child,
326 &cdp_prep->scratch[CDP_primary_val].
328 else if (xmlStrcmp(child->name, (const xmlChar *) "secondary_value")
331 get_double_from_node(doc, child,
332 &cdp_prep->scratch[CDP_secondary_val].
334 else if (xmlStrcmp(child->name, (const xmlChar *) "intercept") == 0)
335 status = get_double_from_node(doc, child,
337 scratch[CDP_hw_intercept].u_val);
338 else if (xmlStrcmp(child->name, (const xmlChar *) "last_intercept") ==
341 get_double_from_node(doc, child,
343 scratch[CDP_hw_last_intercept].u_val);
344 else if (xmlStrcmp(child->name, (const xmlChar *) "slope") == 0)
345 status = get_double_from_node(doc, child,
346 &cdp_prep->scratch[CDP_hw_slope].
348 else if (xmlStrcmp(child->name, (const xmlChar *) "last_slope") == 0)
349 status = get_double_from_node(doc, child,
351 scratch[CDP_hw_last_slope].u_val);
352 else if (xmlStrcmp(child->name, (const xmlChar *) "nan_count") == 0)
353 status = get_int_from_node(doc, child,
355 scratch[CDP_null_count].u_cnt);
356 else if (xmlStrcmp(child->name, (const xmlChar *) "last_nan_count") ==
359 get_int_from_node(doc, child,
361 scratch[CDP_last_null_count].u_cnt);
362 else if (xmlStrcmp(child->name, (const xmlChar *) "seasonal") == 0)
363 status = get_double_from_node(doc, child,
364 &cdp_prep->scratch[CDP_hw_seasonal].
366 else if (xmlStrcmp(child->name, (const xmlChar *) "last_seasonal") ==
369 get_double_from_node(doc, child,
370 &cdp_prep->scratch[CDP_hw_last_seasonal].
372 else if (xmlStrcmp(child->name, (const xmlChar *) "init_flag") == 0)
373 status = get_int_from_node(doc, child,
375 scratch[CDP_init_seasonal].u_cnt);
376 else if (xmlStrcmp(child->name, (const xmlChar *) "history") == 0)
377 status = parse_tag_rra_cdp_prep_ds_history(doc, child, cdp_prep);
378 else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0)
379 status = get_double_from_node(doc, child,
380 &cdp_prep->scratch[CDP_val].u_val);
381 else if (xmlStrcmp(child->name,
382 (const xmlChar *) "unknown_datapoints") == 0)
383 status = get_int_from_node(doc, child,
385 scratch[CDP_unkn_pdp_cnt].u_cnt);
387 * Compatibility code for 1.0.49
389 else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0) { /* {{{ */
391 rra_def_t *rra_def = rrd->rra_def + (rrd->stat_head->rra_cnt - 1);
394 if (i >= ARRAY_LENGTH(cdp_prep->scratch)) {
399 if ((cf_conv(rra_def->cf_nam) == CF_FAILURES)
400 || (i == CDP_unkn_pdp_cnt)
401 || (i == CDP_null_count)
402 || (i == CDP_last_null_count))
403 status = get_int_from_node(doc, child,
404 (int *) &cdp_prep->scratch[i].
407 status = get_double_from_node(doc, child,
408 &cdp_prep->scratch[i].
414 /* When this loops exits (sucessfully) `child' points to the last
415 * `value' tag in the list. */
416 if ((child->next == NULL)
417 || (xmlStrcmp(child->name, (const xmlChar *) "value") !=
426 rrd_set_error("parse_tag_rra_cdp_prep: Unknown tag: %s",
436 } /* int parse_tag_rra_cdp_prep_ds */
438 static int parse_tag_rra_cdp_prep(
442 cdp_prep_t *cdp_prep)
447 unsigned int ds_count = 0;
450 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
451 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
452 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
454 else if (xmlStrcmp(child->name, (const xmlChar *) "ds") == 0) {
455 if (ds_count >= rrd->stat_head->ds_cnt)
458 status = parse_tag_rra_cdp_prep_ds(doc, child, rrd,
459 cdp_prep + ds_count);
463 rrd_set_error("parse_tag_rra_cdp_prep: Unknown tag: %s",
472 if (ds_count != rrd->stat_head->ds_cnt) {
473 rrd_set_error("parse_tag_rra_cdp_prep: There are %i data sources in "
474 "the RRD file, but %i in this cdp_prep block!",
475 (int) rrd->stat_head->ds_cnt, ds_count);
480 } /* int parse_tag_rra_cdp_prep */
483 * Parse the <params> block within an RRA definition
485 static int parse_tag_rra_params(
494 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
495 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
496 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
499 * Parameters for CF_HWPREDICT
501 else if (xmlStrcmp(child->name, (const xmlChar *) "hw_alpha") == 0)
502 status = get_double_from_node(doc, child,
503 &rra_def->par[RRA_hw_alpha].u_val);
504 else if (xmlStrcmp(child->name, (const xmlChar *) "hw_beta") == 0)
505 status = get_double_from_node(doc, child,
506 &rra_def->par[RRA_hw_beta].u_val);
507 else if (xmlStrcmp(child->name,
508 (const xmlChar *) "dependent_rra_idx") == 0)
509 status = get_int_from_node(doc, child,
511 par[RRA_dependent_rra_idx].u_cnt);
513 * Parameters for CF_SEASONAL and CF_DEVSEASONAL
515 else if (xmlStrcmp(child->name, (const xmlChar *) "seasonal_gamma") ==
518 get_double_from_node(doc, child,
519 &rra_def->par[RRA_seasonal_gamma].u_val);
521 (child->name, (const xmlChar *) "seasonal_smooth_idx") == 0)
523 get_int_from_node(doc, child,
525 par[RRA_seasonal_smooth_idx].u_cnt);
526 else if (xmlStrcmp(child->name, (const xmlChar *) "smoothing_window")
529 get_double_from_node(doc, child,
531 par[RRA_seasonal_smoothing_window].
533 /* else if (dependent_rra_idx) ...; */
535 * Parameters for CF_FAILURES
537 else if (xmlStrcmp(child->name, (const xmlChar *) "delta_pos") == 0)
538 status = get_double_from_node(doc, child,
539 &rra_def->par[RRA_delta_pos].u_val);
540 else if (xmlStrcmp(child->name, (const xmlChar *) "delta_neg") == 0)
541 status = get_double_from_node(doc, child,
542 &rra_def->par[RRA_delta_neg].u_val);
543 else if (xmlStrcmp(child->name, (const xmlChar *) "window_len") == 0)
544 status = get_int_from_node(doc, child,
545 (int *) &rra_def->par[RRA_window_len].
547 else if (xmlStrcmp(child->name, (const xmlChar *) "failure_threshold")
550 get_int_from_node(doc, child,
552 par[RRA_failure_threshold].u_cnt);
554 * Parameters for CF_AVERAGE, CF_MAXIMUM, CF_MINIMUM, and CF_LAST
556 else if (xmlStrcmp(child->name, (const xmlChar *) "xff") == 0)
557 status = get_double_from_node(doc, child,
558 &rra_def->par[RRA_cdp_xff_val].
561 * Compatibility code for 1.0.49
563 else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0) { /* {{{ */
567 if (i >= ARRAY_LENGTH(rra_def->par)) {
572 if ((i == RRA_dependent_rra_idx)
573 || (i == RRA_seasonal_smooth_idx)
574 || (i == RRA_failure_threshold))
575 status = get_int_from_node(doc, child,
576 (int *) &rra_def->par[i].
579 status = get_double_from_node(doc, child,
580 &rra_def->par[i].u_val);
585 /* When this loops exits (sucessfully) `child' points to the last
586 * `value' tag in the list. */
587 if ((child->next == NULL)
588 || (xmlStrcmp(child->name, (const xmlChar *) "value") !=
597 rrd_set_error("parse_tag_rra_params: Unknown tag: %s",
607 } /* int parse_tag_rra_params */
610 * Parse an RRA definition
612 static int parse_tag_rra_cf(
619 status = get_string_from_node(doc, node,
620 rra_def->cf_nam, sizeof(rra_def->cf_nam));
624 status = cf_conv(rra_def->cf_nam);
626 rrd_set_error("parse_tag_rra_cf: Unknown consolidation function: %s",
632 } /* int parse_tag_rra_cf */
634 static int parse_tag_rra(
642 rra_def_t *cur_rra_def;
643 cdp_prep_t *cur_cdp_prep;
644 rra_ptr_t *cur_rra_ptr;
646 /* Allocate more rra_def space for this RRA */
650 temp = (rra_def_t *) realloc(rrd->rra_def,
652 (rrd->stat_head->rra_cnt + 1));
654 rrd_set_error("parse_tag_rra: realloc failed.");
658 cur_rra_def = rrd->rra_def + rrd->stat_head->rra_cnt;
659 memset(cur_rra_def, '\0', sizeof(rra_def_t));
662 /* allocate cdp_prep_t */
666 temp = (cdp_prep_t *) realloc(rrd->cdp_prep, sizeof(cdp_prep_t)
667 * rrd->stat_head->ds_cnt
668 * (rrd->stat_head->rra_cnt + 1));
670 rrd_set_error("parse_tag_rra: realloc failed.");
673 rrd->cdp_prep = temp;
674 cur_cdp_prep = rrd->cdp_prep
675 + (rrd->stat_head->ds_cnt * rrd->stat_head->rra_cnt);
676 memset(cur_cdp_prep, '\0',
677 sizeof(cdp_prep_t) * rrd->stat_head->ds_cnt);
680 /* allocate rra_ptr_t */
684 temp = (rra_ptr_t *) realloc(rrd->rra_ptr,
686 (rrd->stat_head->rra_cnt + 1));
688 rrd_set_error("parse_tag_rra: realloc failed.");
692 cur_rra_ptr = rrd->rra_ptr + rrd->stat_head->rra_cnt;
693 memset(cur_rra_ptr, '\0', sizeof(rra_ptr_t));
696 /* All space successfully allocated, increment number of RRAs. */
697 rrd->stat_head->rra_cnt++;
700 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
701 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
702 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
704 else if (xmlStrcmp(child->name, (const xmlChar *) "cf") == 0)
705 status = parse_tag_rra_cf(doc, child, cur_rra_def);
706 else if (xmlStrcmp(child->name, (const xmlChar *) "pdp_per_row") == 0)
707 status = get_int_from_node(doc, child,
708 (int *) &cur_rra_def->pdp_cnt);
709 else if (xmlStrcmp(child->name, (const xmlChar *) "params") == 0)
710 status = parse_tag_rra_params(doc, child, cur_rra_def);
711 else if (xmlStrcmp(child->name, (const xmlChar *) "cdp_prep") == 0)
712 status = parse_tag_rra_cdp_prep(doc, child, rrd, cur_cdp_prep);
713 else if (xmlStrcmp(child->name, (const xmlChar *) "database") == 0)
714 status = parse_tag_rra_database(doc, child, rrd);
716 rrd_set_error("parse_tag_rra: Unknown tag: %s", child->name);
724 /* Set the RRA pointer to the last value in the archive */
725 cur_rra_ptr->cur_row = cur_rra_def->row_cnt - 1;
728 } /* int parse_tag_rra */
731 * Parse a DS definition
733 static int parse_tag_ds_cdef(
741 status = get_string_from_node(doc, node, buffer, sizeof(buffer));
745 /* We're always working on the last DS that has been added to the structure
746 * when we get here */
747 parseCDEF_DS(buffer, rrd, rrd->stat_head->ds_cnt - 1);
750 } /* int parse_tag_ds_cdef */
752 static int parse_tag_ds_type(
759 status = get_string_from_node(doc, node,
760 ds_def->dst, sizeof(ds_def->dst));
764 status = dst_conv(ds_def->dst);
766 rrd_set_error("parse_tag_ds_type: Unknown data source type: %s",
772 } /* int parse_tag_ds_type */
774 static int parse_tag_ds(
782 ds_def_t *cur_ds_def;
783 pdp_prep_t *cur_pdp_prep;
786 * If there are DS definitions after RRA definitions the number of values,
787 * cdp_prep areas and so on will be calculated wrong. Thus, enforce a
788 * specific order in this case.
790 if (rrd->stat_head->rra_cnt > 0) {
791 rrd_set_error("parse_tag_ds: All data source definitions MUST "
792 "precede the RRA definitions!");
796 /* Allocate space for the new DS definition */
800 temp = (ds_def_t *) realloc(rrd->ds_def,
802 (rrd->stat_head->ds_cnt + 1));
804 rrd_set_error("parse_tag_ds: malloc failed.");
808 cur_ds_def = rrd->ds_def + rrd->stat_head->ds_cnt;
809 memset(cur_ds_def, '\0', sizeof(ds_def_t));
812 /* Allocate pdp_prep space for the new DS definition */
816 temp = (pdp_prep_t *) realloc(rrd->pdp_prep,
818 (rrd->stat_head->ds_cnt + 1));
820 rrd_set_error("parse_tag_ds: malloc failed.");
823 rrd->pdp_prep = temp;
824 cur_pdp_prep = rrd->pdp_prep + rrd->stat_head->ds_cnt;
825 memset(cur_pdp_prep, '\0', sizeof(pdp_prep_t));
828 /* All allocations successful, let's increment the number of DSes. */
829 rrd->stat_head->ds_cnt++;
832 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
833 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
834 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
836 else if (xmlStrcmp(child->name, (const xmlChar *) "name") == 0)
837 status = get_string_from_node(doc, child,
839 sizeof(cur_ds_def->ds_nam));
840 else if (xmlStrcmp(child->name, (const xmlChar *) "type") == 0)
841 status = parse_tag_ds_type(doc, child, cur_ds_def);
842 else if (xmlStrcmp(child->name,
843 (const xmlChar *) "minimal_heartbeat") == 0)
844 status = get_int_from_node(doc, child,
845 (int *) &cur_ds_def->par[DS_mrhb_cnt].
847 else if (xmlStrcmp(child->name, (const xmlChar *) "min") == 0)
848 status = get_double_from_node(doc, child,
849 &cur_ds_def->par[DS_min_val].u_val);
850 else if (xmlStrcmp(child->name, (const xmlChar *) "max") == 0)
851 status = get_double_from_node(doc, child,
852 &cur_ds_def->par[DS_max_val].u_val);
853 else if (xmlStrcmp(child->name, (const xmlChar *) "cdef") == 0)
854 status = parse_tag_ds_cdef(doc, child, rrd);
855 else if (xmlStrcmp(child->name, (const xmlChar *) "last_ds") == 0)
856 status = get_string_from_node(doc, child,
857 cur_pdp_prep->last_ds,
858 sizeof(cur_pdp_prep->last_ds));
859 else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0)
860 status = get_double_from_node(doc, child,
861 &cur_pdp_prep->scratch[PDP_val].
863 else if (xmlStrcmp(child->name, (const xmlChar *) "unknown_sec") == 0)
864 status = get_int_from_node(doc, child,
865 (int *) &cur_pdp_prep->
866 scratch[PDP_unkn_sec_cnt].u_cnt);
868 rrd_set_error("parse_tag_ds: Unknown tag: %s", child->name);
877 } /* int parse_tag_ds */
882 static int parse_tag_rrd(
891 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
892 if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
893 || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
895 else if (xmlStrcmp(child->name, (const xmlChar *) "version") == 0)
896 status = get_string_from_node(doc, child,
897 rrd->stat_head->version,
898 sizeof(rrd->stat_head->version));
899 else if (xmlStrcmp(child->name, (const xmlChar *) "step") == 0)
900 status = get_int_from_node(doc, child,
901 (int *) &rrd->stat_head->pdp_step);
902 else if (xmlStrcmp(child->name, (const xmlChar *) "lastupdate") == 0)
903 status = get_int_from_node(doc, child,
904 (int *) &rrd->live_head->last_up);
905 else if (xmlStrcmp(child->name, (const xmlChar *) "ds") == 0)
906 status = parse_tag_ds(doc, child, rrd);
907 else if (xmlStrcmp(child->name, (const xmlChar *) "rra") == 0)
908 status = parse_tag_rra(doc, child, rrd);
910 rrd_set_error("parse_tag_rrd: Unknown tag: %s", child->name);
919 } /* int parse_tag_rrd */
921 static rrd_t *parse_file(
922 const char *filename)
930 doc = xmlParseFile(filename);
932 rrd_set_error("Document not parsed successfully.");
936 cur = xmlDocGetRootElement(doc);
938 rrd_set_error("Document is empty.");
943 if (xmlStrcmp(cur->name, (const xmlChar *) "rrd") != 0) {
945 ("Document of the wrong type, root node is not \"rrd\".");
950 rrd = (rrd_t *) malloc(sizeof(rrd_t));
952 rrd_set_error("parse_file: malloc failed.");
956 memset(rrd, '\0', sizeof(rrd_t));
958 rrd->stat_head = (stat_head_t *) malloc(sizeof(stat_head_t));
959 if (rrd->stat_head == NULL) {
960 rrd_set_error("parse_tag_rrd: malloc failed.");
965 memset(rrd->stat_head, '\0', sizeof(stat_head_t));
967 strncpy(rrd->stat_head->cookie, "RRD", sizeof(rrd->stat_head->cookie));
968 rrd->stat_head->float_cookie = FLOAT_COOKIE;
970 rrd->live_head = (live_head_t *) malloc(sizeof(live_head_t));
971 if (rrd->live_head == NULL) {
972 rrd_set_error("parse_tag_rrd: malloc failed.");
974 free(rrd->stat_head);
978 memset(rrd->live_head, '\0', sizeof(live_head_t));
980 status = parse_tag_rrd(doc, cur, rrd);
989 } /* rrd_t *parse_file */
991 static int write_file(
992 const char *file_name,
997 unsigned int value_count;
999 if (strcmp("-", file_name) == 0)
1002 int fd_flags = O_WRONLY | O_CREAT;
1005 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
1006 fd_flags |= O_BINARY;
1009 if (opt_force_overwrite == 0)
1012 fd = open(file_name, fd_flags, 0666);
1014 rrd_set_error("creating '%s': %s", file_name,
1015 rrd_strerror(errno));
1019 fh = fdopen(fd, "wb");
1021 rrd_set_error("fdopen failed: %s", rrd_strerror(errno));
1027 fwrite(rrd->stat_head, sizeof(stat_head_t), 1, fh);
1028 fwrite(rrd->ds_def, sizeof(ds_def_t), rrd->stat_head->ds_cnt, fh);
1029 fwrite(rrd->rra_def, sizeof(rra_def_t), rrd->stat_head->rra_cnt, fh);
1030 fwrite(rrd->live_head, sizeof(live_head_t), 1, fh);
1031 fwrite(rrd->pdp_prep, sizeof(pdp_prep_t), rrd->stat_head->ds_cnt, fh);
1032 fwrite(rrd->cdp_prep, sizeof(cdp_prep_t),
1033 rrd->stat_head->rra_cnt * rrd->stat_head->ds_cnt, fh);
1034 fwrite(rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt, fh);
1036 /* calculate the number of rrd_values to dump */
1038 for (i = 0; i < rrd->stat_head->rra_cnt; i++)
1039 value_count += (rrd->rra_def[i].row_cnt * rrd->stat_head->ds_cnt);
1041 fwrite(rrd->rrd_value, sizeof(rrd_value_t), value_count, fh);
1043 /* lets see if we had an error */
1045 rrd_set_error("a file error occurred while creating '%s'", file_name);
1052 } /* int write_file */
1060 /* init rrd clean */
1062 opterr = 0; /* initialize getopt */
1065 int option_index = 0;
1066 static struct option long_options[] = {
1067 {"range-check", no_argument, 0, 'r'},
1068 {"force-overwrite", no_argument, 0, 'f'},
1072 opt = getopt_long(argc, argv, "rf", long_options, &option_index);
1079 opt_range_check = 1;
1083 opt_force_overwrite = 1;
1087 rrd_set_error("usage rrdtool %s [--range-check|-r] "
1088 "[--force-overwrite/-f] file.xml file.rrd",
1095 if ((argc - optind) != 2) {
1096 rrd_set_error("usage rrdtool %s [--range-check/-r] "
1097 "[--force-overwrite/-f] file.xml file.rrd", argv[0]);
1101 rrd = parse_file(argv[optind]);
1105 if (write_file(argv[optind + 1], rrd) != 0) {
1112 } /* int rrd_restore */
1114 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */