summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 06d7fbe)
raw | patch | inline | side by side (parent: 06d7fbe)
author | Pierre-Yves Ritschard <pyr@spootnik.org> | |
Thu, 16 Jan 2014 18:23:52 +0000 (19:23 +0100) | ||
committer | Pierre-Yves Ritschard <pyr@spootnik.org> | |
Thu, 16 Jan 2014 18:23:52 +0000 (19:23 +0100) |
src/common.c | patch | blob | history | |
src/common.h | patch | blob | history |
diff --git a/src/common.c b/src/common.c
index 81142fd0084bb4d9c89ea7061c62aca72cd12b7d..18b5c43253d0d4124de0d40e30287f0bcdcda9ff 100644 (file)
--- a/src/common.c
+++ b/src/common.c
char ident[128];
*ksp_ptr = NULL;
-
+
if (kc == NULL)
return (-1);
while ((ent = readdir (dh)) != NULL)
{
int status;
-
+
if (include_hidden)
{
if ((strcmp (".", ent->d_name) == 0)
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;
diff --git a/src/common.h b/src/common.h
index 67f307c32da05137721adef7028e0fc691a58267..29590ff26e64d3e8527e14d67c572f80fd085a99 100644 (file)
--- a/src/common.h
+++ b/src/common.h
};
typedef struct rate_to_value_state_s rate_to_value_state_t;
+struct value_to_rate_state_s
+{
+ value_t last_value;
+ cdtime_t last_time;
+};
+typedef struct value_to_rate_state_s value_to_rate_state_t;
+
char *sstrncpy (char *dest, const char *src, size_t n);
__attribute__ ((format(printf,3,4)))
int rate_to_value (value_t *ret_value, gauge_t rate,
rate_to_value_state_t *state, int ds_type, cdtime_t t);
+int value_to_rate (value_t *ret_rate, derive_t value,
+ value_to_rate_state_t *state, int ds_type, cdtime_t t);
+
/* Converts a service name (a string) to a port number
* (in the range [1-65535]). Returns less than zero on error. */
int service_name_to_port_number (const char *service_name);