1 /*****************************************************************************
2 * RRDtool 1.0.33 Copyright Tobias Oetiker, 1997 - 2000
3 *****************************************************************************
4 * rrd_create.c creates new rrds
5 *****************************************************************************/
7 #include "rrd_tool.h"
8 #include "rrd_rpncalc.h"
9 #include "rrd_hw.h"
11 unsigned long FnvHash(char *str);
12 int create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashed_name);
13 void parseGENERIC_DS(char *def,rrd_t *rrd, int ds_idx);
15 /* #define DEBUG */
16 int
17 rrd_create(int argc, char **argv)
18 {
19 rrd_t rrd;
20 long i,long_tmp;
21 int offset;
22 time_t last_up;
23 struct time_value last_up_tv;
24 char *parsetime_error = NULL;
25 char *token;
26 unsigned short token_idx, error_flag, period=0;
27 unsigned long hashed_name;
28 /* init last_up */
29 last_up = time(NULL)-10;
30 /* init rrd clean */
31 rrd_init(&rrd);
32 /* static header */
33 if((rrd.stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
34 rrd_set_error("allocating rrd.stat_head");
35 return(-1);
36 }
38 /* live header */
39 if((rrd.live_head = calloc(1,sizeof(live_head_t)))==NULL){
40 rrd_set_error("allocating rrd.live_head");
41 return(-1);
42 }
44 /* set some defaults */
45 strcpy(rrd.stat_head->cookie,RRD_COOKIE);
46 /* assume the will be version 1 compatible */
47 strcpy(rrd.stat_head->version,"0001");
48 rrd.stat_head->float_cookie = FLOAT_COOKIE;
49 rrd.stat_head->ds_cnt = 0; /* this will be adjusted later */
50 rrd.stat_head->rra_cnt = 0; /* ditto */
51 rrd.stat_head->pdp_step = 300; /* 5 minute default */
53 /* a default value */
54 rrd.ds_def = NULL;
55 rrd.rra_def = NULL;
57 while (1){
58 static struct option long_options[] =
59 {
60 {"start", required_argument, 0, 'b'},
61 {"step", required_argument,0,'s'},
62 {0,0,0,0}
63 };
64 int option_index = 0;
65 int opt;
66 opt = getopt_long(argc, argv, "b:s:",
67 long_options, &option_index);
69 if (opt == EOF)
70 break;
72 switch(opt) {
73 case 'b':
74 if ((parsetime_error = parsetime(optarg, &last_up_tv))) {
75 rrd_set_error("start time: %s", parsetime_error );
76 rrd_free(&rrd);
77 return(-1);
78 }
79 if (last_up_tv.type == RELATIVE_TO_END_TIME ||
80 last_up_tv.type == RELATIVE_TO_START_TIME) {
81 rrd_set_error("specifying time relative to the 'start' "
82 "or 'end' makes no sense here");
83 rrd_free(&rrd);
84 return(-1);
85 }
87 last_up = mktime(&last_up_tv.tm) + last_up_tv.offset;
89 if (last_up < 3600*24*365*10){
90 rrd_set_error("the first entry to the RRD should be after 1980");
91 rrd_free(&rrd);
92 return(-1);
93 }
94 break;
96 case 's':
97 long_tmp = atol(optarg);
98 if (long_tmp < 1){
99 rrd_set_error("step size should be no less than one second");
100 rrd_free(&rrd);
101 return(-1);
102 }
103 rrd.stat_head->pdp_step = long_tmp;
104 break;
106 case '?':
107 if (optopt != 0)
108 rrd_set_error("unknown option '%c'", optopt);
109 else
110 rrd_set_error("unknown option '%s'",argv[optind-1]);
111 rrd_free(&rrd);
112 return(-1);
113 }
114 }
115 rrd.live_head->last_up = last_up;
117 /* optind points to the first non-option command line arg,
118 * in this case, the file name. */
119 /* Compute the FNV hash value (used by SEASONAL and DEVSEASONAL
120 * arrays. */
121 hashed_name = FnvHash(argv[optind]);
122 for(i=optind+1;i<argc;i++){
123 int ii;
124 if (strncmp(argv[i],"DS:",3)==0){
125 size_t old_size = sizeof(ds_def_t)*(rrd.stat_head->ds_cnt);
126 if((rrd.ds_def = rrd_realloc(rrd.ds_def,
127 old_size+sizeof(ds_def_t)))==NULL){
128 rrd_set_error("allocating rrd.ds_def");
129 rrd_free(&rrd);
130 return(-1);
131 }
132 memset(&rrd.ds_def[rrd.stat_head->ds_cnt], 0, sizeof(ds_def_t));
133 /* extract the name and type */
134 if (sscanf(&argv[i][3],
135 DS_NAM_FMT ":" DST_FMT ":%n",
136 rrd.ds_def[rrd.stat_head->ds_cnt].ds_nam,
137 rrd.ds_def[rrd.stat_head->ds_cnt].dst,&offset) == 2)
138 {
139 /* check for duplicate datasource names */
140 for(ii=0;ii<rrd.stat_head->ds_cnt;ii++)
141 if(strcmp(rrd.ds_def[rrd.stat_head->ds_cnt].ds_nam,
142 rrd.ds_def[ii].ds_nam) == 0){
143 rrd_set_error("Duplicate DS name: %s",rrd.ds_def[ii].ds_nam);
144 }
145 } else {
146 rrd_set_error("invalid DS format");
147 }
149 /* parse the remainder of the arguments */
150 switch(dst_conv(rrd.ds_def[rrd.stat_head->ds_cnt].dst))
151 {
152 case DST_COUNTER:
153 case DST_ABSOLUTE:
154 case DST_GAUGE:
155 case DST_DERIVE:
156 parseGENERIC_DS(&argv[i][offset+3],&rrd, rrd.stat_head->ds_cnt);
157 break;
158 case DST_CDEF:
159 parseCDEF_DS(&argv[i][offset+3],&rrd, rrd.stat_head->ds_cnt);
160 break;
161 default:
162 rrd_set_error("invalid DS type specified");
163 break;
164 }
166 if (rrd_test_error()) {
167 rrd_free(&rrd);
168 return -1;
169 }
170 rrd.stat_head -> ds_cnt++;
171 } else if (strncmp(argv[i],"RRA:",3)==0){
172 size_t old_size = sizeof(rra_def_t)*(rrd.stat_head->rra_cnt);
173 if((rrd.rra_def = rrd_realloc(rrd.rra_def,
174 old_size+sizeof(rra_def_t)))==NULL)
175 {
176 rrd_set_error("allocating rrd.rra_def");
177 rrd_free(&rrd);
178 return(-1);
179 }
180 memset(&rrd.rra_def[rrd.stat_head->rra_cnt], 0, sizeof(rra_def_t));
182 token = strtok(&argv[i][4],":");
183 token_idx = error_flag = 0;
184 while (token != NULL)
185 {
186 switch(token_idx)
187 {
188 case 0:
189 if (sscanf(token,CF_NAM_FMT,
190 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) != 1)
191 rrd_set_error("Failed to parse CF name");
192 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
193 {
194 case CF_HWPREDICT:
195 /* initialize some parameters */
196 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha].u_val = 0.1;
197 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].u_val = 1.0/288;
198 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
199 rrd.stat_head -> rra_cnt;
200 /* need to mark the file version */
201 strcpy(rrd.stat_head->version,RRD_VERSION);
202 break;
203 case CF_DEVSEASONAL:
204 case CF_SEASONAL:
205 /* initialize some parameters */
206 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val = 0.1;
207 /* fall through */
208 case CF_DEVPREDICT:
209 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt = -1;
210 break;
211 case CF_FAILURES:
212 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_delta_pos].u_val = 2.0;
213 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_delta_neg].u_val = 2.0;
214 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt = 3;
215 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_failure_threshold].u_cnt = 2;
216 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt = -1;
217 break;
218 /* invalid consolidation function */
219 case -1:
220 rrd_set_error("Unrecognized consolidation function %s",
221 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
222 default:
223 break;
224 }
225 /* default: 1 pdp per cdp */
226 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = 1;
227 break;
228 case 1:
229 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
230 {
231 case CF_HWPREDICT:
232 case CF_DEVSEASONAL:
233 case CF_SEASONAL:
234 case CF_DEVPREDICT:
235 case CF_FAILURES:
236 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
237 break;
238 default:
239 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_cdp_xff_val].u_val = atof(token);
240 if (rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_cdp_xff_val].u_val<0.0 ||
241 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_cdp_xff_val].u_val>=1.0)
242 rrd_set_error("Invalid xff: must be between 0 and 1");
243 break;
244 }
245 break;
246 case 2:
247 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
248 {
249 case CF_HWPREDICT:
250 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha].u_val = atof(token);
251 if (atof(token) <= 0.0 || atof(token) >= 1.0)
252 rrd_set_error("Invalid alpha: must be between 0 and 1");
253 break;
254 case CF_DEVSEASONAL:
255 case CF_SEASONAL:
256 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val =
257 atof(token);
258 if (atof(token) <= 0.0 || atof(token) >= 1.0)
259 rrd_set_error("Invalid gamma: must be between 0 and 1");
260 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_smooth_idx].u_cnt
261 = hashed_name % rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt;
262 break;
263 case CF_FAILURES:
264 /* specifies the # of violations that constitutes the failure threshold */
265 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_failure_threshold].u_cnt =
266 atoi(token);
267 if (atoi(token) < 1 || atoi(token) > MAX_FAILURES_WINDOW_LEN)
268 rrd_set_error("Failure threshold is out of range %d, %d",1,
269 MAX_FAILURES_WINDOW_LEN);
270 break;
271 case CF_DEVPREDICT:
272 /* specifies the index (1-based) of CF_DEVSEASONAL array
273 * associated with this CF_DEVPREDICT array. */
274 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
275 atoi(token) - 1;
276 break;
277 default:
278 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = atoi(token);
279 break;
280 }
281 break;
282 case 3:
283 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
284 {
285 case CF_HWPREDICT:
286 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].u_val = atof(token);
287 if (atof(token) < 0.0 || atof(token) > 1.0)
288 rrd_set_error("Invalid beta: must be between 0 and 1");
289 break;
290 case CF_DEVSEASONAL:
291 case CF_SEASONAL:
292 /* specifies the index (1-based) of CF_HWPREDICT array
293 * associated with this CF_DEVSEASONAL or CF_SEASONAL array.
294 * */
295 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
296 atoi(token) - 1;
297 break;
298 case CF_FAILURES:
299 /* specifies the window length */
300 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt =
301 atoi(token);
302 if (atoi(token) < 1 || atoi(token) > MAX_FAILURES_WINDOW_LEN)
303 rrd_set_error("Window length is out of range %d, %d",1,
304 MAX_FAILURES_WINDOW_LEN);
305 /* verify that window length exceeds the failure threshold */
306 if (rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt <
307 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_failure_threshold].u_cnt)
308 rrd_set_error("Window length is shorter than the failure threshold");
309 break;
310 case CF_DEVPREDICT:
311 /* shouldn't be any more arguments */
312 rrd_set_error("Unexpected extra argument for consolidation function DEVPREDICT");
313 break;
314 default:
315 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
316 break;
317 }
318 break;
319 case 4:
320 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
321 {
322 case CF_FAILURES:
323 /* specifies the index (1-based) of CF_DEVSEASONAL array
324 * associated with this CF_DEVFAILURES array. */
325 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
326 atoi(token) - 1;
327 break;
328 case CF_HWPREDICT:
329 /* length of the associated CF_SEASONAL and CF_DEVSEASONAL arrays. */
330 period = atoi(token);
331 if (period > rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt)
332 rrd_set_error("Length of seasonal cycle exceeds length of HW prediction array");
333 break;
334 default:
335 /* shouldn't be any more arguments */
336 rrd_set_error("Unexpected extra argument for consolidation function %s",
337 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
338 break;
339 }
340 break;
341 case 5:
342 /* If we are here, this must be a CF_HWPREDICT RRA.
343 * Specifies the index (1-based) of CF_SEASONAL array
344 * associated with this CF_HWPREDICT array. If this argument
345 * is missing, then the CF_SEASONAL, CF_DEVSEASONAL, CF_DEVPREDICT,
346 * CF_FAILURES.
347 * arrays are created automatically. */
348 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
349 atoi(token) - 1;
350 break;
351 default:
352 /* should never get here */
353 rrd_set_error("Unknown error");
354 break;
355 } /* end switch */
356 if (rrd_test_error())
357 {
358 /* all errors are unrecoverable */
359 rrd_free(&rrd);
360 return (-1);
361 }
362 token = strtok(NULL,":");
363 token_idx++;
364 } /* end while */
365 #ifdef DEBUG
366 fprintf(stderr,"Creating RRA CF: %s, dep idx %lu, current idx %lu\n",
367 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam,
368 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt,
369 rrd.stat_head -> rra_cnt);
370 #endif
371 /* should we create CF_SEASONAL, CF_DEVSEASONAL, and CF_DEVPREDICT? */
372 if (cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) == CF_HWPREDICT
373 && rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt
374 == rrd.stat_head -> rra_cnt)
375 {
376 #ifdef DEBUG
377 fprintf(stderr,"Creating HW contingent RRAs\n");
378 #endif
379 if (create_hw_contingent_rras(&rrd,period,hashed_name) == -1) {
380 rrd_free(&rrd);
381 return -1;
382 }
383 }
384 rrd.stat_head->rra_cnt++;
385 } else {
386 rrd_set_error("can't parse argument '%s'",argv[i]);
387 rrd_free(&rrd);
388 return -1;
389 }
390 }
393 if (rrd.stat_head->rra_cnt < 1){
394 rrd_set_error("you must define at least one Round Robin Archive");
395 rrd_free(&rrd);
396 return(-1);
397 }
399 if (rrd.stat_head->ds_cnt < 1){
400 rrd_set_error("you must define at least one Data Source");
401 rrd_free(&rrd);
402 return(-1);
403 }
404 return rrd_create_fn(argv[optind],&rrd);
405 }
407 void parseGENERIC_DS(char *def,rrd_t *rrd, int ds_idx)
408 {
409 char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];
410 /*
411 int temp;
413 temp = sscanf(def,"%lu:%18[^:]:%18[^:]",
414 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
415 minstr,maxstr);
416 */
417 if (sscanf(def,"%lu:%18[^:]:%18[^:]",
418 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
419 minstr,maxstr) == 3)
420 {
421 if (minstr[0] == 'U' && minstr[1] == 0)
422 rrd -> ds_def[ds_idx].par[DS_min_val].u_val = DNAN;
423 else
424 rrd -> ds_def[ds_idx].par[DS_min_val].u_val = atof(minstr);
426 if (maxstr[0] == 'U' && maxstr[1] == 0)
427 rrd -> ds_def[ds_idx].par[DS_max_val].u_val = DNAN;
428 else
429 rrd -> ds_def[ds_idx].par[DS_max_val].u_val = atof(maxstr);
431 if (! isnan(rrd -> ds_def[ds_idx].par[DS_min_val].u_val) &&
432 ! isnan(rrd -> ds_def[ds_idx].par[DS_max_val].u_val) &&
433 rrd -> ds_def[ds_idx].par[DS_min_val].u_val
434 >= rrd -> ds_def[ds_idx].par[DS_max_val].u_val ) {
435 rrd_set_error("min must be less than max in DS definition");
436 return;
437 }
438 } else {
439 rrd_set_error("failed to parse data source %s", def);
440 }
441 }
443 /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs
444 * associated with a CF_HWPREDICT RRA. */
445 int
446 create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashed_name)
447 {
448 size_t old_size;
449 rra_def_t* current_rra;
451 /* save index to CF_HWPREDICT */
452 unsigned long hw_index = rrd -> stat_head -> rra_cnt;
453 /* advance the pointer */
454 (rrd -> stat_head -> rra_cnt)++;
455 /* allocate the memory for the 4 contingent RRAs */
456 old_size = sizeof(rra_def_t)*(rrd -> stat_head->rra_cnt);
457 if ((rrd -> rra_def = rrd_realloc(rrd -> rra_def,
458 old_size+4*sizeof(rra_def_t)))==NULL)
459 {
460 rrd_set_error("allocating rrd.rra_def");
461 return(-1);
462 }
463 /* clear memory */
464 memset(&(rrd -> rra_def[rrd -> stat_head->rra_cnt]), 0, 4*sizeof(rra_def_t));
466 /* create the CF_SEASONAL RRA */
467 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
468 strcpy(current_rra -> cf_nam,"SEASONAL");
469 current_rra -> row_cnt = period;
470 current_rra -> par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
471 current_rra -> pdp_cnt = 1;
472 current_rra -> par[RRA_seasonal_gamma].u_val =
473 rrd -> rra_def[hw_index].par[RRA_hw_alpha].u_val;
474 current_rra -> par[RRA_dependent_rra_idx].u_cnt = hw_index;
475 rrd -> rra_def[hw_index].par[RRA_dependent_rra_idx].u_cnt = rrd -> stat_head -> rra_cnt;
477 /* create the CF_DEVSEASONAL RRA */
478 (rrd -> stat_head -> rra_cnt)++;
479 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
480 strcpy(current_rra -> cf_nam,"DEVSEASONAL");
481 current_rra -> row_cnt = period;
482 current_rra -> par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
483 current_rra -> pdp_cnt = 1;
484 current_rra -> par[RRA_seasonal_gamma].u_val =
485 rrd -> rra_def[hw_index].par[RRA_hw_alpha].u_val;
486 current_rra -> par[RRA_dependent_rra_idx].u_cnt = hw_index;
488 /* create the CF_DEVPREDICT RRA */
489 (rrd -> stat_head -> rra_cnt)++;
490 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
491 strcpy(current_rra -> cf_nam,"DEVPREDICT");
492 current_rra -> row_cnt = (rrd -> rra_def[hw_index]).row_cnt;
493 current_rra -> pdp_cnt = 1;
494 current_rra -> par[RRA_dependent_rra_idx].u_cnt
495 = hw_index + 2; /* DEVSEASONAL */
497 /* create the CF_FAILURES RRA */
498 (rrd -> stat_head -> rra_cnt)++;
499 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
500 strcpy(current_rra -> cf_nam,"FAILURES");
501 current_rra -> row_cnt = period;
502 current_rra -> pdp_cnt = 1;
503 current_rra -> par[RRA_delta_pos].u_val = 2.0;
504 current_rra -> par[RRA_delta_neg].u_val = 2.0;
505 current_rra -> par[RRA_failure_threshold].u_cnt = 7;
506 current_rra -> par[RRA_window_len].u_cnt = 9;
507 current_rra -> par[RRA_dependent_rra_idx].u_cnt =
508 hw_index + 2; /* DEVSEASONAL */
509 return 0;
510 }
512 /* create and empty rrd file according to the specs given */
514 int
515 rrd_create_fn(char *file_name, rrd_t *rrd)
516 {
517 unsigned long i,ii;
518 FILE *rrd_file;
519 rrd_value_t unknown = DNAN ;
521 if ((rrd_file = fopen(file_name,"wb")) == NULL ) {
522 rrd_set_error("creating '%s': %s",file_name,strerror(errno));
523 free(rrd->stat_head);
524 free(rrd->ds_def);
525 free(rrd->rra_def);
526 return(-1);
527 }
529 fwrite(rrd->stat_head,
530 sizeof(stat_head_t), 1, rrd_file);
532 fwrite(rrd->ds_def,
533 sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
535 fwrite(rrd->rra_def,
536 sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
538 fwrite(rrd->live_head,
539 sizeof(live_head_t),1, rrd_file);
541 if((rrd->pdp_prep = calloc(1,sizeof(pdp_prep_t))) == NULL){
542 rrd_set_error("allocating pdp_prep");
543 rrd_free(rrd);
544 fclose(rrd_file);
545 return(-1);
546 }
548 strcpy(rrd->pdp_prep->last_ds,"UNKN");
550 rrd->pdp_prep->scratch[PDP_val].u_val = 0.0;
551 rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt =
552 rrd->live_head->last_up % rrd->stat_head->pdp_step;
554 for(i=0; i < rrd->stat_head->ds_cnt; i++)
555 fwrite( rrd->pdp_prep,sizeof(pdp_prep_t),1,rrd_file);
557 if((rrd->cdp_prep = calloc(1,sizeof(cdp_prep_t))) == NULL){
558 rrd_set_error("allocating cdp_prep");
559 rrd_free(rrd);
560 fclose(rrd_file);
561 return(-1);
562 }
565 for(i=0; i < rrd->stat_head->rra_cnt; i++) {
566 switch (cf_conv(rrd->rra_def[i].cf_nam))
567 {
568 case CF_HWPREDICT:
569 init_hwpredict_cdp(rrd->cdp_prep);
570 break;
571 case CF_SEASONAL:
572 case CF_DEVSEASONAL:
573 init_seasonal_cdp(rrd->cdp_prep);
574 break;
575 case CF_FAILURES:
576 /* initialize violation history to 0 */
577 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
578 {
579 /* We can zero everything out, by setting u_val to the
580 * NULL address. Each array entry in scratch is 8 bytes
581 * (a double), but u_cnt only accessed 4 bytes (long) */
582 rrd->cdp_prep->scratch[ii].u_val = 0.0;
583 }
584 break;
585 default:
586 /* can not be zero because we don't know anything ... */
587 rrd->cdp_prep->scratch[CDP_val].u_val = DNAN;
588 /* startup missing pdp count */
589 rrd->cdp_prep->scratch[CDP_unkn_pdp_cnt].u_cnt =
590 ((rrd->live_head->last_up -
591 rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt)
592 % (rrd->stat_head->pdp_step
593 * rrd->rra_def[i].pdp_cnt)) / rrd->stat_head->pdp_step;
594 break;
595 }
597 for(ii=0; ii < rrd->stat_head->ds_cnt; ii++)
598 {
599 fwrite( rrd->cdp_prep,sizeof(cdp_prep_t),1,rrd_file);
600 }
601 }
603 /* now, we must make sure that the rest of the rrd
604 struct is properly initialized */
606 if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t))) == NULL) {
607 rrd_set_error("allocating rra_ptr");
608 rrd_free(rrd);
609 fclose(rrd_file);
610 return(-1);
611 }
613 /* changed this initialization to be consistent with
614 * rrd_restore. With the old value (0), the first update
615 * would occur for cur_row = 1 because rrd_update increments
616 * the pointer a priori. */
617 for (i=0; i < rrd->stat_head->rra_cnt; i++)
618 {
619 rrd->rra_ptr->cur_row = rrd->rra_def[i].row_cnt - 1;
620 fwrite( rrd->rra_ptr, sizeof(rra_ptr_t),1,rrd_file);
621 }
623 /* write the empty data area */
624 for(i=0;
625 i < rrd->stat_head->rra_cnt;
626 i++)
627 {
628 for(ii=0;
629 ii < rrd->rra_def[i].row_cnt
630 * rrd->stat_head->ds_cnt;
631 ii++){
632 fwrite(&unknown,sizeof(rrd_value_t),1,rrd_file);
633 }
634 }
636 /* lets see if we had an error */
637 if(ferror(rrd_file)){
638 rrd_set_error("a file error occurred while creating '%s'",file_name);
639 fclose(rrd_file);
640 rrd_free(rrd);
641 return(-1);
642 }
644 fclose(rrd_file);
645 rrd_free(rrd);
646 return (0);
647 }