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 #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 time_value last_up_tv;
23 char *parsetime_error = NULL;
28 static struct option long_options[] =
30 {"start", required_argument, 0, 'b'},
31 {"step", required_argument,0,'s'},
36 opt = getopt_long(argc, argv, "b:s:",
37 long_options, &option_index);
44 if ((parsetime_error = parsetime(optarg, &last_up_tv))) {
45 rrd_set_error("start time: %s", parsetime_error );
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");
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");
64 long_tmp = atol(optarg);
66 rrd_set_error("step size should be no less than one second");
74 rrd_set_error("unknown option '%c'", optopt);
76 rrd_set_error("unknown option '%s'",argv[optind-1]);
81 rc = rrd_create_r(argv[optind],
83 argc - optind - 1, argv + optind + 1);
90 rrd_create_r(char *filename,
91 unsigned long pdp_step, time_t last_up,
92 int argc, char **argv)
98 unsigned short token_idx, error_flag, period=0;
99 unsigned long hashed_name;
104 if((rrd.stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
105 rrd_set_error("allocating rrd.stat_head");
110 if((rrd.live_head = calloc(1,sizeof(live_head_t)))==NULL){
111 rrd_set_error("allocating rrd.live_head");
115 /* set some defaults */
116 strcpy(rrd.stat_head->cookie,RRD_COOKIE);
117 /* assume the will be version 1 compatible */
118 strcpy(rrd.stat_head->version,"0001");
119 rrd.stat_head->float_cookie = FLOAT_COOKIE;
120 rrd.stat_head->ds_cnt = 0; /* this will be adjusted later */
121 rrd.stat_head->rra_cnt = 0; /* ditto */
122 rrd.stat_head->pdp_step = pdp_step; /* 5 minute default */
124 /* a default value */
128 rrd.live_head->last_up = last_up;
130 /* optind points to the first non-option command line arg,
131 * in this case, the file name. */
132 /* Compute the FNV hash value (used by SEASONAL and DEVSEASONAL
134 hashed_name = FnvHash(filename);
137 if (strncmp(argv[i],"DS:",3)==0){
138 size_t old_size = sizeof(ds_def_t)*(rrd.stat_head->ds_cnt);
139 if((rrd.ds_def = rrd_realloc(rrd.ds_def,
140 old_size+sizeof(ds_def_t)))==NULL){
141 rrd_set_error("allocating rrd.ds_def");
145 memset(&rrd.ds_def[rrd.stat_head->ds_cnt], 0, sizeof(ds_def_t));
146 /* extract the name and type */
147 if (sscanf(&argv[i][3],
148 DS_NAM_FMT ":" DST_FMT ":%n",
149 rrd.ds_def[rrd.stat_head->ds_cnt].ds_nam,
150 rrd.ds_def[rrd.stat_head->ds_cnt].dst,&offset) == 2)
152 /* check for duplicate datasource names */
153 for(ii=0;ii<rrd.stat_head->ds_cnt;ii++)
154 if(strcmp(rrd.ds_def[rrd.stat_head->ds_cnt].ds_nam,
155 rrd.ds_def[ii].ds_nam) == 0){
156 rrd_set_error("Duplicate DS name: %s",rrd.ds_def[ii].ds_nam);
159 rrd_set_error("invalid DS format");
162 /* parse the remainder of the arguments */
163 switch(dst_conv(rrd.ds_def[rrd.stat_head->ds_cnt].dst))
169 parseGENERIC_DS(&argv[i][offset+3],&rrd, rrd.stat_head->ds_cnt);
172 parseCDEF_DS(&argv[i][offset+3],&rrd, rrd.stat_head->ds_cnt);
173 /* need to mark the file w/ current version */
174 strcpy(rrd.stat_head->version,RRD_VERSION);
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;
216 /* need to mark the file w/ current version */
217 strcpy(rrd.stat_head->version,RRD_VERSION);
221 /* initialize some parameters */
222 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val = 0.1;
225 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt = -1;
228 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_delta_pos].u_val = 2.0;
229 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_delta_neg].u_val = 2.0;
230 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt = 3;
231 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_failure_threshold].u_cnt = 2;
232 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt = -1;
234 /* invalid consolidation function */
236 rrd_set_error("Unrecognized consolidation function %s",
237 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
241 /* default: 1 pdp per cdp */
242 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = 1;
245 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
252 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
255 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_cdp_xff_val].u_val = atof(token);
256 if (rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_cdp_xff_val].u_val<0.0 ||
257 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_cdp_xff_val].u_val>=1.0)
258 rrd_set_error("Invalid xff: must be between 0 and 1");
263 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
266 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha].u_val = atof(token);
267 if (atof(token) <= 0.0 || atof(token) >= 1.0)
268 rrd_set_error("Invalid alpha: must be between 0 and 1");
272 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val =
274 if (atof(token) <= 0.0 || atof(token) >= 1.0)
275 rrd_set_error("Invalid gamma: must be between 0 and 1");
276 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_smooth_idx].u_cnt
277 = hashed_name % rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt;
280 /* specifies the # of violations that constitutes the failure threshold */
281 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_failure_threshold].u_cnt =
283 if (atoi(token) < 1 || atoi(token) > MAX_FAILURES_WINDOW_LEN)
284 rrd_set_error("Failure threshold is out of range %d, %d",1,
285 MAX_FAILURES_WINDOW_LEN);
288 /* specifies the index (1-based) of CF_DEVSEASONAL array
289 * associated with this CF_DEVPREDICT array. */
290 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
294 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = atoi(token);
299 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
302 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].u_val = atof(token);
303 if (atof(token) < 0.0 || atof(token) > 1.0)
304 rrd_set_error("Invalid beta: must be between 0 and 1");
308 /* specifies the index (1-based) of CF_HWPREDICT array
309 * associated with this CF_DEVSEASONAL or CF_SEASONAL array.
311 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
315 /* specifies the window length */
316 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt =
318 if (atoi(token) < 1 || atoi(token) > MAX_FAILURES_WINDOW_LEN)
319 rrd_set_error("Window length is out of range %d, %d",1,
320 MAX_FAILURES_WINDOW_LEN);
321 /* verify that window length exceeds the failure threshold */
322 if (rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt <
323 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_failure_threshold].u_cnt)
324 rrd_set_error("Window length is shorter than the failure threshold");
327 /* shouldn't be any more arguments */
328 rrd_set_error("Unexpected extra argument for consolidation function DEVPREDICT");
331 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
336 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
339 /* specifies the index (1-based) of CF_DEVSEASONAL array
340 * associated with this CF_DEVFAILURES array. */
341 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
345 /* length of the associated CF_SEASONAL and CF_DEVSEASONAL arrays. */
346 period = atoi(token);
347 if (period > rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt)
348 rrd_set_error("Length of seasonal cycle exceeds length of HW prediction array");
351 /* shouldn't be any more arguments */
352 rrd_set_error("Unexpected extra argument for consolidation function %s",
353 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
358 /* If we are here, this must be a CF_HWPREDICT RRA.
359 * Specifies the index (1-based) of CF_SEASONAL array
360 * associated with this CF_HWPREDICT array. If this argument
361 * is missing, then the CF_SEASONAL, CF_DEVSEASONAL, CF_DEVPREDICT,
363 * arrays are created automatically. */
364 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
368 /* should never get here */
369 rrd_set_error("Unknown error");
372 if (rrd_test_error())
374 /* all errors are unrecoverable */
378 token = strtok_r(NULL,":", &tokptr);
382 fprintf(stderr,"Creating RRA CF: %s, dep idx %lu, current idx %lu\n",
383 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam,
384 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt,
385 rrd.stat_head -> rra_cnt);
387 /* should we create CF_SEASONAL, CF_DEVSEASONAL, and CF_DEVPREDICT? */
388 if (cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) == CF_HWPREDICT
389 && rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt
390 == rrd.stat_head -> rra_cnt)
393 fprintf(stderr,"Creating HW contingent RRAs\n");
395 if (create_hw_contingent_rras(&rrd,period,hashed_name) == -1) {
400 rrd.stat_head->rra_cnt++;
402 rrd_set_error("can't parse argument '%s'",argv[i]);
409 if (rrd.stat_head->rra_cnt < 1){
410 rrd_set_error("you must define at least one Round Robin Archive");
415 if (rrd.stat_head->ds_cnt < 1){
416 rrd_set_error("you must define at least one Data Source");
420 return rrd_create_fn(filename, &rrd);
423 void parseGENERIC_DS(char *def,rrd_t *rrd, int ds_idx)
425 char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];
429 temp = sscanf(def,"%lu:%18[^:]:%18[^:]",
430 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
433 if (sscanf(def,"%lu:%18[^:]:%18[^:]",
434 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
437 if (minstr[0] == 'U' && minstr[1] == 0)
438 rrd -> ds_def[ds_idx].par[DS_min_val].u_val = DNAN;
440 rrd -> ds_def[ds_idx].par[DS_min_val].u_val = atof(minstr);
442 if (maxstr[0] == 'U' && maxstr[1] == 0)
443 rrd -> ds_def[ds_idx].par[DS_max_val].u_val = DNAN;
445 rrd -> ds_def[ds_idx].par[DS_max_val].u_val = atof(maxstr);
447 if (! isnan(rrd -> ds_def[ds_idx].par[DS_min_val].u_val) &&
448 ! isnan(rrd -> ds_def[ds_idx].par[DS_max_val].u_val) &&
449 rrd -> ds_def[ds_idx].par[DS_min_val].u_val
450 >= rrd -> ds_def[ds_idx].par[DS_max_val].u_val ) {
451 rrd_set_error("min must be less than max in DS definition");
455 rrd_set_error("failed to parse data source %s", def);
459 /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs
460 * associated with a CF_HWPREDICT RRA. */
462 create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashed_name)
465 rra_def_t* current_rra;
467 /* save index to CF_HWPREDICT */
468 unsigned long hw_index = rrd -> stat_head -> rra_cnt;
469 /* advance the pointer */
470 (rrd -> stat_head -> rra_cnt)++;
471 /* allocate the memory for the 4 contingent RRAs */
472 old_size = sizeof(rra_def_t)*(rrd -> stat_head->rra_cnt);
473 if ((rrd -> rra_def = rrd_realloc(rrd -> rra_def,
474 old_size+4*sizeof(rra_def_t)))==NULL)
476 rrd_set_error("allocating rrd.rra_def");
480 memset(&(rrd -> rra_def[rrd -> stat_head->rra_cnt]), 0, 4*sizeof(rra_def_t));
482 /* create the CF_SEASONAL RRA */
483 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
484 strcpy(current_rra -> cf_nam,"SEASONAL");
485 current_rra -> row_cnt = period;
486 current_rra -> par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
487 current_rra -> pdp_cnt = 1;
488 current_rra -> par[RRA_seasonal_gamma].u_val =
489 rrd -> rra_def[hw_index].par[RRA_hw_alpha].u_val;
490 current_rra -> par[RRA_dependent_rra_idx].u_cnt = hw_index;
491 rrd -> rra_def[hw_index].par[RRA_dependent_rra_idx].u_cnt = rrd -> stat_head -> rra_cnt;
493 /* create the CF_DEVSEASONAL RRA */
494 (rrd -> stat_head -> rra_cnt)++;
495 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
496 strcpy(current_rra -> cf_nam,"DEVSEASONAL");
497 current_rra -> row_cnt = period;
498 current_rra -> par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
499 current_rra -> pdp_cnt = 1;
500 current_rra -> par[RRA_seasonal_gamma].u_val =
501 rrd -> rra_def[hw_index].par[RRA_hw_alpha].u_val;
502 current_rra -> par[RRA_dependent_rra_idx].u_cnt = hw_index;
504 /* create the CF_DEVPREDICT RRA */
505 (rrd -> stat_head -> rra_cnt)++;
506 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
507 strcpy(current_rra -> cf_nam,"DEVPREDICT");
508 current_rra -> row_cnt = (rrd -> rra_def[hw_index]).row_cnt;
509 current_rra -> pdp_cnt = 1;
510 current_rra -> par[RRA_dependent_rra_idx].u_cnt
511 = hw_index + 2; /* DEVSEASONAL */
513 /* create the CF_FAILURES RRA */
514 (rrd -> stat_head -> rra_cnt)++;
515 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
516 strcpy(current_rra -> cf_nam,"FAILURES");
517 current_rra -> row_cnt = period;
518 current_rra -> pdp_cnt = 1;
519 current_rra -> par[RRA_delta_pos].u_val = 2.0;
520 current_rra -> par[RRA_delta_neg].u_val = 2.0;
521 current_rra -> par[RRA_failure_threshold].u_cnt = 7;
522 current_rra -> par[RRA_window_len].u_cnt = 9;
523 current_rra -> par[RRA_dependent_rra_idx].u_cnt =
524 hw_index + 2; /* DEVSEASONAL */
528 /* create and empty rrd file according to the specs given */
531 rrd_create_fn(char *file_name, rrd_t *rrd)
535 rrd_value_t unknown = DNAN ;
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 */
641 i < rrd->stat_head->rra_cnt;
645 ii < rrd->rra_def[i].row_cnt
646 * rrd->stat_head->ds_cnt;
648 fwrite(&unknown,sizeof(rrd_value_t),1,rrd_file);
652 /* lets see if we had an error */
653 if(ferror(rrd_file)){
654 rrd_set_error("a file error occurred while creating '%s'",file_name);