netapp plugin: process_volume_flag: Use the boolean set/unset option here, too.
[collectd.git] / src / netapp.c
index 70422f0..c3ce7aa 100644 (file)
@@ -44,8 +44,6 @@ typedef void service_handler_t(host_config_t *host, na_elem_t *result, void *dat
 
 typedef struct {
        uint32_t flags;
-       uint64_t last_cpu_busy;
-       uint64_t last_cpu_total;
 } perf_system_data_t;
 
 /*!
@@ -225,15 +223,16 @@ static disk_t *get_disk(host_config_t *host, const char *name) /* {{{ */
        return v;
 } /* }}} disk_t *get_disk */
 
-static int submit_one_value (const char *host, /* {{{ */
+static int submit_values (const char *host, /* {{{ */
                const char *plugin_inst,
-               const char *type, const char *type_inst, value_t value,
+               const char *type, const char *type_inst,
+               value_t *values, int values_len,
                time_t timestamp)
 {
        value_list_t vl = VALUE_LIST_INIT;
 
-       vl.values = &value;
-       vl.values_len = 1;
+       vl.values = values;
+       vl.values_len = values_len;
 
        if (timestamp > 0)
                vl.time = timestamp;
@@ -252,17 +251,29 @@ static int submit_one_value (const char *host, /* {{{ */
        return (plugin_dispatch_values (&vl));
 } /* }}} int submit_uint64 */
 
-#if 0
-static int submit_uint64 (const char *host, const char *plugin_inst, /* {{{ */
-               const char *type, const char *type_inst, uint64_t ui64)
+static int submit_two_counters (const char *host, const char *plugin_inst, /* {{{ */
+               const char *type, const char *type_inst, counter_t val0, counter_t val1,
+               time_t timestamp)
+{
+       value_t values[2];
+
+       values[0].counter = val0;
+       values[1].counter = val1;
+
+       return (submit_values (host, plugin_inst, type, type_inst,
+                               values, 2, timestamp));
+} /* }}} int submit_two_counters */
+
+static int submit_counter (const char *host, const char *plugin_inst, /* {{{ */
+               const char *type, const char *type_inst, counter_t counter, time_t timestamp)
 {
        value_t v;
 
-       v.counter = (counter_t) ui64;
+       v.counter = counter;
 
-       return (submit_one_value (host, plugin_inst, type, type_inst, v));
-} /* }}} int submit_uint64 */
-#endif
+       return (submit_values (host, plugin_inst, type, type_inst,
+                               &v, 1, timestamp));
+} /* }}} int submit_counter */
 
 static int submit_double (const char *host, const char *plugin_inst, /* {{{ */
                const char *type, const char *type_inst, double d, time_t timestamp)
@@ -271,7 +282,8 @@ static int submit_double (const char *host, const char *plugin_inst, /* {{{ */
 
        v.gauge = (gauge_t) d;
 
-       return (submit_one_value (host, plugin_inst, type, type_inst, v, timestamp));
+       return (submit_values (host, plugin_inst, type, type_inst,
+                               &v, 1, timestamp));
 } /* }}} int submit_uint64 */
 
 static int submit_cache_ratio (const char *host, /* {{{ */
@@ -300,7 +312,8 @@ static int submit_cache_ratio (const char *host, /* {{{ */
        *old_hits = new_hits;
        *old_misses = new_misses;
 
-       return (submit_one_value (host, plugin_inst, "cache_ratio", type_inst, v, timestamp));
+       return (submit_values (host, plugin_inst, "cache_ratio", type_inst,
+                               &v, 1, timestamp));
 } /* }}} int submit_cache_ratio */
 
 static void collect_perf_wafl_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
@@ -324,52 +337,56 @@ static void collect_perf_wafl_data(host_config_t *host, na_elem_t *out, void *da
 
                name = na_child_get_string(counter, "name");
                if (!strcmp(name, "name_cache_hit"))
-                       name_cache_hit = na_child_get_uint64(counter, "value", 0);
+                       name_cache_hit = na_child_get_uint64(counter, "value", UINT64_MAX);
                else if (!strcmp(name, "name_cache_miss"))
-                       name_cache_miss = na_child_get_uint64(counter, "value", 0);
+                       name_cache_miss = na_child_get_uint64(counter, "value", UINT64_MAX);
                else if (!strcmp(name, "find_dir_hit"))
-                       find_dir_hit = na_child_get_uint64(counter, "value", 0);
+                       find_dir_hit = na_child_get_uint64(counter, "value", UINT64_MAX);
                else if (!strcmp(name, "find_dir_miss"))
-                       find_dir_miss = na_child_get_uint64(counter, "value", 0);
+                       find_dir_miss = na_child_get_uint64(counter, "value", UINT64_MAX);
                else if (!strcmp(name, "buf_hash_hit"))
-                       buf_hash_hit = na_child_get_uint64(counter, "value", 0);
+                       buf_hash_hit = na_child_get_uint64(counter, "value", UINT64_MAX);
                else if (!strcmp(name, "buf_hash_miss"))
-                       buf_hash_miss = na_child_get_uint64(counter, "value", 0);
+                       buf_hash_miss = na_child_get_uint64(counter, "value", UINT64_MAX);
                else if (!strcmp(name, "inode_cache_hit"))
-                       inode_cache_hit = na_child_get_uint64(counter, "value", 0);
+                       inode_cache_hit = na_child_get_uint64(counter, "value", UINT64_MAX);
                else if (!strcmp(name, "inode_cache_miss"))
-                       inode_cache_miss = na_child_get_uint64(counter, "value", 0);
+                       inode_cache_miss = na_child_get_uint64(counter, "value", UINT64_MAX);
                else
-                       INFO ("netapp plugin: Found unexpected child: %s", name);
+                       DEBUG("netapp plugin: Found unexpected child: %s", name);
        }
 
        /* Submit requested counters */
-       if (wafl->flags & PERF_WAFL_NAME_CACHE)
+       if ((wafl->flags & PERF_WAFL_NAME_CACHE)
+                       && (name_cache_hit != UINT64_MAX) && (name_cache_miss != UINT64_MAX))
                submit_cache_ratio (host->name, plugin_inst, "name_cache_hit",
                                name_cache_hit, name_cache_miss,
                                &wafl->last_name_cache_hit, &wafl->last_name_cache_miss,
                                timestamp);
 
-       if (wafl->flags & PERF_WAFL_DIR_CACHE)
+       if ((wafl->flags & PERF_WAFL_DIR_CACHE)
+                       && (find_dir_hit != UINT64_MAX) && (find_dir_miss != UINT64_MAX))
                submit_cache_ratio (host->name, plugin_inst, "find_dir_hit",
                                find_dir_hit, find_dir_miss,
                                &wafl->last_find_dir_hit, &wafl->last_find_dir_miss,
                                timestamp);
 
-       if (wafl->flags & PERF_WAFL_BUF_CACHE)
+       if ((wafl->flags & PERF_WAFL_BUF_CACHE)
+                       && (buf_hash_hit != UINT64_MAX) && (buf_hash_miss != UINT64_MAX))
                submit_cache_ratio (host->name, plugin_inst, "buf_hash_hit",
                                buf_hash_hit, buf_hash_miss,
                                &wafl->last_buf_hash_hit, &wafl->last_buf_hash_miss,
                                timestamp);
 
-       if (wafl->flags & PERF_WAFL_INODE_CACHE)
+       if ((wafl->flags & PERF_WAFL_INODE_CACHE)
+                       && (inode_cache_hit != UINT64_MAX) && (inode_cache_miss != UINT64_MAX))
                submit_cache_ratio (host->name, plugin_inst, "inode_cache_hit",
                                inode_cache_hit, inode_cache_miss,
                                &wafl->last_inode_cache_hit, &wafl->last_inode_cache_miss,
                                timestamp);
 } /* }}} void collect_perf_wafl_data */
 
-static void collect_perf_disk_data(host_config_t *host, na_elem_t *out, void *data) {
+static void collect_perf_disk_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
        perf_disk_data_t *perf = data;
        const char *name;
        time_t timestamp;
@@ -397,12 +414,12 @@ static void collect_perf_disk_data(host_config_t *host, na_elem_t *out, void *da
                                continue;
 
                        if (strcmp(name, "disk_busy") == 0)
-                               disk_busy = na_child_get_uint64(counter, "value", 0);
+                               disk_busy = na_child_get_uint64(counter, "value", UINT64_MAX);
                        else if (strcmp(name, "base_for_disk_busy") == 0)
-                               base_for_disk_busy = na_child_get_uint64(counter, "value", 0);
+                               base_for_disk_busy = na_child_get_uint64(counter, "value", UINT64_MAX);
                }
 
-               if ((disk_busy == 0) || (base_for_disk_busy == 0))
+               if ((disk_busy == UINT64_MAX) || (base_for_disk_busy == UINT64_MAX))
                {
                        disk->perf_data.last_disk_busy = 0;
                        disk->perf_data.last_base_for_disk_busy = 0;
@@ -441,81 +458,111 @@ static void collect_perf_disk_data(host_config_t *host, na_elem_t *out, void *da
        if ((perf->flags & PERF_DISK_BUSIEST) && (worst_disk != NULL))
                submit_double (host->name, "system", "percent", "disk_busy",
                                worst_disk->perf_data.last_disk_busy_percent, timestamp);
-} /* void collect_perf_disk_data */
+} /* }}} void collect_perf_disk_data */
 
-static void collect_volume_data(host_config_t *host, na_elem_t *out, void *data) {
-       na_elem_t *inst, *sis;
+static void collect_volume_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
+       na_elem_t *inst;
        volume_t *volume;
        volume_data_t *volume_data = data;
-       value_t values[1];
-       value_list_t vl = VALUE_LIST_INIT;
 
        out = na_elem_child(out, "volumes");
        na_elem_iter_t inst_iter = na_child_iterator(out);
        for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
-               uint64_t size_free = 0, size_used = 0, snap_reserved = 0, sis_saved = 0;
+               uint64_t size_free = 0, size_used = 0, snap_reserved = 0;
+
+               na_elem_t *sis;
+               const char *sis_state;
+               uint64_t sis_saved_reported;
+               uint64_t sis_saved;
+
                volume = get_volume(host, na_child_get_string(inst, "name"));
-               if (!(volume->volume_data.flags & VOLUME_INIT)) volume->volume_data.flags = volume_data->flags;
-               if (!(volume->volume_data.flags & VOLUME_DF)) continue;
-               size_free = na_child_get_uint64(inst, "size-available", 0);
-               size_used = na_child_get_uint64(inst, "size-used", 0);
-               snap_reserved = na_child_get_uint64(inst, "snapshot-blocks-reserved", 0) * 1024;
-
-               vl.values_len = 1;
-               vl.values = values;
-               vl.time = time(0);
-               vl.interval = interval_g;
-               sstrncpy(vl.plugin, "netapp", sizeof(vl.plugin));
-               sstrncpy(vl.host, host->name, sizeof(vl.host));
-               sstrncpy(vl.plugin_instance, volume->name, sizeof(vl.plugin_instance));
-               sstrncpy(vl.type, "df_complex", sizeof(vl.type));
-
-               values[0].gauge = size_used;
-               sstrncpy(vl.type_instance, "used", sizeof(vl.type_instance));
-               DEBUG("%s/netapp-%s/df_complex-used: %"PRIu64, host->name, volume->name, size_used);
-               plugin_dispatch_values (&vl);
-
-               values[0].gauge = size_free;
-               sstrncpy(vl.type_instance, "free", sizeof(vl.type_instance));
-               DEBUG("%s/netapp-%s/df_complex-free: %"PRIu64, host->name, volume->name, size_free);
-               plugin_dispatch_values (&vl);
-
-               if (snap_reserved) {
-                       values[0].gauge = snap_reserved;
-                       sstrncpy(vl.type_instance, "snap_reserved", sizeof(vl.type_instance));
-                       DEBUG("%s/netapp-%s/df_complex-snap_reserved: %"PRIu64, host->name, volume->name, snap_reserved);
-                       plugin_dispatch_values (&vl);
-               }
+               if (volume == NULL)
+                       continue;
+
+               if (!(volume->volume_data.flags & VOLUME_INIT))
+                       volume->volume_data.flags = volume_data->flags;
+
+               if (!(volume->volume_data.flags & VOLUME_DF))
+                       continue;
+
+               /* 2^4 exa-bytes? This will take a while ;) */
+               size_free = na_child_get_uint64(inst, "size-available", UINT64_MAX);
+               if (size_free != UINT64_MAX)
+                       submit_double (host->name, volume->name, "df_complex", "used",
+                                       (double) size_used, /* time = */ 0);
+
+               size_used = na_child_get_uint64(inst, "size-used", UINT64_MAX);
+               if (size_free != UINT64_MAX)
+                       submit_double (host->name, volume->name, "df_complex", "free",
+                                       (double) size_free, /* time = */ 0);
+
+               snap_reserved = na_child_get_uint64(inst, "snapshot-blocks-reserved", UINT64_MAX);
+               if (snap_reserved != UINT64_MAX)
+                       /* 1 block == 1024 bytes  as per API docs */
+                       submit_double (host->name, volume->name, "df_complex", "snap_reserved",
+                                       (double) (1024 * snap_reserved), /* time = */ 0);
 
                sis = na_elem_child(inst, "sis");
-               if (sis && !strcmp(na_child_get_string(sis, "state"), "enabled")) {
-                       uint64_t sis_saved_reported = na_child_get_uint64(sis, "size-saved", 0), sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", 0);
-                       /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
-                       if (sis_saved_reported >> 32) {
-                               /* In case they ever fix this bug. */
-                               sis_saved = sis_saved_reported;
+               if (sis == NULL)
+                       continue;
+
+               sis_state = na_child_get_string(sis, "state");
+               if ((sis_state == NULL)
+                               || (strcmp ("enabled", sis_state) != 0))
+                       continue;
+
+               sis_saved_reported = na_child_get_uint64(sis, "size-saved", UINT64_MAX);
+               if (sis_saved_reported == UINT64_MAX)
+                       continue;
+
+               /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
+               if ((sis_saved_reported >> 32) != 0) {
+                       /* In case they ever fix this bug. */
+                       sis_saved = sis_saved_reported;
+               } else {
+                       uint64_t sis_saved_percent;
+                       uint64_t sis_saved_guess;
+                       uint64_t overflow_guess;
+                       uint64_t guess1, guess2, guess3;
+
+                       sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
+                       if (sis_saved_percent > 100)
+                               continue;
+
+                       /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
+                        * will hopefully be fixed in later versions. To work around the bug, try
+                        * to figure out how often the 32bit integer wrapped around by using the
+                        * "percentage-saved" value. Because the percentage is in the range
+                        * [0-100], this should work as long as the saved space does not exceed
+                        * 400 GBytes. */
+                       /* percentage-saved = size-saved / (size-saved + size-used) */
+                       if (sis_saved_percent < 100)
+                               sis_saved_guess = size_used * sis_saved_percent / (100 - sis_saved_percent);
+                       else
+                               sis_saved_guess = size_used;
+
+                       overflow_guess = sis_saved_guess >> 32;
+                       guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
+                       guess2 = (overflow_guess << 32) + sis_saved_reported;
+                       guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
+
+                       if (sis_saved_guess < guess2) {
+                               if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
+                                       sis_saved = guess1;
+                               else
+                                       sis_saved = guess2;
                        } else {
-                               uint64_t real_saved = sis_saved_percent * size_used / (100 - sis_saved_percent);
-                               uint64_t overflow_guess = real_saved >> 32;
-                               uint64_t guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
-                               uint64_t guess2 = (overflow_guess << 32) + sis_saved_reported;
-                               uint64_t guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
-                               
-                               if (real_saved < guess2) {
-                                       if (real_saved - guess1 < guess2 - real_saved) sis_saved = guess1;
-                                       else sis_saved = guess2;
-                               } else {
-                                       if (real_saved - guess2 < guess3 - real_saved) sis_saved = guess2;
-                                       else sis_saved = guess3;
-                               }
+                               if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
+                                       sis_saved = guess2;
+                               else
+                                       sis_saved = guess3;
                        }
-                       values[0].gauge = sis_saved;
-                       sstrncpy(vl.type_instance, "sis_saved", sizeof(vl.type_instance));
-                       DEBUG("%s/netapp-%s/df_complex-sis_saved: %"PRIu64, host->name, volume->name, sis_saved);
-                       plugin_dispatch_values (&vl);
-               }
+               } /* end of 32-bit workaround */
+
+               submit_double (host->name, volume->name, "df_complex", "sis_saved",
+                               (double) sis_saved, /* time = */ 0);
        }
-}
+} /* }}} void collect_volume_data */
 
 static void collect_perf_volume_data(host_config_t *host, na_elem_t *out, void *data) {
        perf_volume_data_t *perf = data;
@@ -622,14 +669,16 @@ static void collect_perf_volume_data(host_config_t *host, na_elem_t *out, void *
        }
 }
 
-static void collect_perf_system_data(host_config_t *host, na_elem_t *out, void *data) {
-       uint64_t disk_read = 0, disk_written = 0, net_recv = 0, net_sent = 0, cpu_busy = 0, cpu_total = 0;
+static void collect_perf_system_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
+       counter_t disk_read = 0, disk_written = 0;
+       counter_t net_recv = 0, net_sent = 0;
+       counter_t cpu_busy = 0, cpu_total = 0;
+       unsigned int counter_flags = 0;
+
        perf_system_data_t *perf = data;
-       const char *instance, *name;
+       const char *instance;
        time_t timestamp;
        na_elem_t *counter;
-       value_t values[2];
-       value_list_t vl = VALUE_LIST_INIT;
        
        timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
        out = na_elem_child(na_elem_child(out, "instances"), "instance-data");
@@ -637,103 +686,63 @@ static void collect_perf_system_data(host_config_t *host, na_elem_t *out, void *
 
        na_elem_iter_t iter = na_child_iterator(na_elem_child(out, "counters"));
        for (counter = na_iterator_next(&iter); counter; counter = na_iterator_next(&iter)) {
+               const char *name;
+               uint64_t value;
+
                name = na_child_get_string(counter, "name");
+               if (name == NULL)
+                       continue;
+
+               value = na_child_get_uint64(counter, "value", UINT64_MAX);
+               if (value == UINT64_MAX)
+                       continue;
+
                if (!strcmp(name, "disk_data_read")) {
-                       disk_read = na_child_get_uint64(counter, "value", 0) * 1024;
+                       disk_read = (counter_t) (value * 1024);
+                       counter_flags |= 0x01;
                } else if (!strcmp(name, "disk_data_written")) {
-                       disk_written = na_child_get_uint64(counter, "value", 0) * 1024;
+                       disk_written = (counter_t) (value * 1024);
+                       counter_flags |= 0x02;
                } else if (!strcmp(name, "net_data_recv")) {
-                       net_recv = na_child_get_uint64(counter, "value", 0) * 1024;
+                       net_recv = (counter_t) (value * 1024);
+                       counter_flags |= 0x04;
                } else if (!strcmp(name, "net_data_sent")) {
-                       net_sent = na_child_get_uint64(counter, "value", 0) * 1024;
+                       net_sent = (counter_t) (value * 1024);
+                       counter_flags |= 0x08;
                } else if (!strcmp(name, "cpu_busy")) {
-                       cpu_busy = na_child_get_uint64(counter, "value", 0);
+                       cpu_busy = (counter_t) value;
+                       counter_flags |= 0x10;
                } else if (!strcmp(name, "cpu_elapsed_time")) {
-                       cpu_total = na_child_get_uint64(counter, "value", 0);
-               } else if ((perf->flags & PERF_SYSTEM_OPS) && strlen(name) > 4 && !strcmp(name + strlen(name) - 4, "_ops")) {
-                       values[0].counter = na_child_get_uint64(counter, "value", 0);
-                       if (!values[0].counter) continue;
-                       vl.values = values;
-                       vl.values_len = 1;
-                       vl.time = timestamp;
-                       vl.interval = interval_g;
-                       sstrncpy(vl.plugin, "netapp", sizeof(vl.plugin));
-                       sstrncpy(vl.host, host->name, sizeof(vl.host));
-                       sstrncpy(vl.plugin_instance, instance, sizeof(vl.plugin_instance));
-                       sstrncpy(vl.type, "disk_ops_complex", sizeof(vl.type));
-                       sstrncpy(vl.type_instance, name, sizeof(vl.plugin_instance));
-                       DEBUG("%s/netapp-%s/disk_ops_complex-%s: %llu",
-                                       host->name, instance, name, values[0].counter);
-                       plugin_dispatch_values (&vl);
+                       cpu_total = (counter_t) value;
+                       counter_flags |= 0x20;
+               } else if ((perf->flags & PERF_SYSTEM_OPS)
+                               && (strlen(name) > 4)
+                               && (!strcmp(name + strlen(name) - 4, "_ops"))) {
+                       submit_counter (host->name, instance, "disk_ops_complex", name,
+                                       (counter_t) value, timestamp);
                }
+       } /* for (counter) */
+
+       if ((perf->flags & PERF_SYSTEM_DISK)
+                       && ((counter_flags & 0x03) == 0x03))
+               submit_two_counters (host->name, instance, "disk_octets", NULL,
+                               disk_read, disk_written, timestamp);
+                               
+       if ((perf->flags & PERF_SYSTEM_NET)
+                       && ((counter_flags & 0x0c) == 0x0c))
+               submit_two_counters (host->name, instance, "if_octets", NULL,
+                               net_recv, net_sent, timestamp);
+
+       if ((perf->flags & PERF_SYSTEM_CPU)
+                       && ((counter_flags & 0x30) == 0x30)) {
+               submit_counter (host->name, instance, "cpu", "system",
+                               cpu_busy, timestamp);
+               submit_counter (host->name, instance, "cpu", "idle",
+                               cpu_total - cpu_busy, timestamp);
        }
-       if ((perf->flags & PERF_SYSTEM_DISK) && disk_read && disk_written) {
-               values[0].counter = disk_read;
-               values[1].counter = disk_written;
-               vl.values = values;
-               vl.values_len = 2;
-               vl.time = timestamp;
-               vl.interval = interval_g;
-               sstrncpy(vl.plugin, "netapp", sizeof(vl.plugin));
-               sstrncpy(vl.host, host->name, sizeof(vl.host));
-               sstrncpy(vl.plugin_instance, instance, sizeof(vl.plugin_instance));
-               sstrncpy(vl.type, "disk_octets", sizeof(vl.type));
-               vl.type_instance[0] = 0;
-               DEBUG("%s/netapp-%s/disk_octets: %"PRIu64" %"PRIu64, host->name, instance, disk_read, disk_written);
-               plugin_dispatch_values (&vl);
-       }
-       if ((perf->flags & PERF_SYSTEM_NET) && net_recv && net_sent) {
-               values[0].counter = net_recv;
-               values[1].counter = net_sent;
-               vl.values = values;
-               vl.values_len = 2;
-               vl.time = timestamp;
-               vl.interval = interval_g;
-               sstrncpy(vl.plugin, "netapp", sizeof(vl.plugin));
-               sstrncpy(vl.host, host->name, sizeof(vl.host));
-               sstrncpy(vl.plugin_instance, instance, sizeof(vl.plugin_instance));
-               sstrncpy(vl.type, "if_octets", sizeof(vl.type));
-               vl.type_instance[0] = 0;
-               DEBUG("%s/netapp-%s/if_octects: %"PRIu64" %"PRIu64, host->name, instance, net_recv, net_sent);
-               plugin_dispatch_values (&vl);
-       }
-       if ((perf->flags & PERF_SYSTEM_CPU) && cpu_busy && cpu_total) {
-               /* values[0].gauge = (double) (cpu_busy - perf->last_cpu_busy) / (cpu_total - perf->last_cpu_total) * 100; */
-               values[0].counter = cpu_busy / 10000;
-               vl.values = values;
-               vl.values_len = 1;
-               vl.time = timestamp;
-               vl.interval = interval_g;
-               sstrncpy(vl.plugin, "netapp", sizeof(vl.plugin));
-               sstrncpy(vl.host, host->name, sizeof(vl.host));
-               sstrncpy(vl.plugin_instance, instance, sizeof(vl.plugin_instance));
-               sstrncpy(vl.type, "cpu", sizeof(vl.type));
-               sstrncpy(vl.type_instance, "system", sizeof(vl.plugin_instance));
-               /* if (perf->last_cpu_busy && perf->last_cpu_total) printf("CPU: busy: %lf - idle: %lf\n", values[0].gauge, 100.0 - values[0].gauge); */
-               /* if (perf->last_cpu_busy && perf->last_cpu_total) plugin_dispatch_values ("cpu", &vl); */
-               DEBUG("%s/netapp-%s/cpu: busy: %"PRIu64" - idle: %"PRIu64, host->name, instance, cpu_busy / 10000, cpu_total / 10000);
-               plugin_dispatch_values (&vl);
-
-               /* values[0].gauge = 100.0 - (double) (cpu_busy - perf->last_cpu_busy) / (cpu_total - perf->last_cpu_total) * 100; */
-               values[0].counter = (cpu_total - cpu_busy) / 10000;
-               vl.values = values;
-               vl.values_len = 1;
-               vl.time = timestamp;
-               vl.interval = interval_g;
-               sstrncpy(vl.plugin, "netapp", sizeof(vl.plugin));
-               sstrncpy(vl.host, host->name, sizeof(vl.host));
-               sstrncpy(vl.plugin_instance, instance, sizeof(vl.plugin_instance));
-               sstrncpy(vl.type, "cpu", sizeof(vl.type));
-               sstrncpy(vl.type_instance, "idle", sizeof(vl.plugin_instance));
-               /* if (perf->last_cpu_busy && perf->last_cpu_total) plugin_dispatch_values ("cpu", &vl); */
-               plugin_dispatch_values (&vl);
-
-               perf->last_cpu_busy = cpu_busy;
-               perf->last_cpu_total = cpu_total;
-       }
-}
+} /* }}} void collect_perf_system_data */
 
-int config_init() {
+static int config_init(void) { /* {{{ */
        char err[256];
        na_elem_t *e;
        host_config_t *host;
@@ -809,89 +818,153 @@ int config_init() {
                }
        }
        return 0;
-}
+} /* }}} int config_init */
+
+static int config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */
+               uint32_t *flags, uint32_t flag)
+{
+       if ((ci == NULL) || (flags == NULL))
+               return (EINVAL);
+
+       if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
+       {
+               WARNING ("netapp plugin: The %s option needs exactly one boolean argument.",
+                               ci->key);
+               return (-1);
+       }
 
-static void set_global_perf_vol_flag(const host_config_t *host, uint32_t flag, int value) {
+       if (ci->values[0].value.boolean)
+               *flags |= flag;
+       else
+               *flags &= ~flag;
+
+       return (0);
+} /* }}} int config_bool_to_flag */
+
+static void set_global_perf_vol_flag(const host_config_t *host, /* {{{ */
+               uint32_t flag, _Bool set)
+{
        volume_t *v;
        
        for (v = host->volumes; v; v = v->next) {
-               v->perf_data.flags &= ~flag;
-               if (value) v->perf_data.flags |= flag;
+               if (set)
+                       v->perf_data.flags |= flag;
+               else /* if (!set) */
+                       v->perf_data.flags &= ~flag;
        }
-}
+} /* }}} void set_global_perf_vol_flag */
 
