From e0f43d29ba65148a0a0baf9bffc4345ddc8fe8ef Mon Sep 17 00:00:00 2001 From: Pavel Rochnyack Date: Mon, 9 Oct 2017 16:25:16 +0700 Subject: [PATCH] memcached: Replace gauge_t with derive_t --- src/memcached.c | 119 +++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 63 deletions(-) diff --git a/src/memcached.c b/src/memcached.c index 42917983..39400d9a 100644 --- a/src/memcached.c +++ b/src/memcached.c @@ -48,12 +48,12 @@ #define MEMCACHED_IO_TIMEOUT 5000 struct prev_s { - gauge_t hits; - gauge_t gets; - gauge_t incr_hits; - gauge_t incr_misses; - gauge_t decr_hits; - gauge_t decr_misses; + derive_t hits; + derive_t gets; + derive_t incr_hits; + derive_t incr_misses; + derive_t decr_hits; + derive_t decr_misses; }; typedef struct prev_s prev_t; @@ -392,46 +392,46 @@ static void submit_gauge2(const char *type, const char *type_inst, plugin_dispatch_values(&vl); } -static gauge_t calculate_ratio_percent(gauge_t part, gauge_t total, - gauge_t *prev_part, - gauge_t *prev_total) { - if (isnan(*prev_part) || isnan(*prev_total) || (part < *prev_part) || +static gauge_t calculate_ratio_percent(derive_t part, derive_t total, + derive_t *prev_part, + derive_t *prev_total) { + if ((*prev_part == 0) || (*prev_total == 0) || (part < *prev_part) || (total < *prev_total)) { *prev_part = part; *prev_total = total; return NAN; } - gauge_t num = part - *prev_part; - gauge_t denom = total - *prev_total; + derive_t num = part - *prev_part; + derive_t denom = total - *prev_total; *prev_part = part; *prev_total = total; if (num == 0 || denom == 0) - return 0; + return NAN; - return 100.0 * num / denom; + return 100.0 * (gauge_t)num / (gauge_t)denom; } -static gauge_t calculate_ratio_percent2(gauge_t part1, gauge_t part2, - gauge_t *prev1, gauge_t *prev2) { - if (isnan(*prev1) || isnan(*prev2) || (part1 < *prev1) || (part2 < *prev2)) { +static gauge_t calculate_ratio_percent2(derive_t part1, derive_t part2, + derive_t *prev1, derive_t *prev2) { + if ((*prev1 == 0) || (*prev2 == 0) || (part1 < *prev1) || (part2 < *prev2)) { *prev1 = part1; *prev2 = part2; return NAN; } - gauge_t num = part1 - *prev1; - gauge_t denom = part2 - *prev2 + num; + derive_t num = part1 - *prev1; + derive_t denom = part2 - *prev2 + num; *prev1 = part1; *prev2 = part2; if (num == 0 || denom == 0) - return 0; + return NAN; - return 100.0 * num / denom; + return 100.0 * (gauge_t)num / (gauge_t)denom; } static int memcached_read(user_data_t *user_data) { @@ -442,21 +442,20 @@ static int memcached_read(user_data_t *user_data) { char *saveptr; int fields_num; - gauge_t bytes_used = NAN; - gauge_t bytes_total = NAN; - gauge_t hits = NAN; - gauge_t gets = NAN; - gauge_t incr_hits = NAN; - gauge_t incr_misses = NAN; - gauge_t decr_hits = NAN; - gauge_t decr_misses = NAN; + derive_t bytes_used = 0; + derive_t bytes_total = 0; + derive_t get_hits = 0; + derive_t cmd_get = 0; + derive_t incr_hits = 0; + derive_t incr_misses = 0; + derive_t decr_hits = 0; + derive_t decr_misses = 0; derive_t rusage_user = 0; derive_t rusage_syst = 0; derive_t octets_rx = 0; derive_t octets_tx = 0; - memcached_t *st; - st = user_data->data; + memcached_t *st = user_data->data; prev_t *prev = &st->prev; /* get data from daemon */ @@ -515,9 +514,9 @@ static int memcached_read(user_data_t *user_data) { * Number of bytes used and available (total - used) */ else if (FIELD_IS("bytes")) { - bytes_used = atof(fields[2]); + bytes_used = atoll(fields[2]); } else if (FIELD_IS("limit_maxbytes")) { - bytes_total = atof(fields[2]); + bytes_total = atoll(fields[2]); } /* @@ -526,14 +525,14 @@ static int memcached_read(user_data_t *user_data) { else if (FIELD_IS("curr_connections")) { submit_gauge("memcached_connections", "current", atof(fields[2]), st); } else if (FIELD_IS("listen_disabled_num")) { - submit_derive("connections", "listen_disabled", atof(fields[2]), st); + submit_derive("connections", "listen_disabled", atoll(fields[2]), st); } /* * Total number of connections opened since the server started running * Report this as connection rate. */ else if (FIELD_IS("total_connections")) { - submit_derive("connections", "opened", atof(fields[2]), st); + submit_derive("connections", "opened", atoll(fields[2]), st); } /* @@ -543,28 +542,24 @@ static int memcached_read(user_data_t *user_data) { const char *name = fields[1] + 4; submit_derive("memcached_command", name, atoll(fields[2]), st); if (strcmp(name, "get") == 0) - gets = atof(fields[2]); + cmd_get = atoll(fields[2]); } /* * Increment/Decrement */ else if (FIELD_IS("incr_misses")) { - derive_t incr_count = atoll(fields[2]); - submit_derive("memcached_ops", "incr_misses", incr_count, st); - incr_misses = atof(fields[2]); + incr_misses = atoll(fields[2]); + submit_derive("memcached_ops", "incr_misses", incr_misses, st); } else if (FIELD_IS("incr_hits")) { - derive_t incr_count = atoll(fields[2]); - submit_derive("memcached_ops", "incr_hits", incr_count, st); - incr_hits = atof(fields[2]); + incr_hits = atoll(fields[2]); + submit_derive("memcached_ops", "incr_hits", incr_hits, st); } else if (FIELD_IS("decr_misses")) { - derive_t decr_count = atoll(fields[2]); - submit_derive("memcached_ops", "decr_misses", decr_count, st); - decr_misses = atof(fields[2]); + decr_misses = atoll(fields[2]); + submit_derive("memcached_ops", "decr_misses", decr_misses, st); } else if (FIELD_IS("decr_hits")) { - derive_t decr_count = atoll(fields[2]); - submit_derive("memcached_ops", "decr_hits", decr_count, st); - decr_hits = atof(fields[2]); + decr_hits = atoll(fields[2]); + submit_derive("memcached_ops", "decr_hits", decr_hits, st); } /* @@ -574,8 +569,8 @@ static int memcached_read(user_data_t *user_data) { * - evictions */ else if (FIELD_IS("get_hits")) { - submit_derive("memcached_ops", "hits", atoll(fields[2]), st); - hits = atof(fields[2]); + get_hits = atoll(fields[2]); + submit_derive("memcached_ops", "hits", get_hits, st); } else if (FIELD_IS("get_misses")) { submit_derive("memcached_ops", "misses", atoll(fields[2]), st); } else if (FIELD_IS("evictions")) { @@ -596,7 +591,7 @@ static int memcached_read(user_data_t *user_data) { } } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */ - if (!isnan(bytes_used) && !isnan(bytes_total) && (bytes_used <= bytes_total)) + if ((bytes_total > 0) && (bytes_used <= bytes_total)) submit_gauge2("df", "cache", bytes_used, bytes_total - bytes_used, st); if ((rusage_user != 0) || (rusage_syst != 0)) @@ -605,22 +600,20 @@ static int memcached_read(user_data_t *user_data) { if ((octets_rx != 0) || (octets_tx != 0)) submit_derive2("memcached_octets", NULL, octets_rx, octets_tx, st); - if (!isnan(gets) && !isnan(hits)) { + if ((cmd_get != 0) && (get_hits != 0)) { gauge_t ratio = - calculate_ratio_percent(hits, gets, &prev->hits, &prev->gets); + calculate_ratio_percent(get_hits, cmd_get, &prev->hits, &prev->gets); submit_gauge("percent", "hitratio", ratio, st); } - if (!isnan(incr_hits) && !isnan(incr_misses) && - (incr_hits + incr_misses > 0)) { + if ((incr_hits != 0) && (incr_misses != 0)) { gauge_t ratio = calculate_ratio_percent2( incr_hits, incr_misses, &prev->incr_hits, &prev->incr_misses); submit_gauge("percent", "incr_hitratio", ratio, st); submit_derive("memcached_ops", "incr", incr_hits + incr_misses, st); } - if (!isnan(decr_hits) && !isnan(decr_misses) && - (decr_hits + decr_misses > 0)) { + if ((decr_hits != 0) && (decr_misses != 0)) { gauge_t ratio = calculate_ratio_percent2( decr_hits, decr_misses, &prev->decr_hits, &prev->decr_misses); submit_gauge("percent", "decr_hitratio", ratio, st); @@ -667,12 +660,12 @@ static int memcached_set_defaults(memcached_t *st) { assert(st->connhost != NULL); assert(st->connport != NULL); - st->prev.hits = NAN; - st->prev.gets = NAN; - st->prev.incr_hits = NAN; - st->prev.incr_misses = NAN; - st->prev.decr_hits = NAN; - st->prev.decr_misses = NAN; + st->prev.hits = 0; + st->prev.gets = 0; + st->prev.incr_hits = 0; + st->prev.incr_misses = 0; + st->prev.decr_hits = 0; + st->prev.decr_misses = 0; return 0; } /* int memcached_set_defaults */ -- 2.30.2