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