1 /*****************************************************************************
2 * RRDtool 1.2.29 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 if (atoi(token) < 1)
316 rrd_set_error("Invalid step: must be >= 1");
317 break;
318 }
319 break;
320 case 3:
321 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
322 {
323 case CF_HWPREDICT:
324 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].u_val = atof(token);
325 if (atof(token) < 0.0 || atof(token) > 1.0)
326 rrd_set_error("Invalid beta: must be between 0 and 1");
327 break;
328 case CF_DEVSEASONAL:
329 case CF_SEASONAL:
330 /* specifies the index (1-based) of CF_HWPREDICT array
331 * associated with this CF_DEVSEASONAL or CF_SEASONAL array.
332 * */
333 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
334 atoi(token) - 1;
335 break;
336 case CF_FAILURES:
337 /* specifies the window length */
338 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt =
339 atoi(token);
340 if (atoi(token) < 1 || atoi(token) > MAX_FAILURES_WINDOW_LEN)
341 rrd_set_error("Window length is out of range %d, %d",1,
342 MAX_FAILURES_WINDOW_LEN);
343 /* verify that window length exceeds the failure threshold */
344 if (rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_window_len].u_cnt <
345 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_failure_threshold].u_cnt)
346 rrd_set_error("Window length is shorter than the failure threshold");
347 break;
348 case CF_DEVPREDICT:
349 /* shouldn't be any more arguments */
350 rrd_set_error("Unexpected extra argument for consolidation function DEVPREDICT");
351 break;
352 default:
353 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = atoi(token);
354 break;
355 }
356 break;
357 case 4:
358 switch(cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam))
359 {
360 case CF_FAILURES:
361 /* specifies the index (1-based) of CF_DEVSEASONAL array
362 * associated with this CF_DEVFAILURES array. */
363 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
364 atoi(token) - 1;
365 break;
366 case CF_HWPREDICT:
367 /* length of the associated CF_SEASONAL and CF_DEVSEASONAL arrays. */
368 period = atoi(token);
369 if (period > rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt)
370 rrd_set_error("Length of seasonal cycle exceeds length of HW prediction array");
371 break;
372 default:
373 /* shouldn't be any more arguments */
374 rrd_set_error("Unexpected extra argument for consolidation function %s",
375 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
376 break;
377 }
378 break;
379 case 5:
380 /* If we are here, this must be a CF_HWPREDICT RRA.
381 * Specifies the index (1-based) of CF_SEASONAL array
382 * associated with this CF_HWPREDICT array. If this argument
383 * is missing, then the CF_SEASONAL, CF_DEVSEASONAL, CF_DEVPREDICT,
384 * CF_FAILURES.
385 * arrays are created automatically. */
386 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt =
387 atoi(token) - 1;
388 break;
389 default:
390 /* should never get here */
391 rrd_set_error("Unknown error");
392 break;
393 } /* end switch */
394 if (rrd_test_error())
395 {
396 /* all errors are unrecoverable */
397 free(argvcopy);
398 rrd_free(&rrd);
399 return (-1);
400 }
401 token = strtok_r(NULL,":", &tokptr);
402 token_idx++;
403 } /* end while */
404 free(argvcopy);
405 #ifdef DEBUG
406 fprintf(stderr,"Creating RRA CF: %s, dep idx %lu, current idx %lu\n",
407 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam,
408 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt,
409 rrd.stat_head -> rra_cnt);
410 #endif
411 /* should we create CF_SEASONAL, CF_DEVSEASONAL, and CF_DEVPREDICT? */
412 if (cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) == CF_HWPREDICT
413 && rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_dependent_rra_idx].u_cnt
414 == rrd.stat_head -> rra_cnt)
415 {
416 #ifdef DEBUG
417 fprintf(stderr,"Creating HW contingent RRAs\n");
418 #endif
419 if (create_hw_contingent_rras(&rrd,period,hashed_name) == -1) {
420 rrd_set_error("creating contingent RRA");
421 rrd_free(&rrd);
422 return -1;
423 }
424 }
425 rrd.stat_head->rra_cnt++;
426 } else {
427 rrd_set_error("can't parse argument '%s'",argv[i]);
428 rrd_free(&rrd);
429 return -1;
430 }
431 }
434 if (rrd.stat_head->rra_cnt < 1){
435 rrd_set_error("you must define at least one Round Robin Archive");
436 rrd_free(&rrd);
437 return(-1);
438 }
440 if (rrd.stat_head->ds_cnt < 1){
441 rrd_set_error("you must define at least one Data Source");
442 rrd_free(&rrd);
443 return(-1);
444 }
445 return rrd_create_fn(filename, &rrd);
446 }
448 void parseGENERIC_DS(const char *def,rrd_t *rrd, int ds_idx)
449 {
450 char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];
451 /*
452 int temp;
454 temp = sscanf(def,"%lu:%18[^:]:%18[^:]",
455 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
456 minstr,maxstr);
457 */
458 if (sscanf(def,"%lu:%18[^:]:%18[^:]",
459 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
460 minstr,maxstr) == 3)
461 {
462 if (minstr[0] == 'U' && minstr[1] == 0)
463 rrd -> ds_def[ds_idx].par[DS_min_val].u_val = DNAN;
464 else
465 rrd -> ds_def[ds_idx].par[DS_min_val].u_val = atof(minstr);
467 if (maxstr[0] == 'U' && maxstr[1] == 0)
468 rrd -> ds_def[ds_idx].par[DS_max_val].u_val = DNAN;
469 else
470 rrd -> ds_def[ds_idx].par[DS_max_val].u_val = atof(maxstr);
472 if (! isnan(rrd -> ds_def[ds_idx].par[DS_min_val].u_val) &&
473 ! isnan(rrd -> ds_def[ds_idx].par[DS_max_val].u_val) &&
474 rrd -> ds_def[ds_idx].par[DS_min_val].u_val
475 >= rrd -> ds_def[ds_idx].par[DS_max_val].u_val ) {
476 rrd_set_error("min must be less than max in DS definition");
477 return;
478 }
479 } else {
480 rrd_set_error("failed to parse data source %s", def);
481 }
482 }
484 /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs
485 * associated with a CF_HWPREDICT RRA. */
486 int
487 create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashed_name)
488 {
489 size_t old_size;
490 rra_def_t* current_rra;
492 /* save index to CF_HWPREDICT */
493 unsigned long hw_index = rrd -> stat_head -> rra_cnt;
494 /* advance the pointer */
495 (rrd -> stat_head -> rra_cnt)++;
496 /* allocate the memory for the 4 contingent RRAs */
497 old_size = sizeof(rra_def_t)*(rrd -> stat_head->rra_cnt);
498 if ((rrd -> rra_def = rrd_realloc(rrd -> rra_def,
499 old_size+4*sizeof(rra_def_t)))==NULL)
500 {
501 rrd_set_error("allocating rrd.rra_def");
502 return(-1);
503 }
504 /* clear memory */
505 memset(&(rrd -> rra_def[rrd -> stat_head->rra_cnt]), 0, 4*sizeof(rra_def_t));
507 /* create the CF_SEASONAL RRA */
508 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
509 strcpy(current_rra -> cf_nam,"SEASONAL");
510 current_rra -> row_cnt = period;
511 current_rra -> par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
512 current_rra -> pdp_cnt = 1;
513 current_rra -> par[RRA_seasonal_gamma].u_val =
514 rrd -> rra_def[hw_index].par[RRA_hw_alpha].u_val;
515 current_rra -> par[RRA_dependent_rra_idx].u_cnt = hw_index;
516 rrd -> rra_def[hw_index].par[RRA_dependent_rra_idx].u_cnt = rrd -> stat_head -> rra_cnt;
518 /* create the CF_DEVSEASONAL RRA */
519 (rrd -> stat_head -> rra_cnt)++;
520 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
521 strcpy(current_rra -> cf_nam,"DEVSEASONAL");
522 current_rra -> row_cnt = period;
523 current_rra -> par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
524 current_rra -> pdp_cnt = 1;
525 current_rra -> par[RRA_seasonal_gamma].u_val =
526 rrd -> rra_def[hw_index].par[RRA_hw_alpha].u_val;
527 current_rra -> par[RRA_dependent_rra_idx].u_cnt = hw_index;
529 /* create the CF_DEVPREDICT RRA */
530 (rrd -> stat_head -> rra_cnt)++;
531 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
532 strcpy(current_rra -> cf_nam,"DEVPREDICT");
533 current_rra -> row_cnt = (rrd -> rra_def[hw_index]).row_cnt;
534 current_rra -> pdp_cnt = 1;
535 current_rra -> par[RRA_dependent_rra_idx].u_cnt
536 = hw_index + 2; /* DEVSEASONAL */
538 /* create the CF_FAILURES RRA */
539 (rrd -> stat_head -> rra_cnt)++;
540 current_rra = &(rrd -> rra_def[rrd -> stat_head -> rra_cnt]);
541 strcpy(current_rra -> cf_nam,"FAILURES");
542 current_rra -> row_cnt = period;
543 current_rra -> pdp_cnt = 1;
544 current_rra -> par[RRA_delta_pos].u_val = 2.0;
545 current_rra -> par[RRA_delta_neg].u_val = 2.0;
546 current_rra -> par[RRA_failure_threshold].u_cnt = 7;
547 current_rra -> par[RRA_window_len].u_cnt = 9;
548 current_rra -> par[RRA_dependent_rra_idx].u_cnt =
549 hw_index + 2; /* DEVSEASONAL */
550 return 0;
551 }
553 /* create and empty rrd file according to the specs given */
555 int
556 rrd_create_fn(const char *file_name, rrd_t *rrd)
557 {
558 unsigned long i,ii;
559 FILE *rrd_file;
560 rrd_value_t *unknown;
561 int unkn_cnt;
563 if ((rrd_file = fopen(file_name,"wb")) == NULL ) {
564 rrd_set_error("creating '%s': %s",file_name, rrd_strerror(errno));
565 rrd_free(rrd);
566 return(-1);
567 }
569 fwrite(rrd->stat_head,
570 sizeof(stat_head_t), 1, rrd_file);
572 fwrite(rrd->ds_def,
573 sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
575 fwrite(rrd->rra_def,
576 sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
578 fwrite(rrd->live_head,
579 sizeof(live_head_t),1, rrd_file);
581 if((rrd->pdp_prep = calloc(1,sizeof(pdp_prep_t))) == NULL){
582 rrd_set_error("allocating pdp_prep");
583 rrd_free(rrd);
584 fclose(rrd_file);
585 return(-1);
586 }
588 strcpy(rrd->pdp_prep->last_ds,"UNKN");
590 rrd->pdp_prep->scratch[PDP_val].u_val = 0.0;
591 rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt =
592 rrd->live_head->last_up % rrd->stat_head->pdp_step;
594 for(i=0; i < rrd->stat_head->ds_cnt; i++)
595 fwrite( rrd->pdp_prep,sizeof(pdp_prep_t),1,rrd_file);
597 if((rrd->cdp_prep = calloc(1,sizeof(cdp_prep_t))) == NULL){
598 rrd_set_error("allocating cdp_prep");
599 rrd_free(rrd);
600 fclose(rrd_file);
601 return(-1);
602 }
605 for(i=0; i < rrd->stat_head->rra_cnt; i++) {
606 switch (cf_conv(rrd->rra_def[i].cf_nam))
607 {
608 case CF_HWPREDICT:
609 init_hwpredict_cdp(rrd->cdp_prep);
610 break;
611 case CF_SEASONAL:
612 case CF_DEVSEASONAL:
613 init_seasonal_cdp(rrd->cdp_prep);
614 break;
615 case CF_FAILURES:
616 /* initialize violation history to 0 */
617 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
618 {
619 /* We can zero everything out, by setting u_val to the
620 * NULL address. Each array entry in scratch is 8 bytes
621 * (a double), but u_cnt only accessed 4 bytes (long) */
622 rrd->cdp_prep->scratch[ii].u_val = 0.0;
623 }
624 break;
625 default:
626 /* can not be zero because we don't know anything ... */
627 rrd->cdp_prep->scratch[CDP_val].u_val = DNAN;
628 /* startup missing pdp count */
629 rrd->cdp_prep->scratch[CDP_unkn_pdp_cnt].u_cnt =
630 ((rrd->live_head->last_up -
631 rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt)
632 % (rrd->stat_head->pdp_step
633 * rrd->rra_def[i].pdp_cnt)) / rrd->stat_head->pdp_step;
634 break;
635 }
637 for(ii=0; ii < rrd->stat_head->ds_cnt; ii++)
638 {
639 fwrite( rrd->cdp_prep,sizeof(cdp_prep_t),1,rrd_file);
640 }
641 }
643 /* now, we must make sure that the rest of the rrd
644 struct is properly initialized */
646 if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t))) == NULL) {
647 rrd_set_error("allocating rra_ptr");
648 rrd_free(rrd);
649 fclose(rrd_file);
650 return(-1);
651 }
653 /* changed this initialization to be consistent with
654 * rrd_restore. With the old value (0), the first update
655 * would occur for cur_row = 1 because rrd_update increments
656 * the pointer a priori. */
657 for (i=0; i < rrd->stat_head->rra_cnt; i++)
658 {
659 rrd->rra_ptr->cur_row = rra_random_row(&rrd->rra_def[i]);
660 fwrite( rrd->rra_ptr, sizeof(rra_ptr_t),1,rrd_file);
661 }
663 /* write the empty data area */
664 if ((unknown = (rrd_value_t *)malloc(512 * sizeof(rrd_value_t))) == NULL) {
665 rrd_set_error("allocating unknown");
666 rrd_free(rrd);
667 fclose(rrd_file);
668 return(-1);
669 }
670 for (i = 0; i < 512; ++i)
671 unknown[i] = DNAN;
673 unkn_cnt = 0;
674 for (i = 0; i < rrd->stat_head->rra_cnt; i++)
675 unkn_cnt += rrd->stat_head->ds_cnt * rrd->rra_def[i].row_cnt;
677 while (unkn_cnt > 0) {
678 fwrite(unknown, sizeof(rrd_value_t), min(unkn_cnt, 512), rrd_file);
679 unkn_cnt -= 512;
680 }
681 free(unknown);
683 /* lets see if we had an error */
684 if(ferror(rrd_file)){
685 rrd_set_error("a file error occurred while creating '%s'",file_name);
686 fclose(rrd_file);
687 rrd_free(rrd);
688 return(-1);
689 }
691 fclose(rrd_file);
692 rrd_free(rrd);
693 return (0);
694 }
696 static int rand_init = 0;
698 long int
699 rra_random_row(rra_def_t *rra)
700 {
701 if (!rand_init)
702 {
703 srandom((unsigned int)time(NULL) + (unsigned int)getpid());
704 rand_init++;
705 }
707 return random() % rra->row_cnt;
708 }