From 988b5145d792b2465314a2a4f2d1cc66b18c16b7 Mon Sep 17 00:00:00 2001 From: Pavel Rochnyack Date: Tue, 19 Jun 2018 11:41:42 +0700 Subject: [PATCH] redis plugin: Added option ReportCpuUsage for reported CPU consumption --- src/collectd.conf.pod | 9 ++++-- src/redis.c | 89 ++++++++++++++++++++++++++++----------------------- 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 34a3ed7d..75b956fe 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -7285,7 +7285,6 @@ The I connects to one or more Redis servers, gathers information about each server's state and executes user-defined queries. For each server there is a I block which configures the connection parameters and set of user-defined queries for this node. -Server state is requested by sending I query. @@ -7293,6 +7292,7 @@ Server state is requested by sending I query. Port "6379" Timeout 2000 ReportCommandStats false + ReportCpuUsage true #Database 0 Type "queue_length" @@ -7339,9 +7339,14 @@ Defaults to 2000 (2 seconds). =item B B|B Enables or disables reporting of statistics based on the command type, including -rate of command calls and average CPU time consumed. +rate of command calls and average CPU time consumed by command processing. Defaults to B. +=item B B|B + +Enables or disables reporting of CPU consumption statistics. +Defaults to B. + =item B I The B block identifies a query to execute against the redis server. diff --git a/src/redis.c b/src/redis.c index afb23732..23234b83 100644 --- a/src/redis.c +++ b/src/redis.c @@ -68,6 +68,7 @@ struct redis_node_s { int port; struct timeval timeout; bool report_command_stats; + bool report_cpu_usage; redisContext *redisContext; redis_query_t *queries; @@ -177,6 +178,7 @@ static int redis_config_node(oconfig_item_t *ci) /* {{{ */ 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) { @@ -223,6 +225,8 @@ static int redis_config_node(oconfig_item_t *ci) /* {{{ */ 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.", @@ -257,8 +261,8 @@ static int redis_config(oconfig_item_t *ci) /* {{{ */ } /* }}} */ __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; @@ -275,7 +279,7 @@ redis_submit(char *plugin_instance, const char *type, const char *type_instance, } /* }}} */ __attribute__((nonnull(2))) static void -redis_submit2(char *plugin_instance, const char *type, +redis_submit2(const char *plugin_instance, const char *type, const char *type_instance, value_t value0, value_t value1) /* {{{ */ { @@ -450,7 +454,7 @@ 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. @@ -489,6 +493,44 @@ static int redis_db_stats(char *node, char const *info_line) /* {{{ */ } /* }}} 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 void redis_check_connection(redis_node_t *rn) { if (rn->redisContext) return; @@ -578,44 +620,11 @@ static void redis_read_server_info(redis_node_t *rn) { redis_handle_info(rn->name, rr->str, "total_bytes", "output", "total_net_output_bytes", DS_TYPE_DERIVE); - while (42) { - value_t rusage_user; - value_t rusage_syst; - - if (redis_get_info_value(rr->str, "used_cpu_user", DS_TYPE_GAUGE, - &rusage_user) != 0) - break; - - if (redis_get_info_value(rr->str, "used_cpu_sys", DS_TYPE_GAUGE, - &rusage_syst) != 0) - break; - - redis_submit2(rn->name, "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(rr->str, "used_cpu_user_children", DS_TYPE_GAUGE, - &rusage_user) != 0) - break; - - if (redis_get_info_value(rr->str, "used_cpu_sys_children", DS_TYPE_GAUGE, - &rusage_syst) != 0) - break; - - redis_submit2(rn->name, "ps_cputime", "children", - (value_t){.derive = rusage_user.gauge * 1000000}, - (value_t){.derive = rusage_syst.gauge * 1000000}); - break; - } - 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 */ -- 2.11.0