Don't initialize static numeric variables to 0
[collectd.git] / src / ceph.c
index 421c7ef..2622dc7 100644 (file)
@@ -148,7 +148,7 @@ enum perfcounter_type_d {
 };
 
 /** Give user option to use default (long run = since daemon started) avg */
-static int long_run_latency_avg = 0;
+static int long_run_latency_avg;
 
 /**
  * Give user option to use default type for special cases -
@@ -161,10 +161,10 @@ static int long_run_latency_avg = 0;
 static int convert_special_metrics = 1;
 
 /** Array of daemons to monitor */
-static struct ceph_daemon **g_daemons = NULL;
+static struct ceph_daemon **g_daemons;
 
 /** Number of elements in g_daemons */
-static size_t g_num_daemons = 0;
+static size_t g_num_daemons;
 
 /**
  * A set of data that we build up in memory while parsing the JSON.
@@ -280,7 +280,7 @@ static int ceph_cb_number(void *ctx, const char *number_val,
    * the same type of other "Bytes". Instead of keeping an "average" or
    * "rate", use the "sum" in the pair and assign that to the derive
    * value. */
-  if (convert_special_metrics && (state->depth >= 2) &&
+  if (convert_special_metrics && (state->depth > 2) &&
       (strcmp("filestore", state->stack[state->depth - 2]) == 0) &&
       (strcmp("journal_wr_bytes", state->stack[state->depth - 1]) == 0) &&
       (strcmp("avgcount", state->key) == 0)) {
@@ -396,7 +396,7 @@ static void ceph_daemon_free(struct ceph_daemon *d) {
 }
 
 /* compact_ds_name removed the special characters ":", "_", "-" and "+" from the
- * intput string. Characters following these special characters are capitalized.
+ * input string. Characters following these special characters are capitalized.
  * Trailing "+" and "-" characters are replaces with the strings "Plus" and
  * "Minus". */
 static int compact_ds_name(char *buffer, size_t buffer_size, char const *src) {
@@ -404,8 +404,8 @@ static int compact_ds_name(char *buffer, size_t buffer_size, char const *src) {
   size_t src_len;
   char *ptr = buffer;
   size_t ptr_size = buffer_size;
-  _Bool append_plus = 0;
-  _Bool append_minus = 0;
+  bool append_plus = false;
+  bool append_minus = false;
 
   if ((buffer == NULL) || (buffer_size <= strlen("Minus")) || (src == NULL))
     return EINVAL;
@@ -415,11 +415,11 @@ static int compact_ds_name(char *buffer, size_t buffer_size, char const *src) {
 
   /* Remove trailing "+" and "-". */
   if (src_copy[src_len - 1] == '+') {
-    append_plus = 1;
+    append_plus = true;
     src_len--;
     src_copy[src_len] = 0;
   } else if (src_copy[src_len - 1] == '-') {
-    append_minus = 1;
+    append_minus = true;
     src_len--;
     src_copy[src_len] = 0;
   }
@@ -470,36 +470,34 @@ static int compact_ds_name(char *buffer, size_t buffer_size, char const *src) {
   return 0;
 }
 
-static _Bool has_suffix(char const *str, char const *suffix) {
+static bool has_suffix(char const *str, char const *suffix) {
   size_t str_len = strlen(str);
   size_t suffix_len = strlen(suffix);
   size_t offset;
 
   if (suffix_len > str_len)
-    return 0;
+    return false;
   offset = str_len - suffix_len;
 
   if (strcmp(str + offset, suffix) == 0)
-    return 1;
+    return true;
 
-  return 0;
+  return false;
 }
 
-static int cut_suffix(char new_str[], size_t new_str_len, char const *str,
-                      char const *suffix) {
+static void cut_suffix(char *buffer, size_t buffer_size, char const *str,
+                       char const *suffix) {
 
   size_t str_len = strlen(str);
   size_t suffix_len = strlen(suffix);
 
   size_t offset = str_len - suffix_len + 1;
 
-  if (offset > new_str_len) {
-    offset = new_str_len;
+  if (offset > buffer_size) {
+    offset = buffer_size;
   }
 
-  sstrncpy(new_str, str, offset);
-
-  return 0;
+  sstrncpy(buffer, str, offset);
 }
 
 /* count_parts returns the number of elements a "foo.bar.baz" style key has. */
@@ -518,23 +516,22 @@ static size_t count_parts(char const *key) {
 static int parse_keys(char *buffer, size_t buffer_size, const char *key_str) {
   char tmp[2 * buffer_size];
   size_t tmp_size = sizeof(tmp);
+  const char *cut_suffixes[] = {".type", ".avgcount", ".sum", ".avgtime"};
 
   if (buffer == NULL || buffer_size == 0 || key_str == NULL ||
       strlen(key_str) == 0)
     return EINVAL;
+
+  sstrncpy(tmp, key_str, tmp_size);
+
   /* Strip suffix if it is ".type" or one of latency metric suffix. */
   if (count_parts(key_str) > 2) {
-    if (has_suffix(key_str, ".type")) {
-      cut_suffix(tmp, tmp_size, key_str, ".type");
-    } else if (has_suffix(key_str, ".avgcount")) {
-      cut_suffix(tmp, tmp_size, key_str, ".avgcount");
-    } else if (has_suffix(key_str, ".sum")) {
-      cut_suffix(tmp, tmp_size, key_str, ".sum");
-    } else if (has_suffix(key_str, ".avgtime")) {
-      cut_suffix(tmp, tmp_size, key_str, ".avgtime");
+    for (size_t i = 0; i < STATIC_ARRAY_SIZE(cut_suffixes); i++) {
+      if (has_suffix(key_str, cut_suffixes[i])) {
+        cut_suffix(tmp, tmp_size, key_str, cut_suffixes[i]);
+        break;
+      }
     }
-  } else {
-    sstrncpy(tmp, key_str, sizeof(tmp));
   }
 
   return compact_ds_name(buffer, buffer_size, tmp);
@@ -930,7 +927,13 @@ static int node_handler_fetch_data(void *arg, const char *val,
       uv.gauge = result;
       vtmp->latency_index = (vtmp->latency_index + 1);
     } else if (has_suffix(key, ".avgtime")) {
-      // skip this step if no need in long run latency
+
+      /* The "avgtime" metric reports ("sum" / "avgcount"), i.e. the average
+       * time per request since the start of the Ceph daemon. Report this only
+       * when the user has configured "long running average". Otherwise, use the
+       * rate of "sum" and "avgcount" to calculate the current latency.
+       */
+
       if (!long_run_latency_avg) {
         return 0;
       }
@@ -1338,7 +1341,7 @@ static int cconn_main_loop(uint32_t request_type) {
       struct cconn *io = io_array + i;
       ret = cconn_prepare(io, fds + nfds);
       if (ret < 0) {
-        WARNING("ceph plugin: cconn_prepare(name=%s,i=%zu,st=%d)=%d",
+        WARNING("ceph plugin: cconn_prepare(name=%s,i=%" PRIsz ",st=%d)=%d",
                 io->d->name, i, io->state, ret);
         cconn_close(io);
         io->request_type = ASOK_REQ_NONE;