Code

Imported upstream version 1.3.5.
[pkg-rrdtool.git] / src / rrd_restore.c
1 /*****************************************************************************
2  * RRDtool 1.3.5  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  *****************************************************************************
8  * $Id: rrd_restore.c 1710 2008-12-15 22:06:22Z oetiker $
9  *************************************************************************** */
11 /*
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)
15  * any later version.
16  * 
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
20  * more details.
21  *
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
25  *
26  * Authors:
27  *   Florian octo Forster <octo at verplant.org>
28  **/
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <fcntl.h>
36 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
37 #include <math.h>
38 # include <io.h>
39 # define open _open
40 # define close _close
41 #else
42 # include <unistd.h>
43 #endif
45 #include <libxml/parser.h>
46 #include "rrd_tool.h"
47 #include "rrd_rpncalc.h"
48 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof ((a)[0]))
49 static int opt_range_check = 0;
50 static int opt_force_overwrite = 0;
52 /*
53  * Auxiliary functions
54  */
55 static int get_string_from_node(
56     xmlDoc * doc,
57     xmlNode * node,
58     char *buffer,
59     size_t buffer_size)
60 {
61     xmlChar  *temp0;
62     char     *begin_ptr;
63     char     *end_ptr;
65     temp0 = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
66     if (temp0 == NULL) {
67         rrd_set_error("get_string_from_node: xmlNodeListGetString failed.");
68         return (-1);
69     }
71     begin_ptr = (char *) temp0;
72     while ((begin_ptr[0] != 0) && (isspace(begin_ptr[0])))
73         begin_ptr++;
75     if (begin_ptr[0] == 0) {
76         xmlFree(temp0);
77         buffer[0] = 0;
78         return (0);
79     }
81     end_ptr = begin_ptr;
82     while ((end_ptr[0] != 0) && (!isspace(end_ptr[0])))
83         end_ptr++;
84     end_ptr[0] = 0;
86     strncpy(buffer, begin_ptr, buffer_size);
87     buffer[buffer_size - 1] = 0;
89     xmlFree(temp0);
91     return (0);
92 }                       /* int get_string_from_node */
94 static int get_int_from_node(
95     xmlDoc * doc,
96     xmlNode * node,
97     int *value)
98 {
99     int       temp;
100     char     *str_ptr;
101     char     *end_ptr;
103     str_ptr = (char *) xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
104     if (str_ptr == NULL) {
105         rrd_set_error("get_int_from_node: xmlNodeListGetString failed.");
106         return (-1);
107     }
109     end_ptr = NULL;
110     temp = strtol(str_ptr, &end_ptr, 0);
111     xmlFree(str_ptr);
113     if (str_ptr == end_ptr) {
114         rrd_set_error("get_int_from_node: Cannot parse buffer as int: %s",
115                       str_ptr);
116         return (-1);
117     }
119     *value = temp;
121     return (0);
122 }                       /* int get_int_from_node */
124 static int get_double_from_node(
125     xmlDoc * doc,
126     xmlNode * node,
127     double *value)
129     double    temp;
130     char     *str_ptr;
131     char     *end_ptr;
133     str_ptr = (char *) xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
134     if (str_ptr == NULL) {
135         rrd_set_error("get_double_from_node: xmlNodeListGetString failed.");
136         return (-1);
137     }
139     if (strstr(str_ptr, "NaN") != NULL)
140     {
141         *value = DNAN;
142         xmlFree(str_ptr);
143         return 0;
144     }
146     end_ptr = NULL;
147     temp = strtod(str_ptr, &end_ptr);
148     xmlFree(str_ptr);
150     if (str_ptr == end_ptr) {
151         rrd_set_error
152             ("get_double_from_node: Cannot parse buffer as double: %s",
153              str_ptr);
154         return (-1);
155     }
157     *value = temp;
159     return (0);
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)
166     double    min;
167     double    max;
169     if (opt_range_check == 0)
170         return (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)))
177         *rrd_value = DNAN;
179     return (0);
180 }                       /* int value_check_range */
182 /*
183  * Parse the <database> block within an RRA definition
184  */
185 static int parse_tag_rra_database_row(
186     xmlDoc * doc,
187     xmlNode * node,
188     rrd_t *rrd,
189     rrd_value_t *rrd_value)
191     unsigned int values_count = 0;
192     xmlNode  *child;
193     int       status;
195     status = 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))
199             /* ignore */ ;
200         else if (xmlStrcmp(child->name, (const xmlChar *) "v") == 0) {
201             if (values_count < rrd->stat_head->ds_cnt) {
202                 status =
203                     get_double_from_node(doc, child,
204                                          rrd_value + values_count);
205                 if (status == 0)
206                     value_check_range(rrd_value + values_count,
207                                       rrd->ds_def + values_count);
208             }
210             values_count++;
211         } else {
212             rrd_set_error("parse_tag_rra_database_row: Unknown tag: %s",
213                           child->name);
214             status = -1;
215         }
217         if (status != 0)
218             break;
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);
225         status = -1;
226     }
228     return (status);
229 }                       /* int parse_tag_rra_database_row */
231 static int parse_tag_rra_database(
232     xmlDoc * doc,
233     xmlNode * node,
234     rrd_t *rrd)
236     rra_def_t *cur_rra_def;
237     unsigned int total_row_cnt;
238     xmlNode  *child;
239     int       status;
240     int       i;
242     total_row_cnt = 0;
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;
248     status = 0;
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))
252             /* ignore */ ;
253         else if (xmlStrcmp(child->name, (const xmlChar *) "row") == 0) {
254             rrd_value_t *temp;
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) *
262                                            total_values_count);
263             if (temp == NULL) {
264                 rrd_set_error("parse_tag_rra_database: realloc failed.");
265                 status = -1;
266                 break;
267             }
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);
273             total_row_cnt++;
274             cur_rra_def->row_cnt++;
276             status =
277                 parse_tag_rra_database_row(doc, child, rrd, cur_rrd_value);
278         } /* if (xmlStrcmp (child->name, (const xmlChar *) "row") == 0) */
279         else {
280             rrd_set_error("parse_tag_rra_database: Unknown tag: %s",
281                           child->name);
282             status = -1;
283         }
285         if (status != 0)
286             break;
287     }                   /* for (child = node->xmlChildrenNode) */
289     return (status);
290 }                       /* int parse_tag_rra_database */
292 /*
293  * Parse the <cdp_prep> block within an RRA definition
294  */
295 static int parse_tag_rra_cdp_prep_ds_history(
296     xmlDoc * doc,
297     xmlNode * node,
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];
303     char     *history_ptr;
304     int       status;
305     int       i;
307     status = get_string_from_node(doc, node,
308                                   history_buffer, sizeof(history_buffer));
309     if (status != 0)
310         return (-1);
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;
316     return (0);
317 }                       /* int parse_tag_rra_cdp_prep_ds_history */
319 static int parse_tag_rra_cdp_prep_ds(
320     xmlDoc * doc,
321     xmlNode * node,
322     rrd_t *rrd,
323     cdp_prep_t *cdp_prep)
325     xmlNode  *child;
326     int       status;
328     memset(cdp_prep, '\0', sizeof(cdp_prep_t));
330     status = 0;
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;
335         }
336         if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
337             || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
338             /* ignore */ ;
339         else if (xmlStrcmp(child->name, (const xmlChar *) "primary_value") ==
340                  0)
341             status =
342                 get_double_from_node(doc, child,
343                                      &cdp_prep->scratch[CDP_primary_val].
344                                      u_val);
345         else if (xmlStrcmp(child->name, (const xmlChar *) "secondary_value")
346                  == 0)
347             status =
348                 get_double_from_node(doc, child,
349                                      &cdp_prep->scratch[CDP_secondary_val].
350                                      u_val);
351         else if (xmlStrcmp(child->name, (const xmlChar *) "intercept") == 0)
352             status = get_double_from_node(doc, child,
353                                           &cdp_prep->
354                                           scratch[CDP_hw_intercept].u_val);
355         else if (xmlStrcmp(child->name, (const xmlChar *) "last_intercept") ==
356                  0)
357             status =
358                 get_double_from_node(doc, child,
359                                      &cdp_prep->
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].
364                                           u_val);
365         else if (xmlStrcmp(child->name, (const xmlChar *) "last_slope") == 0)
366             status = get_double_from_node(doc, child,
367                                           &cdp_prep->
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,
371                                        (int *) &cdp_prep->
372                                        scratch[CDP_null_count].u_cnt);
373         else if (xmlStrcmp(child->name, (const xmlChar *) "last_nan_count") ==
374                  0)
375             status =
376                 get_int_from_node(doc, child,
377                                   (int *) &cdp_prep->
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].
382                                           u_val);
383         else if (xmlStrcmp(child->name, (const xmlChar *) "last_seasonal") ==
384                  0)
385             status =
386                 get_double_from_node(doc, child,
387                                      &cdp_prep->scratch[CDP_hw_last_seasonal].
388                                      u_val);
389         else if (xmlStrcmp(child->name, (const xmlChar *) "init_flag") == 0)
390             status = get_int_from_node(doc, child,
391                                        (int *) &cdp_prep->
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,
401                                        (int *) &cdp_prep->
402                                        scratch[CDP_unkn_pdp_cnt].u_cnt);
403         else {
404             rrd_set_error("parse_tag_rra_cdp_prep: Unknown tag: %s",
405                           child->name);
406             status = -1;
407         }
409         if (status != 0)
410             break;
411     }
413     return (status);
414 }                       /* int parse_tag_rra_cdp_prep_ds */
416 static int parse_tag_rra_cdp_prep(
417     xmlDoc * doc,
418     xmlNode * node,
419     rrd_t *rrd,
420     cdp_prep_t *cdp_prep)
422     xmlNode  *child;
423     int       status;
425     unsigned int ds_count = 0;
427     status = 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))
431             /* ignore */ ;
432         else if (xmlStrcmp(child->name, (const xmlChar *) "ds") == 0) {
433             if (ds_count >= rrd->stat_head->ds_cnt)
434                 status = -1;
435             else {
436                 status = parse_tag_rra_cdp_prep_ds(doc, child, rrd,
437                                                    cdp_prep + ds_count);
438                 ds_count++;
439             }
440         } else {
441             rrd_set_error("parse_tag_rra_cdp_prep: Unknown tag: %s",
442                           child->name);
443             status = -1;
444         }
446         if (status != 0)
447             break;
448     }
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);
454         status = -1;
455     }
457     return (status);
458 }                       /* int parse_tag_rra_cdp_prep */
460 /*
461  * Parse the <params> block within an RRA definition
462  */
463 static int parse_tag_rra_params(
464     xmlDoc * doc,
465     xmlNode * node,
466     rra_def_t *rra_def)
468     xmlNode  *child;
469     int       status;
471     status = 0;
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))
475             /* ignore */ ;
476         /*
477          * Parameters for CF_HWPREDICT
478          */
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,
488                                        (int *) &rra_def->
489                                        par[RRA_dependent_rra_idx].u_cnt);
490         /*
491          * Parameters for CF_SEASONAL and CF_DEVSEASONAL
492          */
493         else if (xmlStrcmp(child->name, (const xmlChar *) "seasonal_gamma") ==
494                  0)
495             status =
496                 get_double_from_node(doc, child,
497                                      &rra_def->par[RRA_seasonal_gamma].u_val);
498         else if (xmlStrcmp
499                  (child->name, (const xmlChar *) "seasonal_smooth_idx") == 0)
500             status =
501                 get_int_from_node(doc, child,
502                                   (int *) &rra_def->
503                                   par[RRA_seasonal_smooth_idx].u_cnt);
504         else if (xmlStrcmp(child->name, (const xmlChar *) "smoothing_window")
505                  == 0)
506             status =
507                 get_double_from_node(doc, child,
508                                      &rra_def->
509                                      par[RRA_seasonal_smoothing_window].
510                                      u_val);
511         /* else if (dependent_rra_idx) ...; */
512         /*
513          * Parameters for CF_FAILURES
514          */
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].
524                                        u_cnt);
525         else if (xmlStrcmp(child->name, (const xmlChar *) "failure_threshold")
526                  == 0)
527             status =
528                 get_int_from_node(doc, child,
529                                   (int *) &rra_def->
530                                   par[RRA_failure_threshold].u_cnt);
531         /*
532          * Parameters for CF_AVERAGE, CF_MAXIMUM, CF_MINIMUM, and CF_LAST
533          */
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].
537                                           u_val);
538         /*
539          * Compatibility code for 1.0.49
540          */
541         else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0) {  /* {{{ */
542             unsigned int i = 0;
544             while (42) {
545                 if (i >= ARRAY_LENGTH(rra_def->par)) {
546                     status = -1;
547                     break;
548                 }
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].
555                                                u_cnt);
556                 else
557                     status = get_double_from_node(doc, child,
558                                                   &rra_def->par[i].u_val);
560                 if (status != 0)
561                     break;
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") !=
567                         0))
568                     break;
570                 child = child->next;
571                 i++;
572             }
573         } /* }}} */
574         else {
575             rrd_set_error("parse_tag_rra_params: Unknown tag: %s",
576                           child->name);
577             status = -1;
578         }
580         if (status != 0)
581             break;
582     }
584     return (status);
585 }                       /* int parse_tag_rra_params */
587 /*
588  * Parse an RRA definition
589  */
590 static int parse_tag_rra_cf(
591     xmlDoc * doc,
592     xmlNode * node,
593     rra_def_t *rra_def)
595     int       status;
597     status = get_string_from_node(doc, node,
598                                   rra_def->cf_nam, sizeof(rra_def->cf_nam));
599     if (status != 0)
600         return (-1);
602     status = cf_conv(rra_def->cf_nam);
603     if (status == -1) {
604         rrd_set_error("parse_tag_rra_cf: Unknown consolidation function: %s",
605                       rra_def->cf_nam);
606         return (-1);
607     }
609     return (0);
610 }                       /* int parse_tag_rra_cf */
612 static int parse_tag_rra(
613     xmlDoc * doc,
614     xmlNode * node,
615     rrd_t *rrd)
617     xmlNode  *child;
618     int       status;
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 */
625     {                   /* {{{ */
626         rra_def_t *temp;
628         temp = (rra_def_t *) realloc(rrd->rra_def,
629                                      sizeof(rra_def_t) *
630                                      (rrd->stat_head->rra_cnt + 1));
631         if (temp == NULL) {
632             rrd_set_error("parse_tag_rra: realloc failed.");
633             return (-1);
634         }
635         rrd->rra_def = temp;
636         cur_rra_def = rrd->rra_def + rrd->stat_head->rra_cnt;
637         memset(cur_rra_def, '\0', sizeof(rra_def_t));
638     }                   /* }}} */
640     /* allocate cdp_prep_t */
641     {                   /* {{{ */
642         cdp_prep_t *temp;
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));
647         if (temp == NULL) {
648             rrd_set_error("parse_tag_rra: realloc failed.");
649             return (-1);
650         }
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);
656     }                   /* }}} */
658     /* allocate rra_ptr_t */
659     {                   /* {{{ */
660         rra_ptr_t *temp;
662         temp = (rra_ptr_t *) realloc(rrd->rra_ptr,
663                                      sizeof(rra_ptr_t) *
664                                      (rrd->stat_head->rra_cnt + 1));
665         if (temp == NULL) {
666             rrd_set_error("parse_tag_rra: realloc failed.");
667             return (-1);
668         }
669         rrd->rra_ptr = temp;
670         cur_rra_ptr = rrd->rra_ptr + rrd->stat_head->rra_cnt;
671         memset(cur_rra_ptr, '\0', sizeof(rra_ptr_t));
672     }                   /* }}} */
674     /* All space successfully allocated, increment number of RRAs. */
675     rrd->stat_head->rra_cnt++;
677     status = 0;
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))
681             /* ignore */ ;
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);
699         else {
700             rrd_set_error("parse_tag_rra: Unknown tag: %s", child->name);
701             status = -1;
702         }
704         if (status != 0)
705             break;
706     }
708     /* Set the RRA pointer to a random location */
709 #ifdef WIN32
710     cur_rra_ptr->cur_row = rand() % cur_rra_def->row_cnt;
711 #else
712     cur_rra_ptr->cur_row = random() % cur_rra_def->row_cnt;
713 #endif
715     return (status);
716 }                       /* int parse_tag_rra */
718 /*
719  * Parse a DS definition
720  */
721 static int parse_tag_ds_cdef(
722     xmlDoc * doc,
723     xmlNode * node,
724     rrd_t *rrd)
726     char      buffer[1024];
727     int       status;
729     status = get_string_from_node(doc, node, buffer, sizeof(buffer));
730     if (status != 0)
731         return (-1);
733     /* We're always working on the last DS that has been added to the structure
734      * when we get here */
735     parseCDEF_DS(buffer, rrd, rrd->stat_head->ds_cnt - 1);
737     return (0);
738 }                       /* int parse_tag_ds_cdef */
740 static int parse_tag_ds_type(
741     xmlDoc * doc,
742     xmlNode * node,
743     ds_def_t *ds_def)
745     int       status;
747     status = get_string_from_node(doc, node,
748                                   ds_def->dst, sizeof(ds_def->dst));
749     if (status != 0)
750         return (-1);
752     status = dst_conv(ds_def->dst);
753     if (status == -1) {
754         rrd_set_error("parse_tag_ds_type: Unknown data source type: %s",
755                       ds_def->dst);
756         return (-1);
757     }
759     return (0);
760 }                       /* int parse_tag_ds_type */
762 static int parse_tag_ds(
763     xmlDoc * doc,
764     xmlNode * node,
765     rrd_t *rrd)
767     xmlNode  *child;
768     int       status;
770     ds_def_t *cur_ds_def;
771     pdp_prep_t *cur_pdp_prep;
773     /*
774      * If there are DS definitions after RRA definitions the number of values,
775      * cdp_prep areas and so on will be calculated wrong. Thus, enforce a
776      * specific order in this case.
777      */
778     if (rrd->stat_head->rra_cnt > 0) {
779         rrd_set_error("parse_tag_ds: All data source definitions MUST "
780                       "precede the RRA definitions!");
781         return (-1);
782     }
784     /* Allocate space for the new DS definition */
785     {                   /* {{{ */
786         ds_def_t *temp;
788         temp = (ds_def_t *) realloc(rrd->ds_def,
789                                     sizeof(ds_def_t) *
790                                     (rrd->stat_head->ds_cnt + 1));
791         if (temp == NULL) {
792             rrd_set_error("parse_tag_ds: malloc failed.");
793             return (-1);
794         }
795         rrd->ds_def = temp;
796         cur_ds_def = rrd->ds_def + rrd->stat_head->ds_cnt;
797         memset(cur_ds_def, '\0', sizeof(ds_def_t));
798     }                   /* }}} */
800     /* Allocate pdp_prep space for the new DS definition */
801     {                   /* {{{ */
802         pdp_prep_t *temp;
804         temp = (pdp_prep_t *) realloc(rrd->pdp_prep,
805                                       sizeof(pdp_prep_t) *
806                                       (rrd->stat_head->ds_cnt + 1));
807         if (temp == NULL) {
808             rrd_set_error("parse_tag_ds: malloc failed.");
809             return (-1);
810         }
811         rrd->pdp_prep = temp;
812         cur_pdp_prep = rrd->pdp_prep + rrd->stat_head->ds_cnt;
813         memset(cur_pdp_prep, '\0', sizeof(pdp_prep_t));
814     }                   /* }}} */
816     /* All allocations successful, let's increment the number of DSes. */
817     rrd->stat_head->ds_cnt++;
819     status = 0;
820     for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
821         if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
822             || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
823             /* ignore */ ;
824         else if (xmlStrcmp(child->name, (const xmlChar *) "name") == 0)
825             status = get_string_from_node(doc, child,
826                                           cur_ds_def->ds_nam,
827                                           sizeof(cur_ds_def->ds_nam));
828         else if (xmlStrcmp(child->name, (const xmlChar *) "type") == 0)
829             status = parse_tag_ds_type(doc, child, cur_ds_def);
830         else if (xmlStrcmp(child->name,
831                            (const xmlChar *) "minimal_heartbeat") == 0)
832             status = get_int_from_node(doc, child,
833                                        (int *) &cur_ds_def->par[DS_mrhb_cnt].
834                                        u_cnt);
835         else if (xmlStrcmp(child->name, (const xmlChar *) "min") == 0)
836             status = get_double_from_node(doc, child,
837                                           &cur_ds_def->par[DS_min_val].u_val);
838         else if (xmlStrcmp(child->name, (const xmlChar *) "max") == 0)
839             status = get_double_from_node(doc, child,
840                                           &cur_ds_def->par[DS_max_val].u_val);
841         else if (xmlStrcmp(child->name, (const xmlChar *) "cdef") == 0)
842             status = parse_tag_ds_cdef(doc, child, rrd);
843         else if (xmlStrcmp(child->name, (const xmlChar *) "last_ds") == 0)
844             status = get_string_from_node(doc, child,
845                                           cur_pdp_prep->last_ds,
846                                           sizeof(cur_pdp_prep->last_ds));
847         else if (xmlStrcmp(child->name, (const xmlChar *) "value") == 0)
848             status = get_double_from_node(doc, child,
849                                           &cur_pdp_prep->scratch[PDP_val].
850                                           u_val);
851         else if (xmlStrcmp(child->name, (const xmlChar *) "unknown_sec") == 0)
852             status = get_int_from_node(doc, child,
853                                        (int *) &cur_pdp_prep->
854                                        scratch[PDP_unkn_sec_cnt].u_cnt);
855         else {
856             rrd_set_error("parse_tag_ds: Unknown tag: %s", child->name);
857             status = -1;
858         }
860         if (status != 0)
861             break;
862     }
864     return (status);
865 }                       /* int parse_tag_ds */
867 /*
868  * Parse root nodes
869  */
870 static int parse_tag_rrd(
871     xmlDoc * doc,
872     xmlNode * node,
873     rrd_t *rrd)
875     xmlNode  *child;
876     int       status;
878     status = 0;
879     for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
880         if ((xmlStrcmp(child->name, (const xmlChar *) "comment") == 0)
881             || (xmlStrcmp(child->name, (const xmlChar *) "text") == 0))
882             /* ignore */ ;
883         else if (xmlStrcmp(child->name, (const xmlChar *) "version") == 0)
884             status = get_string_from_node(doc, child,
885                                           rrd->stat_head->version,
886                                           sizeof(rrd->stat_head->version));
887         else if (xmlStrcmp(child->name, (const xmlChar *) "step") == 0)
888             status = get_int_from_node(doc, child,
889                                        (int *) &rrd->stat_head->pdp_step);
890         else if (xmlStrcmp(child->name, (const xmlChar *) "lastupdate") == 0)
891             status = get_int_from_node(doc, child,
892                                        (int *) &rrd->live_head->last_up);
893         else if (xmlStrcmp(child->name, (const xmlChar *) "ds") == 0)
894             status = parse_tag_ds(doc, child, rrd);
895         else if (xmlStrcmp(child->name, (const xmlChar *) "rra") == 0)
896             status = parse_tag_rra(doc, child, rrd);
897         else {
898             rrd_set_error("parse_tag_rrd: Unknown tag: %s", child->name);
899             status = -1;
900         }
902         if (status != 0)
903             break;
904     }
906     return (status);
907 }                       /* int parse_tag_rrd */
909 static rrd_t *parse_file(
910     const char *filename)
912     xmlDoc   *doc;
913     xmlNode  *cur;
914     int       status;
916     rrd_t    *rrd;
918     doc = xmlParseFile(filename);
919     if (doc == NULL) {
920         rrd_set_error("Document not parsed successfully.");
921         return (NULL);
922     }
924     cur = xmlDocGetRootElement(doc);
925     if (cur == NULL) {
926         rrd_set_error("Document is empty.");
927         xmlFreeDoc(doc);
928         return (NULL);
929     }
931     if (xmlStrcmp(cur->name, (const xmlChar *) "rrd") != 0) {
932         rrd_set_error
933             ("Document of the wrong type, root node is not \"rrd\".");
934         xmlFreeDoc(doc);
935         return (NULL);
936     }
938     rrd = (rrd_t *) malloc(sizeof(rrd_t));
939     if (rrd == NULL) {
940         rrd_set_error("parse_file: malloc failed.");
941         xmlFreeDoc(doc);
942         return (NULL);
943     }
944     memset(rrd, '\0', sizeof(rrd_t));
946     rrd->stat_head = (stat_head_t *) malloc(sizeof(stat_head_t));
947     if (rrd->stat_head == NULL) {
948         rrd_set_error("parse_tag_rrd: malloc failed.");
949         xmlFreeDoc(doc);
950         free(rrd);
951         return (NULL);
952     }
953     memset(rrd->stat_head, '\0', sizeof(stat_head_t));
955     strncpy(rrd->stat_head->cookie, "RRD", sizeof(rrd->stat_head->cookie));
956     rrd->stat_head->float_cookie = FLOAT_COOKIE;
958     rrd->live_head = (live_head_t *) malloc(sizeof(live_head_t));
959     if (rrd->live_head == NULL) {
960         rrd_set_error("parse_tag_rrd: malloc failed.");
961         xmlFreeDoc(doc);
962         free(rrd->stat_head);
963         free(rrd);
964         return (NULL);
965     }
966     memset(rrd->live_head, '\0', sizeof(live_head_t));
968     status = parse_tag_rrd(doc, cur, rrd);
970     xmlFreeDoc(doc);
971     if (status != 0) {
972         rrd_free(rrd);
973         rrd = NULL;
974     }
976     return (rrd);
977 }                       /* rrd_t *parse_file */
979 static int write_file(
980     const char *file_name,
981     rrd_t *rrd)
983     FILE     *fh;
984     unsigned int i;
985     unsigned int rra_offset;
987     if (strcmp("-", file_name) == 0)
988         fh = stdout;
989     else {
990         int       fd_flags = O_WRONLY | O_CREAT;
991         int       fd;
993 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
994         fd_flags |= O_BINARY;
995 #endif
997         if (opt_force_overwrite == 0)
998             fd_flags |= O_EXCL;
1000         fd = open(file_name, fd_flags, 0666);
1001         if (fd == -1) {
1002             rrd_set_error("creating '%s': %s", file_name,
1003                           rrd_strerror(errno));
1004             return (-1);
1005         }
1007         fh = fdopen(fd, "wb");
1008         if (fh == NULL) {
1009             rrd_set_error("fdopen failed: %s", rrd_strerror(errno));
1010             close(fd);
1011             return (-1);
1012         }
1013     }
1014     if (atoi(rrd->stat_head->version) < 3) {
1015         /* we output 3 or higher */
1016         strcpy(rrd->stat_head->version, "0003");
1017     }
1018     fwrite(rrd->stat_head, sizeof(stat_head_t), 1, fh);
1019     fwrite(rrd->ds_def, sizeof(ds_def_t), rrd->stat_head->ds_cnt, fh);
1020     fwrite(rrd->rra_def, sizeof(rra_def_t), rrd->stat_head->rra_cnt, fh);
1021     fwrite(rrd->live_head, sizeof(live_head_t), 1, fh);
1022     fwrite(rrd->pdp_prep, sizeof(pdp_prep_t), rrd->stat_head->ds_cnt, fh);
1023     fwrite(rrd->cdp_prep, sizeof(cdp_prep_t),
1024            rrd->stat_head->rra_cnt * rrd->stat_head->ds_cnt, fh);
1025     fwrite(rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt, fh);
1027     /* calculate the number of rrd_values to dump */
1028     rra_offset = 0;
1029     for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
1030         unsigned long num_rows = rrd->rra_def[i].row_cnt;
1031         unsigned long cur_row = rrd->rra_ptr[i].cur_row;
1032         unsigned long ds_cnt = rrd->stat_head->ds_cnt;
1034         fwrite(rrd->rrd_value +
1035                (rra_offset + num_rows - 1 - cur_row) * ds_cnt,
1036                sizeof(rrd_value_t), (cur_row + 1) * ds_cnt, fh);
1038         fwrite(rrd->rrd_value + rra_offset * ds_cnt,
1039                sizeof(rrd_value_t), (num_rows - 1 - cur_row) * ds_cnt, fh);
1041         rra_offset += num_rows;
1042     }
1044     /* lets see if we had an error */
1045     if (ferror(fh)) {
1046         rrd_set_error("a file error occurred while creating '%s'", file_name);
1047         fclose(fh);
1048         return (-1);
1049     }
1051     fclose(fh);
1052     return (0);
1053 }                       /* int write_file */
1055 int rrd_restore(
1056     int argc,
1057     char **argv)
1059     rrd_t    *rrd;
1061 #ifdef WIN32
1062     srand((unsigned int) time(NULL));
1063 #else
1064     srandom((unsigned int) time(NULL) + (unsigned int) getpid());
1065 #endif
1066     /* init rrd clean */
1067     optind = 0;
1068     opterr = 0;         /* initialize getopt */
1069     while (42) {
1070         int       opt;
1071         int       option_index = 0;
1072         static struct option long_options[] = {
1073             {"range-check", no_argument, 0, 'r'},
1074             {"force-overwrite", no_argument, 0, 'f'},
1075             {0, 0, 0, 0}
1076         };
1078         opt = getopt_long(argc, argv, "rf", long_options, &option_index);
1080         if (opt == EOF)
1081             break;
1083         switch (opt) {
1084         case 'r':
1085             opt_range_check = 1;
1086             break;
1088         case 'f':
1089             opt_force_overwrite = 1;
1090             break;
1092         default:
1093             rrd_set_error("usage rrdtool %s [--range-check|-r] "
1094                           "[--force-overwrite/-f]  file.xml file.rrd",
1095                           argv[0]);
1096             return (-1);
1097             break;
1098         }
1099     }                   /* while (42) */
1101     if ((argc - optind) != 2) {
1102         rrd_set_error("usage rrdtool %s [--range-check/-r] "
1103                       "[--force-overwrite/-f] file.xml file.rrd", argv[0]);
1104         return (-1);
1105     }
1107     rrd = parse_file(argv[optind]);
1108     if (rrd == NULL)
1109         return (-1);
1111     if (write_file(argv[optind + 1], rrd) != 0) {
1112         rrd_free(rrd);
1113         return (-1);
1114     }
1116     rrd_free(rrd);
1117     return (0);
1118 }                       /* int rrd_restore */
1120 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */