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].u_val))) {
431 strcpy(rrd.stat_head->version, RRD_VERSION); /* smoothing-window causes Version 4 */
432 if (rrd.rra_def[rrd.stat_head->rra_cnt].
433 par[RRA_seasonal_smoothing_window].u_val < 0.0
434 || rrd.rra_def[rrd.stat_head->rra_cnt].
435 par[RRA_seasonal_smoothing_window].u_val > 1.0) {
436 rrd_set_error("Invalid smoothing-window %f: must be between 0 and 1",
437 rrd.rra_def[rrd.stat_head->rra_cnt].
438 par[RRA_seasonal_smoothing_window].u_val);
439 }
440 } else {
441 rrd_set_error("Invalid option %s", token);
442 }
443 break;
444 case CF_HWPREDICT:
445 case CF_MHWPREDICT:
446 /* length of the associated CF_SEASONAL and CF_DEVSEASONAL arrays. */
447 period = atoi(token);
448 if (period >
449 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt)
450 rrd_set_error
451 ("Length of seasonal cycle exceeds length of HW prediction array");
452 break;
453 default:
454 /* shouldn't be any more arguments */
455 rrd_set_error
456 ("Unexpected extra argument for consolidation function %s",
457 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
458 break;
459 }
460 break;
461 case 5:
462 /* If we are here, this must be a CF_HWPREDICT RRA.
463 * Specifies the index (1-based) of CF_SEASONAL array
464 * associated with this CF_HWPREDICT array. If this argument
465 * is missing, then the CF_SEASONAL, CF_DEVSEASONAL, CF_DEVPREDICT,
466 * CF_FAILURES.
467 * arrays are created automatically. */
468 rrd.rra_def[rrd.stat_head->rra_cnt].
469 par[RRA_dependent_rra_idx].u_cnt = atoi(token) - 1;
470 break;
471 default:
472 /* should never get here */
473 rrd_set_error("Unknown error");
474 break;
475 } /* end switch */
476 if (rrd_test_error()) {
477 /* all errors are unrecoverable */
478 free(argvcopy);
479 rrd_free(&rrd);
480 return (-1);
481 }
482 token = strtok_r(NULL, ":", &tokptr);
483 token_idx++;
484 } /* end while */
485 free(argvcopy);
486 #ifdef DEBUG
487 fprintf(stderr,
488 "Creating RRA CF: %s, dep idx %lu, current idx %lu\n",
489 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam,
490 rrd.rra_def[rrd.stat_head->rra_cnt].
491 par[RRA_dependent_rra_idx].u_cnt, rrd.stat_head->rra_cnt);
492 #endif
493 /* should we create CF_SEASONAL, CF_DEVSEASONAL, and CF_DEVPREDICT? */
494 if ((cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) ==
495 CF_HWPREDICT
496 || cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) ==
497 CF_MHWPREDICT)
498 && rrd.rra_def[rrd.stat_head->rra_cnt].
499 par[RRA_dependent_rra_idx].u_cnt == rrd.stat_head->rra_cnt) {
500 #ifdef DEBUG
501 fprintf(stderr, "Creating HW contingent RRAs\n");
502 #endif
503 if (create_hw_contingent_rras(&rrd, period, hashed_name) ==
504 -1) {
505 rrd_set_error("creating contingent RRA");
506 rrd_free(&rrd);
507 return -1;
508 }
509 }
510 rrd.stat_head->rra_cnt++;
511 } else {
512 rrd_set_error("can't parse argument '%s'", argv[i]);
513 rrd_free(&rrd);
514 return -1;
515 }
516 }
519 if (rrd.stat_head->rra_cnt < 1) {
520 rrd_set_error("you must define at least one Round Robin Archive");
521 rrd_free(&rrd);
522 return (-1);
523 }
525 if (rrd.stat_head->ds_cnt < 1) {
526 rrd_set_error("you must define at least one Data Source");
527 rrd_free(&rrd);
528 return (-1);
529 }
530 return rrd_create_fn(filename, &rrd);
531 }
533 void parseGENERIC_DS(
534 const char *def,
535 rrd_t *rrd,
536 int ds_idx)
537 {
538 char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];
539 char *old_locale;
541 /*
542 int temp;
544 temp = sscanf(def,"%lu:%18[^:]:%18[^:]",
545 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
546 minstr,maxstr);
547 */
548 old_locale = setlocale(LC_NUMERIC, "C");
549 if (sscanf(def, "%lu:%18[^:]:%18[^:]",
550 &(rrd->ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
551 minstr, maxstr) == 3) {
552 if (minstr[0] == 'U' && minstr[1] == 0)
553 rrd->ds_def[ds_idx].par[DS_min_val].u_val = DNAN;
554 else
555 rrd->ds_def[ds_idx].par[DS_min_val].u_val = atof(minstr);
557 if (maxstr[0] == 'U' && maxstr[1] == 0)
558 rrd->ds_def[ds_idx].par[DS_max_val].u_val = DNAN;
559 else
560 rrd->ds_def[ds_idx].par[DS_max_val].u_val = atof(maxstr);
562 if (!isnan(rrd->ds_def[ds_idx].par[DS_min_val].u_val) &&
563 !isnan(rrd->ds_def[ds_idx].par[DS_max_val].u_val) &&
564 rrd->ds_def[ds_idx].par[DS_min_val].u_val
565 >= rrd->ds_def[ds_idx].par[DS_max_val].u_val) {
566 rrd_set_error("min must be less than max in DS definition");
567 setlocale(LC_NUMERIC, old_locale);
568 return;
569 }
570 } else {
571 rrd_set_error("failed to parse data source %s", def);
572 }
573 setlocale(LC_NUMERIC, old_locale);
574 }
576 /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs
577 * associated with a CF_HWPREDICT RRA. */
578 int create_hw_contingent_rras(
579 rrd_t *rrd,
580 unsigned short period,
581 unsigned long hashed_name)
582 {
583 size_t old_size;
584 rra_def_t *current_rra;
586 /* save index to CF_HWPREDICT */
587 unsigned long hw_index = rrd->stat_head->rra_cnt;
589 /* advance the pointer */
590 (rrd->stat_head->rra_cnt)++;
591 /* allocate the memory for the 4 contingent RRAs */
592 old_size = sizeof(rra_def_t) * (rrd->stat_head->rra_cnt);
593 if ((rrd->rra_def = rrd_realloc(rrd->rra_def,
594 old_size + 4 * sizeof(rra_def_t))) ==
595 NULL) {
596 rrd_set_error("allocating rrd.rra_def");
597 return (-1);
598 }
599 /* clear memory */
600 memset(&(rrd->rra_def[rrd->stat_head->rra_cnt]), 0,
601 4 * sizeof(rra_def_t));
603 /* create the CF_SEASONAL RRA */
604 current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
605 strcpy(current_rra->cf_nam, "SEASONAL");
606 current_rra->row_cnt = period;
607 current_rra->par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
608 current_rra->pdp_cnt = 1;
609 current_rra->par[RRA_seasonal_gamma].u_val =
610 rrd->rra_def[hw_index].par[RRA_hw_alpha].u_val;
611 current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index;
612 rrd->rra_def[hw_index].par[RRA_dependent_rra_idx].u_cnt =
613 rrd->stat_head->rra_cnt;
615 /* create the CF_DEVSEASONAL RRA */
616 (rrd->stat_head->rra_cnt)++;
617 current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
618 strcpy(current_rra->cf_nam, "DEVSEASONAL");
619 current_rra->row_cnt = period;
620 current_rra->par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
621 current_rra->pdp_cnt = 1;
622 current_rra->par[RRA_seasonal_gamma].u_val =
623 rrd->rra_def[hw_index].par[RRA_hw_alpha].u_val;
624 current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index;
626 /* create the CF_DEVPREDICT RRA */
627 (rrd->stat_head->rra_cnt)++;
628 current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
629 strcpy(current_rra->cf_nam, "DEVPREDICT");
630 current_rra->row_cnt = (rrd->rra_def[hw_index]).row_cnt;
631 current_rra->pdp_cnt = 1;
632 current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index + 2; /* DEVSEASONAL */
634 /* create the CF_FAILURES RRA */
635 (rrd->stat_head->rra_cnt)++;
636 current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
637 strcpy(current_rra->cf_nam, "FAILURES");
638 current_rra->row_cnt = period;
639 current_rra->pdp_cnt = 1;
640 current_rra->par[RRA_delta_pos].u_val = 2.0;
641 current_rra->par[RRA_delta_neg].u_val = 2.0;
642 current_rra->par[RRA_failure_threshold].u_cnt = 7;
643 current_rra->par[RRA_window_len].u_cnt = 9;
644 current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index + 2; /* DEVSEASONAL */
645 return 0;
646 }
648 /* create and empty rrd file according to the specs given */
650 int rrd_create_fn(
651 const char *file_name,
652 rrd_t *rrd)
653 {
654 unsigned long i, ii;
655 int rrd_file;
656 rrd_value_t *unknown;
657 int unkn_cnt;
658 rrd_file_t *rrd_file_dn;
659 rrd_t rrd_dn;
661 if ((rrd_file =
662 open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) == NULL) {
663 rrd_set_error("creating '%s': %s", file_name, rrd_strerror(errno));
664 rrd_free(rrd);
665 return (-1);
666 }
668 write(rrd_file, rrd->stat_head, sizeof(stat_head_t));
670 write(rrd_file, rrd->ds_def, sizeof(ds_def_t) * rrd->stat_head->ds_cnt);
672 write(rrd_file, rrd->rra_def,
673 sizeof(rra_def_t) * rrd->stat_head->rra_cnt);
675 write(rrd_file, rrd->live_head, sizeof(live_head_t));
677 if ((rrd->pdp_prep = calloc(1, sizeof(pdp_prep_t))) == NULL) {
678 rrd_set_error("allocating pdp_prep");
679 rrd_free(rrd);
680 close(rrd_file);
681 return (-1);
682 }
684 strcpy(rrd->pdp_prep->last_ds, "UNKN");
686 rrd->pdp_prep->scratch[PDP_val].u_val = 0.0;
687 rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt =
688 rrd->live_head->last_up % rrd->stat_head->pdp_step;
690 for (i = 0; i < rrd->stat_head->ds_cnt; i++)
691 write(rrd_file, rrd->pdp_prep, sizeof(pdp_prep_t));
693 if ((rrd->cdp_prep = calloc(1, sizeof(cdp_prep_t))) == NULL) {
694 rrd_set_error("allocating cdp_prep");
695 rrd_free(rrd);
696 close(rrd_file);
697 return (-1);
698 }
701 for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
702 switch (cf_conv(rrd->rra_def[i].cf_nam)) {
703 case CF_HWPREDICT:
704 case CF_MHWPREDICT:
705 init_hwpredict_cdp(rrd->cdp_prep);
706 break;
707 case CF_SEASONAL:
708 case CF_DEVSEASONAL:
709 init_seasonal_cdp(rrd->cdp_prep);
710 break;
711 case CF_FAILURES:
712 /* initialize violation history to 0 */
713 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++) {
714 /* We can zero everything out, by setting u_val to the
715 * NULL address. Each array entry in scratch is 8 bytes
716 * (a double), but u_cnt only accessed 4 bytes (long) */
717 rrd->cdp_prep->scratch[ii].u_val = 0.0;
718 }
719 break;
720 default:
721 /* can not be zero because we don't know anything ... */
722 rrd->cdp_prep->scratch[CDP_val].u_val = DNAN;
723 /* startup missing pdp count */
724 rrd->cdp_prep->scratch[CDP_unkn_pdp_cnt].u_cnt =
725 ((rrd->live_head->last_up -
726 rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt)
727 % (rrd->stat_head->pdp_step
728 * rrd->rra_def[i].pdp_cnt)) / rrd->stat_head->pdp_step;
729 break;
730 }
732 for (ii = 0; ii < rrd->stat_head->ds_cnt; ii++) {
733 write(rrd_file, rrd->cdp_prep, sizeof(cdp_prep_t));
734 }
735 }
737 /* now, we must make sure that the rest of the rrd
738 struct is properly initialized */
740 if ((rrd->rra_ptr = calloc(1, sizeof(rra_ptr_t))) == NULL) {
741 rrd_set_error("allocating rra_ptr");
742 rrd_free(rrd);
743 close(rrd_file);
744 return (-1);
745 }
747 /* changed this initialization to be consistent with
748 * rrd_restore. With the old value (0), the first update
749 * would occur for cur_row = 1 because rrd_update increments
750 * the pointer a priori. */
751 for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
752 rrd->rra_ptr->cur_row = rrd->rra_def[i].row_cnt - 1;
753 write(rrd_file, rrd->rra_ptr, sizeof(rra_ptr_t));
754 }
756 /* write the empty data area */
757 if ((unknown = (rrd_value_t *) malloc(512 * sizeof(rrd_value_t))) == NULL) {
758 rrd_set_error("allocating unknown");
759 rrd_free(rrd);
760 close(rrd_file);
761 return (-1);
762 }
763 for (i = 0; i < 512; ++i)
764 unknown[i] = DNAN;
766 unkn_cnt = 0;
767 for (i = 0; i < rrd->stat_head->rra_cnt; i++)
768 unkn_cnt += rrd->stat_head->ds_cnt * rrd->rra_def[i].row_cnt;
770 while (unkn_cnt > 0) {
771 write(rrd_file, unknown, sizeof(rrd_value_t) * min(unkn_cnt, 512));
773 unkn_cnt -= 512;
774 }
775 free(unknown);
776 fdatasync(rrd_file);
777 rrd_free(rrd);
778 if (close(rrd_file) == -1) {
779 rrd_set_error("creating rrd: %s", rrd_strerror(errno));
780 return -1;
781 }
782 /* flush all we don't need out of the cache */
783 rrd_file_dn = rrd_open(file_name, &rrd_dn, RRD_READONLY);
784 rrd_dontneed(rrd_file_dn, &rrd_dn);
785 rrd_close(rrd_file_dn);
786 return (0);
787 }