Merge remote-tracking branch 'github/pr/2446'
[collectd.git] / src / memcached.c
index 5933bd4..9cc7a05 100644 (file)
@@ -377,6 +377,13 @@ static int memcached_read(user_data_t *user_data) {
     } else if (FIELD_IS("listen_disabled_num")) {
       submit_derive("connections", "listen_disabled", atof(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);
+    }
 
     /*
      * Commands
@@ -412,8 +419,10 @@ static int memcached_read(user_data_t *user_data) {
     }
 
     /*
-     * Operations on the cache, i. e. cache hits, cache misses and evictions of
-     * items
+     * Operations on the cache:
+     * - get hits/misses
+     * - delete hits/misses
+     * - evictions
      */
     else if (FIELD_IS("get_hits")) {
       submit_derive("memcached_ops", "hits", atoll(fields[2]), st);
@@ -422,6 +431,10 @@ static int memcached_read(user_data_t *user_data) {
       submit_derive("memcached_ops", "misses", atoll(fields[2]), st);
     } else if (FIELD_IS("evictions")) {
       submit_derive("memcached_ops", "evictions", atoll(fields[2]), st);
+    } else if (FIELD_IS("delete_hits")) {
+      submit_derive("memcached_ops", "delete_hits", atoll(fields[2]), st);
+    } else if (FIELD_IS("delete_misses")) {
+      submit_derive("memcached_ops", "delete_misses", atoll(fields[2]), st);
     }
 
     /*
@@ -467,13 +480,7 @@ static int memcached_read(user_data_t *user_data) {
   return 0;
 } /* int memcached_read */
 
-static int memcached_add_read_callback(memcached_t *st) {
-  char callback_name[3 * DATA_MAX_NAME_LEN];
-  int status;
-
-  ssnprintf(callback_name, sizeof(callback_name), "memcached/%s",
-            (st->name != NULL) ? st->name : "__legacy__");
-
+static int memcached_set_defaults(memcached_t *st) {
   /* If no <Address> used then:
    * - Connect to the destination specified by <Host>, if present.
    *   If not, use the default address.
@@ -510,15 +517,28 @@ static int memcached_add_read_callback(memcached_t *st) {
   assert(st->connhost != NULL);
   assert(st->connport != NULL);
 
-  status = plugin_register_complex_read(
+  return 0;
+} /* int memcached_set_defaults */
+
+static int memcached_add_read_callback(memcached_t *st) {
+  char callback_name[3 * DATA_MAX_NAME_LEN];
+
+  if (memcached_set_defaults(st) != 0) {
+    memcached_free(st);
+    return -1;
+  }
+
+  snprintf(callback_name, sizeof(callback_name), "memcached/%s",
+           (st->name != NULL) ? st->name : "__legacy__");
+
+  return plugin_register_complex_read(
       /* group = */ "memcached",
       /* name      = */ callback_name,
       /* callback  = */ memcached_read,
-      /* interval  = */ 0, &(user_data_t){
-                               .data = st, .free_func = memcached_free,
-                           });
-
-  return status;
+      /* interval  = */ 0,
+      &(user_data_t){
+          .data = st, .free_func = memcached_free,
+      });
 } /* int memcached_add_read_callback */
 
 /* Configuration handling functiions
@@ -577,19 +597,15 @@ static int config_add_instance(oconfig_item_t *ci) {
       break;
   }
 
-  if (status == 0)
-    status = memcached_add_read_callback(st);
-
   if (status != 0) {
     memcached_free(st);
     return -1;
   }
 
-  return 0;
-}
+  return memcached_add_read_callback(st);
+} /* int config_add_instance */
 
 static int memcached_config(oconfig_item_t *ci) {
-  int status = 0;
   _Bool have_instance_block = 0;
 
   for (int i = 0; i < ci->children_num; i++) {
@@ -610,8 +626,8 @@ static int memcached_config(oconfig_item_t *ci) {
               child->key);
   } /* for (ci->children) */
 
-  return status;
-}
+  return 0;
+} /* int memcached_config */
 
 static int memcached_init(void) {
   memcached_t *st;
@@ -633,8 +649,6 @@ static int memcached_init(void) {
   status = memcached_add_read_callback(st);
   if (status == 0)
     memcached_have_instances = 1;
-  else
-    memcached_free(st);
 
   return status;
 } /* int memcached_init */