make sure vdef calcs only calc as far as originally requested.
[rrdtool.git] / src / rrd_create.c
1 /*****************************************************************************
2  * RRDtool 1.3rc6  Copyright by Tobi Oetiker, 1997-2008
3  *****************************************************************************
4  * rrd_create.c  creates new rrds
5  *****************************************************************************/
6
7 #include <stdlib.h>
8 #include <time.h>
9 #include <locale.h>
10
11 #include "rrd_tool.h"
12 #include "rrd_rpncalc.h"
13 #include "rrd_hw.h"
14
15 #include "rrd_is_thread_safe.h"
16
17 unsigned long FnvHash(
18     const char *str);
19 int       create_hw_contingent_rras(
20     rrd_t *rrd,
21     unsigned short period,
22     unsigned long hashed_name);
23 void      parseGENERIC_DS(
24     const char *def,
25     rrd_t *rrd,
26     int ds_idx);
27 long int  rra_random_row(
28     rra_def_t *);
29
30 int rrd_create(
31     int argc,
32     char **argv)
33 {
34     struct option long_options[] = {
35         {"start", required_argument, 0, 'b'},
36         {"step", required_argument, 0, 's'},
37         {0, 0, 0, 0}
38     };
39     int       option_index = 0;
40     int       opt;
41     time_t    last_up = time(NULL) - 10;
42     unsigned long pdp_step = 300;
43     struct rrd_time_value last_up_tv;
44     char     *parsetime_error = NULL;
45     long      long_tmp;
46     int       rc;
47
48     optind = 0;
49     opterr = 0;         /* initialize getopt */
50
51     while (1) {
52         opt = getopt_long(argc, argv, "b:s:", long_options, &option_index);
53
54         if (opt == EOF)
55             break;
56
57         switch (opt) {
58         case 'b':
59             if ((parsetime_error = parsetime(optarg, &last_up_tv))) {
60                 rrd_set_error("start time: %s", parsetime_error);
61                 return (-1);
62             }
63             if (last_up_tv.type == RELATIVE_TO_END_TIME ||
64                 last_up_tv.type == RELATIVE_TO_START_TIME) {
65                 rrd_set_error("specifying time relative to the 'start' "
66                               "or 'end' makes no sense here");
67                 return (-1);
68             }
69
70             last_up = mktime(&last_up_tv.tm) +last_up_tv.offset;
71
72             if (last_up < 3600 * 24 * 365 * 10) {
73                 rrd_set_error
74                     ("the first entry to the RRD should be after 1980");
75                 return (-1);
76             }
77             break;
78
79         case 's':
80             long_tmp = atol(optarg);
81             if (long_tmp < 1) {
82                 rrd_set_error("step size should be no less than one second");
83                 return (-1);
84             }
85             pdp_step = long_tmp;
86             break;
87
88         case '?':
89             if (optopt != 0)
90                 rrd_set_error("unknown option '%c'", optopt);
91             else
92                 rrd_set_error("unknown option '%s'", argv[optind - 1]);
93             return (-1);
94         }
95     }
96     if (optind == argc) {
97         rrd_set_error("need name of an rrd file to create");
98         return -1;
99     }
100     rc = rrd_create_r(argv[optind],
101                       pdp_step, last_up,
102                       argc - optind - 1, (const char **) (argv + optind + 1));
103
104     return rc;
105 }
106
107 /* #define DEBUG */
108 int rrd_create_r(
109     const char *filename,
110     unsigned long pdp_step,
111     time_t last_up,
112     int argc,
113     const char **argv)
114 {
115     rrd_t     rrd;
116     long      i;
117     int       offset;
118     char     *token;
119     char      dummychar1[2], dummychar2[2];
120     unsigned short token_idx, error_flag, period = 0;
121     unsigned long hashed_name;
122
123     /* init rrd clean */
124     rrd_init(&rrd);
125     /* static header */
126     if ((rrd.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) {
127         rrd_set_error("allocating rrd.stat_head");
128         free(rrd.stat_head);
129         return (-1);
130     }
131
132     /* live header */
133     if ((rrd.live_head = calloc(1, sizeof(live_head_t))) == NULL) {
134         rrd_set_error("allocating rrd.live_head");
135         free(rrd.stat_head);
136         free(rrd.live_head);
137         return (-1);
138     }
139
140     /* set some defaults */
141     strcpy(rrd.stat_head->cookie, RRD_COOKIE);
142     strcpy(rrd.stat_head->version, RRD_VERSION3);   /* by default we are still version 3 */
143     rrd.stat_head->float_cookie = FLOAT_COOKIE;
144     rrd.stat_head->ds_cnt = 0;  /* this will be adjusted later */
145     rrd.stat_head->rra_cnt = 0; /* ditto */
146     rrd.stat_head->pdp_step = pdp_step; /* 5 minute default */
147
148     /* a default value */
149     rrd.ds_def = NULL;
150     rrd.rra_def = NULL;
151
152     rrd.live_head->last_up = last_up;
153
154     /* optind points to the first non-option command line arg,
155      * in this case, the file name. */
156     /* Compute the FNV hash value (used by SEASONAL and DEVSEASONAL
157      * arrays. */
158     hashed_name = FnvHash(filename);
159     for (i = 0; i < argc; i++) {
160         unsigned int ii;
161
162         if (strncmp(argv[i], "DS:", 3) == 0) {
163             size_t    old_size = sizeof(ds_def_t) * (rrd.stat_head->ds_cnt);
164
165             if ((rrd.ds_def = rrd_realloc(rrd.ds_def,
166                                           old_size + sizeof(ds_def_t))) ==
167                 NULL) {
168                 rrd_set_error("allocating rrd.ds_def");
169                 free(rrd.stat_head);
170                 free(rrd.live_head);
171                 return (-1);
172             }
173             memset(&rrd.ds_def[rrd.stat_head->ds_cnt], 0, sizeof(ds_def_t));
174             /* extract the name and type */
175             switch (sscanf(&argv[i][3],
176                            DS_NAM_FMT "%1[:]" DST_FMT "%1[:]%n",
177                            rrd.ds_def[rrd.stat_head->ds_cnt].ds_nam,
178                            dummychar1,
179                            rrd.ds_def[rrd.stat_head->ds_cnt].dst,
180                            dummychar2, &offset)) {
181             case 0:
182             case 1:
183                 rrd_set_error("Invalid DS name");
184                 break;
185             case 2:
186             case 3:
187                 rrd_set_error("Invalid DS type");
188                 break;
189             case 4:    /* (%n may or may not be counted) */
190             case 5:    /* check for duplicate datasource names */
191                 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++)
192                     if (strcmp(rrd.ds_def[rrd.stat_head->ds_cnt].ds_nam,
193                                rrd.ds_def[ii].ds_nam) == 0)
194                         rrd_set_error("Duplicate DS name: %s",
195                                       rrd.ds_def[ii].ds_nam);
196                 /* DS_type may be valid or not. Checked later */
197                 break;
198             default:
199                 rrd_set_error("invalid DS format");
200             }
201             if (rrd_test_error()) {
202                 free(rrd.stat_head);
203                 free(rrd.live_head);
204                 return -1;
205             }
206
207             /* parse the remainder of the arguments */
208             switch (dst_conv(rrd.ds_def[rrd.stat_head->ds_cnt].dst)) {
209             case DST_COUNTER:
210             case DST_ABSOLUTE:
211             case DST_GAUGE:
212             case DST_DERIVE:
213                 parseGENERIC_DS(&argv[i][offset + 3], &rrd,
214                                 rrd.stat_head->ds_cnt);
215                 break;
216             case DST_CDEF:
217                 parseCDEF_DS(&argv[i][offset + 3], &rrd,
218                              rrd.stat_head->ds_cnt);
219                 break;
220             default:
221                 rrd_set_error("invalid DS type specified");
222                 break;
223             }
224
225             if (rrd_test_error()) {
226                 free(rrd.stat_head);
227                 free(rrd.live_head);
228                 return -1;
229             }
230             rrd.stat_head->ds_cnt++;
231         } else if (strncmp(argv[i], "RRA:", 4) == 0) {
232             char     *argvcopy;
233             char     *tokptr;
234             size_t    old_size = sizeof(rra_def_t) * (rrd.stat_head->rra_cnt);
235
236             if ((rrd.rra_def = rrd_realloc(rrd.rra_def,
237                                            old_size + sizeof(rra_def_t))) ==
238                 NULL) {
239                 rrd_set_error("allocating rrd.rra_def");
240                 free(rrd.stat_head);
241                 free(rrd.live_head);
242                 return (-1);
243             }
244             memset(&rrd.rra_def[rrd.stat_head->rra_cnt], 0,
245                    sizeof(rra_def_t));
246
247             argvcopy = strdup(argv[i]);
248             token = strtok_r(&argvcopy[4], ":", &tokptr);
249             token_idx = error_flag = 0;
250             while (token != NULL) {
251                 switch (token_idx) {
252                 case 0:
253                     if (sscanf(token, CF_NAM_FMT,
254                                rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) !=
255                         1)
256                         rrd_set_error("Failed to parse CF name");
257                     switch (cf_conv
258                             (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
259                     case CF_MHWPREDICT:
260                         strcpy(rrd.stat_head->version, RRD_VERSION);    /* MHWPREDICT causes Version 4 */
261                     case CF_HWPREDICT:
262                         /* initialize some parameters */
263                         rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha].
264                             u_val = 0.1;
265                         rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].
266                             u_val = 1.0 / 288;
267                         rrd.rra_def[rrd.stat_head->rra_cnt].
268                             par[RRA_dependent_rra_idx].u_cnt =
269                             rrd.stat_head->rra_cnt;
270                         break;
271                     case CF_DEVSEASONAL:
272                     case CF_SEASONAL:
273                         /* initialize some parameters */
274                         rrd.rra_def[rrd.stat_head->rra_cnt].
275                             par[RRA_seasonal_gamma].u_val = 0.1;
276                         rrd.rra_def[rrd.stat_head->rra_cnt].
277                             par[RRA_seasonal_smoothing_window].u_val = 0.05;
278                         /* fall through */
279                     case CF_DEVPREDICT:
280                         rrd.rra_def[rrd.stat_head->rra_cnt].
281                             par[RRA_dependent_rra_idx].u_cnt = -1;
282                         break;
283                     case CF_FAILURES:
284                         rrd.rra_def[rrd.stat_head->rra_cnt].
285                             par[RRA_delta_pos].u_val = 2.0;
286                         rrd.rra_def[rrd.stat_head->rra_cnt].
287                             par[RRA_delta_neg].u_val = 2.0;
288                         rrd.rra_def[rrd.stat_head->rra_cnt].
289                             par[RRA_window_len].u_cnt = 3;
290                         rrd.rra_def[rrd.stat_head->rra_cnt].
291                             par[RRA_failure_threshold].u_cnt = 2;
292                         rrd.rra_def[rrd.stat_head->rra_cnt].
293                             par[RRA_dependent_rra_idx].u_cnt = -1;
294                         break;
295                         /* invalid consolidation function */
296                     case -1:
297                         rrd_set_error
298                             ("Unrecognized consolidation function %s",
299                              rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
300                     default:
301                         break;
302                     }
303                     /* default: 1 pdp per cdp */
304                     rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = 1;
305                     break;
306                 case 1:
307                     switch (cf_conv
308                             (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
309                     case CF_HWPREDICT:
310                     case CF_MHWPREDICT:
311                     case CF_DEVSEASONAL:
312                     case CF_SEASONAL:
313                     case CF_DEVPREDICT:
314                     case CF_FAILURES:
315                         rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt =
316                             atoi(token);
317                         break;
318                     default:
319                         rrd.rra_def[rrd.stat_head->rra_cnt].
320                             par[RRA_cdp_xff_val].u_val = atof(token);
321                         if (rrd.rra_def[rrd.stat_head->rra_cnt].
322                             par[RRA_cdp_xff_val].u_val < 0.0
323                             || rrd.rra_def[rrd.stat_head->rra_cnt].
324                             par[RRA_cdp_xff_val].u_val >= 1.0)
325                             rrd_set_error
326                                 ("Invalid xff: must be between 0 and 1");
327                         break;
328                     }
329                     break;
330                 case 2:
331                     switch (cf_conv
332                             (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
333                     case CF_HWPREDICT:
334                     case CF_MHWPREDICT:
335                         rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha].
336                             u_val = atof(token);
337                         if (atof(token) <= 0.0 || atof(token) >= 1.0)
338                             rrd_set_error
339                                 ("Invalid alpha: must be between 0 and 1");
340                         break;
341                     case CF_DEVSEASONAL:
342                     case CF_SEASONAL:
343                         rrd.rra_def[rrd.stat_head->rra_cnt].
344                             par[RRA_seasonal_gamma].u_val = atof(token);
345                         if (atof(token) <= 0.0 || atof(token) >= 1.0)
346                             rrd_set_error
347                                 ("Invalid gamma: must be between 0 and 1");
348                         rrd.rra_def[rrd.stat_head->rra_cnt].
349                             par[RRA_seasonal_smooth_idx].u_cnt =
350                             hashed_name %
351                             rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt;
352                         break;
353                     case CF_FAILURES:
354                         /* specifies the # of violations that constitutes the failure threshold */
355                         rrd.rra_def[rrd.stat_head->rra_cnt].
356                             par[RRA_failure_threshold].u_cnt = atoi(token);
357                         if (atoi(token) < 1
358                             || atoi(token) > MAX_FAILURES_WINDOW_LEN)
359                             rrd_set_error
360                                 ("Failure threshold is out of range %d, %d",
361                                  1, MAX_FAILURES_WINDOW_LEN);
362                         break;
363                     case CF_DEVPREDICT:
364                         /* specifies the index (1-based) of CF_DEVSEASONAL array
365                          * associated with this CF_DEVPREDICT array. */
366                         rrd.rra_def[rrd.stat_head->rra_cnt].
367                             par[RRA_dependent_rra_idx].u_cnt =
368                             atoi(token) - 1;
369                         break;
370                     default:
371                         rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt =
372                             atoi(token);
373                         break;
374                     }
375                     break;
376                 case 3:
377                     switch (cf_conv
378                             (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
379                     case CF_HWPREDICT:
380                     case CF_MHWPREDICT:
381                         rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].
382                             u_val = atof(token);
383                         if (atof(token) < 0.0 || atof(token) > 1.0)
384                             rrd_set_error
385                                 ("Invalid beta: must be between 0 and 1");
386                         break;
387                     case CF_DEVSEASONAL:
388                     case CF_SEASONAL:
389                         /* specifies the index (1-based) of CF_HWPREDICT array
390                          * associated with this CF_DEVSEASONAL or CF_SEASONAL array. 
391                          * */
392                         rrd.rra_def[rrd.stat_head->rra_cnt].
393                             par[RRA_dependent_rra_idx].u_cnt =
394                             atoi(token) - 1;
395                         break;
396                     case CF_FAILURES:
397                         /* specifies the window length */
398                         rrd.rra_def[rrd.stat_head->rra_cnt].
399                             par[RRA_window_len].u_cnt = atoi(token);
400                         if (atoi(token) < 1
401                             || atoi(token) > MAX_FAILURES_WINDOW_LEN)
402                             rrd_set_error
403                                 ("Window length is out of range %d, %d", 1,
404                                  MAX_FAILURES_WINDOW_LEN);
405                         /* verify that window length exceeds the failure threshold */
406                         if (rrd.rra_def[rrd.stat_head->rra_cnt].
407                             par[RRA_window_len].u_cnt <
408                             rrd.rra_def[rrd.stat_head->rra_cnt].
409                             par[RRA_failure_threshold].u_cnt)
410                             rrd_set_error
411                                 ("Window length is shorter than the failure threshold");
412                         break;
413                     case CF_DEVPREDICT:
414                         /* shouldn't be any more arguments */
415                         rrd_set_error
416                             ("Unexpected extra argument for consolidation function DEVPREDICT");
417                         break;
418                     default:
419                         rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt =
420                             atoi(token);
421                         break;
422                     }
423                     break;
424                 case 4:
425                     switch (cf_conv
426                             (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
427                     case CF_FAILURES:
428                         /* specifies the index (1-based) of CF_DEVSEASONAL array
429                          * associated with this CF_DEVFAILURES array. */
430                         rrd.rra_def[rrd.stat_head->rra_cnt].
431                             par[RRA_dependent_rra_idx].u_cnt =
432                             atoi(token) - 1;
433                         break;
434                     case CF_DEVSEASONAL:
435                     case CF_SEASONAL:
436                         /* optional smoothing window */
437                         if (sscanf(token, "smoothing-window=%lf",
438                                    &(rrd.rra_def[rrd.stat_head->rra_cnt].
439                                      par[RRA_seasonal_smoothing_window].
440                                      u_val))) {
441                             strcpy(rrd.stat_head->version, RRD_VERSION);    /* smoothing-window causes Version 4 */
442                             if (rrd.rra_def[rrd.stat_head->rra_cnt].
443                                 par[RRA_seasonal_smoothing_window].u_val < 0.0
444                                 || rrd.rra_def[rrd.stat_head->rra_cnt].
445                                 par[RRA_seasonal_smoothing_window].u_val >
446                                 1.0) {
447                                 rrd_set_error
448                                     ("Invalid smoothing-window %f: must be between 0 and 1",
449                                      rrd.rra_def[rrd.stat_head->rra_cnt].
450                                      par[RRA_seasonal_smoothing_window].
451                                      u_val);
452                             }
453                         } else {
454                             rrd_set_error("Invalid option %s", token);
455                         }
456                         break;
457                     case CF_HWPREDICT:
458                     case CF_MHWPREDICT:
459                         /* length of the associated CF_SEASONAL and CF_DEVSEASONAL arrays. */
460                         period = atoi(token);
461                         if (period >
462                             rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt)
463                             rrd_set_error
464                                 ("Length of seasonal cycle exceeds length of HW prediction array");
465                         break;
466                     default:
467                         /* shouldn't be any more arguments */
468                         rrd_set_error
469                             ("Unexpected extra argument for consolidation function %s",
470                              rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
471                         break;
472                     }
473                     break;
474                 case 5:
475                     /* If we are here, this must be a CF_HWPREDICT RRA.
476                      * Specifies the index (1-based) of CF_SEASONAL array
477                      * associated with this CF_HWPREDICT array. If this argument 
478                      * is missing, then the CF_SEASONAL, CF_DEVSEASONAL, CF_DEVPREDICT,
479                      * CF_FAILURES.
480                      * arrays are created automatically. */
481                     rrd.rra_def[rrd.stat_head->rra_cnt].
482                         par[RRA_dependent_rra_idx].u_cnt = atoi(token) - 1;
483                     break;
484                 default:
485                     /* should never get here */
486                     rrd_set_error("Unknown error");
487                     break;
488                 }       /* end switch */
489                 if (rrd_test_error()) {
490                     /* all errors are unrecoverable */
491                     free(argvcopy);
492                     free(rrd.stat_head);
493                     free(rrd.live_head);
494                     return (-1);
495                 }
496                 token = strtok_r(NULL, ":", &tokptr);
497                 token_idx++;
498             }           /* end while */
499             free(argvcopy);
500 #ifdef DEBUG
501             fprintf(stderr,
502                     "Creating RRA CF: %s, dep idx %lu, current idx %lu\n",
503                     rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam,
504                     rrd.rra_def[rrd.stat_head->rra_cnt].
505                     par[RRA_dependent_rra_idx].u_cnt, rrd.stat_head->rra_cnt);
506 #endif
507             /* should we create CF_SEASONAL, CF_DEVSEASONAL, and CF_DEVPREDICT? */
508             if ((cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) ==
509                  CF_HWPREDICT
510                  || cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) ==
511                  CF_MHWPREDICT)
512                 && rrd.rra_def[rrd.stat_head->rra_cnt].
513                 par[RRA_dependent_rra_idx].u_cnt == rrd.stat_head->rra_cnt) {
514 #ifdef DEBUG
515                 fprintf(stderr, "Creating HW contingent RRAs\n");
516 #endif
517                 if (create_hw_contingent_rras(&rrd, period, hashed_name) ==
518                     -1) {
519                     rrd_set_error("creating contingent RRA");
520                     free(rrd.stat_head);
521                     free(rrd.live_head);
522                     return -1;
523                 }
524             }
525             rrd.stat_head->rra_cnt++;
526         } else {
527             rrd_set_error("can't parse argument '%s'", argv[i]);
528             free(rrd.stat_head);
529             free(rrd.live_head);
530             return -1;
531         }
532     }
533
534
535     if (rrd.stat_head->rra_cnt < 1) {
536         rrd_set_error("you must define at least one Round Robin Archive");
537         free(rrd.stat_head);
538         free(rrd.live_head);
539         return (-1);
540     }
541
542     if (rrd.stat_head->ds_cnt < 1) {
543         rrd_set_error("you must define at least one Data Source");
544         free(rrd.stat_head);
545         free(rrd.live_head);
546         return (-1);
547     }
548     return rrd_create_fn(filename, &rrd);
549 }
550
551 void parseGENERIC_DS(
552     const char *def,
553     rrd_t *rrd,
554     int ds_idx)
555 {
556     char      minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];
557     char     *old_locale;
558
559     /*
560        int temp;
561
562        temp = sscanf(def,"%lu:%18[^:]:%18[^:]", 
563        &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
564        minstr,maxstr);
565      */
566     old_locale = setlocale(LC_NUMERIC, "C");
567     if (sscanf(def, "%lu:%18[^:]:%18[^:]",
568                &(rrd->ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
569                minstr, maxstr) == 3) {
570         if (minstr[0] == 'U' && minstr[1] == 0)
571             rrd->ds_def[ds_idx].par[DS_min_val].u_val = DNAN;
572         else
573             rrd->ds_def[ds_idx].par[DS_min_val].u_val = atof(minstr);
574
575         if (maxstr[0] == 'U' && maxstr[1] == 0)
576             rrd->ds_def[ds_idx].par[DS_max_val].u_val = DNAN;
577         else
578             rrd->ds_def[ds_idx].par[DS_max_val].u_val = atof(maxstr);
579
580         if (!isnan(rrd->ds_def[ds_idx].par[DS_min_val].u_val) &&
581             !isnan(rrd->ds_def[ds_idx].par[DS_max_val].u_val) &&
582             rrd->ds_def[ds_idx].par[DS_min_val].u_val
583             >= rrd->ds_def[ds_idx].par[DS_max_val].u_val) {
584             rrd_set_error("min must be less than max in DS definition");
585             setlocale(LC_NUMERIC, old_locale);
586             return;
587         }
588     } else {
589         rrd_set_error("failed to parse data source %s", def);
590     }
591     setlocale(LC_NUMERIC, old_locale);
592 }
593
594 /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs
595  * associated with a CF_HWPREDICT RRA. */
596 int create_hw_contingent_rras(
597     rrd_t *rrd,
598     unsigned short period,
599     unsigned long hashed_name)
600 {
601     size_t    old_size;
602     rra_def_t *current_rra;
603
604     /* save index to CF_HWPREDICT */
605     unsigned long hw_index = rrd->stat_head->rra_cnt;
606
607     /* advance the pointer */
608     (rrd->stat_head->rra_cnt)++;
609     /* allocate the memory for the 4 contingent RRAs */
610     old_size = sizeof(rra_def_t) * (rrd->stat_head->rra_cnt);
611     if ((rrd->rra_def = rrd_realloc(rrd->rra_def,
612                                     old_size + 4 * sizeof(rra_def_t))) ==
613         NULL) {
614         rrd_set_error("allocating rrd.rra_def");
615         return (-1);
616     }
617     /* clear memory */
618     memset(&(rrd->rra_def[rrd->stat_head->rra_cnt]), 0,
619            4 * sizeof(rra_def_t));
620
621     /* create the CF_SEASONAL RRA */
622     current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
623     strcpy(current_rra->cf_nam, "SEASONAL");
624     current_rra->row_cnt = period;
625     current_rra->par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
626     current_rra->pdp_cnt = 1;
627     current_rra->par[RRA_seasonal_gamma].u_val =
628         rrd->rra_def[hw_index].par[RRA_hw_alpha].u_val;
629     current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index;
630     rrd->rra_def[hw_index].par[RRA_dependent_rra_idx].u_cnt =
631         rrd->stat_head->rra_cnt;
632
633     /* create the CF_DEVSEASONAL RRA */
634     (rrd->stat_head->rra_cnt)++;
635     current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
636     strcpy(current_rra->cf_nam, "DEVSEASONAL");
637     current_rra->row_cnt = period;
638     current_rra->par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
639     current_rra->pdp_cnt = 1;
640     current_rra->par[RRA_seasonal_gamma].u_val =
641         rrd->rra_def[hw_index].par[RRA_hw_alpha].u_val;
642     current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index;
643
644     /* create the CF_DEVPREDICT RRA */
645     (rrd->stat_head->rra_cnt)++;
646     current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
647     strcpy(current_rra->cf_nam, "DEVPREDICT");
648     current_rra->row_cnt = (rrd->rra_def[hw_index]).row_cnt;
649     current_rra->pdp_cnt = 1;
650     current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index + 2;   /* DEVSEASONAL */
651
652     /* create the CF_FAILURES RRA */
653     (rrd->stat_head->rra_cnt)++;
654     current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
655     strcpy(current_rra->cf_nam, "FAILURES");
656     current_rra->row_cnt = period;
657     current_rra->pdp_cnt = 1;
658     current_rra->par[RRA_delta_pos].u_val = 2.0;
659     current_rra->par[RRA_delta_neg].u_val = 2.0;
660     current_rra->par[RRA_failure_threshold].u_cnt = 7;
661     current_rra->par[RRA_window_len].u_cnt = 9;
662     current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index + 2;   /* DEVSEASONAL */
663     return 0;
664 }
665
666 /* create and empty rrd file according to the specs given */
667
668 int rrd_create_fn(
669     const char *file_name,
670     rrd_t *rrd)
671 {
672     unsigned long i, ii;
673     int       rrd_file;
674     rrd_value_t *unknown;
675     int       unkn_cnt;
676     rrd_file_t *rrd_file_dn;
677     rrd_t     rrd_dn;
678
679     if ((rrd_file = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
680         rrd_set_error("creating '%s': %s", file_name, rrd_strerror(errno));
681         free(rrd->stat_head);
682         free(rrd->live_head);
683         return (-1);
684     }
685
686     write(rrd_file, rrd->stat_head, sizeof(stat_head_t));
687
688     write(rrd_file, rrd->ds_def, sizeof(ds_def_t) * rrd->stat_head->ds_cnt);
689
690     write(rrd_file, rrd->rra_def,
691           sizeof(rra_def_t) * rrd->stat_head->rra_cnt);
692
693     write(rrd_file, rrd->live_head, sizeof(live_head_t));
694
695     if ((rrd->pdp_prep = calloc(1, sizeof(pdp_prep_t))) == NULL) {
696         rrd_set_error("allocating pdp_prep");
697         free(rrd->stat_head);
698         free(rrd->live_head);
699         close(rrd_file);
700         return (-1);
701     }
702
703     strcpy(rrd->pdp_prep->last_ds, "U");
704
705     rrd->pdp_prep->scratch[PDP_val].u_val = 0.0;
706     rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt =
707         rrd->live_head->last_up % rrd->stat_head->pdp_step;
708
709     for (i = 0; i < rrd->stat_head->ds_cnt; i++)
710         write(rrd_file, rrd->pdp_prep, sizeof(pdp_prep_t));
711
712     if ((rrd->cdp_prep = calloc(1, sizeof(cdp_prep_t))) == NULL) {
713         rrd_set_error("allocating cdp_prep");
714         free(rrd->stat_head);
715         free(rrd->live_head);
716         close(rrd_file);
717         return (-1);
718     }
719
720
721     for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
722         switch (cf_conv(rrd->rra_def[i].cf_nam)) {
723         case CF_HWPREDICT:
724         case CF_MHWPREDICT:
725             init_hwpredict_cdp(rrd->cdp_prep);
726             break;
727         case CF_SEASONAL:
728         case CF_DEVSEASONAL:
729             init_seasonal_cdp(rrd->cdp_prep);
730             break;
731         case CF_FAILURES:
732             /* initialize violation history to 0 */
733             for (ii = 0; ii < MAX_CDP_PAR_EN; ii++) {
734                 /* We can zero everything out, by setting u_val to the
735                  * NULL address. Each array entry in scratch is 8 bytes
736                  * (a double), but u_cnt only accessed 4 bytes (long) */
737                 rrd->cdp_prep->scratch[ii].u_val = 0.0;
738             }
739             break;
740         default:
741             /* can not be zero because we don't know anything ... */
742             rrd->cdp_prep->scratch[CDP_val].u_val = DNAN;
743             /* startup missing pdp count */
744             rrd->cdp_prep->scratch[CDP_unkn_pdp_cnt].u_cnt =
745                 ((rrd->live_head->last_up -
746                   rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt)
747                  % (rrd->stat_head->pdp_step
748                     * rrd->rra_def[i].pdp_cnt)) / rrd->stat_head->pdp_step;
749             break;
750         }
751
752         for (ii = 0; ii < rrd->stat_head->ds_cnt; ii++) {
753             write(rrd_file, rrd->cdp_prep, sizeof(cdp_prep_t));
754         }
755     }
756
757     /* now, we must make sure that the rest of the rrd
758        struct is properly initialized */
759
760     if ((rrd->rra_ptr = calloc(1, sizeof(rra_ptr_t))) == NULL) {
761         rrd_set_error("allocating rra_ptr");
762         free(rrd->stat_head);
763         free(rrd->live_head);
764         close(rrd_file);
765         return (-1);
766     }
767
768     /* changed this initialization to be consistent with
769      * rrd_restore. With the old value (0), the first update
770      * would occur for cur_row = 1 because rrd_update increments
771      * the pointer a priori. */
772     for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
773         rrd->rra_ptr->cur_row = rra_random_row(&rrd->rra_def[i]);
774         write(rrd_file, rrd->rra_ptr, sizeof(rra_ptr_t));
775     }
776
777     /* write the empty data area */
778     if ((unknown = (rrd_value_t *) malloc(512 * sizeof(rrd_value_t))) == NULL) {
779         rrd_set_error("allocating unknown");
780         free(rrd->stat_head);
781         free(rrd->live_head);
782         close(rrd_file);
783         return (-1);
784     }
785     for (i = 0; i < 512; ++i)
786         unknown[i] = DNAN;
787
788     unkn_cnt = 0;
789     for (i = 0; i < rrd->stat_head->rra_cnt; i++)
790         unkn_cnt += rrd->stat_head->ds_cnt * rrd->rra_def[i].row_cnt;
791
792     while (unkn_cnt > 0) {
793         write(rrd_file, unknown, sizeof(rrd_value_t) * min(unkn_cnt, 512));
794
795         unkn_cnt -= 512;
796     }
797     free(unknown);
798     fdatasync(rrd_file);
799     free(rrd->stat_head);
800     free(rrd->live_head);
801     if (close(rrd_file) == -1) {
802         rrd_set_error("creating rrd: %s", rrd_strerror(errno));
803         return -1;
804     }
805     /* flush all we don't need out of the cache */
806     rrd_file_dn = rrd_open(file_name, &rrd_dn, RRD_READONLY);
807     rrd_dontneed(rrd_file_dn, &rrd_dn);
808     rrd_free(&rrd_dn);
809     rrd_close(rrd_file_dn);
810     return (0);
811 }
812
813 static int rand_init = 0;
814
815 long int rra_random_row(
816     rra_def_t *rra)
817 {
818     if (!rand_init) {
819         srandom((unsigned int) time(NULL) + (unsigned int) getpid());
820         rand_init++;
821     }
822
823     return random() % rra->row_cnt;
824 }