+ if (cw == NULL)
+ return (EINVAL);
+
+ if (cw->query != NULL)
+ return (0);
+
+ cw->query = na_elem_new("perf-object-get-instances");
+ if (cw->query == NULL)
+ {
+ ERROR ("netapp plugin: na_elem_new failed.");
+ return (-1);
+ }
+ na_child_add_string (cw->query, "objectname", "wafl");
+
+ e = na_elem_new("counters");
+ if (e == NULL)
+ {
+ na_elem_free (cw->query);
+ cw->query = NULL;
+ ERROR ("netapp plugin: na_elem_new failed.");
+ return (-1);
+ }
+ na_child_add_string(e, "foo", "name_cache_hit");
+ na_child_add_string(e, "foo", "name_cache_miss");
+ na_child_add_string(e, "foo", "find_dir_hit");
+ na_child_add_string(e, "foo", "find_dir_miss");
+ na_child_add_string(e, "foo", "buf_hash_hit");
+ na_child_add_string(e, "foo", "buf_hash_miss");
+ na_child_add_string(e, "foo", "inode_cache_hit");
+ na_child_add_string(e, "foo", "inode_cache_miss");
+
+ na_child_add(cw->query, e);
+
+ return (0);
+} /* }}} int cna_setup_wafl */
+
+static int cna_query_wafl (host_config_t *host) /* {{{ */
+{
+ na_elem_t *data;
+ int status;
+ time_t now;
+
+ if (host == NULL)
+ return (EINVAL);
+
+ /* If WAFL was not configured, return without doing anything. */
+ if (host->cfg_wafl == NULL)
+ return (0);
+
+ now = time (NULL);
+ if ((host->cfg_wafl->interval.interval + host->cfg_wafl->interval.last_read) > now)
+ return (0);
+
+ status = cna_setup_wafl (host->cfg_wafl);
+ if (status != 0)
+ return (status);
+ assert (host->cfg_wafl->query != NULL);
+
+ data = na_server_invoke_elem(host->srv, host->cfg_wafl->query);
+ if (na_results_status (data) != NA_OK)
+ {
+ ERROR ("netapp plugin: cna_query_wafl: na_server_invoke_elem failed: %s",
+ na_results_reason (data));
+ na_elem_free (data);
+ return (-1);
+ }
+
+ status = cna_handle_wafl_data (host->name, host->cfg_wafl, data);
+
+ if (status == 0)
+ host->cfg_wafl->interval.last_read = now;
+
+ na_elem_free (data);
+ return (status);
+} /* }}} int cna_query_wafl */
+
+/* Data corresponding to <Disks /> */
+static int cna_handle_disk_data (const char *hostname, /* {{{ */
+ cfg_disk_t *cfg_disk, na_elem_t *data)
+{
+ time_t timestamp;
+ na_elem_t *instances;
+ na_elem_t *instance;
+ na_elem_iter_t instance_iter;
+ disk_t *worst_disk = NULL;
+
+ if ((cfg_disk == NULL) || (data == NULL))
+ return (EINVAL);
+
+ timestamp = (time_t) na_child_get_uint64(data, "timestamp", 0);
+
+ instances = na_elem_child (data, "instances");
+ if (instances == NULL)
+ {
+ ERROR ("netapp plugin: cna_handle_disk_data: "
+ "na_elem_child (\"instances\") failed.");
+ return (-1);
+ }
+
+ /* Iterate over all children */
+ instance_iter = na_child_iterator (instances);
+ for (instance = na_iterator_next (&instance_iter);
+ instance != NULL;
+ instance = na_iterator_next(&instance_iter))
+ {
+ disk_t *old_data;
+ disk_t new_data;
+
+ na_elem_iter_t counter_iterator;
+ na_elem_t *counter;
+
+ memset (&new_data, 0, sizeof (new_data));
+ new_data.timestamp = timestamp;
+ new_data.disk_busy_percent = NAN;
+
+ old_data = get_disk(cfg_disk, na_child_get_string (instance, "name"));
+ if (old_data == NULL)
+ continue;
+
+ /* Look for the "disk_busy" and "base_for_disk_busy" counters */
+ counter_iterator = na_child_iterator(na_elem_child(instance, "counters"));
+ for (counter = na_iterator_next(&counter_iterator);
+ counter != NULL;
+ counter = na_iterator_next(&counter_iterator))
+ {
+ 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_busy") == 0)
+ {
+ new_data.disk_busy = value;
+ new_data.flags |= HAVE_DISK_BUSY;
+ }
+ else if (strcmp(name, "base_for_disk_busy") == 0)
+ {
+ new_data.base_for_disk_busy = value;
+ new_data.flags |= HAVE_DISK_BASE;
+ }
+ else
+ {
+ DEBUG ("netapp plugin: cna_handle_disk_data: "
+ "Counter not handled: %s = %"PRIu64,
+ name, value);
+ }
+ }
+
+ /* If all required counters are available and did not just wrap around,
+ * calculate the busy percentage. Otherwise, the value is initialized to
+ * NAN at the top of the for-loop. */
+ if (HAS_ALL_FLAGS (old_data->flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
+ && HAS_ALL_FLAGS (new_data.flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
+ && (new_data.disk_busy >= old_data->disk_busy)
+ && (new_data.base_for_disk_busy > old_data->base_for_disk_busy))
+ {
+ uint64_t busy_diff;
+ uint64_t base_diff;
+
+ busy_diff = new_data.disk_busy - old_data->disk_busy;
+ base_diff = new_data.base_for_disk_busy - old_data->base_for_disk_busy;
+
+ new_data.disk_busy_percent = 100.0
+ * ((gauge_t) busy_diff) / ((gauge_t) base_diff);
+ }
+
+ /* Clear HAVE_* flags */
+ old_data->flags &= ~HAVE_DISK_ALL;
+
+ /* Copy data */
+ old_data->timestamp = new_data.timestamp;
+ old_data->disk_busy = new_data.disk_busy;
+ old_data->base_for_disk_busy = new_data.base_for_disk_busy;
+ old_data->disk_busy_percent = new_data.disk_busy_percent;
+
+ /* Copy flags */
+ old_data->flags |= (new_data.flags & HAVE_DISK_ALL);
+
+ if ((worst_disk == NULL)
+ || (worst_disk->disk_busy_percent < old_data->disk_busy_percent))
+ worst_disk = old_data;
+ } /* for (all disks) */
+
+ if ((cfg_disk->flags & CFG_DISK_BUSIEST) && (worst_disk != NULL))
+ submit_double (hostname, "system", "percent", "disk_busy",
+ worst_disk->disk_busy_percent, timestamp);
+
+ return (0);
+} /* }}} int cna_handle_disk_data */
+
+static int cna_setup_disk (cfg_disk_t *cd) /* {{{ */
+{
+ na_elem_t *e;
+
+ if (cd == NULL)
+ return (EINVAL);
+
+ if (cd->query != NULL)
+ return (0);
+
+ cd->query = na_elem_new ("perf-object-get-instances");
+ if (cd->query == NULL)
+ {
+ ERROR ("netapp plugin: na_elem_new failed.");
+ return (-1);
+ }
+ na_child_add_string (cd->query, "objectname", "disk");
+
+ e = na_elem_new("counters");
+ if (e == NULL)
+ {
+ na_elem_free (cd->query);
+ cd->query = NULL;
+ ERROR ("netapp plugin: na_elem_new failed.");
+ return (-1);
+ }
+ na_child_add_string(e, "foo", "disk_busy");
+ na_child_add_string(e, "foo", "base_for_disk_busy");
+ na_child_add(cd->query, e);
+
+ return (0);
+} /* }}} int cna_setup_disk */
+
+static int cna_query_disk (host_config_t *host) /* {{{ */
+{
+ na_elem_t *data;
+ int status;
+ time_t now;
+
+ if (host == NULL)
+ return (EINVAL);
+
+ /* If the user did not configure disk statistics, return without doing
+ * anything. */
+ if (host->cfg_disk == NULL)
+ return (0);
+
+ now = time (NULL);
+ if ((host->cfg_disk->interval.interval + host->cfg_disk->interval.last_read) > now)
+ return (0);
+
+ status = cna_setup_disk (host->cfg_disk);
+ if (status != 0)
+ return (status);
+ assert (host->cfg_disk->query != NULL);
+
+ data = na_server_invoke_elem(host->srv, host->cfg_disk->query);
+ if (na_results_status (data) != NA_OK)
+ {
+ ERROR ("netapp plugin: cna_query_disk: na_server_invoke_elem failed: %s",
+ na_results_reason (data));
+ na_elem_free (data);
+ return (-1);
+ }
+
+ status = cna_handle_disk_data (host->name, host->cfg_disk, data);
+
+ if (status == 0)
+ host->cfg_disk->interval.last_read = now;
+
+ na_elem_free (data);
+ return (status);
+} /* }}} int cna_query_disk */
+
+/* Data corresponding to <GetVolumeData /> */
+static void collect_volume_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
+ na_elem_t *inst;
+ volume_t *volume;
+ cfg_volume_usage_t *cfg_volume_data = data;
+
+ 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;
+
+ 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"),
+ cfg_volume_data->flags, /* perf_flags = */ 0);
+ if (volume == NULL)
+ continue;
+
+ if (!(volume->cfg_volume_usage.flags & CFG_VOLUME_USAGE_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", "free",
+ (double) size_free, /* time = */ 0);
+
+ size_used = na_child_get_uint64(inst, "size-used", UINT64_MAX);
+ if (size_used != UINT64_MAX) {
+ if ((volume->cfg_volume_usage.flags & HAVE_VOLUME_USAGE_SNAP)
+ && (size_used >= volume->cfg_volume_usage.snap_used))
+ size_used -= volume->cfg_volume_usage.snap_used;
+ submit_double (host->name, volume->name, "df_complex", "used",
+ (double) size_used, /* time = */ 0);
+ }
+
+ snap_reserved = na_child_get_uint64(inst, "snapshot-blocks-reserved", UINT64_MAX);
+ if (!(volume->cfg_volume_usage.flags & HAVE_VOLUME_USAGE_SNAP) && (snap_reserved != UINT64_MAX))
+ /* If we have snap usage data this value has already been submitted. */
+ /* 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 == 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;