nfs plugin: Fix typo
[collectd.git] / src / nfs.c
index 74c6cbc..a7adc92 100644 (file)
--- a/src/nfs.c
+++ b/src/nfs.c
 #include <kstat.h>
 #endif
 
+static const char *config_keys[] = {"ReportV2", "ReportV3", "ReportV4"};
+static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
+static _Bool report_v2 = 1;
+static _Bool report_v3 = 1;
+static _Bool report_v4 = 1;
+
 /*
 see /proc/net/rpc/nfs
 see http://www.missioncriticallinux.com/orph/NFS-Statistics
@@ -295,8 +301,21 @@ static kstat_t *nfs4_ksp_client;
 static kstat_t *nfs4_ksp_server;
 #endif
 
+static int nfs_config(const char *key, const char *value) {
+  if (strcasecmp(key, "ReportV2") == 0)
+    report_v2 = IS_TRUE(value);
+  else if (strcasecmp(key, "ReportV3") == 0)
+    report_v3 = IS_TRUE(value);
+  else if (strcasecmp(key, "ReportV4") == 0)
+    report_v4 = IS_TRUE(value);
+  else
+    return -1;
+
+  return 0;
+}
+
 #if KERNEL_LINUX
-static int nfs_init(void) { return (0); }
+static int nfs_init(void) { return 0; }
 /* #endif KERNEL_LINUX */
 
 #elif HAVE_LIBKSTAT
@@ -309,7 +328,7 @@ static int nfs_init(void) {
   nfs4_ksp_server = NULL;
 
   if (kc == NULL)
-    return (-1);
+    return -1;
 
   for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
        ksp_chain = ksp_chain->ks_next) {
@@ -329,7 +348,7 @@ static int nfs_init(void) {
       nfs4_ksp_client = ksp_chain;
   }
 
-  return (0);
+  return 0;
 } /* int nfs_init */
 #endif
 
@@ -357,8 +376,8 @@ static void nfs_submit_fields(int nfs_version, const char *instance,
   char plugin_instance[DATA_MAX_NAME_LEN];
   value_t values[fields_num];
 
-  ssnprintf(plugin_instance, sizeof(plugin_instance), "v%i%s", nfs_version,
-            instance);
+  snprintf(plugin_instance, sizeof(plugin_instance), "v%i%s", nfs_version,
+           instance);
 
   for (size_t i = 0; i < fields_num; i++)
     (void)parse_value(fields[i], &values[i], DS_TYPE_DERIVE);
@@ -374,12 +393,12 @@ static int nfs_submit_fields_safe(int nfs_version, const char *instance,
     WARNING("nfs plugin: Wrong number of fields for "
             "NFSv%i %s statistics. Expected %zu, got %zu.",
             nfs_version, instance, proc_names_num, fields_num);
-    return (EINVAL);
+    return EINVAL;
   }
 
   nfs_submit_fields(nfs_version, instance, fields, fields_num, proc_names);
 
-  return (0);
+  return 0;
 }
 
 static int nfs_submit_nfs4_server(const char *instance, char **fields,
@@ -398,7 +417,7 @@ static int nfs_submit_nfs4_server(const char *instance, char **fields,
       fields_num = NFS4_SERVER_MAX_PROC;
       suppress_warning = 1;
     } else {
-      return (EINVAL);
+      return EINVAL;
     }
   }
 
@@ -412,7 +431,7 @@ static int nfs_submit_nfs4_server(const char *instance, char **fields,
                       nfs4_server41_procedures_names);
   }
 
-  return (0);
+  return 0;
 }
 
 static int nfs_submit_nfs4_client(const char *instance, char **fields,
@@ -462,7 +481,7 @@ static int nfs_submit_nfs4_client(const char *instance, char **fields,
 
       suppress_warning = 1;
     } else {
-      return (EINVAL);
+      return EINVAL;
     }
   }
 
@@ -477,7 +496,7 @@ static int nfs_submit_nfs4_client(const char *instance, char **fields,
                       nfs4_client41_procedures_names);
   }
 
