redis plugin: Added option ReportCpuUsage for reported CPU consumption
authorPavel Rochnyack <pavel2000@ngs.ru>
Tue, 19 Jun 2018 04:41:42 +0000 (11:41 +0700)
committerPavel Rochnyack <pavel2000@ngs.ru>
Tue, 19 Jun 2018 04:42:01 +0000 (11:42 +0700)
src/collectd.conf.pod
src/redis.c

index 34a3ed7..75b956f 100644 (file)
@@ -7285,7 +7285,6 @@ The I<Redis plugin> 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<Node> block which configures the connection
 parameters and set of user-defined queries for this node.
-Server state is requested by sending I<INFO> query.
 
   <Plugin redis>
     <Node "example">
@@ -7293,6 +7292,7 @@ Server state is requested by sending I<INFO> query.
         Port "6379"
         Timeout 2000
         ReportCommandStats false
+        ReportCpuUsage true
         <Query "LLEN myqueue">
           #Database 0
           Type "queue_length"
@@ -7339,9 +7339,14 @@ Defaults to 2000 (2 seconds).
 =item B<ReportCommandStats> B<false>|B<true>
 
 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<false>.
 
+=item B<ReportCpuUsage> B<true>|B<false>
+
+Enables or disables reporting of CPU consumption statistics.
+Defaults to B<true>.
+
 =item B<Query> I<Querystring>
 
 The B<Query> block identifies a query to execute against the redis server.
index afb2373..23234b8 100644 (file)
@@ -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 */