prepare for the release of rrdtool-1.2.5
[rrdtool.git] / src / rrd_create.c
1 /*****************************************************************************
2  * RRDtool 1.2.5  Copyright by Tobi Oetiker, 1997-2005
3  *****************************************************************************
4  * rrd_create.c  creates new rrds
5  *****************************************************************************/
6
7 #include "rrd_tool.h"
8 #include "rrd_rpncalc.h"
9 #include "rrd_hw.h"
10
11 #include "rrd_is_thread_safe.h"
12
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);
16
17 int
18 rrd_create(int argc, char **argv) 
19 {
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;
24     long              long_tmp;
25     int               rc;
26     optind = 0; opterr = 0;  /* initialize getopt */
27
28     while (1){
29         static struct option long_options[] =
30         {
31             {"start",      required_argument, 0, 'b'},
32             {"step",        required_argument,0,'s'},
33             {0,0,0,0}
34         };
35         int option_index = 0;
36         int opt;
37         opt = getopt_long(argc, argv, "b:s:", 
38                           long_options, &option_index);
39         
40         if (opt == EOF)
41             break;
42         
43         switch(opt) {
44         case 'b':
45             if ((parsetime_error = parsetime(optarg, &last_up_tv))) {
46                 rrd_set_error("start time: %s", parsetime_error );
47                 return(-1);
48             }
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");
53                 return(-1);
54             }
55
56             last_up = mktime(&last_up_tv.tm) + last_up_tv.offset;
57             
58             if (last_up < 3600*24*365*10){
59                 rrd_set_error("the first entry to the RRD should be after 1980");
60                 return(-1);
61             }   
62             break;
63
64         case 's':
65             long_tmp = atol(optarg);
66             if (long_tmp < 1){
67                 rrd_set_error("step size should be no less than one second");
68                 return(-1);
69             }
70             pdp_step = long_tmp;
71             break;
72
73         case '?':
74             if (optopt != 0)
75                 rrd_set_error("unknown option '%c'", optopt);
76             else
77                 rrd_set_error("unknown option '%s'",argv[optind-1]);
78             return(-1);
79         }
80     }
81
82     rc = rrd_create_r(argv[optind],
83                       pdp_step, last_up,
84                       argc - optind - 1, argv + optind + 1);
85     
86     return rc;
87 }
88
89 /* #define DEBUG */
90 int
91 rrd_create_r(char *filename,
92              unsigned long pdp_step, time_t last_up,
93              int argc, char **argv) 
94 {
95     rrd_t             rrd;
96     long              i;
97     int               offset;
98     char *token;
99     unsigned short token_idx, error_flag, period=0;
100     unsigned long hashed_name;
101
102     /* init rrd clean */
103     rrd_init(&rrd);
104     /* static header */
105     if((rrd.stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
106         rrd_set_error("allocating rrd.stat_head");
107         rrd_free(&rrd);
108         return(-1);
109     }
110
111     /* live header */
112     if((rrd.live_head = calloc(1,sizeof(live_head_t)))==NULL){
113         rrd_set_error("allocating rrd.live_head");
114         rrd_free(&rrd);
115         return(-1);
116     }
117
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 */
125
126     /* a default value */
127     rrd.ds_def = NULL;
128     rrd.rra_def = NULL;
129
130     rrd.live_head->last_up = last_up;
131         
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
135          * arrays. */
136     hashed_name = FnvHash(filename);
137     for(i=0;i<argc;i++){
138         unsigned int ii;
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");
144                 rrd_free(&rrd);
145                 return(-1);     
146             }
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)
153             {
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);
159                     }                                                           
160             } else {
161                 rrd_set_error("invalid DS format");
162             }
163             
164             /* parse the remainder of the arguments */
165             switch(dst_conv(rrd.ds_def[rrd.stat_head->ds_cnt].dst))
166             {
167             case DST_COUNTER:
168             case DST_ABSOLUTE:
169             case DST_GAUGE:
170             case DST_DERIVE:
171                 parseGENERIC_DS(&argv[i][offset+3],&rrd, rrd.stat_head->ds_cnt);
172                 break;
173             case DST_CDEF:
174                 parseCDEF_DS(&argv[i][offset+3],&rrd, rrd.stat_head->ds_cnt);
175                 break;
176             default:
177                 rrd_set_error("invalid DS type specified");
178                 break;
179             }
180             
181             if (rrd_test_error()) {
182                 rrd_free(&rrd);
183                 return -1;
184             }
185             rrd.stat_head -> ds_cnt++;
186         } else if (strncmp(argv[i],"RRA:",4)==0){
187             char *argvcopy;
188             char *tokptr;
189             size_t old_size = sizeof(rra_def_t)*(rrd.stat_head->rra_cnt);
190             if((rrd.rra_def = rrd_realloc(rrd.rra_def,
191                                           old_size+sizeof(rra_def_t)))==NULL)
192             {
193                 rrd_set_error("allocating rrd.rra_def");
194                 rrd_free(&rrd);
195                 return(-1);     
196             }
197             memset(&rrd.rra_def[rrd.stat_head->rra_cnt], 0, sizeof(rra_def_t));
198
199             argvcopy = strdup(argv[i]);
200             token = strtok_r(&argvcopy[4],":", &tokptr);
201             token_idx = error_flag = 0;
202             while (token != NULL)
203             {
204                 switch(token_idx)
205                 {
206                 case 0:
207                     if (sscanf(token,CF_NAM_FMT,
208                                rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) != 1)
209                         rrd_set_error("Failed to parse CF name");
210                     switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
211                     {
212                     case CF_HWPREDICT:
213                         /* initialize some parameters */
214                         rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha].u_val = 0.1;
215                         rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].u_val = 1.0/288;
216                         rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt = 
217                             rrd.stat_head -> rra_cnt;
218                         break;
219                     case CF_DEVSEASONAL:
220                     case CF_SEASONAL:
221                         /* initialize some parameters */
222                         rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val = 0.1;
223                         /* fall through */
224                     case CF_DEVPREDICT:
225                         rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt = -1;
226                         break;
227                     case CF_FAILURES:
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;
233                         break;
234                         /* invalid consolidation function */
235                     case -1:
236                         rrd_set_error("Unrecognized consolidation function %s",
237                                       rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
238                     default:
239                         break;
240                     }
241                     /* default: 1 pdp per cdp */ 
242                     rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = 1;
243                     break;
244                 case 1:
245                     switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
246                     {
247                     case CF_HWPREDICT:
248                     case CF_DEVSEASONAL:
249                     case CF_SEASONAL:
250                     case CF_DEVPREDICT:
251                     case CF_FAILURES:
252                         rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
253                         break;
254                     default:
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");
259                         break;
260                     }
261                     break;
262                 case 2:
263                     switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
264                     {
265                     case CF_HWPREDICT:
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");
269                         break;
270                     case CF_DEVSEASONAL:
271                     case CF_SEASONAL:
272                         rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_seasonal_gamma].u_val = 
273                             atof(token);
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; 
278                         break;
279                     case CF_FAILURES:
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 =
282                             atoi(token);
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);
286                         break;
287                     case CF_DEVPREDICT:
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 =
291                             atoi(token) - 1;
292                         break;
293                     default:
294                         rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = atoi(token);
295                         break;
296                     }
297                     break;
298                 case 3:
299                     switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
300                     {
301                     case CF_HWPREDICT:
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");
305                         break;
306                     case CF_DEVSEASONAL:
307                     case CF_SEASONAL:
308                         /* specifies the index (1-based) of CF_HWPREDICT array
309                          * associated with this CF_DEVSEASONAL or CF_SEASONAL array. 
310                          * */
311                         rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
312                             atoi(token) - 1;
313                         break;
314                     case CF_FAILURES:
315                         /* specifies the window length */
316                         rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt =
317                             atoi(token);
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");
325                         break;
326                     case CF_DEVPREDICT:
327                         /* shouldn't be any more arguments */
328                         rrd_set_error("Unexpected extra argument for consolidation function DEVPREDICT");
329                         break;
330                     default:
331                         rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
332                         break;
333                     }
334                     break;
335                 case 4:
336                     switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
337                     {
338                     case CF_FAILURES:
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 =
342                             atoi(token) - 1;
343                         break;
344                     case CF_HWPREDICT:
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");
349                         break;
350                     default:
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);
354                         break;
355                     }
356                     break;
357                 case 5:
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,
362                      * CF_FAILURES.
363                      * arrays are created automatically. */
364                     rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
365                         atoi(token) - 1;
366                     break;
367                 default:
368                     /* should never get here */
369                     rrd_set_error("Unknown error");
370                     break;
371                 } /* end switch */
372                 if (rrd_test_error())
373                 {
374                     /* all errors are unrecoverable */
375                     free(argvcopy);
376                     rrd_free(&rrd);
377                     return (-1);
378                 }
379                 token = strtok_r(NULL,":", &tokptr);
380                 token_idx++;
381             } /* end while */
382             free(argvcopy);
383 #ifdef DEBUG
384             fprintf(stderr,"Creating RRA CF: %s, dep idx %lu, current idx %lu\n",
385                     rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam,
386                     rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt, 
387                     rrd.stat_head -> rra_cnt);
388 #endif
389             /* should we create CF_SEASONAL, CF_DEVSEASONAL, and CF_DEVPREDICT? */
390             if (cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) == CF_HWPREDICT
391                 && rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt 
392                 == rrd.stat_head -> rra_cnt)
393             {
394 #ifdef DEBUG
395                 fprintf(stderr,"Creating HW contingent RRAs\n");
396 #endif
397                 if (create_hw_contingent_rras(&rrd,period,hashed_name) == -1) {
398                     rrd_set_error("creating contingent RRA");
399                     rrd_free(&rrd);
400                     return -1;
401                 }
402             }
403             rrd.stat_head->rra_cnt++;                   
404         } else {
405             rrd_set_error("can't parse argument '%s'",argv[i]);
406             rrd_free(&rrd);
407             return -1;
408         }
409     }
410     
411     
412     if (rrd.stat_head->rra_cnt < 1){
413         rrd_set_error("you must define at least one Round Robin Archive");
414         rrd_free(&rrd);
415         return(-1);
416     }
417     
418     if (rrd.stat_head->ds_cnt < 1){
419         rrd_set_error("you must define at least one Data Source");
420         rrd_free(&rrd);
421         return(-1);
422     }
423     return rrd_create_fn(filename, &rrd);
424 }
425
426 void parseGENERIC_DS(char *def,rrd_t *rrd, int ds_idx)
427 {
428     char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];      
429     /*
430       int temp;
431       
432       temp = sscanf(def,"%lu:%18[^:]:%18[^:]",  
433       &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
434       minstr,maxstr);
435     */
436     if (sscanf(def,"%lu:%18[^:]:%18[^:]",       
437                &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
438                minstr,maxstr) == 3)
439     {
440         if (minstr[0] == 'U' && minstr[1] == 0)
441             rrd -> ds_def[ds_idx].par[DS_min_val].u_val = DNAN;
442         else
443             rrd -> ds_def[ds_idx].par[DS_min_val].u_val = atof(minstr);
444         
445         if (maxstr[0] == 'U' && maxstr[1] == 0)
446             rrd -> ds_def[ds_idx].par[DS_max_val].u_val = DNAN;
447         else
448             rrd -> ds_def[ds_idx].par[DS_max_val].u_val  = atof(maxstr);
449         
450         if (! isnan(rrd -> ds_def[ds_idx].par[DS_min_val].u_val) &&
451             ! isnan(rrd -> ds_def[ds_idx].par[DS_max_val].u_val) &&
452             rrd -> ds_def[ds_idx].par[DS_min_val].u_val
453             >= rrd -> ds_def[ds_idx].par[DS_max_val].u_val ) {
454             rrd_set_error("min must be less than max in DS definition");
455             return;             
456         }
457     } else {
458         rrd_set_error("failed to parse data source %s", def);
459     }
460 }
461
462 /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs
463  * associated with a CF_HWPREDICT RRA. */
464 int
465 create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashed_name)
466 {
467     size_t old_size;
468     rra_def_t* current_rra;
469     
470     /* save index to CF_HWPREDICT */
471     unsigned long hw_index = rrd -> stat_head -> rra_cnt;
472     /* advance the pointer */
473     (rrd -> stat_head -> rra_cnt)++;                    
474     /* allocate the memory for the 4 contingent RRAs */
475     old_size = sizeof(rra_def_t)*(rrd -> stat_head->rra_cnt);
476     if ((rrd -> rra_def = rrd_realloc(rrd -> rra_def,
477                                       old_size+4*sizeof(rra_def_t)))==NULL)
478     {
479         rrd_set_error("allocating rrd.rra_def");
480         return(-1);     
481     }
482     /* clear memory */
483     memset(&(rrd -> rra_def[rrd -> stat_head->rra_cnt]), 0, 4*sizeof(rra_def_t));
484     
485     /* create the CF_SEASONAL RRA */
486     current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
487     strcpy(current_rra -> cf_nam,"SEASONAL");
488     current_rra -> row_cnt = period;
489     current_rra -> par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
490     current_rra -> pdp_cnt = 1;
491     current_rra -> par[RRA_seasonal_gamma].u_val = 
492         rrd -> rra_def[hw_index].par[RRA_hw_alpha].u_val;
493     current_rra -> par[RRA_dependent_rra_idx].u_cnt = hw_index; 
494     rrd -> rra_def[hw_index].par[RRA_dependent_rra_idx].u_cnt = rrd -> stat_head -> rra_cnt;
495     
496     /* create the CF_DEVSEASONAL RRA */
497     (rrd -> stat_head -> rra_cnt)++; 
498     current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
499     strcpy(current_rra -> cf_nam,"DEVSEASONAL");
500     current_rra -> row_cnt = period;
501     current_rra -> par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
502     current_rra -> pdp_cnt = 1;
503     current_rra -> par[RRA_seasonal_gamma].u_val = 
504         rrd -> rra_def[hw_index].par[RRA_hw_alpha].u_val;
505     current_rra -> par[RRA_dependent_rra_idx].u_cnt = hw_index; 
506     
507     /* create the CF_DEVPREDICT RRA */
508     (rrd -> stat_head -> rra_cnt)++; 
509     current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
510     strcpy(current_rra -> cf_nam,"DEVPREDICT");
511     current_rra -> row_cnt = (rrd -> rra_def[hw_index]).row_cnt;
512     current_rra -> pdp_cnt = 1;
513     current_rra -> par[RRA_dependent_rra_idx].u_cnt 
514         = hw_index + 2; /* DEVSEASONAL */
515     
516     /* create the CF_FAILURES RRA */
517     (rrd -> stat_head -> rra_cnt)++; 
518     current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
519     strcpy(current_rra -> cf_nam,"FAILURES");
520     current_rra -> row_cnt = period; 
521     current_rra -> pdp_cnt = 1;
522     current_rra -> par[RRA_delta_pos].u_val = 2.0;
523     current_rra -> par[RRA_delta_neg].u_val = 2.0;
524     current_rra -> par[RRA_failure_threshold].u_cnt = 7;
525     current_rra -> par[RRA_window_len].u_cnt = 9;
526     current_rra -> par[RRA_dependent_rra_idx].u_cnt = 
527         hw_index + 2; /* DEVSEASONAL */
528     return 0;
529 }
530
531 /* create and empty rrd file according to the specs given */
532
533 int
534 rrd_create_fn(char *file_name, rrd_t *rrd)
535 {
536     unsigned long    i,ii;
537     FILE             *rrd_file;
538     rrd_value_t      *unknown;
539     int unkn_cnt;
540     
541     if ((rrd_file = fopen(file_name,"wb")) == NULL ) {
542         rrd_set_error("creating '%s': %s",file_name, rrd_strerror(errno));
543         free(rrd->stat_head);
544         free(rrd->ds_def);
545         free(rrd->rra_def);
546         return(-1);
547     }
548     
549     fwrite(rrd->stat_head,
550            sizeof(stat_head_t), 1, rrd_file);
551     
552     fwrite(rrd->ds_def,
553            sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
554     
555     fwrite(rrd->rra_def,
556            sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
557     
558     fwrite(rrd->live_head,
559            sizeof(live_head_t),1, rrd_file);
560
561     if((rrd->pdp_prep = calloc(1,sizeof(pdp_prep_t))) == NULL){
562         rrd_set_error("allocating pdp_prep");
563         rrd_free(rrd);
564         fclose(rrd_file);
565         return(-1);
566     }
567
568     strcpy(rrd->pdp_prep->last_ds,"UNKN");
569
570     rrd->pdp_prep->scratch[PDP_val].u_val = 0.0;
571     rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt = 
572         rrd->live_head->last_up % rrd->stat_head->pdp_step;
573
574     for(i=0; i < rrd->stat_head->ds_cnt; i++)
575         fwrite( rrd->pdp_prep,sizeof(pdp_prep_t),1,rrd_file);
576     
577     if((rrd->cdp_prep = calloc(1,sizeof(cdp_prep_t))) == NULL){
578         rrd_set_error("allocating cdp_prep");
579         rrd_free(rrd);
580         fclose(rrd_file);
581         return(-1);
582     }
583
584
585     for(i=0; i < rrd->stat_head->rra_cnt; i++) {
586        switch (cf_conv(rrd->rra_def[i].cf_nam))
587            {
588            case CF_HWPREDICT:
589                init_hwpredict_cdp(rrd->cdp_prep);
590                break;
591            case CF_SEASONAL:
592            case CF_DEVSEASONAL:
593                init_seasonal_cdp(rrd->cdp_prep);
594                break;
595            case CF_FAILURES:
596                /* initialize violation history to 0 */
597                for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
598                {
599                                 /* We can zero everything out, by setting u_val to the
600                                  * NULL address. Each array entry in scratch is 8 bytes
601                                  * (a double), but u_cnt only accessed 4 bytes (long) */
602                    rrd->cdp_prep->scratch[ii].u_val = 0.0;
603                }
604                break;
605            default:
606                /* can not be zero because we don't know anything ... */
607                rrd->cdp_prep->scratch[CDP_val].u_val = DNAN;
608                /* startup missing pdp count */
609                rrd->cdp_prep->scratch[CDP_unkn_pdp_cnt].u_cnt = 
610                    ((rrd->live_head->last_up -
611                  rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt)
612                     % (rrd->stat_head->pdp_step 
613                        * rrd->rra_def[i].pdp_cnt)) / rrd->stat_head->pdp_step;  
614                break;
615            }
616        
617        for(ii=0; ii < rrd->stat_head->ds_cnt; ii++) 
618        {
619            fwrite( rrd->cdp_prep,sizeof(cdp_prep_t),1,rrd_file);
620        }
621     }
622     
623     /* now, we must make sure that the rest of the rrd
624        struct is properly initialized */
625     
626     if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t))) == NULL) {
627         rrd_set_error("allocating rra_ptr");
628         rrd_free(rrd);
629         fclose(rrd_file);
630         return(-1);
631     }
632     
633     /* changed this initialization to be consistent with
634      * rrd_restore. With the old value (0), the first update
635      * would occur for cur_row = 1 because rrd_update increments
636      * the pointer a priori. */
637     for (i=0; i < rrd->stat_head->rra_cnt; i++)
638     {
639         rrd->rra_ptr->cur_row = rrd->rra_def[i].row_cnt - 1;
640         fwrite( rrd->rra_ptr, sizeof(rra_ptr_t),1,rrd_file);
641     }
642     
643     /* write the empty data area */
644     if ((unknown = (rrd_value_t *)malloc(512 * sizeof(rrd_value_t))) == NULL) {
645         rrd_set_error("allocating unknown");
646         rrd_free(rrd);
647         fclose(rrd_file);
648         return(-1);
649     }
650     for (i = 0; i < 512; ++i)
651         unknown[i] = DNAN;
652     
653     unkn_cnt = 0;
654     for (i = 0; i < rrd->stat_head->rra_cnt; i++)
655         unkn_cnt += rrd->stat_head->ds_cnt * rrd->rra_def[i].row_cnt;
656                       
657     while (unkn_cnt > 0) {
658         fwrite(unknown, sizeof(rrd_value_t), min(unkn_cnt, 512), rrd_file);
659         unkn_cnt -= 512;
660      }
661     free(unknown);
662     
663     /* lets see if we had an error */
664     if(ferror(rrd_file)){
665         rrd_set_error("a file error occurred while creating '%s'",file_name);
666         fclose(rrd_file);       
667         rrd_free(rrd);
668         return(-1);
669     }
670     
671     fclose(rrd_file);    
672     rrd_free(rrd);
673     return (0);
674 }