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