+ if (update_aberrant_cdps(rrd, rrd_file, rra_begin, rra_current,
+ elapsed_pdp_st, pdp_temp,
+ &seasonal_coef) == -1) {
+ goto err_free_coefficients;
+ }
+ if (write_to_rras(rrd, rrd_file, rra_step_cnt, rra_begin,
+ rra_current, *current_time, skip_update,
+ pcdp_summary) == -1) {
+ goto err_free_coefficients;
+ }
+ } /* endif a pdp_st has occurred */
+ rrd->live_head->last_up = *current_time;
+ rrd->live_head->last_up_usec = *current_time_usec;
+
+ free(seasonal_coef);
+ free(last_seasonal_coef);
+ return 0;
+
+ err_free_coefficients:
+ free(seasonal_coef);
+ free(last_seasonal_coef);
+ return -1;
+}
+
+/*
+ * Parse a DS string (time + colon-separated values), storing the
+ * results in current_time, current_time_usec, and updvals.
+ *
+ * Returns 0 on success, -1 on error.
+ */
+static int parse_ds(
+ rrd_t *rrd,
+ char **updvals,
+ long *tmpl_idx,
+ char *input,
+ unsigned long tmpl_cnt,
+ time_t *current_time,
+ unsigned long *current_time_usec,
+ int version)
+{
+ char *p;
+ unsigned long i;
+ char timesyntax;
+
+ updvals[0] = input;
+ /* initialize all ds input to unknown except the first one
+ which has always got to be set */
+ for (i = 1; i <= rrd->stat_head->ds_cnt; i++)
+ updvals[i] = "U";
+
+ /* separate all ds elements; first must be examined separately
+ due to alternate time syntax */
+ if ((p = strchr(input, '@')) != NULL) {
+ timesyntax = '@';
+ } else if ((p = strchr(input, ':')) != NULL) {
+ timesyntax = ':';
+ } else {
+ rrd_set_error("expected timestamp not found in data source from %s",
+ input);
+ return -1;
+ }
+ *p = '\0';
+ i = 1;
+ updvals[tmpl_idx[i++]] = p + 1;
+ while (*(++p)) {
+ if (*p == ':') {
+ *p = '\0';
+ if (i < tmpl_cnt) {
+ updvals[tmpl_idx[i++]] = p + 1;
+ }
+ }
+ }
+
+ if (i != tmpl_cnt) {
+ rrd_set_error("expected %lu data source readings (got %lu) from %s",
+ tmpl_cnt - 1, i, input);
+ return -1;
+ }
+
+ if (get_time_from_reading(rrd, timesyntax, updvals,
+ current_time, current_time_usec,
+ version) == -1) {
+ return -1;
+ }
+ return 0;
+}
+
+/*
+ * Parse the time in a DS string, store it in current_time and
+ * current_time_usec and verify that it's later than the last
+ * update for this DS.
+ *
+ * Returns 0 on success, -1 on error.
+ */
+static int get_time_from_reading(
+ rrd_t *rrd,
+ char timesyntax,
+ char **updvals,
+ time_t *current_time,
+ unsigned long *current_time_usec,
+ int version)
+{
+ double tmp;
+ char *parsetime_error = NULL;
+ char *old_locale;
+ struct rrd_time_value ds_tv;
+ struct timeval tmp_time; /* used for time conversion */
+
+ /* get the time from the reading ... handle N */
+ if (timesyntax == '@') { /* at-style */
+ if ((parsetime_error = parsetime(updvals[0], &ds_tv))) {
+ rrd_set_error("ds time: %s: %s", updvals[0], parsetime_error);
+ return -1;
+ }
+ if (ds_tv.type == RELATIVE_TO_END_TIME ||
+ ds_tv.type == RELATIVE_TO_START_TIME) {
+ rrd_set_error("specifying time relative to the 'start' "
+ "or 'end' makes no sense here: %s", updvals[0]);
+ return -1;
+ }
+ *current_time = mktime(&ds_tv.tm) +ds_tv.offset;
+ *current_time_usec = 0; /* FIXME: how to handle usecs here ? */
+ } else if (strcmp(updvals[0], "N") == 0) {
+ gettimeofday(&tmp_time, 0);
+ normalize_time(&tmp_time);
+ *current_time = tmp_time.tv_sec;
+ *current_time_usec = tmp_time.tv_usec;
+ } else {
+ old_locale = setlocale(LC_NUMERIC, "C");
+ tmp = strtod(updvals[0], 0);
+ setlocale(LC_NUMERIC, old_locale);
+ *current_time = floor(tmp);
+ *current_time_usec = (long) ((tmp - (double) *current_time) * 1e6f);
+ }
+ /* dont do any correction for old version RRDs */
+ if (version < 3)
+ *current_time_usec = 0;
+
+ if (*current_time < rrd->live_head->last_up ||
+ (*current_time == rrd->live_head->last_up &&
+ (long) *current_time_usec <= (long) rrd->live_head->last_up_usec)) {
+ rrd_set_error("illegal attempt to update using time %ld when "
+ "last update time is %ld (minimum one second step)",
+ *current_time, rrd->live_head->last_up);
+ return -1;
+ }
+ return 0;
+}
+
+/*
+ * Update pdp_new by interpreting the updvals according to the DS type
+ * (COUNTER, GAUGE, etc.).
+ *
+ * Returns 0 on success, -1 on error.
+ */
+static int update_pdp_prep(
+ rrd_t *rrd,
+ char **updvals,
+ rrd_value_t *pdp_new,
+ double interval)
+{
+ unsigned long ds_idx;
+ int ii;
+ char *endptr; /* used in the conversion */
+ double rate;
+ char *old_locale;
+ enum dst_en dst_idx;
+
+ for (ds_idx = 0; ds_idx < rrd->stat_head->ds_cnt; ds_idx++) {
+ dst_idx = dst_conv(rrd->ds_def[ds_idx].dst);
+
+ /* make sure we do not build diffs with old last_ds values */
+ if (rrd->ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt < interval) {
+ strncpy(rrd->pdp_prep[ds_idx].last_ds, "U", LAST_DS_LEN - 1);
+ rrd->pdp_prep[ds_idx].last_ds[LAST_DS_LEN - 1] = '\0';
+ }
+
+ /* NOTE: DST_CDEF should never enter this if block, because
+ * updvals[ds_idx+1][0] is initialized to 'U'; unless the caller
+ * accidently specified a value for the DST_CDEF. To handle this case,
+ * an extra check is required. */
+
+ if ((updvals[ds_idx + 1][0] != 'U') &&
+ (dst_idx != DST_CDEF) &&
+ rrd->ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt >= interval) {
+ rate = DNAN;
+
+ /* pdp_new contains rate * time ... eg the bytes transferred during
+ * the interval. Doing it this way saves a lot of math operations
+ */
+ switch (dst_idx) {
+ case DST_COUNTER:
+ case DST_DERIVE:
+ for (ii = 0; updvals[ds_idx + 1][ii] != '\0'; ii++) {
+ if ((updvals[ds_idx + 1][ii] < '0'
+ || updvals[ds_idx + 1][ii] > '9')
+ && (ii != 0 && updvals[ds_idx + 1][ii] != '-')) {
+ rrd_set_error("not a simple integer: '%s'",
+ updvals[ds_idx + 1]);
+ return -1;