1 /*****************************************************************************
2 * RRDtool 1.2rc5 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;
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");
111 if((rrd.live_head = calloc(1,sizeof(live_head_t)))==NULL){
112 rrd_set_error("allocating rrd.live_head");
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 */
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
135 hashed_name = FnvHash(filename);
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");
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)
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);
160 rrd_set_error("invalid DS format");
163 /* parse the remainder of the arguments */
164 switch(dst_conv(rrd.ds_def[rrd.stat_head->ds_cnt].dst))
170 parseGENERIC_DS(&argv[i][offset+3],&rrd, rrd.stat_head->ds_cnt);
173 parseCDEF_DS(&argv[i][offset+3],&rrd, rrd.stat_head->ds_cnt);
176 rrd_set_error("invalid DS type specified");
180 if (rrd_test_error()) {
184 rrd.stat_head -> ds_cnt++;
185 } else if (strncmp(argv[i],"RRA:",3)==0){
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)
191 rrd_set_error("allocating rrd.rra_def");
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)
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))
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;
218 /* initialize some parameters */
219 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val = 0.1;
222 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt = -1;
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;
231 /* invalid consolidation function */
233 rrd_set_error("Unrecognized consolidation function %s",
234 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
238 /* default: 1 pdp per cdp */
239 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = 1;
242 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
249 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
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");
260 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
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");
269 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val =
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;
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 =
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);
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 =
291 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = atoi(token);
296 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
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");
305 /* specifies the index (1-based) of CF_HWPREDICT array
306 * associated with this CF_DEVSEASONAL or CF_SEASONAL array.
308 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
312 /* specifies the window length */
313 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt =
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");
324 /* shouldn't be any more arguments */
325 rrd_set_error("Unexpected extra argument for consolidation function DEVPREDICT");
328 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
333 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
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 =
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");
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);
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,
360 * arrays are created automatically. */
361 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
365 /* should never get here */
366 rrd_set_error("Unknown error");
369 if (rrd_test_error())
371 /* all errors are unrecoverable */
375 token = strtok_r(NULL,":", &tokptr);
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);
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)
390 fprintf(stderr,"Creating HW contingent RRAs\n");
392 if (create_hw_contingent_rras(&rrd,period,hashed_name) == -1) {
393 rrd_set_error("creating contingent RRA");
398 rrd.stat_head->rra_cnt++;
400 rrd_set_error("can't parse argument '%s'",argv[i]);
407 if (rrd.stat_head->rra_cnt < 1){
408 rrd_set_error("you must define at least one Round Robin Archive");
413 if (rrd.stat_head->ds_cnt < 1){
414 rrd_set_error("you must define at least one Data Source");
418 return rrd_create_fn(filename, &rrd);
421 void parseGENERIC_DS(char *def,rrd_t *rrd, int ds_idx)
423 char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];
427 temp = sscanf(def,"%lu:%18[^:]:%18[^:]",
428 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
431 if (sscanf(def,"%lu:%18[^:]:%18[^:]",
432 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
435 if (minstr[0] == 'U' && minstr[1] == 0)
436 rrd -> ds_def[ds_idx].par[DS_min_val].u_val = DNAN;
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;
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");
453 rrd_set_error("failed to parse data source %s", def);
457 /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs
458 * associated with a CF_HWPREDICT RRA. */
460 create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashed_name)
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)
474 rrd_set_error("allocating rrd.rra_def");
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 */
526 /* create and empty rrd file according to the specs given */
529 rrd_create_fn(char *file_name, rrd_t *rrd)
533 rrd_value_t *unknown;
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);
544 fwrite(rrd->stat_head,
545 sizeof(stat_head_t), 1, rrd_file);
548 sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
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");
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");
580 for(i=0; i < rrd->stat_head->rra_cnt; i++) {
581 switch (cf_conv(rrd->rra_def[i].cf_nam))
584 init_hwpredict_cdp(rrd->cdp_prep);
588 init_seasonal_cdp(rrd->cdp_prep);
591 /* initialize violation history to 0 */
592 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
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;
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;
612 for(ii=0; ii < rrd->stat_head->ds_cnt; ii++)
614 fwrite( rrd->cdp_prep,sizeof(cdp_prep_t),1,rrd_file);
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");
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++)
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);
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");
645 for (i = 0; i < 512; ++i)
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);
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);