#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_command_stats;
+ 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 */
redis_have_instances = true;
char cb_name[sizeof("redis/") + DATA_MAX_NAME_LEN];
- snprintf(cb_name, sizeof(cb_name), "redis/%s", rn->name);
+ ssnprintf(cb_name, sizeof(cb_name), "redis/%s", rn->name);
return plugin_register_complex_read(
/* group = */ "redis",
/* callback = */ redis_read,
/* interval = */ 0,
&(user_data_t){
- .data = rn, .free_func = redis_node_free,
+ .data = rn,
+ .free_func = redis_node_free,
});
} /* }}} */
rn->port = REDIS_DEF_PORT;
rn->timeout.tv_sec = REDIS_DEF_TIMEOUT_SEC;
+ rn->report_cpu_usage = true;
rn->host = strdup(REDIS_DEF_HOST);
if (rn->host == NULL) {
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) {
}
} else if (strcasecmp("Password", option->key) == 0)
status = cf_util_get_string(option, &rn->passwd);
+ else if (strcasecmp("ReportCommandStats", option->key) == 0)
+ status = cf_util_get_boolean(option, &rn->report_command_stats);
+ else if (strcasecmp("ReportCpuUsage", option->key) == 0)
+ status = cf_util_get_boolean(option, &rn->report_cpu_usage);
else
WARNING("redis plugin: Option `%s' not allowed inside a `Node' "
"block. I'll ignore this option.",
} /* }}} */
__attribute__((nonnull(2))) static void
-redis_submit(char *plugin_instance, const char *type, const char *type_instance,
- value_t value) /* {{{ */
+redis_submit(const char *plugin_instance, const char *type,
+ const char *type_instance, value_t value) /* {{{ */
{
value_list_t vl = VALUE_LIST_INIT;
plugin_dispatch_values(&vl);
} /* }}} */
+__attribute__((nonnull(2))) static void
+redis_submit2(const char *plugin_instance, const char *type,
+ const char *type_instance, value_t value0,
+ value_t value1) /* {{{ */
+{
+ value_list_t vl = VALUE_LIST_INIT;
+ value_t values[] = {value0, value1};
+
+ vl.values = values;
+ vl.values_len = STATIC_ARRAY_SIZE(values);
+
+ sstrncpy(vl.plugin, "redis", sizeof(vl.plugin));
+ sstrncpy(vl.type, type, sizeof(vl.type));
+
+ if (plugin_instance != NULL)
+ sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
+
+ if (type_instance != NULL)
+ sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
+
+ plugin_dispatch_values(&vl);
+} /* }}} */
+
static int redis_init(void) /* {{{ */
{
if (redis_have_instances)
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 */
return reply;
} /* void c_redisCommand */
-static int redis_handle_info(char *node, char const *info_line,
- char const *type, char const *type_instance,
- char const *field_name, int ds_type) /* {{{ */
-{
+static int redis_get_info_value(char const *info_line, char const *field_name,
+ int ds_type, value_t *val) {
char *str = strstr(info_line, field_name);
static char buf[MAX_REDIS_VAL_SIZE];
- value_t val;
if (str) {
int i;
buf[i] = *str;
buf[i] = '\0';
- if (parse_value(buf, &val, ds_type) == -1) {
+ if (parse_value(buf, val, ds_type) == -1) {
WARNING("redis plugin: Unable to parse field `%s'.", field_name);
return -1;
}
- redis_submit(node, type, type_instance, val);
return 0;
}
return -1;
+} /* int redis_get_info_value */
+static int redis_handle_info(char *node, char const *info_line,
+ char const *type, char const *type_instance,
+ char const *field_name, int ds_type) /* {{{ */
+{
+ value_t val;
+ if (redis_get_info_value(info_line, field_name, ds_type, &val) != 0)
+ return -1;
+
+ redis_submit(node, type, type_instance, val);
+ return 0;
} /* }}} int redis_handle_info */
static int redis_handle_query(redis_node_t *rn, redis_query_t *rq) /* {{{ */
return 0;
} /* }}} int redis_handle_query */
-static int redis_db_stats(char *node, char const *info_line) /* {{{ */
+static int redis_db_stats(const char *node, char const *info_line) /* {{{ */
{
/* redis_db_stats parses and dispatches Redis database statistics,
* currently the number of keys for each database.
char *str;
int i;
- snprintf(field_name, sizeof(field_name), "db%d:keys=", db);
+ ssnprintf(field_name, sizeof(field_name), "db%d:keys=", db);
str = strstr(info_line, field_name);
if (!str)
return -1;
}
- snprintf(db_id, sizeof(db_id), "%d", db);
+ ssnprintf(db_id, sizeof(db_id), "%d", db);
redis_submit(node, "records", db_id, val);
}
return 0;
} /* }}} int redis_db_stats */
+static void redis_cpu_usage(const char *node, char const *info_line) {
+ while (42) {
+ value_t rusage_user;
+ value_t rusage_syst;
+
+ if (redis_get_info_value(info_line, "used_cpu_user", DS_TYPE_GAUGE,
+ &rusage_user) != 0)
+ break;
+
+ if (redis_get_info_value(info_line, "used_cpu_sys", DS_TYPE_GAUGE,
+ &rusage_syst) != 0)
+ break;
+
+ redis_submit2(node, "ps_cputime", "daemon",
+ (value_t){.derive = rusage_user.gauge * 1000000},
+ (value_t){.derive = rusage_syst.gauge * 1000000});
+ break;
+ }
+
+ while (42) {
+ value_t rusage_user;
+ value_t rusage_syst;
+
+ if (redis_get_info_value(info_line, "used_cpu_user_children", DS_TYPE_GAUGE,
+ &rusage_user) != 0)
+ break;
+
+ if (redis_get_info_value(info_line, "used_cpu_sys_children", DS_TYPE_GAUGE,
+ &rusage_syst) != 0)
+ break;
+
+ redis_submit2(node, "ps_cputime", "children",
+ (value_t){.derive = rusage_user.gauge * 1000000},
+ (value_t){.derive = rusage_syst.gauge * 1000000});
+ break;
+ }
+} /* 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_cpu_usage(rn->name, rr->str);
+
freeReplyObject(rr);
} /* void redis_read_server_info */
+static void redis_read_command_stats(redis_node_t *rn) {
+ redisReply *rr;
+
+ if ((rr = c_redisCommand(rn, "INFO commandstats")) == NULL) {
+ WARNING("redis plugin: node `%s': unable to get `INFO commandstats'.",
+ rn->name);
+ return;
+ }
+
+ if (rr->type != REDIS_REPLY_STRING) {
+ WARNING("redis plugin: node `%s' `INFO commandstats' returned unsupported "
+ "redis type %i.",
+ rn->name, rr->type);
+ freeReplyObject(rr);
+ return;
+ }
+
+ char *command;
+ char *line;
+ char *ptr = rr->str;
+ char *saveptr = NULL;
+ while ((line = strtok_r(ptr, "\n\r", &saveptr)) != NULL) {
+ ptr = NULL;
+
+ if (line[0] == '#')
+ continue;
+
+ /* command name */
+ if (strstr(line, "cmdstat_") != line) {
+ ERROR("redis plugin: not found 'cmdstat_' prefix in line '%s'", line);
+ continue;
+ }
+
+ char *values = strstr(line, ":");
+ if (values == NULL) {
+ ERROR("redis plugin: not found ':' separator in line '%s'", line);
+ continue;
+ }
+
+ /* Null-terminate command token */
+ values[0] = '\0';
+ command = line + strlen("cmdstat_");
+ values++;
+
+ /* parse values */
+ /* cmdstat_publish:calls=20795774,usec=111039258,usec_per_call=5.34 */
+ char *field;
+ char *saveptr_field = NULL;
+ while ((field = strtok_r(values, "=", &saveptr_field)) != NULL) {
+ values = NULL;
+
+ const char *type;
+ /* only these are supported */
+ if (strcmp(field, "calls") == 0)
+ type = "commands";
+ else if (strcmp(field, "usec") == 0)
+ type = "redis_command_cputime";
+ else
+ continue;
+
+ if ((field = strtok_r(NULL, ",", &saveptr_field)) == NULL)
+ continue;
+
+ char *endptr = NULL;
+ errno = 0;
+ derive_t value = strtoll(field, &endptr, 0);
+
+ if ((endptr == field) || (errno != 0))
+ continue;
+
+ redis_submit(rn->name, type, command, (value_t){.derive = value});
+ }
+ }
+ freeReplyObject(rr);
+} /* void redis_read_command_stats */
+
static int redis_read(user_data_t *user_data) /* {{{ */
{
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);
if (!rn->redisContext) /* connection lost */
return -1;
+ if (rn->report_command_stats) {
+ redis_read_command_stats(rn);
+
+ if (!rn->redisContext) /* connection lost */
+ return -1;
+ }
+
for (redis_query_t *rq = rn->queries; rq != NULL; rq = rq->next) {
redis_handle_query(rn, rq);
if (!rn->redisContext) /* connection lost */