1 /*****************************************************************************
2 * RRDtool 1.2.0 Copyright by Tobi Oetiker, 1997-2005
3 *****************************************************************************
4 * rrd_create.c creates new rrds
5 *****************************************************************************/
8 #include "rrd_rpncalc.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);
18 rrd_create(int argc, char **argv)
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;
26 optind = 0; opterr = 0; /* initialize getopt */
29 static struct option long_options[] =
31 {"start", required_argument, 0, 'b'},
32 {"step", required_argument,0,'s'},
37 opt = getopt_long(argc, argv, "b:s:",
38 long_options, &option_index);
45 if ((parsetime_error = parsetime(optarg, &last_up_tv))) {
46 rrd_set_error("start time: %s", parsetime_error );
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");
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");
65 long_tmp = atol(optarg);
67 rrd_set_error("step size should be no less than one second");
75 rrd_set_error("unknown option '%c'", optopt);
77 rrd_set_error("unknown option '%s'",argv[optind-1]);
82 rc = rrd_create_r(argv[optind],
84 argc - optind - 1, argv + optind + 1);
91 rrd_create_r(char *filename,
92 unsigned long pdp_step, time_t last_up,
93 int argc, char **argv)
99 unsigned short token_idx, error_flag, period=0;
100 unsigned long hashed_name;
105 if((rrd.stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
106 rrd_set_error("allocating rrd.stat_head");
112 if((rrd.live_head = calloc(1,sizeof(live_head_t)))==NULL){
113 rrd_set_error("allocating rrd.live_head");
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 */
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
136 hashed_name = FnvHash(filename);
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");
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)
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);
161 rrd_set_error("invalid DS format");
164 /* parse the remainder of the arguments */
165 switch(dst_conv(rrd.ds_def[rrd.stat_head->ds_cnt].dst))
171 parseGENERIC_DS(&argv[i][offset+3],&rrd, rrd.stat_head->ds_cnt);
174 parseCDEF_DS(&argv[i][offset+3],&rrd, rrd.stat_head->ds_cnt);
177 rrd_set_error("invalid DS type specified");
181 if (rrd_test_error()) {
185 rrd.stat_head -> ds_cnt++;
186 } else if (strncmp(argv[i],"RRA:",3)==0){
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)
192 rrd_set_error("allocating rrd.rra_def");
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)
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))
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;
219 /* initialize some parameters */
220 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val = 0.1;
223 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt = -1;
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;
232 /* invalid consolidation function */
234 rrd_set_error("Unrecognized consolidation function %s",
235 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
239 /* default: 1 pdp per cdp */
240 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = 1;
243 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
250 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
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");
261 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
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");
270 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val =
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;
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 =
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);
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 =
292 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = atoi(token);
297 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
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");
306 /* specifies the index (1-based) of CF_HWPREDICT array
307 * associated with this CF_DEVSEASONAL or CF_SEASONAL array.
309 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
313 /* specifies the window length */
314 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt =
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");
325 /* shouldn't be any more arguments */
326 rrd_set_error("Unexpected extra argument for consolidation function DEVPREDICT");
329 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
334 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
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 =
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");
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);
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,
361 * arrays are created automatically. */
362 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
366 /* should never get here */
367 rrd_set_error("Unknown error");
370 if (rrd_test_error())
372 /* all errors are unrecoverable */
376 token = strtok_r(NULL,":", &tokptr);
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);
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)
391 fprintf(stderr,"Creating HW contingent RRAs\n");
393 if (create_hw_contingent_rras(&rrd,period,hashed_name) == -1) {
394 rrd_set_error("creating contingent RRA");
399 rrd.stat_head->rra_cnt++;
401 rrd_set_error("can't parse argument '%s'",argv[i]);
408 if (rrd.stat_head->rra_cnt < 1){
409 rrd_set_error("you must define at least one Round Robin Archive");
414 if (rrd.stat_head->ds_cnt < 1){
415 rrd_set_error("you must define at least one Data Source");
419 return rrd_create_fn(filename, &rrd);
422 void parseGENERIC_DS(char *def,rrd_t *rrd, int ds_idx)
424 char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];
428 temp = sscanf(def,"%lu:%18[^:]:%18[^:]",
429 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
432 if (sscanf(def,"%lu:%18[^:]:%18[^:]",
433 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
436 if (minstr[0] == 'U' && minstr[1] == 0)
437 rrd -> ds_def[ds_idx].par[DS_min_val].u_val = DNAN;
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;
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");
454 rrd_set_error("failed to parse data source %s", def);
458 /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs
459 * associated with a CF_HWPREDICT RRA. */
461 create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashed_name)
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)
475 rrd_set_error("allocating rrd.rra_def");
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 */
527 /* create and empty rrd file according to the specs given */
530 rrd_create_fn(char *file_name, rrd_t *rrd)
534 rrd_value_t *unknown;
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);
545 fwrite(rrd->stat_head,
546 sizeof(stat_head_t), 1, rrd_file);
549 sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
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");
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");
581 for(i=0; i < rrd->stat_head->rra_cnt; i++) {
582 switch (cf_conv(rrd->rra_def[i].cf_nam))
585 init_hwpredict_cdp(rrd->cdp_prep);
589 init_seasonal_cdp(rrd->cdp_prep);
592 /* initialize violation history to 0 */
593 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
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;
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;
613 for(ii=0; ii < rrd->stat_head->ds_cnt; ii++)
615 fwrite( rrd->cdp_prep,sizeof(cdp_prep_t),1,rrd_file);
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");
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++)
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);
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");
646 for (i = 0; i < 512; ++i)
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);
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);