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