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