1 /*****************************************************************************
2 * RRDtool 1.0.33 Copyright Tobias Oetiker, 1997 - 2000
3 *****************************************************************************
4 * rrd_restore.c creates new rrd from data dumped by rrd_dump.c
5 *****************************************************************************/
7 #include "rrd_tool.h"
9 /* Prototypes */
11 void xml_lc(char*);
12 int skip(char **);
13 int eat_tag(char **, char *);
14 int read_tag(char **, char *, char *, void *);
15 int xml2rrd(char*, rrd_t*, char);
16 int rrd_write(char *, rrd_t *);
18 /* convert all ocurances of <BlaBlaBla> to <blablabla> */
20 void xml_lc(char* buf){
21 int intag=0;
22 while((*buf)){
23 if (intag ==0 && (*buf) == '<') {
24 intag = 1;
25 }
26 else if (intag ==1 && (*buf) == '>') {
27 intag = 0;
28 continue;
29 } else if (intag ==1) {
30 *buf = tolower(*buf);
31 }
32 buf++;
33 }
34 }
36 int skip(char **buf){
37 char *ptr;
38 ptr=(*buf);
39 do {
40 (*buf)=ptr;
41 while((*(ptr+1)) && ((*ptr)==' ' || (*ptr)=='\r' || (*ptr)=='\n' || (*ptr)=='\t')) ptr++;
42 if (strncmp(ptr,"<!--",4) == 0) {
43 ptr= strstr(ptr,"-->");
44 if (ptr) ptr+=3; else {
45 rrd_set_error("Dangling Comment");
46 (*buf) = NULL;
47 return -1;
48 }
49 }
50 } while ((*buf)!=ptr);
51 return 1;
52 }
54 int eat_tag(char **buf, char *tag){
55 if ((*buf)==NULL) return -1; /* fall though clause */
57 rrd_clear_error();
58 skip(buf);
59 if ((**buf)=='<'
60 && strncmp((*buf)+1,tag,strlen(tag)) == 0
61 && *((*buf)+strlen(tag)+1)=='>') {
62 (*buf) += strlen(tag)+2;
63 }
64 else {
65 rrd_set_error("No <%s> tag found",tag);
66 (*buf) = NULL;
67 return -1;
68 }
69 skip(buf);
70 return 1;
71 }
73 int read_tag(char **buf, char *tag, char *format, void *value){
74 char *end_tag;
75 int matches;
76 if ((*buf)==NULL) return -1; /* fall though clause */
77 rrd_clear_error();
78 if (eat_tag(buf,tag)==1){
79 char *temp;
80 temp = (*buf);
81 while(*((*buf)+1) && (*(*buf) != '<')) (*buf)++; /*find start of endtag*/
82 *(*buf) = '\0';
83 matches =sscanf(temp,format,value);
84 *(*buf) = '<';
85 end_tag = malloc((strlen(tag)+2)*sizeof(char));
86 sprintf(end_tag,"/%s",tag);
87 eat_tag(buf,end_tag);
88 free(end_tag);
89 if (matches == 0 && strcmp(format,"%lf") == 0)
90 (*((double* )(value))) = DNAN;
91 if (matches != 1) return 0;
92 return 1;
93 }
94 return -1;
95 }
98 /* parse the data stored in buf and return a filled rrd structure */
99 int xml2rrd(char* buf, rrd_t* rrd, char rc){
100 /* pass 1 identify number of RRAs */
101 char *ptr,*ptr2,*ptr3; /* walks thought the buffer */
102 long rows=0,mempool=0,i=0,ii;
103 xml_lc(buf); /* lets lowercase all active parts of the xml */
104 ptr=buf;
105 ptr2=buf;
106 ptr3=buf;
107 /* start with an RRD tag */
109 eat_tag(&ptr,"rrd");
110 /* allocate static header */
111 if((rrd->stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
112 rrd_set_error("allocating rrd.stat_head");
113 return -1;
114 };
116 strcpy(rrd->stat_head->cookie,RRD_COOKIE);
117 read_tag(&ptr,"version","%4[0-9]",rrd->stat_head->version);
118 /* added primitive version checking */
119 if (atoi(rrd -> stat_head -> version) < 2)
120 {
121 rrd_set_error("Incompatible file version, detected version %s, required version %s\n",
122 rrd -> stat_head -> version, RRD_VERSION);
123 free(rrd -> stat_head);
124 return -1;
125 }
126 rrd->stat_head->float_cookie = FLOAT_COOKIE;
127 rrd->stat_head->ds_cnt = 0;
128 rrd->stat_head->rra_cnt = 0;
129 read_tag(&ptr,"step","%lu",&(rrd->stat_head->pdp_step));
131 /* allocate live head */
132 if((rrd->live_head = calloc(1,sizeof(live_head_t)))==NULL){
133 rrd_set_error("allocating rrd.live_head");
134 return -1;
135 }
136 read_tag(&ptr,"lastupdate","%lu",&(rrd->live_head->last_up));
138 /* Data Source Definition Part */
139 ptr2 = ptr;
140 while (eat_tag(&ptr2,"ds") == 1){
141 rrd->stat_head->ds_cnt++;
142 if((rrd->ds_def = rrd_realloc(rrd->ds_def,rrd->stat_head->ds_cnt*sizeof(ds_def_t)))==NULL){
143 rrd_set_error("allocating rrd.ds_def");
144 return -1;
145 };
146 /* clean out memory to make sure no data gets stored from previous tasks */
147 memset(&(rrd->ds_def[rrd->stat_head->ds_cnt-1]), 0, sizeof(ds_def_t));
148 if((rrd->pdp_prep = rrd_realloc(rrd->pdp_prep,rrd->stat_head->ds_cnt
149 *sizeof(pdp_prep_t)))==NULL){
150 rrd_set_error("allocating pdp_prep");
151 return(-1);
152 }
153 /* clean out memory to make sure no data gets stored from previous tasks */
154 memset(&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1]), 0, sizeof(pdp_prep_t));
156 read_tag(&ptr2,"name",DS_NAM_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].ds_nam);
158 read_tag(&ptr2,"type",DST_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].dst);
159 /* test for valid type */
160 if(dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt-1].dst) == -1) return -1;
162 read_tag(&ptr2,"minimal_heartbeat","%lu",
163 &(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_mrhb_cnt].u_cnt));
164 read_tag(&ptr2,"min","%lf",&(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_min_val].u_val));
165 read_tag(&ptr2,"max","%lf",&(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_max_val].u_val));
167 read_tag(&ptr2,"last_ds","%30s",rrd->pdp_prep[rrd->stat_head->ds_cnt-1].last_ds);
168 read_tag(&ptr2,"value","%lf",&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1].scratch[PDP_val].u_val));
169 read_tag(&ptr2,"unknown_sec","%lu",&(rrd->pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt));
170 eat_tag(&ptr2,"/ds");
171 ptr=ptr2;
172 }
174 ptr2 = ptr;
175 while (eat_tag(&ptr2,"rra") == 1){
176 rrd->stat_head->rra_cnt++;
178 /* alocate and reset rra definition areas */
179 if((rrd->rra_def = rrd_realloc(rrd->rra_def,rrd->stat_head->rra_cnt*sizeof(rra_def_t)))==NULL){
180 rrd_set_error("allocating rra_def"); return -1; }
181 memset(&(rrd->rra_def[rrd->stat_head->rra_cnt-1]), 0, sizeof(rra_def_t));
183 /* alocate and reset consolidation point areas */
184 if((rrd->cdp_prep = rrd_realloc(rrd->cdp_prep,
185 rrd->stat_head->rra_cnt
186 *rrd->stat_head->ds_cnt*sizeof(cdp_prep_t)))==NULL){
187 rrd_set_error("allocating cdp_prep"); return -1; }
189 memset(&(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rrd->stat_head->rra_cnt-1)]),
190 0, rrd->stat_head->ds_cnt*sizeof(cdp_prep_t));
193 read_tag(&ptr2,"cf",CF_NAM_FMT,rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam);
194 /* test for valid type */
195 if(cf_conv(rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam) == -1) return -1;
197 read_tag(&ptr2,"pdp_per_row","%lu",&(rrd->rra_def[rrd->stat_head->rra_cnt-1].pdp_cnt));
198 /* add support to read RRA parameters */
199 eat_tag(&ptr2, "params");
200 for (i = 0; i < MAX_RRA_PAR_EN; i++)
201 {
202 if (i == RRA_dependent_rra_idx ||
203 i == RRA_seasonal_smooth_idx ||
204 i == RRA_failure_threshold)
205 read_tag(&ptr2, "value","%u",
206 &(rrd->rra_def[rrd->stat_head->rra_cnt-1].par[i].u_cnt));
207 else
208 read_tag(&ptr2, "value","%lf",
209 &(rrd->rra_def[rrd->stat_head->rra_cnt-1].par[i].u_val));
210 }
211 eat_tag(&ptr2, "/params");
212 eat_tag(&ptr2,"cdp_prep");
213 for(i=0;i<rrd->stat_head->ds_cnt;i++)
214 {
215 eat_tag(&ptr2,"ds");
216 /* add suport to read CDP parameters */
217 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
218 {
219 /* handle integer values as a special case */
220 if (cf_conv(rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam) == CF_FAILURES ||
221 ii == CDP_unkn_pdp_cnt ||
222 ii == CDP_null_count ||
223 ii == CDP_last_null_count)
224 {
225 read_tag(&ptr2,"value","%lu",
226 &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rrd->stat_head->rra_cnt-1)
227 +i].scratch[ii].u_cnt));
228 } else {
229 read_tag(&ptr2,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt*
230 (rrd->stat_head->rra_cnt-1) +i].scratch[ii].u_val));
231 }
233 #if 0
234 read_tag(&ptr2,"unknown_datapoints","%lu",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
235 *(rrd->stat_head->rra_cnt-1) +i].scratch[CDP_unkn_pdp_cnt].u_cnt));
236 #endif
237 } /* end for */
238 eat_tag(&ptr2,"/ds");
239 }
240 eat_tag(&ptr2,"/cdp_prep");
241 rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt=0;
242 eat_tag(&ptr2,"database");
243 ptr3 = ptr2;
244 while (eat_tag(&ptr3,"row") == 1){
246 if(mempool==0){
247 mempool = 1000;
248 if((rrd->rrd_value = rrd_realloc(rrd->rrd_value,
249 (rows+mempool)*(rrd->stat_head->ds_cnt)
250 *sizeof(rrd_value_t)))==NULL) {
251 rrd_set_error("allocating rrd_values"); return -1; }
252 }
253 rows++;
254 mempool--;
255 rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt++;
256 for(i=0;i<rrd->stat_head->ds_cnt;i++){
258 rrd_value_t * value = &(rrd->rrd_value[(rows-1)*rrd->stat_head->ds_cnt+i]);
260 read_tag(&ptr3,"v","%lf", value);
262 if (
263 (rc == 1) /* do we have to check for the ranges */
264 &&
265 (!isnan(*value)) /* not a NAN value */
266 &&
267 ( /* min defined and in the range ? */
268 (!isnan(rrd->ds_def[i].par[DS_min_val].u_val)
269 && (*value < rrd->ds_def[i].par[DS_min_val].u_val))
270 || /* max defined and in the range ? */
271 (!isnan(rrd->ds_def[i].par[DS_max_val].u_val)
272 && (*value > rrd->ds_def[i].par[DS_max_val].u_val))
273 )
274 ) {
275 fprintf (stderr, "out of range found [ds: %lu], [value : %0.10e]\n", i, *value);
276 *value = DNAN;
277 }
278 }
279 eat_tag(&ptr3,"/row");
280 ptr2=ptr3;
281 }
282 eat_tag(&ptr2,"/database");
283 eat_tag(&ptr2,"/rra");
284 ptr=ptr2;
285 }
286 eat_tag(&ptr,"/rrd");
288 if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t)*rrd->stat_head->rra_cnt)) == NULL) {
289 rrd_set_error("allocating rra_ptr");
290 return(-1);
291 }
293 for(i=0; i <rrd->stat_head->rra_cnt; i++) {
294 /* last row in the xml file is the most recent; as
295 * rrd_update increments the current row pointer, set cur_row
296 * here to the last row. */
297 rrd->rra_ptr[i].cur_row = rrd->rra_def[i].row_cnt-1;
298 }
299 if (ptr==NULL)
300 return -1;
301 return 1;
302 }
308 /* create and empty rrd file according to the specs given */
310 int
311 rrd_write(char *file_name, rrd_t *rrd)
312 {
313 unsigned long i,ii,val_cnt;
314 FILE *rrd_file=NULL;
316 if (strcmp("-",file_name)==0){
317 *rrd_file= *stdout;
318 } else {
319 if ((rrd_file = fopen(file_name,"wb")) == NULL ) {
320 rrd_set_error("creating '%s': %s",file_name,strerror(errno));
321 rrd_free(rrd);
322 return(-1);
323 }
324 }
325 fwrite(rrd->stat_head,
326 sizeof(stat_head_t), 1, rrd_file);
328 fwrite(rrd->ds_def,
329 sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
331 fwrite(rrd->rra_def,
332 sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
334 fwrite(rrd->live_head, sizeof(live_head_t),1, rrd_file);
336 fwrite( rrd->pdp_prep, sizeof(pdp_prep_t),rrd->stat_head->ds_cnt,rrd_file);
338 fwrite( rrd->cdp_prep, sizeof(cdp_prep_t),rrd->stat_head->rra_cnt*
339 rrd->stat_head->ds_cnt,rrd_file);
340 fwrite( rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt,rrd_file);
344 /* calculate the number of rrd_values to dump */
345 val_cnt=0;
346 for(i=0; i < rrd->stat_head->rra_cnt; i++)
347 for(ii=0; ii < rrd->rra_def[i].row_cnt * rrd->stat_head->ds_cnt;ii++)
348 val_cnt++;
349 fwrite( rrd->rrd_value, sizeof(rrd_value_t),val_cnt,rrd_file);
351 /* lets see if we had an error */
352 if(ferror(rrd_file)){
353 rrd_set_error("a file error occurred while creating '%s'",file_name);
354 fclose(rrd_file);
355 return(-1);
356 }
358 fclose(rrd_file);
359 return 0;
360 }
363 int
364 rrd_restore(int argc, char **argv)
365 {
366 rrd_t rrd;
367 char *buf;
368 char rc = 0;
370 /* init rrd clean */
371 rrd_init(&rrd);
372 if (argc<3) {
373 rrd_set_error("usage rrdtool %s [--range-check/-r] file.xml file.rrd",argv[0]);
374 return -1;
375 }
377 while (1) {
378 static struct option long_options[] =
379 {
380 {"range-check", required_argument, 0, 'r'},
381 {0,0,0,0}
382 };
383 int option_index = 0;
384 int opt;
387 opt = getopt_long(argc, argv, "r", long_options, &option_index);
389 if (opt == EOF)
390 break;
392 switch(opt) {
393 case 'r':
394 rc=1;
395 break;
396 default:
397 rrd_set_error("usage rrdtool %s [--range-check|-r] file.xml file.rrd",argv[0]);
398 return -1;
399 break;
400 }
401 }
403 if (readfile(argv[optind],&buf,0)==-1){
404 return -1;
405 }
406 if (xml2rrd(buf,&rrd,rc)==-1) {
407 rrd_free(&rrd);
408 free(buf);
409 return -1;
410 }
411 free(buf);
412 if(rrd_write(argv[optind+1],&rrd)==-1){
413 rrd_free(&rrd);
414 return -1;
415 };
416 rrd_free(&rrd);
417 return 0;
418 }