X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcommon.c;h=18b5c43253d0d4124de0d40e30287f0bcdcda9ff;hb=866e1b2741ec958e18334094e2770059b710d11e;hp=81142fd0084bb4d9c89ea7061c62aca72cd12b7d;hpb=06d7fbe8bb299e76cac532378e7b1999495979dc;p=collectd.git diff --git a/src/common.c b/src/common.c index 81142fd0..18b5c432 100644 --- a/src/common.c +++ b/src/common.c @@ -650,7 +650,7 @@ int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name) char ident[128]; *ksp_ptr = NULL; - + if (kc == NULL) return (-1); @@ -1236,7 +1236,7 @@ int walk_directory (const char *dir, dirwalk_callback_f callback, while ((ent = readdir (dh)) != NULL) { int status; - + if (include_hidden) { if ((strcmp (".", ent->d_name) == 0) @@ -1399,6 +1399,69 @@ int rate_to_value (value_t *ret_value, gauge_t rate, /* {{{ */ return (0); } /* }}} value_t rate_to_value */ +int value_to_rate (value_t *ret_rate, derive_t value, /* {{{ */ + value_to_rate_state_t *state, + int ds_type, cdtime_t t) +{ + double interval; + + /* Another invalid state: The time is not increasing. */ + if (t <= state->last_time) + { + memset (state, 0, sizeof (*state)); + return (EINVAL); + } + + interval = CDTIME_T_TO_DOUBLE(t - state->last_time); + + /* Previous value is invalid. */ + if (state->last_time == 0) /* {{{ */ + { + if (ds_type == DS_TYPE_DERIVE) + { + state->last_value.derive = value; + } + else if (ds_type == DS_TYPE_COUNTER) + { + state->last_value.counter = (counter_t) value; + } + else if (ds_type == DS_TYPE_ABSOLUTE) + { + state->last_value.absolute = (absolute_t) value; + } + else + { + assert (23 == 42); + } + + state->last_time = t; + return (EAGAIN); + } /* }}} */ + + if (ds_type == DS_TYPE_DERIVE) + { + ret_rate->gauge = (value - state->last_value.derive) / interval; + state->last_value.derive = value; + } + else if (ds_type == DS_TYPE_COUNTER) + { + ret_rate->gauge = (((counter_t)value) - state->last_value.counter) / interval; + state->last_value.counter = (counter_t) value; + } + else if (ds_type == DS_TYPE_ABSOLUTE) + { + ret_rate->gauge = (((absolute_t)value) - state->last_value.absolute) / interval; + state->last_value.absolute = (absolute_t) value; + } + else + { + assert (23 == 42); + } + + state->last_time = t; + return (0); +} /* }}} value_t rate_to_value */ + int service_name_to_port_number (const char *service_name) { struct addrinfo *ai_list;