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