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"
14 #include "rrd_client.h"
16 #include "rrd_is_thread_safe.h"
17 static int opt_no_overwrite = 0;
23 unsigned long FnvHash(
25 int create_hw_contingent_rras(
27 unsigned short period,
28 unsigned long hashed_name);
34 static void rrd_free2(
35 rrd_t *rrd); /* our onwn copy, immmune to mmap */
37 void rrd_create_set_no_overwrite(
40 opt_no_overwrite = (opt?1:0);
47 struct option long_options[] = {
48 {"start", required_argument, 0, 'b'},
49 {"step", required_argument, 0, 's'},
50 {"daemon", required_argument, 0, 'd'},
51 {"no-overwrite", no_argument, 0, 'O'},
56 time_t last_up = time(NULL) - 10;
57 unsigned long pdp_step = 300;
58 rrd_time_value_t last_up_tv;
59 char *parsetime_error = NULL;
62 char * opt_daemon = NULL;
65 opterr = 0; /* initialize getopt */
68 opt = getopt_long(argc, argv, "Ob:s:d:", long_options, &option_index);
75 if (opt_daemon != NULL)
77 opt_daemon = strdup (optarg);
78 if (opt_daemon == NULL)
80 rrd_set_error ("strdup failed.");
86 if ((parsetime_error = rrd_parsetime(optarg, &last_up_tv))) {
87 rrd_set_error("start time: %s", parsetime_error);
90 if (last_up_tv.type == RELATIVE_TO_END_TIME ||
91 last_up_tv.type == RELATIVE_TO_START_TIME) {
92 rrd_set_error("specifying time relative to the 'start' "
93 "or 'end' makes no sense here");
97 last_up = mktime(&last_up_tv.tm) +last_up_tv.offset;
99 if (last_up < 3600 * 24 * 365 * 10) {
101 ("the first entry to the RRD should be after 1980");
107 long_tmp = atol(optarg);
109 rrd_set_error("step size should be no less than one second");
116 opt_no_overwrite = 1;
121 rrd_set_error("unknown option '%c'", optopt);
123 rrd_set_error("unknown option '%s'", argv[optind - 1]);
127 if (optind == argc) {
128 rrd_set_error("need name of an rrd file to create");
132 rrdc_connect (opt_daemon);
133 if (rrdc_is_connected (opt_daemon)) {
134 rc = rrdc_create (argv[optind],
135 pdp_step, last_up, opt_no_overwrite,
136 argc - optind - 1, (const char **) (argv + optind + 1));
138 rc = rrd_create_r(argv[optind],
140 argc - optind - 1, (const char **) (argv + optind + 1));
148 const char *filename,
149 unsigned long pdp_step,
158 char dummychar1[2], dummychar2[2];
159 unsigned short token_idx, error_flag, period = 0;
160 unsigned long hashed_name;
162 /* clear any previous errors */
168 if ((rrd.stat_head = (stat_head_t*)calloc(1, sizeof(stat_head_t))) == NULL) {
169 rrd_set_error("allocating rrd.stat_head");
175 if ((rrd.live_head = (live_head_t*)calloc(1, sizeof(live_head_t))) == NULL) {
176 rrd_set_error("allocating rrd.live_head");
181 /* set some defaults */
182 strcpy(rrd.stat_head->cookie, RRD_COOKIE);
183 strcpy(rrd.stat_head->version, RRD_VERSION3); /* by default we are still version 3 */
184 rrd.stat_head->float_cookie = FLOAT_COOKIE;
185 rrd.stat_head->ds_cnt = 0; /* this will be adjusted later */
186 rrd.stat_head->rra_cnt = 0; /* ditto */
187 rrd.stat_head->pdp_step = pdp_step; /* 5 minute default */
189 /* a default value */
193 rrd.live_head->last_up = last_up;
195 /* optind points to the first non-option command line arg,
196 * in this case, the file name. */
197 /* Compute the FNV hash value (used by SEASONAL and DEVSEASONAL
199 hashed_name = FnvHash(filename);
200 for (i = 0; i < argc; i++) {
203 if (strncmp(argv[i], "DS:", 3) == 0) {
204 size_t old_size = sizeof(ds_def_t) * (rrd.stat_head->ds_cnt);
206 if ((rrd.ds_def = (ds_def_t*)rrd_realloc(rrd.ds_def,
207 old_size + sizeof(ds_def_t))) ==
209 rrd_set_error("allocating rrd.ds_def");
213 memset(&rrd.ds_def[rrd.stat_head->ds_cnt], 0, sizeof(ds_def_t));
214 /* extract the name and type */
215 switch (sscanf(&argv[i][3],
216 DS_NAM_FMT "%1[:]" DST_FMT "%1[:]%n",
217 rrd.ds_def[rrd.stat_head->ds_cnt].ds_nam,
219 rrd.ds_def[rrd.stat_head->ds_cnt].dst,
220 dummychar2, &offset)) {
223 rrd_set_error("Invalid DS name in [%s]",&argv[i][3]);
227 rrd_set_error("Invalid DS type in [%s]",&argv[i][3]);
229 case 4: /* (%n may or may not be counted) */
230 case 5: /* check for duplicate datasource names */
231 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++)
232 if (strcmp(rrd.ds_def[rrd.stat_head->ds_cnt].ds_nam,
233 rrd.ds_def[ii].ds_nam) == 0)
234 rrd_set_error("Duplicate DS name: %s",
235 rrd.ds_def[ii].ds_nam);
236 /* DS_type may be valid or not. Checked later */
239 rrd_set_error("invalid DS format");
241 if (rrd_test_error()) {
246 /* parse the remainder of the arguments */
247 switch (dst_conv(rrd.ds_def[rrd.stat_head->ds_cnt].dst)) {
252 parseGENERIC_DS(&argv[i][offset + 3], &rrd,
253 rrd.stat_head->ds_cnt);
256 parseCDEF_DS(&argv[i][offset + 3], &rrd,
257 rrd.stat_head->ds_cnt);
260 rrd_set_error("invalid DS type specified");
264 if (rrd_test_error()) {
268 rrd.stat_head->ds_cnt++;
269 } else if (strncmp(argv[i], "RRA:", 4) == 0) {
272 size_t old_size = sizeof(rra_def_t) * (rrd.stat_head->rra_cnt);
275 if ((rrd.rra_def = (rra_def_t*)rrd_realloc(rrd.rra_def,
276 old_size + sizeof(rra_def_t))) ==
278 rrd_set_error("allocating rrd.rra_def");
282 memset(&rrd.rra_def[rrd.stat_head->rra_cnt], 0,
285 argvcopy = strdup(argv[i]);
286 token = strtok_r(&argvcopy[4], ":", &tokptr);
287 token_idx = error_flag = 0;
288 while (token != NULL) {
291 if (sscanf(token, CF_NAM_FMT,
292 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) !=
294 rrd_set_error("Failed to parse CF name");
296 (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
298 strcpy(rrd.stat_head->version, RRD_VERSION); /* MHWPREDICT causes Version 4 */
300 /* initialize some parameters */
301 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha].
303 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].
305 rrd.rra_def[rrd.stat_head->rra_cnt].
306 par[RRA_dependent_rra_idx].u_cnt =
307 rrd.stat_head->rra_cnt;
311 /* initialize some parameters */
312 rrd.rra_def[rrd.stat_head->rra_cnt].
313 par[RRA_seasonal_gamma].u_val = 0.1;
314 rrd.rra_def[rrd.stat_head->rra_cnt].
315 par[RRA_seasonal_smoothing_window].u_val = 0.05;
318 rrd.rra_def[rrd.stat_head->rra_cnt].
319 par[RRA_dependent_rra_idx].u_cnt = -1;
322 rrd.rra_def[rrd.stat_head->rra_cnt].
323 par[RRA_delta_pos].u_val = 2.0;
324 rrd.rra_def[rrd.stat_head->rra_cnt].
325 par[RRA_delta_neg].u_val = 2.0;
326 rrd.rra_def[rrd.stat_head->rra_cnt].
327 par[RRA_window_len].u_cnt = 3;
328 rrd.rra_def[rrd.stat_head->rra_cnt].
329 par[RRA_failure_threshold].u_cnt = 2;
330 rrd.rra_def[rrd.stat_head->rra_cnt].
331 par[RRA_dependent_rra_idx].u_cnt = -1;
333 /* invalid consolidation function */
336 ("Unrecognized consolidation function %s",
337 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
341 /* default: 1 pdp per cdp */
342 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = 1;
346 (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
353 row_cnt = atoi(token);
355 rrd_set_error("Invalid row count: %i", row_cnt);
356 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = row_cnt;
359 rrd.rra_def[rrd.stat_head->rra_cnt].
360 par[RRA_cdp_xff_val].u_val = atof(token);
361 if (rrd.rra_def[rrd.stat_head->rra_cnt].
362 par[RRA_cdp_xff_val].u_val < 0.0
363 || rrd.rra_def[rrd.stat_head->rra_cnt].
364 par[RRA_cdp_xff_val].u_val >= 1.0)
366 ("Invalid xff: must be between 0 and 1");
372 (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
375 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha].
377 if (atof(token) <= 0.0 || atof(token) >= 1.0)
379 ("Invalid alpha: must be between 0 and 1");
383 rrd.rra_def[rrd.stat_head->rra_cnt].
384 par[RRA_seasonal_gamma].u_val = atof(token);
385 if (atof(token) <= 0.0 || atof(token) >= 1.0)
387 ("Invalid gamma: must be between 0 and 1");
388 rrd.rra_def[rrd.stat_head->rra_cnt].
389 par[RRA_seasonal_smooth_idx].u_cnt =
391 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt;
394 /* specifies the # of violations that constitutes the failure threshold */
395 rrd.rra_def[rrd.stat_head->rra_cnt].
396 par[RRA_failure_threshold].u_cnt = atoi(token);
398 || atoi(token) > MAX_FAILURES_WINDOW_LEN)
400 ("Failure threshold is out of range %d, %d",
401 1, MAX_FAILURES_WINDOW_LEN);
404 /* specifies the index (1-based) of CF_DEVSEASONAL array
405 * associated with this CF_DEVPREDICT array. */
406 rrd.rra_def[rrd.stat_head->rra_cnt].
407 par[RRA_dependent_rra_idx].u_cnt =
411 rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt =
414 rrd_set_error("Invalid step: must be >= 1");
420 (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
423 rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta].
425 if (atof(token) < 0.0 || atof(token) > 1.0)
427 ("Invalid beta: must be between 0 and 1");
431 /* specifies the index (1-based) of CF_HWPREDICT array
432 * associated with this CF_DEVSEASONAL or CF_SEASONAL array.
434 rrd.rra_def[rrd.stat_head->rra_cnt].
435 par[RRA_dependent_rra_idx].u_cnt =
439 /* specifies the window length */
440 rrd.rra_def[rrd.stat_head->rra_cnt].
441 par[RRA_window_len].u_cnt = atoi(token);
443 || atoi(token) > MAX_FAILURES_WINDOW_LEN)
445 ("Window length is out of range %d, %d", 1,
446 MAX_FAILURES_WINDOW_LEN);
447 /* verify that window length exceeds the failure threshold */
448 if (rrd.rra_def[rrd.stat_head->rra_cnt].
449 par[RRA_window_len].u_cnt <
450 rrd.rra_def[rrd.stat_head->rra_cnt].
451 par[RRA_failure_threshold].u_cnt)
453 ("Window length is shorter than the failure threshold");
456 /* shouldn't be any more arguments */
458 ("Unexpected extra argument for consolidation function DEVPREDICT");
461 row_cnt = atoi(token);
463 rrd_set_error("Invalid row count: %i", row_cnt);
464 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = row_cnt;
470 (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) {
472 /* specifies the index (1-based) of CF_DEVSEASONAL array
473 * associated with this CF_DEVFAILURES array. */
474 rrd.rra_def[rrd.stat_head->rra_cnt].
475 par[RRA_dependent_rra_idx].u_cnt =
480 /* optional smoothing window */
481 if (sscanf(token, "smoothing-window=%lf",
482 &(rrd.rra_def[rrd.stat_head->rra_cnt].
483 par[RRA_seasonal_smoothing_window].
485 strcpy(rrd.stat_head->version, RRD_VERSION); /* smoothing-window causes Version 4 */
486 if (rrd.rra_def[rrd.stat_head->rra_cnt].
487 par[RRA_seasonal_smoothing_window].u_val < 0.0
488 || rrd.rra_def[rrd.stat_head->rra_cnt].
489 par[RRA_seasonal_smoothing_window].u_val >
492 ("Invalid smoothing-window %f: must be between 0 and 1",
493 rrd.rra_def[rrd.stat_head->rra_cnt].
494 par[RRA_seasonal_smoothing_window].
498 rrd_set_error("Invalid option %s", token);
503 /* length of the associated CF_SEASONAL and CF_DEVSEASONAL arrays. */
504 period = atoi(token);
506 rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt)
508 ("Length of seasonal cycle exceeds length of HW prediction array");
511 /* shouldn't be any more arguments */
513 ("Unexpected extra argument for consolidation function %s",
514 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam);
519 /* If we are here, this must be a CF_HWPREDICT RRA.
520 * Specifies the index (1-based) of CF_SEASONAL array
521 * associated with this CF_HWPREDICT array. If this argument
522 * is missing, then the CF_SEASONAL, CF_DEVSEASONAL, CF_DEVPREDICT,
524 * arrays are created automatically. */
525 rrd.rra_def[rrd.stat_head->rra_cnt].
526 par[RRA_dependent_rra_idx].u_cnt = atoi(token) - 1;
529 /* should never get here */
530 rrd_set_error("Unknown error");
533 if (rrd_test_error()) {
534 /* all errors are unrecoverable */
539 token = strtok_r(NULL, ":", &tokptr);
545 "Creating RRA CF: %s, dep idx %lu, current idx %lu\n",
546 rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam,
547 rrd.rra_def[rrd.stat_head->rra_cnt].
548 par[RRA_dependent_rra_idx].u_cnt, rrd.stat_head->rra_cnt);
550 /* should we create CF_SEASONAL, CF_DEVSEASONAL, and CF_DEVPREDICT? */
551 if ((cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) ==
553 || cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) ==
555 && rrd.rra_def[rrd.stat_head->rra_cnt].
556 par[RRA_dependent_rra_idx].u_cnt == rrd.stat_head->rra_cnt) {
558 fprintf(stderr, "Creating HW contingent RRAs\n");
560 if (create_hw_contingent_rras(&rrd, period, hashed_name) ==
562 rrd_set_error("creating contingent RRA");
567 rrd.stat_head->rra_cnt++;
569 rrd_set_error("can't parse argument '%s'", argv[i]);
576 if (rrd.stat_head->rra_cnt < 1) {
577 rrd_set_error("you must define at least one Round Robin Archive");
582 if (rrd.stat_head->ds_cnt < 1) {
583 rrd_set_error("you must define at least one Data Source");
587 return rrd_create_fn(filename, &rrd);
590 void parseGENERIC_DS(
595 char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];
601 temp = sscanf(def,"%lu:%18[^:]:%18[^:]",
602 &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
605 old_locale = setlocale(LC_NUMERIC, NULL);
606 setlocale(LC_NUMERIC, "C");
607 if (sscanf(def, "%lu:%18[^:]:%18[^:]",
608 &(rrd->ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt),
609 minstr, maxstr) == 3) {
610 if (minstr[0] == 'U' && minstr[1] == 0)
611 rrd->ds_def[ds_idx].par[DS_min_val].u_val = DNAN;
613 rrd->ds_def[ds_idx].par[DS_min_val].u_val = atof(minstr);
615 if (maxstr[0] == 'U' && maxstr[1] == 0)
616 rrd->ds_def[ds_idx].par[DS_max_val].u_val = DNAN;
618 rrd->ds_def[ds_idx].par[DS_max_val].u_val = atof(maxstr);
620 if (!isnan(rrd->ds_def[ds_idx].par[DS_min_val].u_val) &&
621 !isnan(rrd->ds_def[ds_idx].par[DS_max_val].u_val) &&
622 rrd->ds_def[ds_idx].par[DS_min_val].u_val
623 >= rrd->ds_def[ds_idx].par[DS_max_val].u_val) {
624 rrd_set_error("min must be less than max in DS definition");
625 setlocale(LC_NUMERIC, old_locale);
629 rrd_set_error("failed to parse data source %s", def);
631 setlocale(LC_NUMERIC, old_locale);
634 /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs
635 * associated with a CF_HWPREDICT RRA. */
636 int create_hw_contingent_rras(
638 unsigned short period,
639 unsigned long hashed_name)
642 rra_def_t *current_rra;
644 /* save index to CF_HWPREDICT */
645 unsigned long hw_index = rrd->stat_head->rra_cnt;
647 /* advance the pointer */
648 (rrd->stat_head->rra_cnt)++;
649 /* allocate the memory for the 4 contingent RRAs */
650 old_size = sizeof(rra_def_t) * (rrd->stat_head->rra_cnt);
651 if ((rrd->rra_def = (rra_def_t*)rrd_realloc(rrd->rra_def,
652 old_size + 4 * sizeof(rra_def_t))) ==
655 rrd_set_error("allocating rrd.rra_def");
659 memset(&(rrd->rra_def[rrd->stat_head->rra_cnt]), 0,
660 4 * sizeof(rra_def_t));
662 /* create the CF_SEASONAL RRA */
663 current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
664 strcpy(current_rra->cf_nam, "SEASONAL");
665 current_rra->row_cnt = period;
666 current_rra->par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
667 current_rra->pdp_cnt = 1;
668 current_rra->par[RRA_seasonal_gamma].u_val =
669 rrd->rra_def[hw_index].par[RRA_hw_alpha].u_val;
670 current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index;
671 rrd->rra_def[hw_index].par[RRA_dependent_rra_idx].u_cnt =
672 rrd->stat_head->rra_cnt;
674 /* create the CF_DEVSEASONAL RRA */
675 (rrd->stat_head->rra_cnt)++;
676 current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
677 strcpy(current_rra->cf_nam, "DEVSEASONAL");
678 current_rra->row_cnt = period;
679 current_rra->par[RRA_seasonal_smooth_idx].u_cnt = hashed_name % period;
680 current_rra->pdp_cnt = 1;
681 current_rra->par[RRA_seasonal_gamma].u_val =
682 rrd->rra_def[hw_index].par[RRA_hw_alpha].u_val;
683 current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index;
685 /* create the CF_DEVPREDICT RRA */
686 (rrd->stat_head->rra_cnt)++;
687 current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
688 strcpy(current_rra->cf_nam, "DEVPREDICT");
689 current_rra->row_cnt = (rrd->rra_def[hw_index]).row_cnt;
690 current_rra->pdp_cnt = 1;
691 current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index + 2; /* DEVSEASONAL */
693 /* create the CF_FAILURES RRA */
694 (rrd->stat_head->rra_cnt)++;
695 current_rra = &(rrd->rra_def[rrd->stat_head->rra_cnt]);
696 strcpy(current_rra->cf_nam, "FAILURES");
697 current_rra->row_cnt = period;
698 current_rra->pdp_cnt = 1;
699 current_rra->par[RRA_delta_pos].u_val = 2.0;
700 current_rra->par[RRA_delta_neg].u_val = 2.0;
701 current_rra->par[RRA_failure_threshold].u_cnt = 7;
702 current_rra->par[RRA_window_len].u_cnt = 9;
703 current_rra->par[RRA_dependent_rra_idx].u_cnt = hw_index + 2; /* DEVSEASONAL */
707 /* create and empty rrd file according to the specs given */
710 const char *file_name,
714 rrd_value_t *unknown;
716 rrd_file_t *rrd_file_dn;
718 unsigned rrd_flags = RRD_READWRITE | RRD_CREAT;
720 if (opt_no_overwrite) {
721 rrd_flags |= RRD_EXCL ;
725 for (i = 0; i < rrd->stat_head->rra_cnt; i++)
726 unkn_cnt += rrd->stat_head->ds_cnt * rrd->rra_def[i].row_cnt;
728 if ((rrd_file_dn = rrd_open(file_name, rrd, rrd_flags)) == NULL) {
729 rrd_set_error("creating '%s': %s", file_name, rrd_strerror(errno));
734 rrd_write(rrd_file_dn, rrd->stat_head, sizeof(stat_head_t));
736 rrd_write(rrd_file_dn, rrd->ds_def, sizeof(ds_def_t) * rrd->stat_head->ds_cnt);
738 rrd_write(rrd_file_dn, rrd->rra_def,
739 sizeof(rra_def_t) * rrd->stat_head->rra_cnt);
741 rrd_write(rrd_file_dn, rrd->live_head, sizeof(live_head_t));
743 if ((rrd->pdp_prep = (pdp_prep_t*)calloc(1, sizeof(pdp_prep_t))) == NULL) {
744 rrd_set_error("allocating pdp_prep");
746 rrd_close(rrd_file_dn);
750 strcpy(rrd->pdp_prep->last_ds, "U");
752 rrd->pdp_prep->scratch[PDP_val].u_val = 0.0;
753 rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt =
754 rrd->live_head->last_up % rrd->stat_head->pdp_step;
756 for (i = 0; i < rrd->stat_head->ds_cnt; i++)
757 rrd_write(rrd_file_dn, rrd->pdp_prep, sizeof(pdp_prep_t));
759 if ((rrd->cdp_prep = (cdp_prep_t*)calloc(1, sizeof(cdp_prep_t))) == NULL) {
760 rrd_set_error("allocating cdp_prep");
762 rrd_close(rrd_file_dn);
767 for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
768 switch (cf_conv(rrd->rra_def[i].cf_nam)) {
771 init_hwpredict_cdp(rrd->cdp_prep);
775 init_seasonal_cdp(rrd->cdp_prep);
778 /* initialize violation history to 0 */
779 for (ii = 0; ii < MAX_CDP_PAR_EN; ii++) {
780 /* We can zero everything out, by setting u_val to the
781 * NULL address. Each array entry in scratch is 8 bytes
782 * (a double), but u_cnt only accessed 4 bytes (long) */
783 rrd->cdp_prep->scratch[ii].u_val = 0.0;
787 /* can not be zero because we don't know anything ... */
788 rrd->cdp_prep->scratch[CDP_val].u_val = DNAN;
789 /* startup missing pdp count */
790 rrd->cdp_prep->scratch[CDP_unkn_pdp_cnt].u_cnt =
791 ((rrd->live_head->last_up -
792 rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt)
793 % (rrd->stat_head->pdp_step
794 * rrd->rra_def[i].pdp_cnt)) / rrd->stat_head->pdp_step;
798 for (ii = 0; ii < rrd->stat_head->ds_cnt; ii++) {
799 rrd_write(rrd_file_dn, rrd->cdp_prep, sizeof(cdp_prep_t));
803 /* now, we must make sure that the rest of the rrd
804 struct is properly initialized */
806 if ((rrd->rra_ptr = (rra_ptr_t*)calloc(1, sizeof(rra_ptr_t))) == NULL) {
807 rrd_set_error("allocating rra_ptr");
809 rrd_close(rrd_file_dn);
813 /* changed this initialization to be consistent with
814 * rrd_restore. With the old value (0), the first update
815 * would occur for cur_row = 1 because rrd_update increments
816 * the pointer a priori. */
817 for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
818 rrd->rra_ptr->cur_row = rrd_select_initial_row(rrd_file_dn, i, &rrd->rra_def[i]);
819 rrd_write(rrd_file_dn, rrd->rra_ptr, sizeof(rra_ptr_t));
822 /* write the empty data area */
823 if ((unknown = (rrd_value_t *) malloc(512 * sizeof(rrd_value_t))) == NULL) {
824 rrd_set_error("allocating unknown");
826 rrd_close(rrd_file_dn);
829 for (i = 0; i < 512; ++i)
832 while (unkn_cnt > 0) {
833 if(rrd_write(rrd_file_dn, unknown, sizeof(rrd_value_t) * min(unkn_cnt, 512)) < 0)
835 rrd_set_error("creating rrd: %s", rrd_strerror(errno));
843 if (rrd_close(rrd_file_dn) == -1) {
844 rrd_set_error("creating rrd: %s", rrd_strerror(errno));
847 /* flush all we don't need out of the cache */
849 if((rrd_file_dn = rrd_open(file_name, &rrd_dn, RRD_READONLY)) != NULL)
851 rrd_dontneed(rrd_file_dn, &rrd_dn);
852 /* rrd_free(&rrd_dn); */
853 rrd_close(rrd_file_dn);
859 static void rrd_free2(
862 free(rrd->live_head);
863 free(rrd->stat_head);
869 free(rrd->rrd_value);