-  return (0);
+  return 0;
 }
 
 static void nfs_read_linux(FILE *fh, const char *inst) {
@@ -495,18 +514,18 @@ static void nfs_read_linux(FILE *fh, const char *inst) {
     if (fields_num < 3)
       continue;
 
-    if (strcmp(fields[0], "proc2") == 0) {
+    if (strcmp(fields[0], "proc2") == 0 && report_v2) {
       nfs_submit_fields_safe(/* version = */ 2, inst, fields + 2,
                              (size_t)(fields_num - 2), nfs2_procedures_names,
                              nfs2_procedures_names_num);
-    } else if (strncmp(fields[0], "proc3", 5) == 0) {
+    } else if (strncmp(fields[0], "proc3", 5) == 0 && report_v3) {
       nfs_submit_fields_safe(/* version = */ 3, inst, fields + 2,
                              (size_t)(fields_num - 2), nfs3_procedures_names,
                              nfs3_procedures_names_num);
-    } else if (strcmp(fields[0], "proc4ops") == 0) {
+    } else if (strcmp(fields[0], "proc4ops") == 0 && report_v4) {
       if (inst[0] == 's')
         nfs_submit_nfs4_server(inst, fields + 2, (size_t)(fields_num - 2));
-    } else if (strcmp(fields[0], "proc4") == 0) {
+    } else if (strcmp(fields[0], "proc4") == 0 && report_v4) {
       if (inst[0] == 'c')
         nfs_submit_nfs4_client(inst, fields + 2, (size_t)(fields_num - 2));
     }
@@ -521,10 +540,10 @@ static int nfs_read_kstat(kstat_t *ksp, int nfs_version, const char *inst,
   value_t values[proc_names_num];
 
   if (ksp == NULL)
-    return (EINVAL);
+    return EINVAL;
 
-  ssnprintf(plugin_instance, sizeof(plugin_instance), "v%i%s", nfs_version,
-            inst);
+  snprintf(plugin_instance, sizeof(plugin_instance), "v%i%s", nfs_version,
+           inst);
 
   kstat_read(kc, ksp, NULL);
   for (size_t i = 0; i < proc_names_num; i++) {
@@ -537,7 +556,7 @@ static int nfs_read_kstat(kstat_t *ksp, int nfs_version, const char *inst,
   }
 
   nfs_procedures_submit(plugin_instance, proc_names, values, proc_names_num);
-  return (0);
+  return 0;
 }
 #endif
 
@@ -555,30 +574,37 @@ static int nfs_read(void) {
     fclose(fh);
   }
 
-  return (0);
+  return 0;
 }
 /* #endif KERNEL_LINUX */
 
 #elif HAVE_LIBKSTAT
 static int nfs_read(void) {
-  nfs_read_kstat(nfs2_ksp_client, /* version = */ 2, "client",
-                 nfs2_procedures_names, nfs2_procedures_names_num);
-  nfs_read_kstat(nfs2_ksp_server, /* version = */ 2, "server",
-                 nfs2_procedures_names, nfs2_procedures_names_num);
-  nfs_read_kstat(nfs3_ksp_client, /* version = */ 3, "client",
-                 nfs3_procedures_names, nfs3_procedures_names_num);
-  nfs_read_kstat(nfs3_ksp_server, /* version = */ 3, "server",
-                 nfs3_procedures_names, nfs3_procedures_names_num);
-  nfs_read_kstat(nfs4_ksp_client, /* version = */ 4, "client",
-                 nfs4_procedures_names, nfs4_procedures_names_num);
-  nfs_read_kstat(nfs4_ksp_server, /* version = */ 4, "server",
-                 nfs4_procedures_names, nfs4_procedures_names_num);
-
-  return (0);
+  if (report_v2) {
+    nfs_read_kstat(nfs2_ksp_client, /* version = */ 2, "client",
+                   nfs2_procedures_names, nfs2_procedures_names_num);
+    nfs_read_kstat(nfs2_ksp_server, /* version = */ 2, "server",
+                   nfs2_procedures_names, nfs2_procedures_names_num);
+  }
+  if (report_v3) {
+    nfs_read_kstat(nfs3_ksp_client, /* version = */ 3, "client",
+                   nfs3_procedures_names, nfs3_procedures_names_num);
+    nfs_read_kstat(nfs3_ksp_server, /* version = */ 3, "server",
+                   nfs3_procedures_names, nfs3_procedures_names_num);
+  }
+  if (report_v4) {
+    nfs_read_kstat(nfs4_ksp_client, /* version = */ 4, "client",
+                   nfs4_procedures_names, nfs4_procedures_names_num);
+    nfs_read_kstat(nfs4_ksp_server, /* version = */ 4, "server",
+                   nfs4_procedures_names, nfs4_procedures_names_num);
+  }
+
+  return 0;
 }
 #endif /* HAVE_LIBKSTAT */
 
 void module_register(void) {
+  plugin_register_config("nfs", nfs_config, config_keys, config_keys_num);
   plugin_register_init("nfs", nfs_init);
   plugin_register_read("nfs", nfs_read);
 } /* void module_register */