-static void set_global_vol_flag(const host_config_t *host, uint32_t flag, int value) {
+static void set_global_vol_flag(const host_config_t *host, /* {{{ */
+               uint32_t flag, _Bool set) {
        volume_t *v;
        
        for (v = host->volumes; v; v = v->next) {
-               v->volume_data.flags &= ~flag;
-               if (value) v->volume_data.flags |= flag;
+               if (set)
+                       v->volume_data.flags |= flag;
+               else /* if (!set) */
+                       v->volume_data.flags &= ~flag;
        }
-}
+} /* }}} void set_global_vol_flag */
 
-static void process_perf_volume_flag(host_config_t *host, perf_volume_data_t *perf_volume, const oconfig_item_t *item, uint32_t flag) {
-       int n;
+static void process_perf_volume_flag (host_config_t *host, /* {{{ */
+               perf_volume_data_t *perf_volume, const oconfig_item_t *item,
+               uint32_t flag)
+{
+       int i;
        
-       for (n = 0; n < item->values_num; ++n) {
-               int minus = 0;
-               const char *name = item->values[n].value.string;
+       for (i = 0; i < item->values_num; ++i) {
+               const char *name;
                volume_t *v;
-               if (item->values[n].type != OCONFIG_TYPE_STRING) {
-                       WARNING("netapp plugin: Ignoring non-string argument in \"GetVolPerfData\" block for host %s", host->name);
+               _Bool set = true;
+
+               if (item->values[i].type != OCONFIG_TYPE_STRING) {
+                       WARNING("netapp plugin: Ignoring non-string argument in "
+                                       "\"GetVolPerfData\" block for host %s", host->name);
                        continue;
                }
+
+               name = item->values[i].value.string;
                if (name[0] == '+') {
+                       set = true;
                        ++name;
                } else if (name[0] == '-') {
-                       minus = 1;
+                       set = false;
                        ++name;
                }
+
                if (!name[0]) {
-                       perf_volume->flags &= ~flag;
-                       if (!minus) perf_volume->flags |= flag;
-                       set_global_perf_vol_flag(host, flag, !minus);
+                       if (set)
+                               perf_volume->flags |= flag;
+                       else /* if (!set) */
+                               perf_volume->flags &= ~flag;
+
+                       set_global_perf_vol_flag(host, flag, set);
                        continue;
                }
-               v = get_volume(host, name);
+
+               v = get_volume (host, name);
+               if (v == NULL)
+                       continue;
+
                if (!v->perf_data.flags) {
                        v->perf_data.flags = perf_volume->flags;
-                       v->perf_data.last_read_latency = v->perf_data.last_read_ops = 0;
-                       v->perf_data.last_write_latency = v->perf_data.last_write_ops = 0;
                }
-               v->perf_data.flags &= ~flag;
-               if (!minus) v->perf_data.flags |= flag;
-       }
-}
 
-static void process_volume_flag(host_config_t *host, volume_data_t *volume_data, const oconfig_item_t *item, uint32_t flag) {
-       int n;
+               if (set)
+                       v->perf_data.flags |= flag;
+               else /* if (!set) */
+                       v->perf_data.flags &= ~flag;
+       } /* for (i = 0 .. item->values_num) */
+} /* }}} void process_perf_volume_flag */
+
+static void process_volume_flag (host_config_t *host, /* {{{ */
+               volume_data_t *volume_data, const oconfig_item_t *item, uint32_t flag)
+{
+       int i;
        
-       for (n = 0; n < item->values_num; ++n) {
-               int minus = 0;
-               const char *name = item->values[n].value.string;
+       for (i = 0; i < item->values_num; ++i) {
+               const char *name;
                volume_t *v;
-               if (item->values[n].type != OCONFIG_TYPE_STRING) {
-                       WARNING("netapp plugin: Ignoring non-string argument in \"GetVolData\" block for host %s", host->name);
+               _Bool set = true;
+
+               if (item->values[i].type != OCONFIG_TYPE_STRING) {
+                       WARNING("netapp plugin: Ignoring non-string argument in \"GetVolData\""
+                                       "block for host %s", host->name);
                        continue;
                }
+
+               name = item->values[i].value.string;
                if (name[0] == '+') {
+                       set = true;
                        ++name;
                } else if (name[0] == '-') {
-                       minus = 1;
+                       set = false;
                        ++name;
                }
+
                if (!name[0]) {
-                       volume_data->flags &= ~flag;
-                       if (!minus) volume_data->flags |= flag;
-                       set_global_vol_flag(host, flag, !minus);
+                       if (set)
+                               volume_data->flags |= flag;
+                       else /* if (!set) */
+                               volume_data->flags &= ~flag;
+
+                       set_global_vol_flag(host, flag, set);
                        continue;
                }
+
                v = get_volume(host, name);
-               if (!v->volume_data.flags) v->volume_data.flags = volume_data->flags;
-               v->volume_data.flags &= ~flag;
-               if (!minus) v->volume_data.flags |= flag;
+               if (v == NULL)
+                       continue;
+
+               if (!v->volume_data.flags)
+                       v->volume_data.flags = volume_data->flags;
+
+               if (set)
+                       v->volume_data.flags |= flag;
+               else /* if (!set) */
+                       v->volume_data.flags &= ~flag;
        }
-}
+} /* }}} void process_volume_flag */
 
 static void build_perf_vol_config(host_config_t *host, const oconfig_item_t *ci) {
        int i, had_io = 0, had_ops = 0, had_latency = 0;
@@ -928,15 +1001,15 @@ static void build_perf_vol_config(host_config_t *host, const oconfig_item_t *ci)
        }
        if (!had_io) {
                perf_volume->flags |= PERF_VOLUME_IO;
-               set_global_perf_vol_flag(host, PERF_VOLUME_IO, 1);
+               set_global_perf_vol_flag(host, PERF_VOLUME_IO, /* set = */ true);
        }
        if (!had_ops) {
                perf_volume->flags |= PERF_VOLUME_OPS;
-               set_global_perf_vol_flag(host, PERF_VOLUME_OPS, 1);
+               set_global_perf_vol_flag(host, PERF_VOLUME_OPS, /* set = */ true);
        }
        if (!had_latency) {
                perf_volume->flags |= PERF_VOLUME_LATENCY;
-               set_global_perf_vol_flag(host, PERF_VOLUME_LATENCY, 1);
+               set_global_perf_vol_flag(host, PERF_VOLUME_LATENCY, /* set = */ true);
        }
 }
 
@@ -969,7 +1042,7 @@ static void build_volume_config(host_config_t *host, oconfig_item_t *ci) {
        }
        if (!had_df) {
                volume_data->flags |= VOLUME_DF;
-               set_global_vol_flag(host, VOLUME_DF, 1);
+               set_global_vol_flag(host, VOLUME_DF, /* set = */ true);
        }
 /*     service = malloc(sizeof(*service));
        service->query = 0;
@@ -1002,11 +1075,7 @@ static void build_perf_disk_config(host_config_t *temp, oconfig_item_t *ci) {
                        }
                        service->skip_countdown = service->multiplier = item->values[0].value.number;
                } else if (!strcasecmp(item->key, "GetBusy")) {
-                       if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN) {
-                               WARNING("netapp plugin: \"GetBusy\" of host %s service GetDiskPerfData needs exactly one bool argument.", ci->values[0].value.string);
-                               continue;
-                       }
-                       perf_disk->flags = (perf_disk->flags & ~PERF_SYSTEM_CPU) | (item->values[0].value.boolean ? PERF_DISK_BUSIEST : 0);
+                       config_bool_to_flag (item, &perf_disk->flags, PERF_SYSTEM_CPU);
                }
        }
 }
@@ -1042,51 +1111,46 @@ static void build_perf_wafl_config(host_config_t *temp, oconfig_item_t *ci) {
                        }
                        service->skip_countdown = service->multiplier = item->values[0].value.number;
                } else if (!strcasecmp(item->key, "GetNameCache")) {
-                       if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN) {
-                               WARNING("netapp plugin: \"GetNameCache\" of host %s service GetWaflPerfData needs exactly one bool argument.", ci->values[0].value.string);
-                               continue;
-                       }
-                       perf_wafl->flags = (perf_wafl->flags & ~PERF_WAFL_NAME_CACHE) | (item->values[0].value.boolean ? PERF_WAFL_NAME_CACHE : 0);
+                       config_bool_to_flag (item, &perf_wafl->flags, PERF_WAFL_NAME_CACHE);
                } else if (!strcasecmp(item->key, "GetDirCache")) {
-                       if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN) {
-                               WARNING("netapp plugin: \"GetDirChache\" of host %s service GetWaflPerfData needs exactly one bool argument.", ci->values[0].value.string);
-                               continue;
-                       }
-                       perf_wafl->flags = (perf_wafl->flags & ~PERF_WAFL_DIR_CACHE) | (item->values[0].value.boolean ? PERF_WAFL_DIR_CACHE : 0);
+                       config_bool_to_flag (item, &perf_wafl->flags, PERF_WAFL_DIR_CACHE);
                } else if (!strcasecmp(item->key, "GetBufCache")) {
-                       if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN) {
-                               WARNING("netapp plugin: \"GetBufCache\" of host %s service GetWaflPerfData needs exactly one bool argument.", ci->values[0].value.string);
-                               continue;
-                       }
-                       perf_wafl->flags = (perf_wafl->flags & ~PERF_WAFL_BUF_CACHE) | (item->values[0].value.boolean ? PERF_WAFL_BUF_CACHE : 0);
+                       config_bool_to_flag (item, &perf_wafl->flags, PERF_WAFL_BUF_CACHE);
                } else if (!strcasecmp(item->key, "GetInodeCache")) {
-                       if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN) {
-                               WARNING("netapp plugin: \"GetInodeCache\" of host %s service GetWaflPerfData needs exactly one bool argument.", ci->values[0].value.string);
-                               continue;
-                       }
-                       perf_wafl->flags = (perf_wafl->flags & ~PERF_WAFL_INODE_CACHE) | (item->values[0].value.boolean ? PERF_WAFL_INODE_CACHE : 0);
+                       config_bool_to_flag (item, &perf_wafl->flags, PERF_WAFL_INODE_CACHE);
+               } else {
+                       WARNING ("netapp plugin: The %s config option is not allowed within "
+                                       "`GetWaflPerfData' blocks.", item->key);
                }
        }
 }
 
-static void build_perf_sys_config(host_config_t *temp, oconfig_item_t *ci, const service_config_t *default_service) {
+static int build_perf_sys_config (host_config_t *host, /* {{{ */
+               oconfig_item_t *ci, const service_config_t *default_service)
+{
        int i;
        service_config_t *service;
        perf_system_data_t *perf_system;
        
        service = malloc(sizeof(*service));
+       if (service == NULL)
+               return (-1);
+       memset (service, 0, sizeof (*service));
        *service = *default_service;
        service->handler = collect_perf_system_data;
-       perf_system = service->data = malloc(sizeof(*perf_system));
+
+       perf_system = malloc(sizeof(*perf_system));
+       if (perf_system == NULL) {
+               sfree (service);
+               return (-1);
+       }
+       memset (perf_system, 0, sizeof (*perf_system));
        perf_system->flags = PERF_SYSTEM_ALL;
-       perf_system->last_cpu_busy = 0;
-       perf_system->last_cpu_total = 0;
-       service->next = temp->services;
-       temp->services = service;
+       service->data = perf_system;
+
        for (i = 0; i < ci->children_num; ++i) {
                oconfig_item_t *item = ci->children + i;
 
-               /* if (!item || !item->key || !*item->key) continue; */
                if (!strcasecmp(item->key, "Multiplier")) {
                        if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_NUMBER || item->values[0].value.number != (int) item->values[0].value.number || item->values[0].value.number < 1) {
                                WARNING("netapp plugin: \"Multiplier\" of host %s service GetSystemPerfData needs exactly one positive integer argument.", ci->values[0].value.string);
@@ -1094,32 +1158,24 @@ static void build_perf_sys_config(host_config_t *temp, oconfig_item_t *ci, const
                        }
                        service->skip_countdown = service->multiplier = item->values[0].value.number;
                } else if (!strcasecmp(item->key, "GetCPULoad")) {
-                       if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN) {
-                               WARNING("netapp plugin: \"GetCPULoad\" of host %s service GetSystemPerfData needs exactly one bool argument.", ci->values[0].value.string);
-                               continue;
-                       }
-                       perf_system->flags = (perf_system->flags & ~PERF_SYSTEM_CPU) | (item->values[0].value.boolean ? PERF_SYSTEM_CPU : 0);
+                       config_bool_to_flag (item, &perf_system->flags, PERF_SYSTEM_CPU);
                } else if (!strcasecmp(item->key, "GetInterfaces")) {
-                       if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN) {
-                               WARNING("netapp plugin: \"GetInterfaces\" of host %s service GetSystemPerfData needs exactly one bool argument.", ci->values[0].value.string);
-                               continue;
-                       }
-                       perf_system->flags = (perf_system->flags & ~PERF_SYSTEM_NET) | (item->values[0].value.boolean ? PERF_SYSTEM_NET : 0);
+                       config_bool_to_flag (item, &perf_system->flags, PERF_SYSTEM_NET);
                } else if (!strcasecmp(item->key, "GetDiskOps")) {
-                       if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN) {
-                               WARNING("netapp plugin: \"GetDiskOps\" of host %s service GetSystemPerfData needs exactly one bool argument.", ci->values[0].value.string);
-                               continue;
-                       }
-                       perf_system->flags = (perf_system->flags & ~PERF_SYSTEM_OPS) | (item->values[0].value.boolean ? PERF_SYSTEM_OPS : 0);
+                       config_bool_to_flag (item, &perf_system->flags, PERF_SYSTEM_OPS);
                } else if (!strcasecmp(item->key, "GetDiskIO")) {
-                       if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN) {
-                               WARNING("netapp plugin: \"GetDiskIO\" of host %s service GetSystemPerfData needs exactly one bool argument.", ci->values[0].value.string);
-                               continue;
-                       }
-                       perf_system->flags = (perf_system->flags & ~PERF_SYSTEM_DISK) | (item->values[0].value.boolean ? PERF_SYSTEM_DISK : 0);
+                       config_bool_to_flag (item, &perf_system->flags, PERF_SYSTEM_DISK);
+               } else {
+                       WARNING ("netapp plugin: The %s config option is not allowed within "
+                                       "`GetSystemPerfData' blocks.", item->key);
                }
        }
-}
+
+       service->next = host->services;
+       host->services = service;
+
+       return (0);
+} /* }}} int build_perf_sys_config */
 
 static host_config_t *build_host_config(const oconfig_item_t *ci, const host_config_t *default_host, const service_config_t *def_def_service) {
        int i;