1 /*****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
3 *****************************************************************************
4 * rrd_create.c creates new rrds
5 *****************************************************************************/
12 #include "rrd_rpncalc.h"
15 #include "rrd_is_thread_safe.h"
16 static int opt_no_overwrite = 0;
22 unsigned long FnvHash(
24 int create_hw_contingent_rras(
26 unsigned short period,
27 unsigned long hashed_name);
33 static void rrd_free2(
34 rrd_t *rrd); /* our onwn copy, immmune to mmap */
40 struct option long_options[] = {
41 {"start", required_argument, 0, 'b'},
42 {"step", required_argument, 0, 's'},
43 {"no-overwrite", no_argument, 0, 'O'},
48 time_t last_up = time(NULL) - 10;
49 unsigned long pdp_step = 300;
50 rrd_time_value_t last_up_tv;
51 char *parsetime_error = NULL;
56 opterr = 0; /* initialize getopt */
59 opt = getopt_long(argc, argv, "Ob:s:", long_options, &option_index);
66 if ((parsetime_error = rrd_parsetime(optarg, &last_up_tv))) {
67 rrd_set_error("start time: %s", parsetime_error);
70 if (last_up_tv.type == RELATIVE_TO_END_TIME ||
71 last_up_tv.type == RELATIVE_TO_START_TIME) {
72 rrd_set_error("specifying time relative to the 'start' "
73 "or 'end' makes no sense here");
77 last_up = mktime(&last_up_tv.tm) +last_up_tv.offset;
79 if (last_up < 3600 * 24 * 365 * 10) {
81 ("the first entry to the RRD should be after 1980");
87 long_tmp = atol(optarg);
89 rrd_set_error("step size should be no less than one second");
101 rrd_set_error("unknown option '%c'", optopt);
103 rrd_set_error("unknown option '%s'", argv[optind - 1]);
107 if (optind == argc) {
108 rrd_set_error("need name of an rrd file to create");
111 rc = rrd_create_r(argv[optind],
113 argc - optind - 1, (const char **) (argv + optind + 1));
120 const char *filename,
121 unsigned long pdp_step,
130 char dummychar1[2], dummychar2[2];
131 unsigned short token_idx, error_flag, period = 0;
132 unsigned long hashed_name;
137 if ((rrd.stat_head = (stat_head_t*)calloc(1, sizeof(stat_head_t))) == NULL) {
138 rrd_set_error("allocating rrd.stat_head");
144 if ((rrd.live_head = (live_head_t*)calloc(1, sizeof(live_head_t))) == NULL) {
145 rrd_set_error("allocating rrd.live_head");
150 /* set some defaults */
151 strcpy(rrd.stat_head->cookie, RRD_COOKIE);
152 strcpy(rrd.stat_head->version, RRD_VERSION3); /* by default we are still version 3 */
153 rrd.stat_head->float_cookie = FLOAT_COOKIE;
154 rrd.stat_head->ds_cnt = 0; /* this will be adjusted later */
155 rrd.stat_head->rra_cnt = 0; /* ditto */
156 rrd.stat_head->pdp_step = pdp_step; /* 5 minute default */
158 /* a default value */
162 rrd.live_head->last_up = last_up;
164 /* optind points to the first non-option command line arg,
165 * in this case, the file name. */
166 /* Compute the FNV hash value (used by SEASONAL and DEVSEASONAL
168 hashed_name = FnvHash(filename);
169 for (i = 0; i < argc; i++) {
172 if (strncmp(argv[i], "DS:", 3) == 0) {
173 size_t old_size = sizeof(ds_def_t) * (rrd.stat_head->ds_cnt);
175 if ((rrd.ds_def = (ds_def_t*)rrd_realloc(rrd.ds_def,
176 old_size + sizeof(ds_def_t))) ==
178 rrd_set_error("allocating rrd.ds_def");
182 memset(&rrd.ds_def[rrd.stat_head->ds_cnt], 0, sizeof(ds_def_t));
183 /* extract the name and type */
184 switch (sscanf(&argv[i][3],
185 DS_NAM_FMT "%1[:]" DST_FMT "%1[:]%n",
186 rrd.ds_def[rrd.stat_head->ds_cnt].ds_nam,
188 rrd.ds_def[rrd.stat_head->ds_cnt].dst,
189 dummychar2, &offset)) {
192 rrd_set_error("Invalid DS name");
196 rrd_set_error("Invalid DS type");
198 case 4: /* (%n may or may not be counted) */
199 case 5: /* check for duplicate datasource names */
200 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++)
201 if (strcmp(rrd.ds_def[rrd.stat_head->ds_cnt].ds_nam,
202 rrd.ds_def[ii].ds_nam) == 0)
203 rrd_set_error("Duplicate DS name: %s",
204 rrd.ds_def[ii].ds_nam);
205 /* DS_type may be valid or not. Checked later */
208 rrd_set_error("invalid DS format");
210 if (rrd_test_error()) {
215 /* parse the remainder of the arguments */
216 switch (dst_conv(rrd.ds_def[rrd.stat_head->ds_cnt].dst)) {
221 parseGENERIC_DS(&argv[i][offset + 3], &rrd,
222 rrd.stat_head->ds_cnt);
225 parseCDEF_DS(&argv[i][offset + 3], &rrd,
226 rrd.stat_head->ds_cnt);
229 rrd_set_error("invalid DS type specified");
233 if (rrd_test_error()) {
237 rrd.stat_head->ds_cnt++;
238 } else if (strncmp(argv[i], "RRA:", 4) == 0) {
241 size_t old_size = sizeof(rra_def_t) * (rrd.stat_head->rra_cnt);
244 if ((rrd.rra_def = (rra_def_t*)rrd_realloc(rrd.rra_def,
245 old_size + sizeof(rra_def_t))) ==
247 rrd_set_error("allocating rrd.rra_def");
251 memset(&rrd.rra_def[rrd.stat_head->rra_cnt], 0,
254 argvcopy = strdup(argv[i]);
255 token = strtok_r(&argvcopy[4], ":", &tokptr);
256 token_idx = error_flag = 0;
257 while (token != NULL) {
260 if (sscanf(token, CF_NAM_FMT,
261 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) !=
263 rrd_set_error("Failed to parse CF name");
265 (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
267 strcpy(rrd.stat_head->version, RRD_VERSION); /* MHWPREDICT causes Version 4 */
269 /* initialize some parameters */
270 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha].
272 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].
274 rrd.rra_def[rrd.stat_head->rra_cnt].
275 par[RRA_dependent_rra_idx].u_cnt =
276 rrd.stat_head->rra_cnt;
280 /* initialize some parameters */
281 rrd.rra_def[rrd.stat_head->rra_cnt].
282 par[RRA_seasonal_gamma].u_val = 0.1;
283 rrd.rra_def[rrd.stat_head->rra_cnt].
284 par[RRA_seasonal_smoothing_window].u_val = 0.05;
287 rrd.rra_def[rrd.stat_head->rra_cnt].
288 par[RRA_dependent_rra_idx].u_cnt = -1;
291 rrd.rra_def[rrd.stat_head->rra_cnt].
292 par[RRA_delta_pos].u_val = 2.0;
293 rrd.rra_def[rrd.stat_head->rra_cnt].
294 par[RRA_delta_neg].u_val = 2.0;
295 rrd.rra_def[rrd.stat_head->rra_cnt].
296 par[RRA_window_len].u_cnt = 3;
297 rrd.rra_def[rrd.stat_head->rra_cnt].
298 par[RRA_failure_threshold].u_cnt = 2;
299 rrd.rra_def[rrd.stat_head->rra_cnt].
300 par[RRA_dependent_rra_idx].u_cnt = -1;
302 /* invalid consolidation function */
305 ("Unrecognized consolidation function %s",
306 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
310 /* default: 1 pdp per cdp */
311 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = 1;
315 (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
322 row_cnt = atoi(token);
324 rrd_set_error("Invalid row count: %i", row_cnt);
325 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = row_cnt;
328 rrd.rra_def[rrd.stat_head->rra_cnt].
329 par[RRA_cdp_xff_val].u_val = atof(token);
330 if (rrd.rra_def[rrd.stat_head->rra_cnt].
331 par[RRA_cdp_xff_val].u_val < 0.0
332 || rrd.rra_def[rrd.stat_head->rra_cnt].
333 par[RRA_cdp_xff_val].u_val >= 1.0)
335 ("Invalid xff: must be between 0 and 1");
341 (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
344 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha].
346 if (atof(token) <= 0.0 || atof(token) >= 1.0)
348 ("Invalid alpha: must be between 0 and 1");
352 rrd.rra_def[rrd.stat_head->rra_cnt].
353 par[RRA_seasonal_gamma].u_val = atof(token);
354 if (atof(token) <= 0.0 || atof(token) >= 1.0)
356 ("Invalid gamma: must be between 0 and 1");
357 rrd.rra_def[rrd.stat_head->rra_cnt].
358 par[RRA_seasonal_smooth_idx].u_cnt =
360 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt;
363 /* specifies the # of violations that constitutes the failure threshold */
364 rrd.rra_def[rrd.stat_head->rra_cnt].
365 par[RRA_failure_threshold].u_cnt = atoi(token);
367 || atoi(token) > MAX_FAILURES_WINDOW_LEN)
369 ("Failure threshold is out of range %d, %d",
370 1, MAX_FAILURES_WINDOW_LEN);
373 /* specifies the index (1-based) of CF_DEVSEASONAL array
374 * associated with this CF_DEVPREDICT array. */
375 rrd.rra_def[rrd.stat_head->rra_cnt].
376 par[RRA_dependent_rra_idx].u_cnt =
380 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt =
383 rrd_set_error("Invalid step: must be >= 1");
389 (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
392 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].
394 if (atof(token) < 0.0 || atof(token) > 1.0)
396 ("Invalid beta: must be between 0 and 1");
400 /* specifies the index (1-based) of CF_HWPREDICT array
401 * associated with this CF_DEVSEASONAL or CF_SEASONAL array.
403 rrd.rra_def[rrd.stat_head->rra_cnt].
404 par[RRA_dependent_rra_idx].u_cnt =
408 /* specifies the window length */
409 rrd.rra_def[rrd.stat_head->rra_cnt].
410 par[RRA_window_len].u_cnt = atoi(token);
412 || atoi(token) > MAX_FAILURES_WINDOW_LEN)
414 ("Window length is out of range %d, %d", 1,
415 MAX_FAILURES_WINDOW_LEN);
416 /* verify that window length exceeds the failure threshold */
417 if (rrd.rra_def[rrd.stat_head->rra_cnt].
418 par[RRA_window_len].u_cnt <
419 rrd.rra_def[rrd.stat_head->rra_cnt].
420 par[RRA_failure_threshold].u_cnt)
422 ("Window length is shorter than the failure threshold");
425 /* shouldn't be any more arguments */
427 ("Unexpected extra argument for consolidation function DEVPREDICT");
430 row_cnt = atoi(token);
432 rrd_set_error("Invalid row count: %i", row_cnt);
433 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = row_cnt;
439 (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
441 /* specifies the index (1-based) of CF_DEVSEASONAL array
442 * associated with this CF_DEVFAILURES array. */
443 rrd.rra_def[rrd.stat_head->rra_cnt].
444 par[RRA_dependent_rra_idx].u_cnt =
449 /* optional smoothing window */
450 if (sscanf(token, "smoothing-window=%lf",
451 &(rrd.rra_def[rrd.stat_head->rra_cnt].
452 par[RRA_seasonal_smoothing_window].
454 strcpy(rrd.stat_head->version, RRD_VERSION); /* smoothing-window causes Version 4 */
455 if (rrd.rra_def[rrd.stat_head->rra_cnt].
456 par[RRA_seasonal_smoothing_window].u_val < 0.0
457 || rrd.rra_def[rrd.stat_head->rra_cnt].
458 par[RRA_seasonal_smoothing_window].u_val >
461 ("Invalid smoothing-window %f: must be between 0 and 1",
462 rrd.rra_def[rrd.stat_head->rra_cnt].
463 par[RRA_seasonal_smoothing_window].
467 rrd_set_error("Invalid option %s", token);
472 /* length of the associated CF_SEASONAL and CF_DEVSEASONAL arrays. */
473 period = atoi(token);
475 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt)
477 ("Length of seasonal cycle exceeds length of HW prediction array");
480 /* shouldn't be any more arguments */
482 ("Unexpected extra argument for consolidation function %s",
483 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
488 /* If we are here, this must be a CF_HWPREDICT RRA.
489 * Specifies the index (1-based) of CF_SEASONAL array
490 * associated with this CF_HWPREDICT array. If this argument
491 * is missing, then the CF_SEASONAL, CF_DEVSEASONAL, CF_DEVPREDICT,
493 * arrays are created automatically. */
494 rrd.rra_def[rrd.stat_head->rra_cnt].
495 par[RRA_dependent_rra_idx].u_cnt = atoi(token) - 1;
498 /* should never get here */
499 rrd_set_error("Unknown error");
502 if (rrd_test_error()) {
503 /* all errors are unrecoverable */
508 token = strtok_r(NULL, ":", &tokptr);
514 "Creating RRA CF: %s, dep idx %lu, current idx %lu\n",
515 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam,
516 rrd.rra_def[rrd.stat_head->rra_cnt].
517 par[RRA_dependent_rra_idx].u_cnt, rrd.stat_head->rra_cnt);
519 /* should we create CF_SEASONAL, CF_DEVSEASONAL, and CF_DEVPREDICT? */
520 if ((cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) ==
522 || cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) ==
524 && rrd.rra_def[rrd.stat_head->rra_cnt].
525 par[RRA_dependent_rra_idx].u_cnt == rrd.stat_head->rra_cnt) {
527 fprintf(stderr, "Creating HW contingent RRAs\n");
529 if (create_hw_contingent_rras(&rrd, period, hashed_name) ==
531 rrd_set_error("creating contingent RRA");
536 rrd.stat_head->rra_cnt++;
538 rrd_set_error("can't parse argument '%s'", argv[i]);
545 if (rrd.stat_head->rra_cnt < 1) {
546 rrd_set_error("you must define at least one Round Robin Archive");
551 if (rrd.stat_head->ds_cnt < 1) {
552 rrd_set_error("you must define at least one Data Source");
556 return rrd_create_fn(filename, &rrd);
559 void parseGENERIC_DS(
564 char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];
570 temp = sscanf(def,"%lu:%18[^:]:%18[^:]",
571 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
574 old_locale = setlocale(LC_NUMERIC, "C");
575 if (sscanf(def, "%lu:%18[^:]:%18[^:]",
576 &(rrd->ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
577 minstr, maxstr) == 3) {
578 if (minstr[0] == 'U' && minstr[1] == 0)
579 rrd->ds_def[ds_idx].par[DS_min_val].u_val = DNAN;
581 rrd->ds_def[ds_idx].par[DS_min_val].u_val = atof(minstr);
583 if (maxstr[0] == 'U' && maxstr[1] == 0)
584 rrd->ds_def[ds_idx].par[DS_max_val].u_val = DNAN;
586 rrd->ds_def[ds_idx].par[DS_max_val].u_val = atof(maxstr);
588 if (!isnan(rrd->ds_def[ds_idx].par[DS_min_val].u_val) &&
589 !isnan(rrd->ds_def[ds_idx].par[DS_max_val].u_val) &&
590 rrd->ds_def[ds_idx].par[DS_min_val].u_val
591 >= rrd->ds_def[ds_idx].par[DS_max_val].u_val) {
592 rrd_set_error("min must be less than max in DS definition");
593 setlocale(LC_NUMERIC, old_locale);
597 rrd_set_error("failed to parse data source %s", def);
599 setlocale(LC_NUMERIC, old_locale);
602 /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs
603 * associated with a CF_HWPREDICT RRA. */
604 int create_hw_contingent_rras(
606 unsigned short period,
607 unsigned long hashed_name)
610 rra_def_t *current_rra;
612 /* save index to CF_HWPREDICT */
613 unsigned long hw_index = rrd->stat_head->rra_cnt;
615 /* advance the pointer */
616 (rrd->stat_head->rra_cnt)++;
617 /* allocate the memory for the 4 contingent RRAs */
618 old_size = sizeof(rra_def_t) * (rrd->stat_head->rra_cnt);
619 if ((rrd->rra_def = (rra_def_t*)rrd_realloc(rrd->rra_def,
620 old_size + 4 * sizeof(rra_def_t))) ==
623 rrd_set_error("allocating rrd.rra_def");
627 memset(&(rrd->rra_def[rrd->stat_head->rra_cnt]), 0,
628 4 * sizeof(rra_def_t));
630 /* create the CF_SEASONAL RRA */
631 current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
632 strcpy(current_rra->cf_nam, "SEASONAL");
633 current_rra->row_cnt = period;
634 current_rra->par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
635 current_rra->pdp_cnt = 1;
636 current_rra->par[RRA_seasonal_gamma].u_val =
637 rrd->rra_def[hw_index].par[RRA_hw_alpha].u_val;
638 current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index;
639 rrd->rra_def[hw_index].par[RRA_dependent_rra_idx].u_cnt =
640 rrd->stat_head->rra_cnt;
642 /* create the CF_DEVSEASONAL RRA */
643 (rrd->stat_head->rra_cnt)++;
644 current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
645 strcpy(current_rra->cf_nam, "DEVSEASONAL");
646 current_rra->row_cnt = period;
647 current_rra->par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
648 current_rra->pdp_cnt = 1;
649 current_rra->par[RRA_seasonal_gamma].u_val =
650 rrd->rra_def[hw_index].par[RRA_hw_alpha].u_val;
651 current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index;
653 /* create the CF_DEVPREDICT RRA */
654 (rrd->stat_head->rra_cnt)++;
655 current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
656 strcpy(current_rra->cf_nam, "DEVPREDICT");
657 current_rra->row_cnt = (rrd->rra_def[hw_index]).row_cnt;
658 current_rra->pdp_cnt = 1;
659 current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index + 2; /* DEVSEASONAL */
661 /* create the CF_FAILURES RRA */
662 (rrd->stat_head->rra_cnt)++;
663 current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
664 strcpy(current_rra->cf_nam, "FAILURES");
665 current_rra->row_cnt = period;
666 current_rra->pdp_cnt = 1;
667 current_rra->par[RRA_delta_pos].u_val = 2.0;
668 current_rra->par[RRA_delta_neg].u_val = 2.0;
669 current_rra->par[RRA_failure_threshold].u_cnt = 7;
670 current_rra->par[RRA_window_len].u_cnt = 9;
671 current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index + 2; /* DEVSEASONAL */
675 /* create and empty rrd file according to the specs given */
678 const char *file_name,
682 rrd_value_t *unknown;
684 rrd_file_t *rrd_file_dn;
686 unsigned rrd_flags = RRD_READWRITE | RRD_CREAT;
688 if (opt_no_overwrite) {
689 rrd_flags |= RRD_EXCL ;
693 for (i = 0; i < rrd->stat_head->rra_cnt; i++)
694 unkn_cnt += rrd->stat_head->ds_cnt * rrd->rra_def[i].row_cnt;
696 if ((rrd_file_dn = rrd_open(file_name, rrd, rrd_flags)) == NULL) {
697 rrd_set_error("creating '%s': %s", file_name, rrd_strerror(errno));
702 rrd_write(rrd_file_dn, rrd->stat_head, sizeof(stat_head_t));
704 rrd_write(rrd_file_dn, rrd->ds_def, sizeof(ds_def_t) * rrd->stat_head->ds_cnt);
706 rrd_write(rrd_file_dn, rrd->rra_def,
707 sizeof(rra_def_t) * rrd->stat_head->rra_cnt);
709 rrd_write(rrd_file_dn, rrd->live_head, sizeof(live_head_t));
711 if ((rrd->pdp_prep = (pdp_prep_t*)calloc(1, sizeof(pdp_prep_t))) == NULL) {
712 rrd_set_error("allocating pdp_prep");
714 rrd_close(rrd_file_dn);
718 strcpy(rrd->pdp_prep->last_ds, "U");
720 rrd->pdp_prep->scratch[PDP_val].u_val = 0.0;
721 rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt =
722 rrd->live_head->last_up % rrd->stat_head->pdp_step;
724 for (i = 0; i < rrd->stat_head->ds_cnt; i++)
725 rrd_write(rrd_file_dn, rrd->pdp_prep, sizeof(pdp_prep_t));
727 if ((rrd->cdp_prep = (cdp_prep_t*)calloc(1, sizeof(cdp_prep_t))) == NULL) {
728 rrd_set_error("allocating cdp_prep");
730 rrd_close(rrd_file_dn);
735 for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
736 switch (cf_conv(rrd->rra_def[i].cf_nam)) {
739 init_hwpredict_cdp(rrd->cdp_prep);
743 init_seasonal_cdp(rrd->cdp_prep);
746 /* initialize violation history to 0 */
747 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++) {
748 /* We can zero everything out, by setting u_val to the
749 * NULL address. Each array entry in scratch is 8 bytes
750 * (a double), but u_cnt only accessed 4 bytes (long) */
751 rrd->cdp_prep->scratch[ii].u_val = 0.0;
755 /* can not be zero because we don't know anything ... */
756 rrd->cdp_prep->scratch[CDP_val].u_val = DNAN;
757 /* startup missing pdp count */
758 rrd->cdp_prep->scratch[CDP_unkn_pdp_cnt].u_cnt =
759 ((rrd->live_head->last_up -
760 rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt)
761 % (rrd->stat_head->pdp_step
762 * rrd->rra_def[i].pdp_cnt)) / rrd->stat_head->pdp_step;
766 for (ii = 0; ii < rrd->stat_head->ds_cnt; ii++) {
767 rrd_write(rrd_file_dn, rrd->cdp_prep, sizeof(cdp_prep_t));
771 /* now, we must make sure that the rest of the rrd
772 struct is properly initialized */
774 if ((rrd->rra_ptr = (rra_ptr_t*)calloc(1, sizeof(rra_ptr_t))) == NULL) {
775 rrd_set_error("allocating rra_ptr");
777 rrd_close(rrd_file_dn);
781 /* changed this initialization to be consistent with
782 * rrd_restore. With the old value (0), the first update
783 * would occur for cur_row = 1 because rrd_update increments
784 * the pointer a priori. */
785 for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
786 rrd->rra_ptr->cur_row = rrd_select_initial_row(rrd_file_dn, i, &rrd->rra_def[i]);
787 rrd_write(rrd_file_dn, rrd->rra_ptr, sizeof(rra_ptr_t));
790 /* write the empty data area */
791 if ((unknown = (rrd_value_t *) malloc(512 * sizeof(rrd_value_t))) == NULL) {
792 rrd_set_error("allocating unknown");
794 rrd_close(rrd_file_dn);
797 for (i = 0; i < 512; ++i)
800 while (unkn_cnt > 0) {
801 if(rrd_write(rrd_file_dn, unknown, sizeof(rrd_value_t) * min(unkn_cnt, 512)) < 0)
803 rrd_set_error("creating rrd: %s", rrd_strerror(errno));
811 if (rrd_close(rrd_file_dn) == -1) {
812 rrd_set_error("creating rrd: %s", rrd_strerror(errno));
815 /* flush all we don't need out of the cache */
817 if((rrd_file_dn = rrd_open(file_name, &rrd_dn, RRD_READONLY)) != NULL)
819 rrd_dontneed(rrd_file_dn, &rrd_dn);
820 /* rrd_free(&rrd_dn); */
821 rrd_close(rrd_file_dn);
827 static void rrd_free2(
830 free(rrd->live_head);
831 free(rrd->stat_head);
837 free(rrd->rrd_value);