1 /*****************************************************************************
2 * RRDtool 1.1.x Copyright Tobias Oetiker, 1997 - 2002
3 *****************************************************************************
4 * rrd_create.c creates new rrds
5 *****************************************************************************/
8 #include "rrd_rpncalc.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);
17 rrd_create(int argc, char **argv)
23 struct time_value last_up_tv;
24 char *parsetime_error = NULL;
26 unsigned short token_idx, error_flag, period=0;
27 unsigned long hashed_name;
29 last_up = time(NULL)-10;
33 if((rrd.stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
34 rrd_set_error("allocating rrd.stat_head");
39 if((rrd.live_head = calloc(1,sizeof(live_head_t)))==NULL){
40 rrd_set_error("allocating rrd.live_head");
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 */
58 static struct option long_options[] =
60 {"start", required_argument, 0, 'b'},
61 {"step", required_argument,0,'s'},
66 opt = getopt_long(argc, argv, "b:s:",
67 long_options, &option_index);
74 if ((parsetime_error = parsetime(optarg, &last_up_tv))) {
75 rrd_set_error("start time: %s", parsetime_error );
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");
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");
97 long_tmp = atol(optarg);
99 rrd_set_error("step size should be no less than one second");
103 rrd.stat_head->pdp_step = long_tmp;
108 rrd_set_error("unknown option '%c'", optopt);
110 rrd_set_error("unknown option '%s'",argv[optind-1]);
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
121 hashed_name = FnvHash(argv[optind]);
122 for(i=optind+1;i<argc;i++){
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");
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)
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);
146 rrd_set_error("invalid DS format");
149 /* parse the remainder of the arguments */
150 switch(dst_conv(rrd.ds_def[rrd.stat_head->ds_cnt].dst))
156 parseGENERIC_DS(&argv[i][offset+3],&rrd, rrd.stat_head->ds_cnt);
159 parseCDEF_DS(&argv[i][offset+3],&rrd, rrd.stat_head->ds_cnt);
162 rrd_set_error("invalid DS type specified");
166 if (rrd_test_error()) {
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)
176 rrd_set_error("allocating rrd.rra_def");
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)
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))
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);
205 /* initialize some parameters */
206 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val = 0.1;
209 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt = -1;
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;
218 /* invalid consolidation function */
220 rrd_set_error("Unrecognized consolidation function %s",
221 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
225 /* default: 1 pdp per cdp */
226 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = 1;
229 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
236 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
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");
247 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
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");
256 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val =
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;
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 =
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);
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 =
278 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = atoi(token);
283 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
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");
292 /* specifies the index (1-based) of CF_HWPREDICT array
293 * associated with this CF_DEVSEASONAL or CF_SEASONAL array.
295 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
299 /* specifies the window length */
300 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt =
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");
311 /* shouldn't be any more arguments */
312 rrd_set_error("Unexpected extra argument for consolidation function DEVPREDICT");
315 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
320 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
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 =
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");
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);
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,
347 * arrays are created automatically. */
348 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
352 /* should never get here */
353 rrd_set_error("Unknown error");
356 if (rrd_test_error())
358 /* all errors are unrecoverable */
362 token = strtok(NULL,":");
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);
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)
377 fprintf(stderr,"Creating HW contingent RRAs\n");
379 if (create_hw_contingent_rras(&rrd,period,hashed_name) == -1) {
384 rrd.stat_head->rra_cnt++;
386 rrd_set_error("can't parse argument '%s'",argv[i]);
393 if (rrd.stat_head->rra_cnt < 1){
394 rrd_set_error("you must define at least one Round Robin Archive");
399 if (rrd.stat_head->ds_cnt < 1){
400 rrd_set_error("you must define at least one Data Source");
404 return rrd_create_fn(argv[optind],&rrd);
407 void parseGENERIC_DS(char *def,rrd_t *rrd, int ds_idx)
409 char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];
413 temp = sscanf(def,"%lu:%18[^:]:%18[^:]",
414 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
417 if (sscanf(def,"%lu:%18[^:]:%18[^:]",
418 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
421 if (minstr[0] == 'U' && minstr[1] == 0)
422 rrd -> ds_def[ds_idx].par[DS_min_val].u_val = DNAN;
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;
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");
439 rrd_set_error("failed to parse data source %s", def);
443 /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs
444 * associated with a CF_HWPREDICT RRA. */
446 create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashed_name)
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)
460 rrd_set_error("allocating rrd.rra_def");
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 */
512 /* create and empty rrd file according to the specs given */
515 rrd_create_fn(char *file_name, rrd_t *rrd)
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);
529 fwrite(rrd->stat_head,
530 sizeof(stat_head_t), 1, rrd_file);
533 sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
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");
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");
565 for(i=0; i < rrd->stat_head->rra_cnt; i++) {
566 switch (cf_conv(rrd->rra_def[i].cf_nam))
569 init_hwpredict_cdp(rrd->cdp_prep);
573 init_seasonal_cdp(rrd->cdp_prep);
576 /* initialize violation history to 0 */
577 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
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;
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;
597 for(ii=0; ii < rrd->stat_head->ds_cnt; ii++)
599 fwrite( rrd->cdp_prep,sizeof(cdp_prep_t),1,rrd_file);
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");
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++)
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);
623 /* write the empty data area */
625 i < rrd->stat_head->rra_cnt;
629 ii < rrd->rra_def[i].row_cnt
630 * rrd->stat_head->ds_cnt;
632 fwrite(&unknown,sizeof(rrd_value_t),1,rrd_file);
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);