#include "collectd.h"
-#include "common.h"
#include "plugin.h"
+#include "utils/common/common.h"
#include <hiredis/hiredis.h>
#include <sys/time.h>
redis_query_t *next;
};
+struct prev_s {
+ derive_t keyspace_hits;
+ derive_t keyspace_misses;
+};
+typedef struct prev_s prev_t;
+
struct redis_node_s;
typedef struct redis_node_s redis_node_t;
struct redis_node_s {
char *name;
char *host;
+ char *socket;
char *passwd;
int port;
struct timeval timeout;
bool report_cpu_usage;
redisContext *redisContext;
redis_query_t *queries;
+ prev_t prev;
redis_node_t *next;
};
rq = next;
}
- redisFree(rn->redisContext);
+ if (rn->redisContext)
+ redisFree(rn->redisContext);
sfree(rn->name);
sfree(rn->host);
+ sfree(rn->socket);
sfree(rn->passwd);
sfree(rn);
} /* void redis_node_free */
rn->port = status;
status = 0;
}
+ } else if (strcasecmp("Socket", option->key) == 0) {
+ status = cf_util_get_string(option, &rn->socket);
} else if (strcasecmp("Query", option->key) == 0) {
redis_query_t *rq = redis_config_query(option);
if (rq == NULL) {
rn->name = strdup("default");
rn->host = strdup(REDIS_DEF_HOST);
- if (rn->name == NULL || rn->host == NULL)
+ if (rn->name == NULL || rn->host == NULL) {
+ sfree(rn->name);
+ sfree(rn->host);
+ sfree(rn);
return ENOMEM;
+ }
return redis_node_add(rn);
} /* }}} int redis_init */
}
} /* void redis_cpu_usage */
+static gauge_t calculate_ratio_percent(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;
+ }
+
+ derive_t num = part1 - *prev1;
+ derive_t denom = part2 - *prev2 + num;
+
+ *prev1 = part1;
+ *prev2 = part2;
+
+ if (denom == 0)
+ return NAN;
+
+ if (num == 0)
+ return 0;
+
+ return 100.0 * (gauge_t)num / (gauge_t)denom;
+} /* gauge_t calculate_ratio_percent */
+
+static void redis_keyspace_usage(redis_node_t *rn, char const *info_line) {
+ value_t hits, misses;
+
+ if (redis_get_info_value(info_line, "keyspace_hits", DS_TYPE_DERIVE, &hits) !=
+ 0)
+ return;
+
+ if (redis_get_info_value(info_line, "keyspace_misses", DS_TYPE_DERIVE,
+ &misses) != 0)
+ return;
+
+ redis_submit(rn->name, "cache_result", "hits", hits);
+ redis_submit(rn->name, "cache_result", "misses", misses);
+
+ prev_t *prev = &rn->prev;
+ gauge_t ratio = calculate_ratio_percent(
+ hits.derive, misses.derive, &prev->keyspace_hits, &prev->keyspace_misses);
+ redis_submit(rn->name, "percent", "hitratio", (value_t){.gauge = ratio});
+
+} /* void redis_keyspace_usage */
+
static void redis_check_connection(redis_node_t *rn) {
if (rn->redisContext)
return;
- redisContext *rh = redisConnectWithTimeout(rn->host, rn->port, rn->timeout);
+ redisContext *rh;
+ if (rn->socket != NULL)
+ rh = redisConnectUnixWithTimeout(rn->socket, rn->timeout);
+ else
+ rh = redisConnectWithTimeout(rn->host, rn->port, rn->timeout);
if (rh == NULL) {
ERROR("redis plugin: can't allocate redis context");
return;
}
if (rh->err) {
- ERROR("redis plugin: unable to connect to node `%s' (%s:%d): %s.", rn->name,
- rn->host, rn->port, rh->errstr);
+ if (rn->socket)
+ ERROR("redis plugin: unable to connect to node `%s' (%s): %s.", rn->name,
+ rn->socket, rh->errstr);
+ else
+ ERROR("redis plugin: unable to connect to node `%s' (%s:%d): %s.",
+ rn->name, rn->host, rn->port, rh->errstr);
redisFree(rh);
return;
}
"total_connections_received", DS_TYPE_DERIVE);
redis_handle_info(rn->name, rr->str, "total_operations", NULL,
"total_commands_processed", DS_TYPE_DERIVE);
- redis_handle_info(rn->name, rr->str, "operations_per_second", NULL,
- "instantaneous_ops_per_sec", DS_TYPE_GAUGE);
redis_handle_info(rn->name, rr->str, "expired_keys", NULL, "expired_keys",
DS_TYPE_DERIVE);
redis_handle_info(rn->name, rr->str, "evicted_keys", NULL, "evicted_keys",
DS_TYPE_GAUGE);
redis_handle_info(rn->name, rr->str, "current_connections", "slaves",
"connected_slaves", DS_TYPE_GAUGE);
- redis_handle_info(rn->name, rr->str, "cache_result", "hits", "keyspace_hits",
- DS_TYPE_DERIVE);
- redis_handle_info(rn->name, rr->str, "cache_result", "misses",
- "keyspace_misses", DS_TYPE_DERIVE);
redis_handle_info(rn->name, rr->str, "total_bytes", "input",
"total_net_input_bytes", DS_TYPE_DERIVE);
redis_handle_info(rn->name, rr->str, "total_bytes", "output",
"total_net_output_bytes", DS_TYPE_DERIVE);
+ redis_keyspace_usage(rn, rr->str);
+
redis_db_stats(rn->name, rr->str);
if (rn->report_cpu_usage)
{
redis_node_t *rn = user_data->data;
- DEBUG("redis plugin: querying info from node `%s' (%s:%d).", rn->name,
- rn->host, rn->port);
+#if COLLECT_DEBUG
+ if (rn->socket)
+ DEBUG("redis plugin: querying info from node `%s' (%s).", rn->name,
+ rn->socket);
+ else
+ DEBUG("redis plugin: querying info from node `%s' (%s:%d).", rn->name,
+ rn->host, rn->port);
+#endif
redis_check_connection(rn);