1 /*****************************************************************************
2 * RRDtool 1.0.33 Copyright Tobias Oetiker, 1997 - 2000
3 *****************************************************************************
4 * rrd_create.c creates new rrds
5 *****************************************************************************/
9 /* prototype for FnvHash */
10 unsigned long FnvHash(char *str);
14 rrd_create(int argc, char **argv)
19 struct time_value last_up_tv;
20 char *parsetime_error = NULL;
22 unsigned short token_idx, error_flag, period=0;
23 unsigned long hashed_name;
25 last_up = time(NULL)-10;
29 if((rrd.stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
30 rrd_set_error("allocating rrd.stat_head");
35 if((rrd.live_head = calloc(1,sizeof(live_head_t)))==NULL){
36 rrd_set_error("allocating rrd.live_head");
40 /* set some defaults */
41 strcpy(rrd.stat_head->cookie,RRD_COOKIE);
42 /* assume the will be version 1 compatible */
43 strcpy(rrd.stat_head->version,"0001");
44 rrd.stat_head->float_cookie = FLOAT_COOKIE;
45 rrd.stat_head->ds_cnt = 0; /* this will be adjusted later */
46 rrd.stat_head->rra_cnt = 0; /* ditto */
47 rrd.stat_head->pdp_step = 300; /* 5 minute default */
54 static struct option long_options[] =
56 {"start", required_argument, 0, 'b'},
57 {"step", required_argument,0,'s'},
62 opt = getopt_long(argc, argv, "b:s:",
63 long_options, &option_index);
70 if ((parsetime_error = parsetime(optarg, &last_up_tv))) {
71 rrd_set_error("start time: %s", parsetime_error );
75 if (last_up_tv.type == RELATIVE_TO_END_TIME ||
76 last_up_tv.type == RELATIVE_TO_START_TIME) {
77 rrd_set_error("specifying time relative to the 'start' "
78 "or 'end' makes no sense here");
83 last_up = mktime(&last_up_tv.tm) + last_up_tv.offset;
85 if (last_up < 3600*24*365*10){
86 rrd_set_error("the first entry to the RRD should be after 1980");
93 long_tmp = atol(optarg);
95 rrd_set_error("step size should be no less than one second");
99 rrd.stat_head->pdp_step = long_tmp;
104 rrd_set_error("unknown option '%c'", optopt);
106 rrd_set_error("unknown option '%s'",argv[optind-1]);
111 rrd.live_head->last_up = last_up;
113 /* optind points to the first non-option command line arg,
114 * in this case, the file name. */
115 /* Compute the FNV hash value (used by SEASONAL and DEVSEASONAL
117 hashed_name = FnvHash(argv[optind]);
118 for(i=optind+1;i<argc;i++){
119 char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];
121 if (strncmp(argv[i],"DS:",3)==0){
122 size_t old_size = sizeof(ds_def_t)*(rrd.stat_head->ds_cnt);
123 if((rrd.ds_def = rrd_realloc(rrd.ds_def,
124 old_size+sizeof(ds_def_t)))==NULL){
125 rrd_set_error("allocating rrd.ds_def");
129 memset(&rrd.ds_def[rrd.stat_head->ds_cnt], 0, sizeof(ds_def_t));
130 if (sscanf(&argv[i][3],
131 DS_NAM_FMT ":" DST_FMT ":%lu:%18[^:]:%18[^:]",
132 rrd.ds_def[rrd.stat_head->ds_cnt].ds_nam,
133 rrd.ds_def[rrd.stat_head->ds_cnt].dst,
134 &rrd.ds_def[rrd.stat_head->ds_cnt].par[DS_mrhb_cnt].u_cnt,
135 minstr,maxstr) == 5){
136 /* check for duplicate datasource names */
137 for(ii=0;ii<rrd.stat_head->ds_cnt;ii++){
138 if(strcmp(rrd.ds_def[rrd.stat_head->ds_cnt].ds_nam,
139 rrd.ds_def[ii].ds_nam) == 0){
140 rrd_set_error("Duplicate DS name: %s",rrd.ds_def[ii].ds_nam);
145 if(dst_conv(rrd.ds_def[rrd.stat_head->ds_cnt].dst) == -1){
149 if (minstr[0] == 'U' && minstr[1] == 0)
150 rrd.ds_def[rrd.stat_head->ds_cnt].par[DS_min_val].u_val = DNAN;
152 rrd.ds_def[rrd.stat_head->ds_cnt].par[DS_min_val].u_val = atof(minstr);
154 if (maxstr[0] == 'U' && maxstr[1] == 0)
155 rrd.ds_def[rrd.stat_head->ds_cnt].par[DS_max_val].u_val = DNAN;
157 rrd.ds_def[rrd.stat_head->ds_cnt].par[DS_max_val].u_val = atof(maxstr);
159 if (! isnan(rrd.ds_def[rrd.stat_head->ds_cnt].par[DS_min_val].u_val) &&
160 ! isnan(rrd.ds_def[rrd.stat_head->ds_cnt].par[DS_max_val].u_val) &&
161 rrd.ds_def[rrd.stat_head->ds_cnt].par[DS_min_val].u_val
162 >= rrd.ds_def[rrd.stat_head->ds_cnt].par[DS_max_val].u_val ) {
163 rrd_set_error("min must be less than max in DS definition");
167 rrd.stat_head->ds_cnt++;
169 rrd_set_error("can't parse argument '%s'",argv[i]);
173 } else if (strncmp(argv[i],"RRA:",3)==0){
174 size_t old_size = sizeof(rra_def_t)*(rrd.stat_head->rra_cnt);
175 if((rrd.rra_def = rrd_realloc(rrd.rra_def,
176 old_size+sizeof(rra_def_t)))==NULL)
178 rrd_set_error("allocating rrd.rra_def");
182 memset(&rrd.rra_def[rrd.stat_head->rra_cnt], 0, sizeof(rra_def_t));
184 token = strtok(&argv[i][4],":");
185 token_idx = error_flag = 0;
186 while (token != NULL)
191 if (sscanf(token,CF_NAM_FMT,
192 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) != 1)
193 rrd_set_error("Failed to parse CF name");
194 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
197 /* initialize some parameters */
198 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha].u_val = 0.1;
199 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].u_val = 1.0/288;
200 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
201 rrd.stat_head -> rra_cnt;
202 /* need to mark the file version */
203 strcpy(rrd.stat_head->version,RRD_VERSION);
207 /* initialize some parameters */
208 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val = 0.1;
211 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt = -1;
214 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_delta_pos].u_val = 2.0;
215 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_delta_neg].u_val = 2.0;
216 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt = 3;
217 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_failure_threshold].u_cnt = 2;
218 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt = -1;
220 /* invalid consolidation function */
222 rrd_set_error("Unrecognized consolidation function %s",
223 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
227 /* default: 1 pdp per cdp */
228 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = 1;
231 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
238 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
241 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_cdp_xff_val].u_val = atof(token);
242 if (rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_cdp_xff_val].u_val<0.0 ||
243 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_cdp_xff_val].u_val>=1.0)
244 rrd_set_error("Invalid xff: must be between 0 and 1");
249 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
252 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha].u_val = atof(token);
253 if (atof(token) <= 0.0 || atof(token) >= 1.0)
254 rrd_set_error("Invalid alpha: must be between 0 and 1");
258 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val =
260 if (atof(token) <= 0.0 || atof(token) >= 1.0)
261 rrd_set_error("Invalid gamma: must be between 0 and 1");
262 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_smooth_idx].u_cnt
263 = hashed_name % rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt;
266 /* specifies the # of violations that constitutes the failure threshold */
267 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_failure_threshold].u_cnt =
269 if (atoi(token) < 1 || atoi(token) > MAX_FAILURES_WINDOW_LEN)
270 rrd_set_error("Failure threshold is out of range %d, %d",1,
271 MAX_FAILURES_WINDOW_LEN);
274 /* specifies the index (1-based) of CF_DEVSEASONAL array
275 * associated with this CF_DEVPREDICT array. */
276 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
280 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = atoi(token);
285 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
288 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].u_val = atof(token);
289 if (atof(token) < 0.0 || atof(token) > 1.0)
290 rrd_set_error("Invalid beta: must be between 0 and 1");
294 /* specifies the index (1-based) of CF_HWPREDICT array
295 * associated with this CF_DEVSEASONAL or CF_SEASONAL array.
297 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
301 /* specifies the window length */
302 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt =
304 if (atoi(token) < 1 || atoi(token) > MAX_FAILURES_WINDOW_LEN)
305 rrd_set_error("Window length is out of range %d, %d",1,
306 MAX_FAILURES_WINDOW_LEN);
307 /* verify that window length exceeds the failure threshold */
308 if (rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt <
309 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_failure_threshold].u_cnt)
310 rrd_set_error("Window length is shorter than the failure threshold");
313 /* shouldn't be any more arguments */
314 rrd_set_error("Unexpected extra argument for consolidation function DEVPREDICT");
317 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
322 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
325 /* specifies the index (1-based) of CF_DEVSEASONAL array
326 * associated with this CF_DEVFAILURES array. */
327 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
331 /* length of the associated CF_SEASONAL and CF_DEVSEASONAL arrays. */
332 period = atoi(token);
333 if (period > rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt)
334 rrd_set_error("Length of seasonal cycle exceeds length of HW prediction array");
337 /* shouldn't be any more arguments */
338 rrd_set_error("Unexpected extra argument for consolidation function %s",
339 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
344 /* If we are here, this must be a CF_HWPREDICT RRA.
345 * Specifies the index (1-based) of CF_SEASONAL array
346 * associated with this CF_HWPREDICT array. If this argument
347 * is missing, then the CF_SEASONAL, CF_DEVSEASONAL, CF_DEVPREDICT,
349 * arrays are created automatically. */
350 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
354 /* should never get here */
355 rrd_set_error("Unknown error");
358 if (rrd_test_error())
360 /* all errors are unrecoverable */
364 token = strtok(NULL,":");
368 fprintf(stderr,"Creating RRA CF: %s, dep idx %lu, current idx %lu\n",
369 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam,
370 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt,
371 rrd.stat_head -> rra_cnt);
373 /* should we create CF_SEASONAL, CF_DEVSEASONAL, and CF_DEVPREDICT? */
374 if (cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) == CF_HWPREDICT
375 && rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt
376 == rrd.stat_head -> rra_cnt)
379 fprintf(stderr,"Creating HW contingent RRAs\n");
381 if (create_hw_contingent_rras(&rrd,period,hashed_name) == -1) {
386 rrd.stat_head->rra_cnt++;
388 rrd_set_error("can't parse argument '%s'",argv[i]);
395 if (rrd.stat_head->rra_cnt < 1){
396 rrd_set_error("you must define at least one Round Robin Archive");
401 if (rrd.stat_head->ds_cnt < 1){
402 rrd_set_error("you must define at least one Data Source");
406 return rrd_create_fn(argv[optind],&rrd);
409 /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs
410 * associated with a CF_HWPREDICT RRA. */
412 create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashed_name)
415 rra_def_t* current_rra;
417 /* save index to CF_HWPREDICT */
418 unsigned long hw_index = rrd -> stat_head -> rra_cnt;
419 /* advance the pointer */
420 (rrd -> stat_head -> rra_cnt)++;
421 /* allocate the memory for the 4 contingent RRAs */
422 old_size = sizeof(rra_def_t)*(rrd -> stat_head->rra_cnt);
423 if ((rrd -> rra_def = rrd_realloc(rrd -> rra_def,
424 old_size+4*sizeof(rra_def_t)))==NULL)
426 rrd_set_error("allocating rrd.rra_def");
430 memset(&(rrd -> rra_def[rrd -> stat_head->rra_cnt]), 0, 4*sizeof(rra_def_t));
432 /* create the CF_SEASONAL RRA */
433 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
434 strcpy(current_rra -> cf_nam,"SEASONAL");
435 current_rra -> row_cnt = period;
436 current_rra -> par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
437 current_rra -> pdp_cnt = 1;
438 current_rra -> par[RRA_seasonal_gamma].u_val =
439 rrd -> rra_def[hw_index].par[RRA_hw_alpha].u_val;
440 current_rra -> par[RRA_dependent_rra_idx].u_cnt = hw_index;
441 rrd -> rra_def[hw_index].par[RRA_dependent_rra_idx].u_cnt = rrd -> stat_head -> rra_cnt;
443 /* create the CF_DEVSEASONAL RRA */
444 (rrd -> stat_head -> rra_cnt)++;
445 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
446 strcpy(current_rra -> cf_nam,"DEVSEASONAL");
447 current_rra -> row_cnt = period;
448 current_rra -> par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
449 current_rra -> pdp_cnt = 1;
450 current_rra -> par[RRA_seasonal_gamma].u_val =
451 rrd -> rra_def[hw_index].par[RRA_hw_alpha].u_val;
452 current_rra -> par[RRA_dependent_rra_idx].u_cnt = hw_index;
454 /* create the CF_DEVPREDICT RRA */
455 (rrd -> stat_head -> rra_cnt)++;
456 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
457 strcpy(current_rra -> cf_nam,"DEVPREDICT");
458 current_rra -> row_cnt = (rrd -> rra_def[hw_index]).row_cnt;
459 current_rra -> pdp_cnt = 1;
460 current_rra -> par[RRA_dependent_rra_idx].u_cnt
461 = hw_index + 2; /* DEVSEASONAL */
463 /* create the CF_FAILURES RRA */
464 (rrd -> stat_head -> rra_cnt)++;
465 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
466 strcpy(current_rra -> cf_nam,"FAILURES");
467 current_rra -> row_cnt = period;
468 current_rra -> pdp_cnt = 1;
469 current_rra -> par[RRA_delta_pos].u_val = 2.0;
470 current_rra -> par[RRA_delta_neg].u_val = 2.0;
471 current_rra -> par[RRA_failure_threshold].u_cnt = 7;
472 current_rra -> par[RRA_window_len].u_cnt = 9;
473 current_rra -> par[RRA_dependent_rra_idx].u_cnt =
474 hw_index + 2; /* DEVSEASONAL */
478 /* create and empty rrd file according to the specs given */
481 rrd_create_fn(char *file_name, rrd_t *rrd)
485 rrd_value_t unknown = DNAN ;
487 if ((rrd_file = fopen(file_name,"wb")) == NULL ) {
488 rrd_set_error("creating '%s': %s",file_name,strerror(errno));
489 free(rrd->stat_head);
495 fwrite(rrd->stat_head,
496 sizeof(stat_head_t), 1, rrd_file);
499 sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
502 sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
504 fwrite(rrd->live_head,
505 sizeof(live_head_t),1, rrd_file);
507 if((rrd->pdp_prep = calloc(1,sizeof(pdp_prep_t))) == NULL){
508 rrd_set_error("allocating pdp_prep");
514 strcpy(rrd->pdp_prep->last_ds,"UNKN");
516 rrd->pdp_prep->scratch[PDP_val].u_val = 0.0;
517 rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt =
518 rrd->live_head->last_up % rrd->stat_head->pdp_step;
520 for(i=0; i < rrd->stat_head->ds_cnt; i++)
521 fwrite( rrd->pdp_prep,sizeof(pdp_prep_t),1,rrd_file);
523 if((rrd->cdp_prep = calloc(1,sizeof(cdp_prep_t))) == NULL){
524 rrd_set_error("allocating cdp_prep");
531 for(i=0; i < rrd->stat_head->rra_cnt; i++) {
532 switch (cf_conv(rrd->rra_def[i].cf_nam))
535 rrd->cdp_prep->scratch[CDP_hw_intercept].u_val = DNAN;
536 rrd->cdp_prep->scratch[CDP_hw_last_intercept].u_val = DNAN;
537 rrd->cdp_prep->scratch[CDP_hw_slope].u_val = DNAN;
538 rrd->cdp_prep->scratch[CDP_hw_last_slope].u_val = DNAN;
539 rrd->cdp_prep->scratch[CDP_null_count].u_cnt = 1;
540 rrd->cdp_prep->scratch[CDP_last_null_count].u_cnt = 1;
544 rrd->cdp_prep->scratch[CDP_hw_seasonal].u_val = DNAN;
545 rrd->cdp_prep->scratch[CDP_hw_last_seasonal].u_val = DNAN;
546 rrd->cdp_prep->scratch[CDP_init_seasonal].u_cnt = 1;
549 /* initialize violation history to 0 */
550 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
552 /* We can zero everything out, by setting u_val to the
553 * NULL address. Each array entry in scratch is 8 bytes
554 * (a double), but u_cnt only accessed 4 bytes (long) */
555 rrd->cdp_prep->scratch[ii].u_val = 0.0;
559 /* can not be zero because we don't know anything ... */
560 rrd->cdp_prep->scratch[CDP_val].u_val = DNAN;
561 /* startup missing pdp count */
562 rrd->cdp_prep->scratch[CDP_unkn_pdp_cnt].u_cnt =
563 ((rrd->live_head->last_up -
564 rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt)
565 % (rrd->stat_head->pdp_step
566 * rrd->rra_def[i].pdp_cnt)) / rrd->stat_head->pdp_step;
570 for(ii=0; ii < rrd->stat_head->ds_cnt; ii++)
572 fwrite( rrd->cdp_prep,sizeof(cdp_prep_t),1,rrd_file);
576 /* now, we must make sure that the rest of the rrd
577 struct is properly initialized */
579 if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t))) == NULL) {
580 rrd_set_error("allocating rra_ptr");
586 /* changed this initialization to be consistent with
587 * rrd_restore. With the old value (0), the first update
588 * would occur for cur_row = 1 because rrd_update increments
589 * the pointer a priori. */
590 for (i=0; i < rrd->stat_head->rra_cnt; i++)
592 rrd->rra_ptr->cur_row = rrd->rra_def[i].row_cnt - 1;
593 fwrite( rrd->rra_ptr, sizeof(rra_ptr_t),1,rrd_file);
596 /* write the empty data area */
598 i < rrd->stat_head->rra_cnt;
602 ii < rrd->rra_def[i].row_cnt
603 * rrd->stat_head->ds_cnt;
605 fwrite(&unknown,sizeof(rrd_value_t),1,rrd_file);
609 /* lets see if we had an error */
610 if(ferror(rrd_file)){
611 rrd_set_error("a file error occurred while creating '%s'",file_name);