Merge branch 'pr/1649'
[collectd.git] / src / netapp.c
1 /**
2  * collectd - src/netapp.c
3  * Copyright (C) 2009,2010  Sven Trenkel
4  * Copyright (C) 2012-2013  teamix GmbH
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *   Sven Trenkel <collectd at semidefinite.de>
26  *   Sebastian 'tokkee' Harl <sh@teamix.net>
27  **/
28
29 #include "collectd.h"
30
31 #include "common.h"
32 #include "utils_ignorelist.h"
33
34 #include <netapp_api.h>
35 #include <netapp_errno.h>
36
37 #define HAS_ALL_FLAGS(has,needs) (((has) & (needs)) == (needs))
38
39 typedef struct host_config_s host_config_t;
40 typedef void service_handler_t(host_config_t *host, na_elem_t *result, void *data);
41
42 struct cna_interval_s
43 {
44         cdtime_t interval;
45         cdtime_t last_read;
46 };
47 typedef struct cna_interval_s cna_interval_t;
48
49 /*! Data types for WAFL statistics {{{
50  *
51  * \brief Persistent data for WAFL performance counters. (a.k.a. cache performance)
52  *
53  * The cache counters use old counter values to calculate a hit ratio for each
54  * counter. The "cfg_wafl_t" struct therefore contains old counter values along
55  * with flags, which are set if the counter is valid.
56  *
57  * The function "cna_handle_wafl_data" will fill a new structure of this kind
58  * with new values, then pass both, new and old data, to "submit_wafl_data".
59  * That function calculates the hit ratios, submits the calculated values and
60  * updates the old counter values for the next iteration.
61  */
62 #define CFG_WAFL_NAME_CACHE        0x0001
63 #define CFG_WAFL_DIR_CACHE         0x0002
64 #define CFG_WAFL_BUF_CACHE         0x0004
65 #define CFG_WAFL_INODE_CACHE       0x0008
66 #define CFG_WAFL_ALL               0x000F
67 #define HAVE_WAFL_NAME_CACHE_HIT   0x0100
68 #define HAVE_WAFL_NAME_CACHE_MISS  0x0200
69 #define HAVE_WAFL_NAME_CACHE       (HAVE_WAFL_NAME_CACHE_HIT | HAVE_WAFL_NAME_CACHE_MISS)
70 #define HAVE_WAFL_FIND_DIR_HIT     0x0400
71 #define HAVE_WAFL_FIND_DIR_MISS    0x0800
72 #define HAVE_WAFL_FIND_DIR         (HAVE_WAFL_FIND_DIR_HIT | HAVE_WAFL_FIND_DIR_MISS)
73 #define HAVE_WAFL_BUF_HASH_HIT     0x1000
74 #define HAVE_WAFL_BUF_HASH_MISS    0x2000
75 #define HAVE_WAFL_BUF_HASH         (HAVE_WAFL_BUF_HASH_HIT | HAVE_WAFL_BUF_HASH_MISS)
76 #define HAVE_WAFL_INODE_CACHE_HIT  0x4000
77 #define HAVE_WAFL_INODE_CACHE_MISS 0x8000
78 #define HAVE_WAFL_INODE_CACHE      (HAVE_WAFL_INODE_CACHE_HIT | HAVE_WAFL_INODE_CACHE_MISS)
79 #define HAVE_WAFL_ALL              0xff00
80 typedef struct {
81         uint32_t flags;
82         cna_interval_t interval;
83         na_elem_t *query;
84
85         cdtime_t timestamp;
86         uint64_t name_cache_hit;
87         uint64_t name_cache_miss;
88         uint64_t find_dir_hit;
89         uint64_t find_dir_miss;
90         uint64_t buf_hash_hit;
91         uint64_t buf_hash_miss;
92         uint64_t inode_cache_hit;
93         uint64_t inode_cache_miss;
94 } cfg_wafl_t;
95 /* }}} cfg_wafl_t */
96
97 /*! Data types for disk statistics {{{
98  *
99  * \brief A disk in the NetApp.
100  *
101  * A disk doesn't have any more information than its name at the moment.
102  * The name includes the "disk_" prefix.
103  */
104 #define HAVE_DISK_BUSY   0x10
105 #define HAVE_DISK_BASE   0x20
106 #define HAVE_DISK_ALL    0x30
107 typedef struct disk_s {
108         char *name;
109         uint32_t flags;
110         cdtime_t timestamp;
111         uint64_t disk_busy;
112         uint64_t base_for_disk_busy;
113         double disk_busy_percent;
114         struct disk_s *next;
115 } disk_t;
116
117 #define CFG_DISK_BUSIEST 0x01
118 #define CFG_DISK_ALL     0x01
119 typedef struct {
120         uint32_t flags;
121         cna_interval_t interval;
122         na_elem_t *query;
123         disk_t *disks;
124 } cfg_disk_t;
125 /* }}} cfg_disk_t */
126
127 /*! Data types for volume performance statistics {{{
128  *
129  * \brief Persistent data for volume performance data.
130  *
131  * The code below uses the difference of the operations and latency counters to
132  * calculate an average per-operation latency. For this, old counters need to
133  * be stored in the "data_volume_perf_t" structure. The byte-counters are just
134  * kept for completeness sake. The "flags" member indicates if each counter is
135  * valid or not.
136  *
137  * The "cna_handle_volume_perf_data" function will fill a new struct of this
138  * type and pass both, old and new data, to "submit_volume_perf_data". In that
139  * function, the per-operation latency is calculated and dispatched, then the
140  * old counters are updated.
141  */
142 #define CFG_VOLUME_PERF_INIT           0x0001
143 #define CFG_VOLUME_PERF_IO             0x0002
144 #define CFG_VOLUME_PERF_OPS            0x0003
145 #define CFG_VOLUME_PERF_LATENCY        0x0008
146 #define CFG_VOLUME_PERF_ALL            0x000F
147 #define HAVE_VOLUME_PERF_BYTES_READ    0x0010
148 #define HAVE_VOLUME_PERF_BYTES_WRITE   0x0020
149 #define HAVE_VOLUME_PERF_OPS_READ      0x0040
150 #define HAVE_VOLUME_PERF_OPS_WRITE     0x0080
151 #define HAVE_VOLUME_PERF_LATENCY_READ  0x0100
152 #define HAVE_VOLUME_PERF_LATENCY_WRITE 0x0200
153 #define HAVE_VOLUME_PERF_ALL           0x03F0
154 struct data_volume_perf_s;
155 typedef struct data_volume_perf_s data_volume_perf_t;
156 struct data_volume_perf_s {
157         char *name;
158         uint32_t flags;
159         cdtime_t timestamp;
160
161         uint64_t read_bytes;
162         uint64_t write_bytes;
163         uint64_t read_ops;
164         uint64_t write_ops;
165         uint64_t read_latency;
166         uint64_t write_latency;
167
168         data_volume_perf_t *next;
169 };
170
171 typedef struct {
172         cna_interval_t interval;
173         na_elem_t *query;
174
175         ignorelist_t *il_octets;
176         ignorelist_t *il_operations;
177         ignorelist_t *il_latency;
178
179         data_volume_perf_t *volumes;
180 } cfg_volume_perf_t;
181 /* }}} data_volume_perf_t */
182
183 /*! Data types for volume usage statistics {{{
184  *
185  * \brief Configuration struct for volume usage data (free / used).
186  */
187 #define CFG_VOLUME_USAGE_DF              0x0002
188 #define CFG_VOLUME_USAGE_SNAP            0x0004
189 #define CFG_VOLUME_USAGE_ALL             0x0006
190 #define HAVE_VOLUME_USAGE_NORM_FREE      0x0010
191 #define HAVE_VOLUME_USAGE_NORM_USED      0x0020
192 #define HAVE_VOLUME_USAGE_SNAP_RSVD      0x0040
193 #define HAVE_VOLUME_USAGE_SNAP_USED      0x0080
194 #define HAVE_VOLUME_USAGE_SIS_SAVED      0x0100
195 #define HAVE_VOLUME_USAGE_COMPRESS_SAVED 0x0200
196 #define HAVE_VOLUME_USAGE_DEDUP_SAVED    0x0400
197 #define HAVE_VOLUME_USAGE_ALL            0x07f0
198 #define IS_VOLUME_USAGE_OFFLINE          0x0800
199 struct data_volume_usage_s;
200 typedef struct data_volume_usage_s data_volume_usage_t;
201 struct data_volume_usage_s {
202         char *name;
203         uint32_t flags;
204
205         na_elem_t *snap_query;
206
207         uint64_t norm_free;
208         uint64_t norm_used;
209         uint64_t snap_reserved;
210         uint64_t snap_used;
211         uint64_t sis_saved;
212         uint64_t compress_saved;
213         uint64_t dedup_saved;
214
215         data_volume_usage_t *next;
216 };
217
218 typedef struct {
219         cna_interval_t interval;
220         na_elem_t *query;
221
222         ignorelist_t *il_capacity;
223         ignorelist_t *il_snapshot;
224
225         data_volume_usage_t *volumes;
226 } cfg_volume_usage_t;
227 /* }}} cfg_volume_usage_t */
228
229 /*! Data types for quota statistics {{{
230  *
231  * \brief Persistent data for quota statistics
232  */
233 typedef struct {
234         cna_interval_t interval;
235         na_elem_t *query;
236 } cfg_quota_t;
237 /* }}} cfg_quota_t */
238
239 /*! Data types for SnapVault statistics {{{
240  *
241  * \brief Persistent data for SnapVault(R) statistics
242  */
243 typedef struct {
244         cna_interval_t interval;
245         na_elem_t *query;
246 } cfg_snapvault_t;
247 /* }}} cfg_snapvault_t */
248
249 /*! Data types for system statistics {{{
250  *
251  * \brief Persistent data for system performance counters
252  */
253 #define CFG_SYSTEM_CPU  0x01
254 #define CFG_SYSTEM_NET  0x02
255 #define CFG_SYSTEM_OPS  0x04
256 #define CFG_SYSTEM_DISK 0x08
257 #define CFG_SYSTEM_ALL  0x0F
258 typedef struct {
259         uint32_t flags;
260         cna_interval_t interval;
261         na_elem_t *query;
262 } cfg_system_t;
263 /* }}} cfg_system_t */
264
265 struct host_config_s {
266         char *name;
267         na_server_transport_t protocol;
268         char *host;
269         int port;
270         char *username;
271         char *password;
272         char *vfiler;
273         cdtime_t interval;
274
275         na_server_t *srv;
276         cfg_wafl_t *cfg_wafl;
277         cfg_disk_t *cfg_disk;
278         cfg_volume_perf_t *cfg_volume_perf;
279         cfg_volume_usage_t *cfg_volume_usage;
280         cfg_quota_t *cfg_quota;
281         cfg_snapvault_t *cfg_snapvault;
282         cfg_system_t *cfg_system;
283
284         struct host_config_s *next;
285 };
286
287 /*
288  * Free functions
289  *
290  * Used to free the various structures above.
291  */
292 static void free_disk (disk_t *disk) /* {{{ */
293 {
294         disk_t *next;
295
296         if (disk == NULL)
297                 return;
298
299         next = disk->next;
300
301         sfree (disk->name);
302         sfree (disk);
303
304         free_disk (next);
305 } /* }}} void free_disk */
306
307 static void free_cfg_wafl (cfg_wafl_t *cw) /* {{{ */
308 {
309         if (cw == NULL)
310                 return;
311
312         if (cw->query != NULL)
313                 na_elem_free (cw->query);
314
315         sfree (cw);
316 } /* }}} void free_cfg_wafl */
317
318 static void free_cfg_disk (cfg_disk_t *cfg_disk) /* {{{ */
319 {
320         if (cfg_disk == NULL)
321                 return;
322
323         if (cfg_disk->query != NULL)
324                 na_elem_free (cfg_disk->query);
325
326         free_disk (cfg_disk->disks);
327         sfree (cfg_disk);
328 } /* }}} void free_cfg_disk */
329
330 static void free_cfg_volume_perf (cfg_volume_perf_t *cvp) /* {{{ */
331 {
332         data_volume_perf_t *data;
333
334         if (cvp == NULL)
335                 return;
336
337         /* Free the ignorelists */
338         ignorelist_free (cvp->il_octets);
339         ignorelist_free (cvp->il_operations);
340         ignorelist_free (cvp->il_latency);
341
342         /* Free the linked list of volumes */
343         data = cvp->volumes;
344         while (data != NULL)
345         {
346                 data_volume_perf_t *next = data->next;
347                 sfree (data->name);
348                 sfree (data);
349                 data = next;
350         }
351
352         if (cvp->query != NULL)
353                 na_elem_free (cvp->query);
354
355         sfree (cvp);
356 } /* }}} void free_cfg_volume_perf */
357
358 static void free_cfg_volume_usage (cfg_volume_usage_t *cvu) /* {{{ */
359 {
360         data_volume_usage_t *data;
361
362         if (cvu == NULL)
363                 return;
364
365         /* Free the ignorelists */
366         ignorelist_free (cvu->il_capacity);
367         ignorelist_free (cvu->il_snapshot);
368
369         /* Free the linked list of volumes */
370         data = cvu->volumes;
371         while (data != NULL)
372         {
373                 data_volume_usage_t *next = data->next;
374                 sfree (data->name);
375                 if (data->snap_query != NULL)
376                         na_elem_free(data->snap_query);
377                 sfree (data);
378                 data = next;
379         }
380
381         if (cvu->query != NULL)
382                 na_elem_free (cvu->query);
383
384         sfree (cvu);
385 } /* }}} void free_cfg_volume_usage */
386
387 static void free_cfg_quota (cfg_quota_t *q) /* {{{ */
388 {
389         if (q == NULL)
390                 return;
391
392         if (q->query != NULL)
393                 na_elem_free (q->query);
394
395         sfree (q);
396 } /* }}} void free_cfg_quota */
397
398 static void free_cfg_snapvault (cfg_snapvault_t *sv) /* {{{ */
399 {
400         if (sv == NULL)
401                 return;
402
403         if (sv->query != NULL)
404                 na_elem_free (sv->query);
405
406         sfree (sv);
407 } /* }}} void free_cfg_snapvault */
408
409 static void free_cfg_system (cfg_system_t *cs) /* {{{ */
410 {
411         if (cs == NULL)
412                 return;
413
414         if (cs->query != NULL)
415                 na_elem_free (cs->query);
416
417         sfree (cs);
418 } /* }}} void free_cfg_system */
419
420 static void free_host_config (host_config_t *hc) /* {{{ */
421 {
422         host_config_t *next;
423
424         if (hc == NULL)
425                 return;
426
427         next = hc->next;
428
429         sfree (hc->name);
430         sfree (hc->host);
431         sfree (hc->username);
432         sfree (hc->password);
433         sfree (hc->vfiler);
434
435         free_cfg_disk (hc->cfg_disk);
436         free_cfg_wafl (hc->cfg_wafl);
437         free_cfg_volume_perf (hc->cfg_volume_perf);
438         free_cfg_volume_usage (hc->cfg_volume_usage);
439         free_cfg_quota (hc->cfg_quota);
440         free_cfg_snapvault (hc->cfg_snapvault);
441         free_cfg_system (hc->cfg_system);
442
443         if (hc->srv != NULL)
444                 na_server_close (hc->srv);
445
446         sfree (hc);
447
448         free_host_config (next);
449 } /* }}} void free_host_config */
450
451 /*
452  * Auxiliary functions
453  *
454  * Used to look up volumes and disks or to handle flags.
455  */
456 static disk_t *get_disk(cfg_disk_t *cd, const char *name) /* {{{ */
457 {
458         disk_t *d;
459
460         if ((cd == NULL) || (name == NULL))
461                 return (NULL);
462
463         for (d = cd->disks; d != NULL; d = d->next) {
464                 if (strcmp(d->name, name) == 0)
465                         return d;
466         }
467
468         d = calloc (1, sizeof (*d));
469         if (d == NULL)
470                 return (NULL);
471         d->next = NULL;
472
473         d->name = strdup(name);
474         if (d->name == NULL) {
475                 sfree (d);
476                 return (NULL);
477         }
478
479         d->next = cd->disks;
480         cd->disks = d;
481
482         return d;
483 } /* }}} disk_t *get_disk */
484
485 static data_volume_usage_t *get_volume_usage (cfg_volume_usage_t *cvu, /* {{{ */
486                 const char *name)
487 {
488         data_volume_usage_t *last;
489         data_volume_usage_t *new;
490
491         int ignore_capacity = 0;
492         int ignore_snapshot = 0;
493
494         if ((cvu == NULL) || (name == NULL))
495                 return (NULL);
496
497         last = cvu->volumes;
498         while (last != NULL)
499         {
500                 if (strcmp (last->name, name) == 0)
501                         return (last);
502
503                 if (last->next == NULL)
504                         break;
505
506                 last = last->next;
507         }
508
509         /* Check the ignorelists. If *both* tell us to ignore a volume, return NULL. */
510         ignore_capacity = ignorelist_match (cvu->il_capacity, name);
511         ignore_snapshot = ignorelist_match (cvu->il_snapshot, name);
512         if ((ignore_capacity != 0) && (ignore_snapshot != 0))
513                 return (NULL);
514
515         /* Not found: allocate. */
516         new = calloc (1, sizeof (*new));
517         if (new == NULL)
518                 return (NULL);
519         new->next = NULL;
520
521         new->name = strdup (name);
522         if (new->name == NULL)
523         {
524                 sfree (new);
525                 return (NULL);
526         }
527
528         if (ignore_capacity == 0)
529                 new->flags |= CFG_VOLUME_USAGE_DF;
530         if (ignore_snapshot == 0) {
531                 new->flags |= CFG_VOLUME_USAGE_SNAP;
532                 new->snap_query = na_elem_new ("snapshot-list-info");
533                 na_child_add_string(new->snap_query, "target-type", "volume");
534                 na_child_add_string(new->snap_query, "target-name", name);
535         } else {
536                 new->snap_query = NULL;
537         }
538
539         /* Add to end of list. */
540         if (last == NULL)
541                 cvu->volumes = new;
542         else
543                 last->next = new;
544
545         return (new);
546 } /* }}} data_volume_usage_t *get_volume_usage */
547
548 static data_volume_perf_t *get_volume_perf (cfg_volume_perf_t *cvp, /* {{{ */
549                 const char *name)
550 {
551         data_volume_perf_t *last;
552         data_volume_perf_t *new;
553
554         int ignore_octets = 0;
555         int ignore_operations = 0;
556         int ignore_latency = 0;
557
558         if ((cvp == NULL) || (name == NULL))
559                 return (NULL);
560
561         last = cvp->volumes;
562         while (last != NULL)
563         {
564                 if (strcmp (last->name, name) == 0)
565                         return (last);
566
567                 if (last->next == NULL)
568                         break;
569
570                 last = last->next;
571         }
572
573         /* Check the ignorelists. If *all three* tell us to ignore a volume, return
574          * NULL. */
575         ignore_octets = ignorelist_match (cvp->il_octets, name);
576         ignore_operations = ignorelist_match (cvp->il_operations, name);
577         ignore_latency = ignorelist_match (cvp->il_latency, name);
578         if ((ignore_octets != 0) || (ignore_operations != 0)
579                         || (ignore_latency != 0))
580                 return (NULL);
581
582         /* Not found: allocate. */
583         new = calloc (1, sizeof (*new));
584         if (new == NULL)
585                 return (NULL);
586         new->next = NULL;
587
588         new->name = strdup (name);
589         if (new->name == NULL)
590         {
591                 sfree (new);
592                 return (NULL);
593         }
594
595         if (ignore_octets == 0)
596                 new->flags |= CFG_VOLUME_PERF_IO;
597         if (ignore_operations == 0)
598                 new->flags |= CFG_VOLUME_PERF_OPS;
599         if (ignore_latency == 0)
600                 new->flags |= CFG_VOLUME_PERF_LATENCY;
601
602         /* Add to end of list. */
603         if (last == NULL)
604                 cvp->volumes = new;
605         else
606                 last->next = new;
607
608         return (new);
609 } /* }}} data_volume_perf_t *get_volume_perf */
610
611 /*
612  * Various submit functions.
613  *
614  * They all eventually call "submit_values" which creates a value_list_t and
615  * dispatches it to the daemon.
616  */
617 static int submit_values (const char *host, /* {{{ */
618                 const char *plugin_inst,
619                 const char *type, const char *type_inst,
620                 value_t *values, size_t values_len,
621                 cdtime_t timestamp, cdtime_t interval)
622 {
623         value_list_t vl = VALUE_LIST_INIT;
624
625         vl.values = values;
626         vl.values_len = values_len;
627
628         if (timestamp > 0)
629                 vl.time = timestamp;
630
631         if (interval > 0)
632                 vl.interval = interval;
633
634         if (host != NULL)
635                 sstrncpy (vl.host, host, sizeof (vl.host));
636         else
637                 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
638         sstrncpy (vl.plugin, "netapp", sizeof (vl.plugin));
639         if (plugin_inst != NULL)
640                 sstrncpy (vl.plugin_instance, plugin_inst, sizeof (vl.plugin_instance));
641         sstrncpy (vl.type, type, sizeof (vl.type));
642         if (type_inst != NULL)
643                 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
644
645         return (plugin_dispatch_values (&vl));
646 } /* }}} int submit_uint64 */
647
648 static int submit_two_derive (const char *host, const char *plugin_inst, /* {{{ */
649                 const char *type, const char *type_inst, derive_t val0, derive_t val1,
650                 cdtime_t timestamp, cdtime_t interval)
651 {
652         value_t values[] = {
653                 { .derive = val0 },
654                 { .derive = val1 },
655         };
656
657         return (submit_values (host, plugin_inst, type, type_inst,
658                                 values, STATIC_ARRAY_SIZE (values), timestamp, interval));
659 } /* }}} int submit_two_derive */
660
661 static int submit_derive (const char *host, const char *plugin_inst, /* {{{ */
662                 const char *type, const char *type_inst, derive_t counter,
663                 cdtime_t timestamp, cdtime_t interval)
664 {
665         return (submit_values (host, plugin_inst, type, type_inst,
666                                 &(value_t) { .derive = counter }, 1, timestamp, interval));
667 } /* }}} int submit_derive */
668
669 static int submit_two_gauge (const char *host, const char *plugin_inst, /* {{{ */
670                 const char *type, const char *type_inst, gauge_t val0, gauge_t val1,
671                 cdtime_t timestamp, cdtime_t interval)
672 {
673         value_t values[] = {
674                 { .gauge = val0 },
675                 { .gauge = val1 },
676         };
677
678         return (submit_values (host, plugin_inst, type, type_inst,
679                                 values, STATIC_ARRAY_SIZE (values), timestamp, interval));
680 } /* }}} int submit_two_gauge */
681
682 static int submit_double (const char *host, const char *plugin_inst, /* {{{ */
683                 const char *type, const char *type_inst, double d,
684                 cdtime_t timestamp, cdtime_t interval)
685 {
686         return (submit_values (host, plugin_inst, type, type_inst,
687                                 &(value_t) { .gauge = counter }, 1, timestamp, interval));
688 } /* }}} int submit_uint64 */
689
690 /* Calculate hit ratio from old and new counters and submit the resulting
691  * percentage. Used by "submit_wafl_data". */
692 static int submit_cache_ratio (const char *host, /* {{{ */
693                 const char *plugin_inst,
694                 const char *type_inst,
695                 uint64_t new_hits,
696                 uint64_t new_misses,
697                 uint64_t old_hits,
698                 uint64_t old_misses,
699                 cdtime_t timestamp,
700                 cdtime_t interval)
701 {
702         value_t v = { .gauge = NAN };
703
704         if ((new_hits >= old_hits) && (new_misses >= old_misses)) {
705                 uint64_t hits;
706                 uint64_t misses;
707
708                 hits = new_hits - old_hits;
709                 misses = new_misses - old_misses;
710
711                 v.gauge = 100.0 * ((gauge_t) hits) / ((gauge_t) (hits + misses));
712         }
713
714         return (submit_values (host, plugin_inst, "cache_ratio", type_inst,
715                                 &v, 1, timestamp, interval));
716 } /* }}} int submit_cache_ratio */
717
718 /* Submits all the caches used by WAFL. Uses "submit_cache_ratio". */
719 static int submit_wafl_data (const char *hostname, const char *instance, /* {{{ */
720                 cfg_wafl_t *old_data, const cfg_wafl_t *new_data, cdtime_t interval)
721 {
722         /* Submit requested counters */
723         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_NAME_CACHE | HAVE_WAFL_NAME_CACHE)
724                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_NAME_CACHE))
725                 submit_cache_ratio (hostname, instance, "name_cache_hit",
726                                 new_data->name_cache_hit, new_data->name_cache_miss,
727                                 old_data->name_cache_hit, old_data->name_cache_miss,
728                                 new_data->timestamp, interval);
729
730         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_DIR_CACHE | HAVE_WAFL_FIND_DIR)
731                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_FIND_DIR))
732                 submit_cache_ratio (hostname, instance, "find_dir_hit",
733                                 new_data->find_dir_hit, new_data->find_dir_miss,
734                                 old_data->find_dir_hit, old_data->find_dir_miss,
735                                 new_data->timestamp, interval);
736
737         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_BUF_CACHE | HAVE_WAFL_BUF_HASH)
738                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_BUF_HASH))
739                 submit_cache_ratio (hostname, instance, "buf_hash_hit",
740                                 new_data->buf_hash_hit, new_data->buf_hash_miss,
741                                 old_data->buf_hash_hit, old_data->buf_hash_miss,
742                                 new_data->timestamp, interval);
743
744         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_INODE_CACHE | HAVE_WAFL_INODE_CACHE)
745                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_INODE_CACHE))
746                 submit_cache_ratio (hostname, instance, "inode_cache_hit",
747                                 new_data->inode_cache_hit, new_data->inode_cache_miss,
748                                 old_data->inode_cache_hit, old_data->inode_cache_miss,
749                                 new_data->timestamp, interval);
750
751         /* Clear old HAVE_* flags */
752         old_data->flags &= ~HAVE_WAFL_ALL;
753
754         /* Copy all counters */
755         old_data->timestamp        = new_data->timestamp;
756         old_data->name_cache_hit   = new_data->name_cache_hit;
757         old_data->name_cache_miss  = new_data->name_cache_miss;
758         old_data->find_dir_hit     = new_data->find_dir_hit;
759         old_data->find_dir_miss    = new_data->find_dir_miss;
760         old_data->buf_hash_hit     = new_data->buf_hash_hit;
761         old_data->buf_hash_miss    = new_data->buf_hash_miss;
762         old_data->inode_cache_hit  = new_data->inode_cache_hit;
763         old_data->inode_cache_miss = new_data->inode_cache_miss;
764
765         /* Copy HAVE_* flags */
766         old_data->flags |= (new_data->flags & HAVE_WAFL_ALL);
767
768         return (0);
769 } /* }}} int submit_wafl_data */
770
771 /* Submits volume performance data to the daemon, taking care to honor and
772  * update flags appropriately. */
773 static int submit_volume_perf_data (const char *hostname, /* {{{ */
774                 data_volume_perf_t *old_data,
775                 const data_volume_perf_t *new_data, int interval)
776 {
777         char plugin_instance[DATA_MAX_NAME_LEN];
778
779         if ((hostname == NULL) || (old_data == NULL) || (new_data == NULL))
780                 return (-1);
781
782         ssnprintf (plugin_instance, sizeof (plugin_instance),
783                         "volume-%s", old_data->name);
784
785         /* Check for and submit disk-octet values */
786         if (HAS_ALL_FLAGS (old_data->flags, CFG_VOLUME_PERF_IO)
787                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_BYTES_READ | HAVE_VOLUME_PERF_BYTES_WRITE))
788         {
789                 submit_two_derive (hostname, plugin_instance, "disk_octets", /* type instance = */ NULL,
790                                 (derive_t) new_data->read_bytes, (derive_t) new_data->write_bytes, new_data->timestamp, interval);
791         }
792
793         /* Check for and submit disk-operations values */
794         if (HAS_ALL_FLAGS (old_data->flags, CFG_VOLUME_PERF_OPS)
795                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE))
796         {
797                 submit_two_derive (hostname, plugin_instance, "disk_ops", /* type instance = */ NULL,
798                                 (derive_t) new_data->read_ops, (derive_t) new_data->write_ops, new_data->timestamp, interval);
799         }
800
801         /* Check for, calculate and submit disk-latency values */
802         if (HAS_ALL_FLAGS (old_data->flags, CFG_VOLUME_PERF_LATENCY
803                                 | HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
804                                 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE)
805                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
806                                 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE))
807         {
808                 gauge_t latency_per_op_read;
809                 gauge_t latency_per_op_write;
810
811                 latency_per_op_read = NAN;
812                 latency_per_op_write = NAN;
813
814                 /* Check if a counter wrapped around. */
815                 if ((new_data->read_ops > old_data->read_ops)
816                                 && (new_data->read_latency > old_data->read_latency))
817                 {
818                         uint64_t diff_ops_read;
819                         uint64_t diff_latency_read;
820
821                         diff_ops_read = new_data->read_ops - old_data->read_ops;
822                         diff_latency_read = new_data->read_latency - old_data->read_latency;
823
824                         if (diff_ops_read > 0)
825                                 latency_per_op_read = ((gauge_t) diff_latency_read) / ((gauge_t) diff_ops_read);
826                 }
827
828                 if ((new_data->write_ops > old_data->write_ops)
829                                 && (new_data->write_latency > old_data->write_latency))
830                 {
831                         uint64_t diff_ops_write;
832                         uint64_t diff_latency_write;
833
834                         diff_ops_write = new_data->write_ops - old_data->write_ops;
835                         diff_latency_write = new_data->write_latency - old_data->write_latency;
836
837                         if (diff_ops_write > 0)
838                                 latency_per_op_write = ((gauge_t) diff_latency_write) / ((gauge_t) diff_ops_write);
839                 }
840
841                 submit_two_gauge (hostname, plugin_instance, "disk_latency", /* type instance = */ NULL,
842                                 latency_per_op_read, latency_per_op_write, new_data->timestamp, interval);
843         }
844
845         /* Clear all HAVE_* flags. */
846         old_data->flags &= ~HAVE_VOLUME_PERF_ALL;
847
848         /* Copy all counters */
849         old_data->timestamp = new_data->timestamp;
850         old_data->read_bytes = new_data->read_bytes;
851         old_data->write_bytes = new_data->write_bytes;
852         old_data->read_ops = new_data->read_ops;
853         old_data->write_ops = new_data->write_ops;
854         old_data->read_latency = new_data->read_latency;
855         old_data->write_latency = new_data->write_latency;
856
857         /* Copy the HAVE_* flags */
858         old_data->flags |= (new_data->flags & HAVE_VOLUME_PERF_ALL);
859
860         return (0);
861 } /* }}} int submit_volume_perf_data */
862
863 static cdtime_t cna_child_get_cdtime (na_elem_t *data) /* {{{ */
864 {
865         time_t t;
866
867         t = (time_t) na_child_get_uint64 (data, "timestamp", /* default = */ 0);
868
869         return (TIME_T_TO_CDTIME_T (t));
870 } /* }}} cdtime_t cna_child_get_cdtime */
871
872
873 /*
874  * Query functions
875  *
876  * These functions are called with appropriate data returned by the libnetapp
877  * interface which is parsed and submitted with the above functions.
878  */
879 /* Data corresponding to <WAFL /> */
880 static int cna_handle_wafl_data (const char *hostname, cfg_wafl_t *cfg_wafl, /* {{{ */
881                 na_elem_t *data, cdtime_t interval)
882 {
883         cfg_wafl_t perf_data = { 0 };
884         const char *plugin_inst;
885
886         na_elem_t *instances;
887         na_elem_iter_t counter_iter;
888
889         perf_data.timestamp = cna_child_get_cdtime (data);
890
891         instances = na_elem_child(na_elem_child (data, "instances"), "instance-data");
892         if (instances == NULL)
893         {
894                 ERROR ("netapp plugin: cna_handle_wafl_data: "
895                                 "na_elem_child (\"instances\") failed "
896                                 "for host %s.", hostname);
897                 return (-1);
898         }
899
900         plugin_inst = na_child_get_string(instances, "name");
901         if (plugin_inst == NULL)
902         {
903                 ERROR ("netapp plugin: cna_handle_wafl_data: "
904                                 "na_child_get_string (\"name\") failed "
905                                 "for host %s.", hostname);
906                 return (-1);
907         }
908
909         /* Iterate over all counters */
910         counter_iter = na_child_iterator (na_elem_child (instances, "counters"));
911         for (na_elem_t *counter = na_iterator_next (&counter_iter);
912                         counter != NULL;
913                         counter = na_iterator_next (&counter_iter))
914         {
915                 const char *name;
916                 uint64_t value;
917
918                 name = na_child_get_string(counter, "name");
919                 if (name == NULL)
920                         continue;
921
922                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
923                 if (value == UINT64_MAX)
924                         continue;
925
926                 if (!strcmp(name, "name_cache_hit")) {
927                         perf_data.name_cache_hit = value;
928                         perf_data.flags |= HAVE_WAFL_NAME_CACHE_HIT;
929                 } else if (!strcmp(name, "name_cache_miss")) {
930                         perf_data.name_cache_miss = value;
931                         perf_data.flags |= HAVE_WAFL_NAME_CACHE_MISS;
932                 } else if (!strcmp(name, "find_dir_hit")) {
933                         perf_data.find_dir_hit = value;
934                         perf_data.flags |= HAVE_WAFL_FIND_DIR_HIT;
935                 } else if (!strcmp(name, "find_dir_miss")) {
936                         perf_data.find_dir_miss = value;
937                         perf_data.flags |= HAVE_WAFL_FIND_DIR_MISS;
938                 } else if (!strcmp(name, "buf_hash_hit")) {
939                         perf_data.buf_hash_hit = value;
940                         perf_data.flags |= HAVE_WAFL_BUF_HASH_HIT;
941                 } else if (!strcmp(name, "buf_hash_miss")) {
942                         perf_data.buf_hash_miss = value;
943                         perf_data.flags |= HAVE_WAFL_BUF_HASH_MISS;
944                 } else if (!strcmp(name, "inode_cache_hit")) {
945                         perf_data.inode_cache_hit = value;
946                         perf_data.flags |= HAVE_WAFL_INODE_CACHE_HIT;
947                 } else if (!strcmp(name, "inode_cache_miss")) {
948                         perf_data.inode_cache_miss = value;
949                         perf_data.flags |= HAVE_WAFL_INODE_CACHE_MISS;
950                 } else {
951                         DEBUG("netapp plugin: cna_handle_wafl_data: "
952                                         "Found unexpected child: %s "
953                                         "for host %s.", name, hostname);
954                 }
955         }
956
957         return (submit_wafl_data (hostname, plugin_inst, cfg_wafl, &perf_data, interval));
958 } /* }}} void cna_handle_wafl_data */
959
960 static int cna_setup_wafl (cfg_wafl_t *cw) /* {{{ */
961 {
962         na_elem_t *e;
963
964         if (cw == NULL)
965                 return (EINVAL);
966
967         if (cw->query != NULL)
968                 return (0);
969
970         cw->query = na_elem_new("perf-object-get-instances");
971         if (cw->query == NULL)
972         {
973                 ERROR ("netapp plugin: na_elem_new failed.");
974                 return (-1);
975         }
976         na_child_add_string (cw->query, "objectname", "wafl");
977
978         e = na_elem_new("counters");
979         if (e == NULL)
980         {
981                 na_elem_free (cw->query);
982                 cw->query = NULL;
983                 ERROR ("netapp plugin: na_elem_new failed.");
984                 return (-1);
985         }
986         na_child_add_string(e, "counter", "name_cache_hit");
987         na_child_add_string(e, "counter", "name_cache_miss");
988         na_child_add_string(e, "counter", "find_dir_hit");
989         na_child_add_string(e, "counter", "find_dir_miss");
990         na_child_add_string(e, "counter", "buf_hash_hit");
991         na_child_add_string(e, "counter", "buf_hash_miss");
992         na_child_add_string(e, "counter", "inode_cache_hit");
993         na_child_add_string(e, "counter", "inode_cache_miss");
994
995         na_child_add(cw->query, e);
996
997         return (0);
998 } /* }}} int cna_setup_wafl */
999
1000 static int cna_query_wafl (host_config_t *host) /* {{{ */
1001 {
1002         na_elem_t *data;
1003         int status;
1004         cdtime_t now;
1005
1006         if (host == NULL)
1007                 return (EINVAL);
1008
1009         /* If WAFL was not configured, return without doing anything. */
1010         if (host->cfg_wafl == NULL)
1011                 return (0);
1012
1013         now = cdtime ();
1014         if ((host->cfg_wafl->interval.interval + host->cfg_wafl->interval.last_read) > now)
1015                 return (0);
1016
1017         status = cna_setup_wafl (host->cfg_wafl);
1018         if (status != 0)
1019                 return (status);
1020         assert (host->cfg_wafl->query != NULL);
1021
1022         data = na_server_invoke_elem(host->srv, host->cfg_wafl->query);
1023         if (na_results_status (data) != NA_OK)
1024         {
1025                 ERROR ("netapp plugin: cna_query_wafl: na_server_invoke_elem failed for host %s: %s",
1026                                 host->name, na_results_reason (data));
1027                 na_elem_free (data);
1028                 return (-1);
1029         }
1030
1031         status = cna_handle_wafl_data (host->name, host->cfg_wafl, data,
1032                         host->cfg_wafl->interval.interval);
1033
1034         if (status == 0)
1035                 host->cfg_wafl->interval.last_read = now;
1036
1037         na_elem_free (data);
1038         return (status);
1039 } /* }}} int cna_query_wafl */
1040
1041 /* Data corresponding to <Disks /> */
1042 static int cna_handle_disk_data (const char *hostname, /* {{{ */
1043                 cfg_disk_t *cfg_disk, na_elem_t *data, cdtime_t interval)
1044 {
1045         cdtime_t timestamp;
1046         na_elem_t *instances;
1047         na_elem_iter_t instance_iter;
1048         disk_t *worst_disk = NULL;
1049
1050         if ((cfg_disk == NULL) || (data == NULL))
1051                 return (EINVAL);
1052
1053         timestamp = cna_child_get_cdtime (data);
1054
1055         instances = na_elem_child (data, "instances");
1056         if (instances == NULL)
1057         {
1058                 ERROR ("netapp plugin: cna_handle_disk_data: "
1059                                 "na_elem_child (\"instances\") failed "
1060                                 "for host %s.", hostname);
1061                 return (-1);
1062         }
1063
1064         /* Iterate over all children */
1065         instance_iter = na_child_iterator (instances);
1066         for (na_elem_t *instance = na_iterator_next (&instance_iter);
1067                         instance != NULL;
1068                         instance = na_iterator_next(&instance_iter))
1069         {
1070                 disk_t *old_data;
1071                 disk_t  new_data = { 0 };
1072
1073                 na_elem_iter_t counter_iterator;
1074
1075                 new_data.timestamp = timestamp;
1076                 new_data.disk_busy_percent = NAN;
1077
1078                 old_data = get_disk(cfg_disk, na_child_get_string (instance, "name"));
1079                 if (old_data == NULL)
1080                         continue;
1081
1082                 /* Look for the "disk_busy" and "base_for_disk_busy" counters */
1083                 counter_iterator = na_child_iterator(na_elem_child(instance, "counters"));
1084                 for (na_elem_t *counter = na_iterator_next(&counter_iterator);
1085                                 counter != NULL;
1086                                 counter = na_iterator_next(&counter_iterator))
1087                 {
1088                         const char *name;
1089                         uint64_t value;
1090
1091                         name = na_child_get_string(counter, "name");
1092                         if (name == NULL)
1093                                 continue;
1094
1095                         value = na_child_get_uint64(counter, "value", UINT64_MAX);
1096                         if (value == UINT64_MAX)
1097                                 continue;
1098
1099                         if (strcmp(name, "disk_busy") == 0)
1100                         {
1101                                 new_data.disk_busy = value;
1102                                 new_data.flags |= HAVE_DISK_BUSY;
1103                         }
1104                         else if (strcmp(name, "base_for_disk_busy") == 0)
1105                         {
1106                                 new_data.base_for_disk_busy = value;
1107                                 new_data.flags |= HAVE_DISK_BASE;
1108                         }
1109                         else
1110                         {
1111                                 DEBUG ("netapp plugin: cna_handle_disk_data: "
1112                                                 "Counter not handled: %s = %"PRIu64,
1113                                                 name, value);
1114                         }
1115                 }
1116
1117                 /* If all required counters are available and did not just wrap around,
1118                  * calculate the busy percentage. Otherwise, the value is initialized to
1119                  * NAN at the top of the for-loop. */
1120                 if (HAS_ALL_FLAGS (old_data->flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
1121                                 && HAS_ALL_FLAGS (new_data.flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
1122                                 && (new_data.disk_busy >= old_data->disk_busy)
1123                                 && (new_data.base_for_disk_busy > old_data->base_for_disk_busy))
1124                 {
1125                         uint64_t busy_diff;
1126                         uint64_t base_diff;
1127
1128                         busy_diff = new_data.disk_busy - old_data->disk_busy;
1129                         base_diff = new_data.base_for_disk_busy - old_data->base_for_disk_busy;
1130
1131                         new_data.disk_busy_percent = 100.0
1132                                 * ((gauge_t) busy_diff) / ((gauge_t) base_diff);
1133                 }
1134
1135                 /* Clear HAVE_* flags */
1136                 old_data->flags &= ~HAVE_DISK_ALL;
1137
1138                 /* Copy data */
1139                 old_data->timestamp = new_data.timestamp;
1140                 old_data->disk_busy = new_data.disk_busy;
1141                 old_data->base_for_disk_busy = new_data.base_for_disk_busy;
1142                 old_data->disk_busy_percent = new_data.disk_busy_percent;
1143
1144                 /* Copy flags */
1145                 old_data->flags |= (new_data.flags & HAVE_DISK_ALL);
1146
1147                 if ((worst_disk == NULL)
1148                                 || (worst_disk->disk_busy_percent < old_data->disk_busy_percent))
1149                         worst_disk = old_data;
1150         } /* for (all disks) */
1151
1152         if ((cfg_disk->flags & CFG_DISK_BUSIEST) && (worst_disk != NULL))
1153                 submit_double (hostname, "system", "percent", "disk_busy",
1154                                 worst_disk->disk_busy_percent, timestamp, interval);
1155
1156         return (0);
1157 } /* }}} int cna_handle_disk_data */
1158
1159 static int cna_setup_disk (cfg_disk_t *cd) /* {{{ */
1160 {
1161         na_elem_t *e;
1162
1163         if (cd == NULL)
1164                 return (EINVAL);
1165
1166         if (cd->query != NULL)
1167                 return (0);
1168
1169         cd->query = na_elem_new ("perf-object-get-instances");
1170         if (cd->query == NULL)
1171         {
1172                 ERROR ("netapp plugin: na_elem_new failed.");
1173                 return (-1);
1174         }
1175         na_child_add_string (cd->query, "objectname", "disk");
1176
1177         e = na_elem_new("counters");
1178         if (e == NULL)
1179         {
1180                 na_elem_free (cd->query);
1181                 cd->query = NULL;
1182                 ERROR ("netapp plugin: na_elem_new failed.");
1183                 return (-1);
1184         }
1185         na_child_add_string(e, "counter", "disk_busy");
1186         na_child_add_string(e, "counter", "base_for_disk_busy");
1187         na_child_add(cd->query, e);
1188
1189         return (0);
1190 } /* }}} int cna_setup_disk */
1191
1192 static int cna_query_disk (host_config_t *host) /* {{{ */
1193 {
1194         na_elem_t *data;
1195         int status;
1196         cdtime_t now;
1197
1198         if (host == NULL)
1199                 return (EINVAL);
1200
1201         /* If the user did not configure disk statistics, return without doing
1202          * anything. */
1203         if (host->cfg_disk == NULL)
1204                 return (0);
1205
1206         now = cdtime ();
1207         if ((host->cfg_disk->interval.interval + host->cfg_disk->interval.last_read) > now)
1208                 return (0);
1209
1210         status = cna_setup_disk (host->cfg_disk);
1211         if (status != 0)
1212                 return (status);
1213         assert (host->cfg_disk->query != NULL);
1214
1215         data = na_server_invoke_elem(host->srv, host->cfg_disk->query);
1216         if (na_results_status (data) != NA_OK)
1217         {
1218                 ERROR ("netapp plugin: cna_query_disk: na_server_invoke_elem failed for host %s: %s",
1219                                 host->name, na_results_reason (data));
1220                 na_elem_free (data);
1221                 return (-1);
1222         }
1223
1224         status = cna_handle_disk_data (host->name, host->cfg_disk, data,
1225                         host->cfg_disk->interval.interval);
1226
1227         if (status == 0)
1228                 host->cfg_disk->interval.last_read = now;
1229
1230         na_elem_free (data);
1231         return (status);
1232 } /* }}} int cna_query_disk */
1233
1234 /* Data corresponding to <VolumePerf /> */
1235 static int cna_handle_volume_perf_data (const char *hostname, /* {{{ */
1236                 cfg_volume_perf_t *cvp, na_elem_t *data, cdtime_t interval)
1237 {
1238         cdtime_t timestamp;
1239         na_elem_t *elem_instances;
1240         na_elem_iter_t iter_instances;
1241
1242         timestamp = cna_child_get_cdtime (data);
1243
1244         elem_instances = na_elem_child(data, "instances");
1245         if (elem_instances == NULL)
1246         {
1247                 ERROR ("netapp plugin: handle_volume_perf_data: "
1248                                 "na_elem_child (\"instances\") failed "
1249                                 "for host %s.", hostname);
1250                 return (-1);
1251         }
1252
1253         iter_instances = na_child_iterator (elem_instances);
1254         for (na_elem_t *elem_instance = na_iterator_next(&iter_instances);
1255                         elem_instance != NULL;
1256                         elem_instance = na_iterator_next(&iter_instances))
1257         {
1258                 const char *name;
1259
1260                 data_volume_perf_t perf_data = { 0 };
1261                 data_volume_perf_t *v;
1262
1263                 na_elem_t *elem_counters;
1264                 na_elem_iter_t iter_counters;
1265
1266                 perf_data.timestamp = timestamp;
1267
1268                 name = na_child_get_string (elem_instance, "name");
1269                 if (name == NULL)
1270                         continue;
1271
1272                 /* get_volume_perf may return NULL if this volume is to be ignored. */
1273                 v = get_volume_perf (cvp, name);
1274                 if (v == NULL)
1275                         continue;
1276
1277                 elem_counters = na_elem_child (elem_instance, "counters");
1278                 if (elem_counters == NULL)
1279                         continue;
1280
1281                 iter_counters = na_child_iterator (elem_counters);
1282                 for (na_elem_t *elem_counter = na_iterator_next(&iter_counters);
1283                                 elem_counter != NULL;
1284                                 elem_counter = na_iterator_next(&iter_counters))
1285                 {
1286                         const char *name;
1287                         uint64_t value;
1288
1289                         name = na_child_get_string (elem_counter, "name");
1290                         if (name == NULL)
1291                                 continue;
1292
1293                         value = na_child_get_uint64 (elem_counter, "value", UINT64_MAX);
1294                         if (value == UINT64_MAX)
1295                                 continue;
1296
1297                         if (!strcmp(name, "read_data")) {
1298                                 perf_data.read_bytes = value;
1299                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_READ;
1300                         } else if (!strcmp(name, "write_data")) {
1301                                 perf_data.write_bytes = value;
1302                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_WRITE;
1303                         } else if (!strcmp(name, "read_ops")) {
1304                                 perf_data.read_ops = value;
1305                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_READ;
1306                         } else if (!strcmp(name, "write_ops")) {
1307                                 perf_data.write_ops = value;
1308                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_WRITE;
1309                         } else if (!strcmp(name, "read_latency")) {
1310                                 perf_data.read_latency = value;
1311                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_READ;
1312                         } else if (!strcmp(name, "write_latency")) {
1313                                 perf_data.write_latency = value;
1314                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_WRITE;
1315                         }
1316                 } /* for (elem_counter) */
1317
1318                 submit_volume_perf_data (hostname, v, &perf_data, interval);
1319         } /* for (volume) */
1320
1321         return (0);
1322 } /* }}} int cna_handle_volume_perf_data */
1323
1324 static int cna_setup_volume_perf (cfg_volume_perf_t *cd) /* {{{ */
1325 {
1326         na_elem_t *e;
1327
1328         if (cd == NULL)
1329                 return (EINVAL);
1330
1331         if (cd->query != NULL)
1332                 return (0);
1333
1334         cd->query = na_elem_new ("perf-object-get-instances");
1335         if (cd->query == NULL)
1336         {
1337                 ERROR ("netapp plugin: na_elem_new failed.");
1338                 return (-1);
1339         }
1340         na_child_add_string (cd->query, "objectname", "volume");
1341
1342         e = na_elem_new("counters");
1343         if (e == NULL)
1344         {
1345                 na_elem_free (cd->query);
1346                 cd->query = NULL;
1347                 ERROR ("netapp plugin: na_elem_new failed.");
1348                 return (-1);
1349         }
1350         na_child_add_string(e, "counter", "read_ops");
1351         na_child_add_string(e, "counter", "write_ops");
1352         na_child_add_string(e, "counter", "read_data");
1353         na_child_add_string(e, "counter", "write_data");
1354         na_child_add_string(e, "counter", "read_latency");
1355         na_child_add_string(e, "counter", "write_latency");
1356         na_child_add(cd->query, e);
1357
1358         return (0);
1359 } /* }}} int cna_setup_volume_perf */
1360
1361 static int cna_query_volume_perf (host_config_t *host) /* {{{ */
1362 {
1363         na_elem_t *data;
1364         int status;
1365         cdtime_t now;
1366
1367         if (host == NULL)
1368                 return (EINVAL);
1369
1370         /* If the user did not configure volume performance statistics, return
1371          * without doing anything. */
1372         if (host->cfg_volume_perf == NULL)
1373                 return (0);
1374
1375         now = cdtime ();
1376         if ((host->cfg_volume_perf->interval.interval + host->cfg_volume_perf->interval.last_read) > now)
1377                 return (0);
1378
1379         status = cna_setup_volume_perf (host->cfg_volume_perf);
1380         if (status != 0)
1381                 return (status);
1382         assert (host->cfg_volume_perf->query != NULL);
1383
1384         data = na_server_invoke_elem (host->srv, host->cfg_volume_perf->query);
1385         if (na_results_status (data) != NA_OK)
1386         {
1387                 ERROR ("netapp plugin: cna_query_volume_perf: na_server_invoke_elem failed for host %s: %s",
1388                                 host->name, na_results_reason (data));
1389                 na_elem_free (data);
1390                 return (-1);
1391         }
1392
1393         status = cna_handle_volume_perf_data (host->name, host->cfg_volume_perf, data,
1394                         host->cfg_volume_perf->interval.interval);
1395
1396         if (status == 0)
1397                 host->cfg_volume_perf->interval.last_read = now;
1398
1399         na_elem_free (data);
1400         return (status);
1401 } /* }}} int cna_query_volume_perf */
1402
1403 /* Data corresponding to <VolumeUsage /> */
1404 static int cna_submit_volume_usage_data (const char *hostname, /* {{{ */
1405                 cfg_volume_usage_t *cfg_volume, int interval)
1406 {
1407         for (data_volume_usage_t *v = cfg_volume->volumes; v != NULL; v = v->next)
1408         {
1409                 char plugin_instance[DATA_MAX_NAME_LEN];
1410
1411                 uint64_t norm_used = v->norm_used;
1412                 uint64_t norm_free = v->norm_free;
1413                 uint64_t sis_saved = v->sis_saved;
1414                 uint64_t compress_saved = v->compress_saved;
1415                 uint64_t dedup_saved = v->dedup_saved;
1416                 uint64_t snap_reserve_used = 0;
1417                 uint64_t snap_reserve_free = v->snap_reserved;
1418                 uint64_t snap_norm_used = v->snap_used;
1419
1420                 ssnprintf (plugin_instance, sizeof (plugin_instance),
1421                                 "volume-%s", v->name);
1422
1423                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_USED | HAVE_VOLUME_USAGE_SNAP_RSVD)) {
1424                         if (v->snap_reserved > v->snap_used) {
1425                                 snap_reserve_free = v->snap_reserved - v->snap_used;
1426                                 snap_reserve_used = v->snap_used;
1427                                 snap_norm_used = 0;
1428                         } else {
1429                                 snap_reserve_free = 0;
1430                                 snap_reserve_used = v->snap_reserved;
1431                                 snap_norm_used = v->snap_used - v->snap_reserved;
1432                         }
1433                 }
1434
1435                 /* The space used by snapshots but not reserved for them is included in
1436                  * both, norm_used and snap_norm_used. If possible, subtract this here. */
1437                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_NORM_USED | HAVE_VOLUME_USAGE_SNAP_USED))
1438                 {
1439                         if (norm_used >= snap_norm_used)
1440                                 norm_used -= snap_norm_used;
1441                         else
1442                         {
1443                                 ERROR ("netapp plugin: (norm_used = %"PRIu64") < (snap_norm_used = "
1444                                                 "%"PRIu64") for host %s. Invalidating both.",
1445                                                 norm_used, snap_norm_used, hostname);
1446                                 v->flags &= ~(HAVE_VOLUME_USAGE_NORM_USED | HAVE_VOLUME_USAGE_SNAP_USED);
1447                         }
1448                 }
1449
1450                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_NORM_FREE))
1451                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1452                                         "df_complex", "free",
1453                                         (double) norm_free, /* timestamp = */ 0, interval);
1454
1455                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SIS_SAVED))
1456                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1457                                         "df_complex", "sis_saved",
1458                                         (double) sis_saved, /* timestamp = */ 0, interval);
1459
1460                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_COMPRESS_SAVED))
1461                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1462                                         "df_complex", "compression_saved",
1463                                         (double) compress_saved, /* timestamp = */ 0, interval);
1464
1465                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_DEDUP_SAVED))
1466                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1467                                         "df_complex", "dedup_saved",
1468                                         (double) dedup_saved, /* timestamp = */ 0, interval);
1469
1470                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_NORM_USED))
1471                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1472                                         "df_complex", "used",
1473                                         (double) norm_used, /* timestamp = */ 0, interval);
1474
1475                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_RSVD))
1476                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1477                                         "df_complex", "snap_reserved",
1478                                         (double) snap_reserve_free, /* timestamp = */ 0, interval);
1479
1480                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_USED | HAVE_VOLUME_USAGE_SNAP_RSVD))
1481                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1482                                         "df_complex", "snap_reserve_used",
1483                                         (double) snap_reserve_used, /* timestamp = */ 0, interval);
1484
1485                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_USED))
1486                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1487                                         "df_complex", "snap_normal_used",
1488                                         (double) snap_norm_used, /* timestamp = */ 0, interval);
1489
1490                 /* Clear all the HAVE_* flags */
1491                 v->flags &= ~HAVE_VOLUME_USAGE_ALL;
1492         } /* for (v = cfg_volume->volumes) */
1493
1494         return (0);
1495 } /* }}} int cna_submit_volume_usage_data */
1496
1497 /* Switch the state of a volume between online and offline and send out a
1498  * notification. */
1499 static int cna_change_volume_status (const char *hostname, /* {{{ */
1500                 data_volume_usage_t *v)
1501 {
1502         notification_t n = { 0 };
1503
1504         n.time = cdtime ();
1505         sstrncpy (n.host, hostname, sizeof (n.host));
1506         sstrncpy (n.plugin, "netapp", sizeof (n.plugin));
1507         sstrncpy (n.plugin_instance, v->name, sizeof (n.plugin_instance));
1508
1509         if ((v->flags & IS_VOLUME_USAGE_OFFLINE) != 0) {
1510                 n.severity = NOTIF_OKAY;
1511                 ssnprintf (n.message, sizeof (n.message),
1512                                 "Volume %s is now online.", v->name);
1513                 v->flags &= ~IS_VOLUME_USAGE_OFFLINE;
1514         } else {
1515                 n.severity = NOTIF_WARNING;
1516                 ssnprintf (n.message, sizeof (n.message),
1517                                 "Volume %s is now offline.", v->name);
1518                 v->flags |= IS_VOLUME_USAGE_OFFLINE;
1519         }
1520
1521         return (plugin_dispatch_notification (&n));
1522 } /* }}} int cna_change_volume_status */
1523
1524 static void cna_handle_volume_snap_usage(const host_config_t *host, /* {{{ */
1525                 data_volume_usage_t *v)
1526 {
1527         uint64_t snap_used = 0, value;
1528         na_elem_t *data, *elem_snapshots;
1529         na_elem_iter_t iter_snap;
1530
1531         data = na_server_invoke_elem(host->srv, v->snap_query);
1532         if (na_results_status(data) != NA_OK)
1533         {
1534                 if (na_results_errno(data) == EVOLUMEOFFLINE) {
1535                         if ((v->flags & IS_VOLUME_USAGE_OFFLINE) == 0)
1536                                 cna_change_volume_status (host->name, v);
1537                 } else {
1538                         ERROR ("netapp plugin: cna_handle_volume_snap_usage: na_server_invoke_elem for "
1539                                         "volume \"%s\" on host %s failed with error %d: %s", v->name,
1540                                         host->name, na_results_errno(data), na_results_reason(data));
1541                 }
1542                 na_elem_free(data);
1543                 return;
1544         }
1545
1546         if ((v->flags & IS_VOLUME_USAGE_OFFLINE) != 0)
1547                 cna_change_volume_status (host->name, v);
1548
1549         elem_snapshots = na_elem_child (data, "snapshots");
1550         if (elem_snapshots == NULL)
1551         {
1552                 ERROR ("netapp plugin: cna_handle_volume_snap_usage: "
1553                                 "na_elem_child (\"snapshots\") failed "
1554                                 "for host %s.", host->name);
1555                 na_elem_free(data);
1556                 return;
1557         }
1558
1559         iter_snap = na_child_iterator (elem_snapshots);
1560         for (na_elem_t *elem_snap = na_iterator_next (&iter_snap);
1561                         elem_snap != NULL;
1562                         elem_snap = na_iterator_next (&iter_snap))
1563         {
1564                 value = na_child_get_uint64(elem_snap, "cumulative-total", 0);
1565                 /* "cumulative-total" is the total size of the oldest snapshot plus all
1566                  * newer ones in blocks (1KB). We therefore are looking for the highest
1567                  * number of all snapshots - that's the size required for the snapshots. */
1568                 if (value > snap_used)
1569                         snap_used = value;
1570         }
1571         na_elem_free (data);
1572         /* snap_used is in 1024 byte blocks */
1573         v->snap_used = snap_used * 1024;
1574         v->flags |= HAVE_VOLUME_USAGE_SNAP_USED;
1575 } /* }}} void cna_handle_volume_snap_usage */
1576
1577 static void cna_handle_volume_sis_data (const host_config_t *host, /* {{{ */
1578                 data_volume_usage_t *v, na_elem_t *sis)
1579 {
1580         const char *sis_state;
1581         uint64_t sis_saved_reported;
1582
1583         if (na_elem_child(sis, "sis-info"))
1584                 sis = na_elem_child(sis, "sis-info");
1585
1586         sis_state = na_child_get_string(sis, "state");
1587         if (sis_state == NULL)
1588                 return;
1589
1590         /* If SIS is not enabled, there's nothing left to do for this volume. */
1591         if (strcmp ("enabled", sis_state) != 0)
1592                 return;
1593
1594         sis_saved_reported = na_child_get_uint64(sis, "size-saved", UINT64_MAX);
1595         if (sis_saved_reported == UINT64_MAX)
1596                 return;
1597
1598         /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
1599         if ((sis_saved_reported >> 32) != 0) {
1600                 /* In case they ever fix this bug. */
1601                 v->sis_saved = sis_saved_reported;
1602                 v->flags |= HAVE_VOLUME_USAGE_SIS_SAVED;
1603         } else { /* really hacky work-around code. {{{ */
1604                 uint64_t sis_saved_percent;
1605                 uint64_t sis_saved_guess;
1606                 uint64_t overflow_guess;
1607                 uint64_t guess1, guess2, guess3;
1608
1609                 /* Check if we have v->norm_used. Without it, we cannot calculate
1610                  * sis_saved_guess. */
1611                 if ((v->flags & HAVE_VOLUME_USAGE_NORM_USED) == 0)
1612                         return;
1613
1614                 sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
1615                 if (sis_saved_percent > 100)
1616                         return;
1617
1618                 /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
1619                  * will hopefully be fixed in later versions. To work around the bug, try
1620                  * to figure out how often the 32bit integer wrapped around by using the
1621                  * "percentage-saved" value. Because the percentage is in the range
1622                  * [0-100], this should work as long as the saved space does not exceed
1623                  * 400 GBytes. */
1624                 /* percentage-saved = size-saved / (size-saved + size-used) */
1625                 if (sis_saved_percent < 100)
1626                         sis_saved_guess = v->norm_used * sis_saved_percent / (100 - sis_saved_percent);
1627                 else
1628                         sis_saved_guess = v->norm_used;
1629
1630                 overflow_guess = sis_saved_guess >> 32;
1631                 guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
1632                 guess2 = (overflow_guess << 32) + sis_saved_reported;
1633                 guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
1634
1635                 if (sis_saved_guess < guess2) {
1636                         if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
1637                                 v->sis_saved = guess1;
1638                         else
1639                                 v->sis_saved = guess2;
1640                 } else {
1641                         if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
1642                                 v->sis_saved = guess2;
1643                         else
1644                                 v->sis_saved = guess3;
1645                 }
1646                 v->flags |= HAVE_VOLUME_USAGE_SIS_SAVED;
1647         } /* }}} end of 32-bit workaround */
1648 } /* }}} void cna_handle_volume_sis_data */
1649
1650 /* ONTAP >= 8.1 uses SIS for managing dedup and compression */
1651 static void cna_handle_volume_sis_saved (const host_config_t *host, /* {{{ */
1652                 data_volume_usage_t *v, na_elem_t *sis)
1653 {
1654         uint64_t saved;
1655
1656         if (na_elem_child(sis, "sis-info"))
1657                 sis = na_elem_child(sis, "sis-info");
1658
1659         saved = na_child_get_uint64(sis, "compress-saved", UINT64_MAX);
1660         if (saved != UINT64_MAX) {
1661                 v->compress_saved = saved;
1662                 v->flags |= HAVE_VOLUME_USAGE_COMPRESS_SAVED;
1663         }
1664
1665         saved = na_child_get_uint64(sis, "dedup-saved", UINT64_MAX);
1666         if (saved != UINT64_MAX) {
1667                 v->dedup_saved = saved;
1668                 v->flags |= HAVE_VOLUME_USAGE_DEDUP_SAVED;
1669         }
1670 } /* }}} void cna_handle_volume_sis_saved */
1671
1672 static int cna_handle_volume_usage_data (const host_config_t *host, /* {{{ */
1673                 cfg_volume_usage_t *cfg_volume, na_elem_t *data)
1674 {
1675         na_elem_t *elem_volumes;
1676         na_elem_iter_t iter_volume;
1677
1678         elem_volumes = na_elem_child (data, "volumes");
1679         if (elem_volumes == NULL)
1680         {
1681                 ERROR ("netapp plugin: cna_handle_volume_usage_data: "
1682                                 "na_elem_child (\"volumes\") failed "
1683                                 "for host %s.", host->name);
1684                 return (-1);
1685         }
1686
1687         iter_volume = na_child_iterator (elem_volumes);
1688         for (na_elem_t *elem_volume = na_iterator_next (&iter_volume);
1689                         elem_volume != NULL;
1690                         elem_volume = na_iterator_next (&iter_volume))
1691         {
1692                 const char *volume_name, *state;
1693
1694                 data_volume_usage_t *v;
1695                 uint64_t value;
1696
1697                 na_elem_t *sis;
1698
1699                 volume_name = na_child_get_string (elem_volume, "name");
1700                 if (volume_name == NULL)
1701                         continue;
1702
1703                 state = na_child_get_string (elem_volume, "state");
1704                 if ((state == NULL) || (strcmp(state, "online") != 0))
1705                         continue;
1706
1707                 /* get_volume_usage may return NULL if the volume is to be ignored. */
1708                 v = get_volume_usage (cfg_volume, volume_name);
1709                 if (v == NULL)
1710                         continue;
1711
1712                 if ((v->flags & CFG_VOLUME_USAGE_SNAP) != 0)
1713                         cna_handle_volume_snap_usage(host, v);
1714
1715                 if ((v->flags & CFG_VOLUME_USAGE_DF) == 0)
1716                         continue;
1717
1718                 /* 2^4 exa-bytes? This will take a while ;) */
1719                 value = na_child_get_uint64(elem_volume, "size-available", UINT64_MAX);
1720                 if (value != UINT64_MAX) {
1721                         v->norm_free = value;
1722                         v->flags |= HAVE_VOLUME_USAGE_NORM_FREE;
1723                 }
1724
1725                 value = na_child_get_uint64(elem_volume, "size-used", UINT64_MAX);
1726                 if (value != UINT64_MAX) {
1727                         v->norm_used = value;
1728                         v->flags |= HAVE_VOLUME_USAGE_NORM_USED;
1729                 }
1730
1731                 value = na_child_get_uint64(elem_volume, "snapshot-blocks-reserved", UINT64_MAX);
1732                 if (value != UINT64_MAX) {
1733                         /* 1 block == 1024 bytes  as per API docs */
1734                         v->snap_reserved = 1024 * value;
1735                         v->flags |= HAVE_VOLUME_USAGE_SNAP_RSVD;
1736                 }
1737
1738                 sis = na_elem_child(elem_volume, "sis");
1739                 if (sis != NULL) {
1740                         cna_handle_volume_sis_data (host, v, sis);
1741                         cna_handle_volume_sis_saved (host, v, sis);
1742                 }
1743         } /* for (elem_volume) */
1744
1745         return (cna_submit_volume_usage_data (host->name, cfg_volume,
1746                                 host->cfg_volume_usage->interval.interval));
1747 } /* }}} int cna_handle_volume_usage_data */
1748
1749 static int cna_setup_volume_usage (cfg_volume_usage_t *cvu) /* {{{ */
1750 {
1751         if (cvu == NULL)
1752                 return (EINVAL);
1753
1754         if (cvu->query != NULL)
1755                 return (0);
1756
1757         cvu->query = na_elem_new ("volume-list-info");
1758         if (cvu->query == NULL)
1759         {
1760                 ERROR ("netapp plugin: na_elem_new failed.");
1761                 return (-1);
1762         }
1763
1764         return (0);
1765 } /* }}} int cna_setup_volume_usage */
1766
1767 static int cna_query_volume_usage (host_config_t *host) /* {{{ */
1768 {
1769         na_elem_t *data;
1770         int status;
1771         cdtime_t now;
1772
1773         if (host == NULL)
1774                 return (EINVAL);
1775
1776         /* If the user did not configure volume_usage statistics, return without
1777          * doing anything. */
1778         if (host->cfg_volume_usage == NULL)
1779                 return (0);
1780
1781         now = cdtime ();
1782         if ((host->cfg_volume_usage->interval.interval + host->cfg_volume_usage->interval.last_read) > now)
1783                 return (0);
1784
1785         status = cna_setup_volume_usage (host->cfg_volume_usage);
1786         if (status != 0)
1787                 return (status);
1788         assert (host->cfg_volume_usage->query != NULL);
1789
1790         data = na_server_invoke_elem(host->srv, host->cfg_volume_usage->query);
1791         if (na_results_status (data) != NA_OK)
1792         {
1793                 ERROR ("netapp plugin: cna_query_volume_usage: na_server_invoke_elem failed for host %s: %s",
1794                                 host->name, na_results_reason (data));
1795                 na_elem_free (data);
1796                 return (-1);
1797         }
1798
1799         status = cna_handle_volume_usage_data (host, host->cfg_volume_usage, data);
1800
1801         if (status == 0)
1802                 host->cfg_volume_usage->interval.last_read = now;
1803
1804         na_elem_free (data);
1805         return (status);
1806 } /* }}} int cna_query_volume_usage */
1807
1808 /* Data corresponding to <Quota /> */
1809 static int cna_handle_quota_data (const host_config_t *host, /* {{{ */
1810                 cfg_quota_t *cfg_quota, na_elem_t *data)
1811 {
1812         na_elem_t *elem_quotas;
1813         na_elem_iter_t iter_quota;
1814
1815         elem_quotas = na_elem_child (data, "quotas");
1816         if (elem_quotas == NULL)
1817         {
1818                 ERROR ("netapp plugin: cna_handle_quota_data: "
1819                                 "na_elem_child (\"quotas\") failed "
1820                                 "for host %s.", host->name);
1821                 return (-1);
1822         }
1823
1824         iter_quota = na_child_iterator (elem_quotas);
1825         for (na_elem_t *elem_quota = na_iterator_next (&iter_quota);
1826                         elem_quota != NULL;
1827                         elem_quota = na_iterator_next (&iter_quota))
1828         {
1829                 const char *quota_type, *volume_name, *tree_name;
1830                 uint64_t value;
1831
1832                 char plugin_instance[DATA_MAX_NAME_LEN];
1833
1834                 quota_type = na_child_get_string (elem_quota, "quota-type");
1835                 if (quota_type == NULL)
1836                         continue;
1837
1838                 /* possible TODO: support other types as well */
1839                 if (strcmp (quota_type, "tree") != 0)
1840                         continue;
1841
1842                 tree_name = na_child_get_string (elem_quota, "tree");
1843                 if ((tree_name == NULL) || (*tree_name == '\0'))
1844                         continue;
1845
1846                 volume_name = na_child_get_string (elem_quota, "volume");
1847                 if (volume_name == NULL)
1848                         continue;
1849
1850                 ssnprintf (plugin_instance, sizeof (plugin_instance),
1851                                 "quota-%s-%s", volume_name, tree_name);
1852
1853                 value = na_child_get_uint64 (elem_quota, "disk-used", UINT64_MAX);
1854                 if (value != UINT64_MAX) {
1855                         value *= 1024; /* disk-used reports kilobytes */
1856                         submit_double (host->name, plugin_instance,
1857                                         /* type = */ "df_complex", /* type instance = */ NULL,
1858                                         (double)value, /* timestamp = */ 0,
1859                                         host->cfg_quota->interval.interval);
1860                 }
1861
1862                 value = na_child_get_uint64 (elem_quota, "files-used", UINT64_MAX);
1863                 if (value != UINT64_MAX) {
1864                         submit_double (host->name, plugin_instance,
1865                                         /* type = */ "files", /* type instance = */ NULL,
1866                                         (double)value, /* timestamp = */ 0,
1867                                         host->cfg_quota->interval.interval);
1868                 }
1869         } /* for (elem_quota) */
1870
1871         return (0);
1872 } /* }}} int cna_handle_volume_usage_data */
1873
1874 static int cna_setup_quota (cfg_quota_t *cq) /* {{{ */
1875 {
1876         if (cq == NULL)
1877                 return (EINVAL);
1878
1879         if (cq->query != NULL)
1880                 return (0);
1881
1882         cq->query = na_elem_new ("quota-report");
1883         if (cq->query == NULL)
1884         {
1885                 ERROR ("netapp plugin: na_elem_new failed.");
1886                 return (-1);
1887         }
1888
1889         return (0);
1890 } /* }}} int cna_setup_quota */
1891
1892 static int cna_query_quota (host_config_t *host) /* {{{ */
1893 {
1894         na_elem_t *data;
1895         int status;
1896         cdtime_t now;
1897
1898         if (host == NULL)
1899                 return (EINVAL);
1900
1901         /* If the user did not configure quota statistics, return without
1902          * doing anything. */
1903         if (host->cfg_quota == NULL)
1904                 return (0);
1905
1906         now = cdtime ();
1907         if ((host->cfg_quota->interval.interval + host->cfg_quota->interval.last_read) > now)
1908                 return (0);
1909
1910         status = cna_setup_quota (host->cfg_quota);
1911         if (status != 0)
1912                 return (status);
1913         assert (host->cfg_quota->query != NULL);
1914
1915         data = na_server_invoke_elem (host->srv, host->cfg_quota->query);
1916         if (na_results_status (data) != NA_OK)
1917         {
1918                 ERROR ("netapp plugin: cna_query_quota: na_server_invoke_elem failed for host %s: %s",
1919                                 host->name, na_results_reason (data));
1920                 na_elem_free (data);
1921                 return (-1);
1922         }
1923
1924         status = cna_handle_quota_data (host, host->cfg_quota, data);
1925
1926         if (status == 0)
1927                 host->cfg_quota->interval.last_read = now;
1928
1929         na_elem_free (data);
1930         return (status);
1931 } /* }}} int cna_query_quota */
1932
1933 /* Data corresponding to <SnapVault /> */
1934 static int cna_handle_snapvault_data (const char *hostname, /* {{{ */
1935                 cfg_snapvault_t *cfg_snapvault, na_elem_t *data, cdtime_t interval)
1936 {
1937         na_elem_iter_t status_iter;
1938
1939         status = na_elem_child (data, "status-list");
1940         if (! status) {
1941                 ERROR ("netapp plugin: SnapVault status record missing status-list");
1942                 return (0);
1943         }
1944
1945         status_iter = na_child_iterator (status);
1946         for (na_elem_t *status = na_iterator_next (&status_iter);
1947                         status != NULL;
1948                         status = na_iterator_next (&status_iter))
1949         {
1950                 const char *dest_sys, *dest_path, *src_sys, *src_path;
1951                 char plugin_instance[DATA_MAX_NAME_LEN];
1952                 uint64_t value;
1953
1954                 dest_sys  = na_child_get_string (status, "destination-system");
1955                 dest_path = na_child_get_string (status, "destination-path");
1956                 src_sys   = na_child_get_string (status, "source-system");
1957                 src_path  = na_child_get_string (status, "source-path");
1958
1959                 if ((! dest_sys) || (! dest_path) || (! src_sys) || (! src_path))
1960                         continue;
1961
1962                 value = na_child_get_uint64 (status, "lag-time", UINT64_MAX);
1963                 if (value == UINT64_MAX) /* no successful baseline transfer yet */
1964                         continue;
1965
1966                 /* possible TODO: make plugin instance configurable */
1967                 ssnprintf (plugin_instance, sizeof (plugin_instance),
1968                                 "snapvault-%s", dest_path);
1969                 submit_double (hostname, plugin_instance, /* type = */ "delay", NULL,
1970                                 (double)value, /* timestamp = */ 0, interval);
1971
1972                 value = na_child_get_uint64 (status, "last-transfer-duration", UINT64_MAX);
1973                 if (value != UINT64_MAX)
1974                         submit_double (hostname, plugin_instance, /* type = */ "duration", "last_transfer",
1975                                         (double)value, /* timestamp = */ 0, interval);
1976
1977                 value = na_child_get_uint64 (status, "transfer-progress", UINT64_MAX);
1978                 if (value == UINT64_MAX)
1979                         value = na_child_get_uint64 (status, "last-transfer-size", UINT64_MAX);
1980                 if (value != UINT64_MAX) {
1981                         value *= 1024; /* this is kilobytes */
1982                         submit_derive (hostname, plugin_instance, /* type = */ "if_rx_octets", "transferred",
1983                                         value, /* timestamp = */ 0, interval);
1984                 }
1985         } /* for (status) */
1986
1987         return (0);
1988 } /* }}} int cna_handle_snapvault_data */
1989
1990 static int cna_handle_snapvault_iter (host_config_t *host, /* {{{ */
1991                 na_elem_t *data)
1992 {
1993         const char *tag;
1994
1995         uint32_t records_count;
1996
1997         records_count = na_child_get_uint32 (data, "records", UINT32_MAX);
1998         if (records_count == UINT32_MAX)
1999                 return 0;
2000
2001         tag = na_child_get_string (data, "tag");
2002         if (! tag)
2003                 return 0;
2004
2005         DEBUG ("netapp plugin: Iterating %u SV records (tag = %s)", records_count, tag);
2006
2007         for (uint32_t i = 0; i < records_count; ++i) {
2008                 na_elem_t *elem;
2009
2010                 elem = na_server_invoke (host->srv,
2011                                 "snapvault-secondary-relationship-status-list-iter-next",
2012                                 "maximum", "1", "tag", tag, NULL);
2013
2014                 if (na_results_status (elem) != NA_OK)
2015                 {
2016                         ERROR ("netapp plugin: cna_handle_snapvault_iter: "
2017                                         "na_server_invoke failed for host %s: %s",
2018                                         host->name, na_results_reason (data));
2019                         na_elem_free (elem);
2020                         return (-1);
2021                 }
2022
2023                 cna_handle_snapvault_data (host->name, host->cfg_snapvault, elem,
2024                                 host->cfg_snapvault->interval.interval);
2025                 na_elem_free (elem);
2026         }
2027
2028         na_elem_free (na_server_invoke (host->srv,
2029                         "snapvault-secondary-relationship-status-list-iter-end",
2030                         "tag", tag, NULL));
2031         return (0);
2032 } /* }}} int cna_handle_snapvault_iter */
2033
2034 static int cna_setup_snapvault (cfg_snapvault_t *sv) /* {{{ */
2035 {
2036         if (sv == NULL)
2037                 return (EINVAL);
2038
2039         if (sv->query != NULL)
2040                 return (0);
2041
2042         sv->query = na_elem_new ("snapvault-secondary-relationship-status-list-iter-start");
2043         if (sv->query == NULL)
2044         {
2045                 ERROR ("netapp plugin: na_elem_new failed.");
2046                 return (-1);
2047         }
2048
2049         return (0);
2050 } /* }}} int cna_setup_snapvault */
2051
2052 static int cna_query_snapvault (host_config_t *host) /* {{{ */
2053 {
2054         na_elem_t *data;
2055         int status;
2056         cdtime_t now;
2057
2058         if (host == NULL)
2059                 return EINVAL;
2060
2061         if (host->cfg_snapvault == NULL)
2062                 return 0;
2063
2064         now = cdtime ();
2065         if ((host->cfg_snapvault->interval.interval + host->cfg_snapvault->interval.last_read) > now)
2066                 return (0);
2067
2068         status = cna_setup_snapvault (host->cfg_snapvault);
2069         if (status != 0)
2070                 return (status);
2071         assert (host->cfg_snapvault->query != NULL);
2072
2073         data = na_server_invoke_elem (host->srv, host->cfg_snapvault->query);
2074         if (na_results_status (data) != NA_OK)
2075         {
2076                 ERROR ("netapp plugin: cna_query_snapvault: na_server_invoke_elem failed for host %s: %s",
2077                                 host->name, na_results_reason (data));
2078                 na_elem_free (data);
2079                 return (-1);
2080         }
2081
2082         status = cna_handle_snapvault_iter (host, data);
2083
2084         if (status == 0)
2085                 host->cfg_snapvault->interval.last_read = now;
2086
2087         na_elem_free (data);
2088         return (status);
2089 } /* }}} int cna_query_snapvault */
2090
2091 /* Data corresponding to <System /> */
2092 static int cna_handle_system_data (const char *hostname, /* {{{ */
2093                 cfg_system_t *cfg_system, na_elem_t *data, int interval)
2094 {
2095         na_elem_t *instances;
2096         na_elem_iter_t counter_iter;
2097
2098         derive_t disk_read = 0, disk_written = 0;
2099         derive_t net_recv = 0, net_sent = 0;
2100         derive_t cpu_busy = 0, cpu_total = 0;
2101         uint32_t counter_flags = 0;
2102
2103         const char *instance;
2104         cdtime_t timestamp;
2105
2106         timestamp = cna_child_get_cdtime (data);
2107
2108         instances = na_elem_child(na_elem_child (data, "instances"), "instance-data");
2109         if (instances == NULL)
2110         {
2111                 ERROR ("netapp plugin: cna_handle_system_data: "
2112                                 "na_elem_child (\"instances\") failed "
2113                                 "for host %s.", hostname);
2114                 return (-1);
2115         }
2116
2117         instance = na_child_get_string (instances, "name");
2118         if (instance == NULL)
2119         {
2120                 ERROR ("netapp plugin: cna_handle_system_data: "
2121                                 "na_child_get_string (\"name\") failed "
2122                                 "for host %s.", hostname);
2123                 return (-1);
2124         }
2125
2126         counter_iter = na_child_iterator (na_elem_child (instances, "counters"));
2127         for (na_elem_t *counter = na_iterator_next (&counter_iter);
2128                         counter != NULL;
2129                         counter = na_iterator_next (&counter_iter))
2130         {
2131                 const char *name;
2132                 uint64_t value;
2133
2134                 name = na_child_get_string(counter, "name");
2135                 if (name == NULL)
2136                         continue;
2137
2138                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
2139                 if (value == UINT64_MAX)
2140                         continue;
2141
2142                 if (!strcmp(name, "disk_data_read")) {
2143                         disk_read = (derive_t) (value * 1024);
2144                         counter_flags |= 0x01;
2145                 } else if (!strcmp(name, "disk_data_written")) {
2146                         disk_written = (derive_t) (value * 1024);
2147                         counter_flags |= 0x02;
2148                 } else if (!strcmp(name, "net_data_recv")) {
2149                         net_recv = (derive_t) (value * 1024);
2150                         counter_flags |= 0x04;
2151                 } else if (!strcmp(name, "net_data_sent")) {
2152                         net_sent = (derive_t) (value * 1024);
2153                         counter_flags |= 0x08;
2154                 } else if (!strcmp(name, "cpu_busy")) {
2155                         cpu_busy = (derive_t) value;
2156                         counter_flags |= 0x10;
2157                 } else if (!strcmp(name, "cpu_elapsed_time")) {
2158                         cpu_total = (derive_t) value;
2159                         counter_flags |= 0x20;
2160                 } else if ((cfg_system->flags & CFG_SYSTEM_OPS)
2161                                 && (value > 0) && (strlen(name) > 4)
2162                                 && (!strcmp(name + strlen(name) - 4, "_ops"))) {
2163                         submit_derive (hostname, instance, "disk_ops_complex", name,
2164                                         (derive_t) value, timestamp, interval);
2165                 }
2166         } /* for (counter) */
2167
2168         if ((cfg_system->flags & CFG_SYSTEM_DISK)
2169                         && (HAS_ALL_FLAGS (counter_flags, 0x01 | 0x02)))
2170                 submit_two_derive (hostname, instance, "disk_octets", NULL,
2171                                 disk_read, disk_written, timestamp, interval);
2172
2173         if ((cfg_system->flags & CFG_SYSTEM_NET)
2174                         && (HAS_ALL_FLAGS (counter_flags, 0x04 | 0x08)))
2175                 submit_two_derive (hostname, instance, "if_octets", NULL,
2176                                 net_recv, net_sent, timestamp, interval);
2177
2178         if ((cfg_system->flags & CFG_SYSTEM_CPU)
2179                         && (HAS_ALL_FLAGS (counter_flags, 0x10 | 0x20)))
2180         {
2181                 submit_derive (hostname, instance, "cpu", "system",
2182                                 cpu_busy, timestamp, interval);
2183                 submit_derive (hostname, instance, "cpu", "idle",
2184                                 cpu_total - cpu_busy, timestamp, interval);
2185         }
2186
2187         return (0);
2188 } /* }}} int cna_handle_system_data */
2189
2190 static int cna_setup_system (cfg_system_t *cs) /* {{{ */
2191 {
2192         if (cs == NULL)
2193                 return (EINVAL);
2194
2195         if (cs->query != NULL)
2196                 return (0);
2197
2198         cs->query = na_elem_new ("perf-object-get-instances");
2199         if (cs->query == NULL)
2200         {
2201                 ERROR ("netapp plugin: na_elem_new failed.");
2202                 return (-1);
2203         }
2204         na_child_add_string (cs->query, "objectname", "system");
2205
2206         return (0);
2207 } /* }}} int cna_setup_system */
2208
2209 static int cna_query_system (host_config_t *host) /* {{{ */
2210 {
2211         na_elem_t *data;
2212         int status;
2213         cdtime_t now;
2214
2215         if (host == NULL)
2216                 return (EINVAL);
2217
2218         /* If system statistics were not configured, return without doing anything. */
2219         if (host->cfg_system == NULL)
2220                 return (0);
2221
2222         now = cdtime ();
2223         if ((host->cfg_system->interval.interval + host->cfg_system->interval.last_read) > now)
2224                 return (0);
2225
2226         status = cna_setup_system (host->cfg_system);
2227         if (status != 0)
2228                 return (status);
2229         assert (host->cfg_system->query != NULL);
2230
2231         data = na_server_invoke_elem(host->srv, host->cfg_system->query);
2232         if (na_results_status (data) != NA_OK)
2233         {
2234                 ERROR ("netapp plugin: cna_query_system: na_server_invoke_elem failed for host %s: %s",
2235                                 host->name, na_results_reason (data));
2236                 na_elem_free (data);
2237                 return (-1);
2238         }
2239
2240         status = cna_handle_system_data (host->name, host->cfg_system, data,
2241                         host->cfg_system->interval.interval);
2242
2243         if (status == 0)
2244                 host->cfg_system->interval.last_read = now;
2245
2246         na_elem_free (data);
2247         return (status);
2248 } /* }}} int cna_query_system */
2249
2250 /*
2251  * Configuration handling
2252  */
2253 /* Sets a given flag if the boolean argument is true and unsets the flag if it
2254  * is false. On error, the flag-field is not changed. */
2255 static int cna_config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */
2256                 uint32_t *flags, uint32_t flag)
2257 {
2258         if ((ci == NULL) || (flags == NULL))
2259                 return (EINVAL);
2260
2261         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
2262         {
2263                 WARNING ("netapp plugin: The %s option needs exactly one boolean argument.",
2264                                 ci->key);
2265                 return (-1);
2266         }
2267
2268         if (ci->values[0].value.boolean)
2269                 *flags |= flag;
2270         else
2271                 *flags &= ~flag;
2272
2273         return (0);
2274 } /* }}} int cna_config_bool_to_flag */
2275
2276 /* Handling of the "Interval" option which is allowed in every block. */
2277 static int cna_config_get_interval (const oconfig_item_t *ci, /* {{{ */
2278                 cna_interval_t *out_interval)
2279 {
2280         cdtime_t tmp = 0;
2281         int status;
2282
2283         status = cf_util_get_cdtime (ci, &tmp);
2284         if (status != 0)
2285                 return (status);
2286
2287         out_interval->interval = tmp;
2288         out_interval->last_read = 0;
2289
2290         return (0);
2291 } /* }}} int cna_config_get_interval */
2292
2293 /* Handling of the "GetIO", "GetOps" and "GetLatency" options within a
2294  * <VolumePerf /> block. */
2295 static void cna_config_volume_perf_option (cfg_volume_perf_t *cvp, /* {{{ */
2296                 const oconfig_item_t *ci)
2297 {
2298         char *name;
2299         ignorelist_t * il;
2300
2301         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2302         {
2303                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2304                                 ci->key);
2305                 return;
2306         }
2307
2308         name = ci->values[0].value.string;
2309
2310         if (strcasecmp ("GetIO", ci->key) == 0)
2311                 il = cvp->il_octets;
2312         else if (strcasecmp ("GetOps", ci->key) == 0)
2313                 il = cvp->il_operations;
2314         else if (strcasecmp ("GetLatency", ci->key) == 0)
2315                 il = cvp->il_latency;
2316         else
2317                 return;
2318
2319         ignorelist_add (il, name);
2320 } /* }}} void cna_config_volume_perf_option */
2321
2322 /* Handling of the "IgnoreSelectedIO", "IgnoreSelectedOps" and
2323  * "IgnoreSelectedLatency" options within a <VolumePerf /> block. */
2324 static void cna_config_volume_perf_default (cfg_volume_perf_t *cvp, /* {{{ */
2325                 const oconfig_item_t *ci)
2326 {
2327         ignorelist_t *il;
2328
2329         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
2330         {
2331                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2332                                 ci->key);
2333                 return;
2334         }
2335
2336         if (strcasecmp ("IgnoreSelectedIO", ci->key) == 0)
2337                 il = cvp->il_octets;
2338         else if (strcasecmp ("IgnoreSelectedOps", ci->key) == 0)
2339                 il = cvp->il_operations;
2340         else if (strcasecmp ("IgnoreSelectedLatency", ci->key) == 0)
2341                 il = cvp->il_latency;
2342         else
2343                 return;
2344
2345         if (ci->values[0].value.boolean)
2346                 ignorelist_set_invert (il, /* invert = */ 0);
2347         else
2348                 ignorelist_set_invert (il, /* invert = */ 1);
2349 } /* }}} void cna_config_volume_perf_default */
2350
2351 /* Corresponds to a <Disks /> block */
2352 /*
2353  * <VolumePerf>
2354  *   GetIO "vol0"
2355  *   GetIO "vol1"
2356  *   IgnoreSelectedIO false
2357  *
2358  *   GetOps "vol0"
2359  *   GetOps "vol2"
2360  *   IgnoreSelectedOps false
2361  *
2362  *   GetLatency "vol2"
2363  *   GetLatency "vol3"
2364  *   IgnoreSelectedLatency false
2365  * </VolumePerf>
2366  */
2367 /* Corresponds to a <VolumePerf /> block */
2368 static int cna_config_volume_performance (host_config_t *host, /* {{{ */
2369                 const oconfig_item_t *ci)
2370 {
2371         cfg_volume_perf_t *cfg_volume_perf;
2372
2373         if ((host == NULL) || (ci == NULL))
2374                 return (EINVAL);
2375
2376         if (host->cfg_volume_perf == NULL)
2377         {
2378                 cfg_volume_perf = calloc (1, sizeof (*cfg_volume_perf));
2379                 if (cfg_volume_perf == NULL)
2380                         return (ENOMEM);
2381
2382                 /* Set default flags */
2383                 cfg_volume_perf->query = NULL;
2384                 cfg_volume_perf->volumes = NULL;
2385
2386                 cfg_volume_perf->il_octets = ignorelist_create (/* invert = */ 1);
2387                 if (cfg_volume_perf->il_octets == NULL)
2388                 {
2389                         sfree (cfg_volume_perf);
2390                         return (ENOMEM);
2391                 }
2392
2393                 cfg_volume_perf->il_operations = ignorelist_create (/* invert = */ 1);
2394                 if (cfg_volume_perf->il_operations == NULL)
2395                 {
2396                         ignorelist_free (cfg_volume_perf->il_octets);
2397                         sfree (cfg_volume_perf);
2398                         return (ENOMEM);
2399                 }
2400
2401                 cfg_volume_perf->il_latency = ignorelist_create (/* invert = */ 1);
2402                 if (cfg_volume_perf->il_latency == NULL)
2403                 {
2404                         ignorelist_free (cfg_volume_perf->il_octets);
2405                         ignorelist_free (cfg_volume_perf->il_operations);
2406                         sfree (cfg_volume_perf);
2407                         return (ENOMEM);
2408                 }
2409
2410                 host->cfg_volume_perf = cfg_volume_perf;
2411         }
2412         cfg_volume_perf = host->cfg_volume_perf;
2413
2414         for (int i = 0; i < ci->children_num; ++i) {
2415                 oconfig_item_t *item = ci->children + i;
2416
2417                 /* if (!item || !item->key || !*item->key) continue; */
2418                 if (strcasecmp(item->key, "Interval") == 0)
2419                         cna_config_get_interval (item, &cfg_volume_perf->interval);
2420                 else if (!strcasecmp(item->key, "GetIO"))
2421                         cna_config_volume_perf_option (cfg_volume_perf, item);
2422                 else if (!strcasecmp(item->key, "GetOps"))
2423                         cna_config_volume_perf_option (cfg_volume_perf, item);
2424                 else if (!strcasecmp(item->key, "GetLatency"))
2425                         cna_config_volume_perf_option (cfg_volume_perf, item);
2426                 else if (!strcasecmp(item->key, "IgnoreSelectedIO"))
2427                         cna_config_volume_perf_default (cfg_volume_perf, item);
2428                 else if (!strcasecmp(item->key, "IgnoreSelectedOps"))
2429                         cna_config_volume_perf_default (cfg_volume_perf, item);
2430                 else if (!strcasecmp(item->key, "IgnoreSelectedLatency"))
2431                         cna_config_volume_perf_default (cfg_volume_perf, item);
2432                 else
2433                         WARNING ("netapp plugin: The option %s is not allowed within "
2434                                         "`VolumePerf' blocks.", item->key);
2435         }
2436
2437         return (0);
2438 } /* }}} int cna_config_volume_performance */
2439
2440 /* Handling of the "GetCapacity" and "GetSnapshot" options within a
2441  * <VolumeUsage /> block. */
2442 static void cna_config_volume_usage_option (cfg_volume_usage_t *cvu, /* {{{ */
2443                 const oconfig_item_t *ci)
2444 {
2445         char *name;
2446         ignorelist_t * il;
2447
2448         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2449         {
2450                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2451                                 ci->key);
2452                 return;
2453         }
2454
2455         name = ci->values[0].value.string;
2456
2457         if (strcasecmp ("GetCapacity", ci->key) == 0)
2458                 il = cvu->il_capacity;
2459         else if (strcasecmp ("GetSnapshot", ci->key) == 0)
2460                 il = cvu->il_snapshot;
2461         else
2462                 return;
2463
2464         ignorelist_add (il, name);
2465 } /* }}} void cna_config_volume_usage_option */
2466
2467 /* Handling of the "IgnoreSelectedCapacity" and "IgnoreSelectedSnapshot"
2468  * options within a <VolumeUsage /> block. */
2469 static void cna_config_volume_usage_default (cfg_volume_usage_t *cvu, /* {{{ */
2470                 const oconfig_item_t *ci)
2471 {
2472         ignorelist_t *il;
2473
2474         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
2475         {
2476                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2477                                 ci->key);
2478                 return;
2479         }
2480
2481         if (strcasecmp ("IgnoreSelectedCapacity", ci->key) == 0)
2482                 il = cvu->il_capacity;
2483         else if (strcasecmp ("IgnoreSelectedSnapshot", ci->key) == 0)
2484                 il = cvu->il_snapshot;
2485         else
2486                 return;
2487
2488         if (ci->values[0].value.boolean)
2489                 ignorelist_set_invert (il, /* invert = */ 0);
2490         else
2491                 ignorelist_set_invert (il, /* invert = */ 1);
2492 } /* }}} void cna_config_volume_usage_default */
2493
2494 /* Corresponds to a <Quota /> block */
2495 static int cna_config_quota (host_config_t *host, oconfig_item_t *ci) /* {{{ */
2496 {
2497         cfg_quota_t *cfg_quota;
2498
2499         if ((host == NULL) || (ci == NULL))
2500                 return (EINVAL);
2501
2502         if (host->cfg_quota == NULL)
2503         {
2504                 cfg_quota = calloc (1, sizeof (*cfg_quota));
2505                 if (cfg_quota == NULL)
2506                         return (ENOMEM);
2507                 cfg_quota->query = NULL;
2508
2509                 host->cfg_quota = cfg_quota;
2510         }
2511         cfg_quota = host->cfg_quota;
2512
2513         for (int i = 0; i < ci->children_num; ++i) {
2514                 oconfig_item_t *item = ci->children + i;
2515
2516                 if (strcasecmp (item->key, "Interval") == 0)
2517                         cna_config_get_interval (item, &cfg_quota->interval);
2518                 else
2519                         WARNING ("netapp plugin: The option %s is not allowed within "
2520                                         "`Quota' blocks.", item->key);
2521         }
2522
2523         return (0);
2524 } /* }}} int cna_config_quota */
2525
2526 /* Corresponds to a <Disks /> block */
2527 static int cna_config_disk(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
2528         cfg_disk_t *cfg_disk;
2529
2530         if ((host == NULL) || (ci == NULL))
2531                 return (EINVAL);
2532
2533         if (host->cfg_disk == NULL)
2534         {
2535                 cfg_disk = calloc (1, sizeof (*cfg_disk));
2536                 if (cfg_disk == NULL)
2537                         return (ENOMEM);
2538
2539                 /* Set default flags */
2540                 cfg_disk->flags = CFG_DISK_ALL;
2541                 cfg_disk->query = NULL;
2542                 cfg_disk->disks = NULL;
2543
2544                 host->cfg_disk = cfg_disk;
2545         }
2546         cfg_disk = host->cfg_disk;
2547
2548         for (int i = 0; i < ci->children_num; ++i) {
2549                 oconfig_item_t *item = ci->children + i;
2550
2551                 /* if (!item || !item->key || !*item->key) continue; */
2552                 if (strcasecmp(item->key, "Interval") == 0)
2553                         cna_config_get_interval (item, &cfg_disk->interval);
2554                 else if (strcasecmp(item->key, "GetBusy") == 0)
2555                         cna_config_bool_to_flag (item, &cfg_disk->flags, CFG_DISK_BUSIEST);
2556         }
2557
2558         if ((cfg_disk->flags & CFG_DISK_ALL) == 0)
2559         {
2560                 NOTICE ("netapp plugin: All disk related values have been disabled. "
2561                                 "Collection of per-disk data will be disabled entirely.");
2562                 free_cfg_disk (host->cfg_disk);
2563                 host->cfg_disk = NULL;
2564         }
2565
2566         return (0);
2567 } /* }}} int cna_config_disk */
2568
2569 /* Corresponds to a <WAFL /> block */
2570 static int cna_config_wafl(host_config_t *host, oconfig_item_t *ci) /* {{{ */
2571 {
2572         cfg_wafl_t *cfg_wafl;
2573
2574         if ((host == NULL) || (ci == NULL))
2575                 return (EINVAL);
2576
2577         if (host->cfg_wafl == NULL)
2578         {
2579                 cfg_wafl = calloc (1, sizeof (*cfg_wafl));
2580                 if (cfg_wafl == NULL)
2581                         return (ENOMEM);
2582
2583                 /* Set default flags */
2584                 cfg_wafl->flags = CFG_WAFL_ALL;
2585
2586                 host->cfg_wafl = cfg_wafl;
2587         }
2588         cfg_wafl = host->cfg_wafl;
2589
2590         for (int i = 0; i < ci->children_num; ++i) {
2591                 oconfig_item_t *item = ci->children + i;
2592
2593                 if (strcasecmp(item->key, "Interval") == 0)
2594                         cna_config_get_interval (item, &cfg_wafl->interval);
2595                 else if (!strcasecmp(item->key, "GetNameCache"))
2596                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_NAME_CACHE);
2597                 else if (!strcasecmp(item->key, "GetDirCache"))
2598                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_DIR_CACHE);
2599                 else if (!strcasecmp(item->key, "GetBufferCache"))
2600                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_BUF_CACHE);
2601                 else if (!strcasecmp(item->key, "GetInodeCache"))
2602                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_INODE_CACHE);
2603                 else
2604                         WARNING ("netapp plugin: The %s config option is not allowed within "
2605                                         "`WAFL' blocks.", item->key);
2606         }
2607
2608         if ((cfg_wafl->flags & CFG_WAFL_ALL) == 0)
2609         {
2610                 NOTICE ("netapp plugin: All WAFL related values have been disabled. "
2611                                 "Collection of WAFL data will be disabled entirely.");
2612                 free_cfg_wafl (host->cfg_wafl);
2613                 host->cfg_wafl = NULL;
2614         }
2615
2616         return (0);
2617 } /* }}} int cna_config_wafl */
2618
2619 /*
2620  * <VolumeUsage>
2621  *   GetCapacity "vol0"
2622  *   GetCapacity "vol1"
2623  *   GetCapacity "vol2"
2624  *   GetCapacity "vol3"
2625  *   GetCapacity "vol4"
2626  *   IgnoreSelectedCapacity false
2627  *
2628  *   GetSnapshot "vol0"
2629  *   GetSnapshot "vol3"
2630  *   GetSnapshot "vol4"
2631  *   GetSnapshot "vol7"
2632  *   IgnoreSelectedSnapshot false
2633  * </VolumeUsage>
2634  */
2635 /* Corresponds to a <VolumeUsage /> block */
2636 static int cna_config_volume_usage(host_config_t *host, /* {{{ */
2637                 const oconfig_item_t *ci)
2638 {
2639         cfg_volume_usage_t *cfg_volume_usage;
2640
2641         if ((host == NULL) || (ci == NULL))
2642                 return (EINVAL);
2643
2644         if (host->cfg_volume_usage == NULL)
2645         {
2646                 cfg_volume_usage = calloc (1, sizeof (*cfg_volume_usage));
2647                 if (cfg_volume_usage == NULL)
2648                         return (ENOMEM);
2649
2650                 /* Set default flags */
2651                 cfg_volume_usage->query = NULL;
2652                 cfg_volume_usage->volumes = NULL;
2653
2654                 cfg_volume_usage->il_capacity = ignorelist_create (/* invert = */ 1);
2655                 if (cfg_volume_usage->il_capacity == NULL)
2656                 {
2657                         sfree (cfg_volume_usage);
2658                         return (ENOMEM);
2659                 }
2660
2661                 cfg_volume_usage->il_snapshot = ignorelist_create (/* invert = */ 1);
2662                 if (cfg_volume_usage->il_snapshot == NULL)
2663                 {
2664                         ignorelist_free (cfg_volume_usage->il_capacity);
2665                         sfree (cfg_volume_usage);
2666                         return (ENOMEM);
2667                 }
2668
2669                 host->cfg_volume_usage = cfg_volume_usage;
2670         }
2671         cfg_volume_usage = host->cfg_volume_usage;
2672
2673         for (int i = 0; i < ci->children_num; ++i) {
2674                 oconfig_item_t *item = ci->children + i;
2675
2676                 /* if (!item || !item->key || !*item->key) continue; */
2677                 if (strcasecmp(item->key, "Interval") == 0)
2678                         cna_config_get_interval (item, &cfg_volume_usage->interval);
2679                 else if (!strcasecmp(item->key, "GetCapacity"))
2680                         cna_config_volume_usage_option (cfg_volume_usage, item);
2681                 else if (!strcasecmp(item->key, "GetSnapshot"))
2682                         cna_config_volume_usage_option (cfg_volume_usage, item);
2683                 else if (!strcasecmp(item->key, "IgnoreSelectedCapacity"))
2684                         cna_config_volume_usage_default (cfg_volume_usage, item);
2685                 else if (!strcasecmp(item->key, "IgnoreSelectedSnapshot"))
2686                         cna_config_volume_usage_default (cfg_volume_usage, item);
2687                 else
2688                         WARNING ("netapp plugin: The option %s is not allowed within "
2689                                         "`VolumeUsage' blocks.", item->key);
2690         }
2691
2692         return (0);
2693 } /* }}} int cna_config_volume_usage */
2694
2695 /* Corresponds to a <SnapVault /> block */
2696 static int cna_config_snapvault (host_config_t *host, /* {{{ */
2697                 const oconfig_item_t *ci)
2698 {
2699         cfg_snapvault_t *cfg_snapvault;
2700
2701         if ((host == NULL) || (ci == NULL))
2702                 return EINVAL;
2703
2704         if (host->cfg_snapvault == NULL)
2705         {
2706                 cfg_snapvault = calloc (1, sizeof (*cfg_snapvault));
2707                 if (cfg_snapvault == NULL)
2708                         return ENOMEM;
2709                 cfg_snapvault->query = NULL;
2710
2711                 host->cfg_snapvault = cfg_snapvault;
2712         }
2713
2714         cfg_snapvault = host->cfg_snapvault;
2715
2716         for (int i = 0; i < ci->children_num; ++i) {
2717                 oconfig_item_t *item = ci->children + i;
2718
2719                 if (strcasecmp (item->key, "Interval") == 0)
2720                         cna_config_get_interval (item, &cfg_snapvault->interval);
2721                 else
2722                         WARNING ("netapp plugin: The option %s is not allowed within "
2723                                         "`SnapVault' blocks.", item->key);
2724         }
2725
2726         return 0;
2727 } /* }}} int cna_config_snapvault */
2728
2729 /* Corresponds to a <System /> block */
2730 static int cna_config_system (host_config_t *host, /* {{{ */
2731                 oconfig_item_t *ci)
2732 {
2733         cfg_system_t *cfg_system;
2734
2735         if ((host == NULL) || (ci == NULL))
2736                 return (EINVAL);
2737
2738         if (host->cfg_system == NULL)
2739         {
2740                 cfg_system = calloc (1, sizeof (*cfg_system));
2741                 if (cfg_system == NULL)
2742                         return (ENOMEM);
2743
2744                 /* Set default flags */
2745                 cfg_system->flags = CFG_SYSTEM_ALL;
2746                 cfg_system->query = NULL;
2747
2748                 host->cfg_system = cfg_system;
2749         }
2750         cfg_system = host->cfg_system;
2751
2752         for (int i = 0; i < ci->children_num; ++i) {
2753                 oconfig_item_t *item = ci->children + i;
2754
2755                 if (strcasecmp(item->key, "Interval") == 0) {
2756                         cna_config_get_interval (item, &cfg_system->interval);
2757                 } else if (!strcasecmp(item->key, "GetCPULoad")) {
2758                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_CPU);
2759                 } else if (!strcasecmp(item->key, "GetInterfaces")) {
2760                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_NET);
2761                 } else if (!strcasecmp(item->key, "GetDiskOps")) {
2762                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_OPS);
2763                 } else if (!strcasecmp(item->key, "GetDiskIO")) {
2764                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_DISK);
2765                 } else {
2766                         WARNING ("netapp plugin: The %s config option is not allowed within "
2767                                         "`System' blocks.", item->key);
2768                 }
2769         }
2770
2771         if ((cfg_system->flags & CFG_SYSTEM_ALL) == 0)
2772         {
2773                 NOTICE ("netapp plugin: All system related values have been disabled. "
2774                                 "Collection of system data will be disabled entirely.");
2775                 free_cfg_system (host->cfg_system);
2776                 host->cfg_system = NULL;
2777         }
2778
2779         return (0);
2780 } /* }}} int cna_config_system */
2781
2782 /* Corresponds to a <Host /> block. */
2783 static host_config_t *cna_alloc_host (void) /* {{{ */
2784 {
2785         host_config_t *host;
2786
2787         host = calloc (1, sizeof (*host));
2788         if (host == NULL)
2789                 return (NULL);
2790
2791         host->name = NULL;
2792         host->protocol = NA_SERVER_TRANSPORT_HTTPS;
2793         host->host = NULL;
2794         host->username = NULL;
2795         host->password = NULL;
2796         host->vfiler = NULL;
2797         host->srv = NULL;
2798         host->cfg_wafl = NULL;
2799         host->cfg_disk = NULL;
2800         host->cfg_volume_perf = NULL;
2801         host->cfg_volume_usage = NULL;
2802         host->cfg_quota = NULL;
2803         host->cfg_snapvault = NULL;
2804         host->cfg_system = NULL;
2805
2806         return (host);
2807 } /* }}} host_config_t *cna_alloc_host */
2808
2809 static host_config_t *cna_shallow_clone_host (host_config_t *host) /* {{{ */
2810 {
2811         host_config_t *clone;
2812
2813         if (host == NULL)
2814                 return (NULL);
2815
2816         clone = cna_alloc_host ();
2817         if (clone == NULL)
2818                 return (NULL);
2819
2820         if (host->name != NULL) {
2821                 clone->name = strdup (host->name);
2822                 if (clone->name == NULL) {
2823                         free_host_config (clone);
2824                         return NULL;
2825                 }
2826         }
2827
2828         clone->protocol = host->protocol;
2829
2830         if (host->host != NULL) {
2831                 clone->host = strdup (host->host);
2832                 if (clone->host == NULL) {
2833                         free_host_config (clone);
2834                         return NULL;
2835                 }
2836         }
2837
2838         clone->port = host->port;
2839
2840         if (host->username != NULL) {
2841                 clone->username = strdup (host->username);
2842                 if (clone->username == NULL) {
2843                         free_host_config (clone);
2844                         return NULL;
2845                 }
2846         }
2847         if (host->password != NULL) {
2848                 clone->password = strdup (host->password);
2849                 if (clone->password == NULL) {
2850                         free_host_config (clone);
2851                         return NULL;
2852                 }
2853         }
2854
2855         clone->interval = host->interval;
2856
2857         return (clone);
2858 } /* }}} host_config_t *cna_shallow_clone_host */
2859
2860 static int cna_read (user_data_t *ud);
2861
2862 static int cna_register_host (host_config_t *host) /* {{{ */
2863 {
2864         char cb_name[256];
2865
2866         if (host->vfiler)
2867                 ssnprintf (cb_name, sizeof (cb_name), "netapp-%s-%s",
2868                                 host->name, host->vfiler);
2869         else
2870                 ssnprintf (cb_name, sizeof (cb_name), "netapp-%s", host->name);
2871
2872         plugin_register_complex_read (/* group = */ NULL, cb_name,
2873                         /* callback  = */ cna_read,
2874                         /* interval  = */ host->interval,
2875                         &(user_data_t) {
2876                                 .data = host,
2877                                 .free_func = (void *) free_host_config,
2878                         });
2879
2880         return (0);
2881 } /* }}} int cna_register_host */
2882
2883 static int cna_config_host (host_config_t *host, /* {{{ */
2884                 const oconfig_item_t *ci)
2885 {
2886         oconfig_item_t *item;
2887         _Bool is_vfiler = 0;
2888         int status;
2889
2890         if (! strcasecmp (ci->key, "VFiler"))
2891                 is_vfiler = 1;
2892
2893         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2894                 WARNING ("netapp plugin: \"%s\" needs exactly one string argument. Ignoring host block.", ci->key);
2895                 return (1);
2896         }
2897
2898         status = cf_util_get_string (ci, &host->name);
2899         if (status != 0)
2900                 return (1);
2901
2902         for (int i = 0; i < ci->children_num; ++i) {
2903                 item = ci->children + i;
2904
2905                 status = 0;
2906
2907                 if (!strcasecmp(item->key, "Address")) {
2908                         status = cf_util_get_string (item, &host->host);
2909                 } else if (!strcasecmp(item->key, "Port")) {
2910                         int tmp;
2911
2912                         tmp = cf_util_get_port_number (item);
2913                         if (tmp > 0)
2914                                 host->port = tmp;
2915                 } else if (!strcasecmp(item->key, "Protocol")) {
2916                         if ((item->values_num != 1) || (item->values[0].type != OCONFIG_TYPE_STRING) || (strcasecmp(item->values[0].value.string, "http") && strcasecmp(item->values[0].value.string, "https"))) {
2917                                 WARNING("netapp plugin: \"Protocol\" needs to be either \"http\" or \"https\". Ignoring host block \"%s\".", ci->values[0].value.string);
2918                                 return (1);
2919                         }
2920                         if (!strcasecmp(item->values[0].value.string, "http")) host->protocol = NA_SERVER_TRANSPORT_HTTP;
2921                         else host->protocol = NA_SERVER_TRANSPORT_HTTPS;
2922                 } else if (!strcasecmp(item->key, "User")) {
2923                         status = cf_util_get_string (item, &host->username);
2924                 } else if (!strcasecmp(item->key, "Password")) {
2925                         status = cf_util_get_string (item, &host->password);
2926                 } else if (!strcasecmp(item->key, "Interval")) {
2927                         status = cf_util_get_cdtime (item, &host->interval);
2928                 } else if (!strcasecmp(item->key, "WAFL")) {
2929                         cna_config_wafl(host, item);
2930                 } else if (!strcasecmp(item->key, "Disks")) {
2931                         cna_config_disk(host, item);
2932                 } else if (!strcasecmp(item->key, "VolumePerf")) {
2933                         cna_config_volume_performance(host, item);
2934                 } else if (!strcasecmp(item->key, "VolumeUsage")) {
2935                         cna_config_volume_usage(host, item);
2936                 } else if (!strcasecmp(item->key, "Quota")) {
2937                         cna_config_quota(host, item);
2938                 } else if (!strcasecmp(item->key, "SnapVault")) {
2939                         cna_config_snapvault(host, item);
2940                 } else if (!strcasecmp(item->key, "System")) {
2941                         cna_config_system(host, item);
2942                 } else if ((!strcasecmp(item->key, "VFiler")) && (! is_vfiler)) {
2943                         host_config_t *vfiler;
2944
2945                         vfiler = cna_shallow_clone_host (host);
2946                         if (! vfiler) {
2947                                 ERROR ("netapp plugin: Failed to allocate host object for vfiler.");
2948                                 continue;
2949                         }
2950
2951                         if (cna_config_host (vfiler, item)) {
2952                                 free_host_config (vfiler);
2953                                 continue;
2954                         }
2955
2956                         cna_register_host (vfiler);
2957                 } else if ((!strcasecmp(item->key, "VFilerName")) && is_vfiler) {
2958                         status = cf_util_get_string (item, &host->vfiler);
2959                 } else {
2960                         WARNING ("netapp plugin: Ignoring unknown config option \"%s\" in %s block \"%s\".",
2961                                         item->key, is_vfiler ? "vfiler" : "host", ci->values[0].value.string);
2962                 }
2963
2964                 if (status != 0)
2965                         break;
2966         }
2967
2968         if (host->host == NULL)
2969                 host->host = strdup (host->name);
2970
2971         if (is_vfiler && (! host->vfiler))
2972                 host->vfiler = strdup (host->name);
2973
2974         if (host->host == NULL)
2975                 status = -1;
2976
2977         if (host->port <= 0)
2978                 host->port = (host->protocol == NA_SERVER_TRANSPORT_HTTP) ? 80 : 443;
2979
2980         if ((host->username == NULL) || (host->password == NULL)) {
2981                 WARNING("netapp plugin: Please supply login information for host \"%s\". "
2982                                 "Ignoring host block.", host->name);
2983                 status = -1;
2984         }
2985
2986         if (status != 0)
2987                 return status;
2988
2989         return (0);
2990 } /* }}} host_config_t *cna_config_host */
2991
2992 /*
2993  * Callbacks registered with the daemon
2994  *
2995  * Pretty standard stuff here.
2996  */
2997 static int cna_init_host (host_config_t *host) /* {{{ */
2998 {
2999         /* Request version 1.1 of the ONTAP API */
3000         int major_version = 1, minor_version = 1;
3001
3002         if (host == NULL)
3003                 return (EINVAL);
3004
3005         if (host->srv != NULL)
3006                 return (0);
3007
3008         if (host->vfiler != NULL) /* Request version 1.7 of the ONTAP API */
3009                 minor_version = 7;
3010
3011         host->srv = na_server_open (host->host, major_version, minor_version);
3012         if (host->srv == NULL) {
3013                 ERROR ("netapp plugin: na_server_open (%s) failed.", host->host);
3014                 return (-1);
3015         }
3016
3017         na_server_set_transport_type(host->srv, host->protocol,
3018                         /* transportarg = */ NULL);
3019         na_server_set_port(host->srv, host->port);
3020         na_server_style(host->srv, NA_STYLE_LOGIN_PASSWORD);
3021         na_server_adminuser(host->srv, host->username, host->password);
3022         na_server_set_timeout(host->srv, 5 /* seconds */);
3023
3024         if (host->vfiler != NULL) {
3025                 if (! na_server_set_vfiler (host->srv, host->vfiler)) {
3026                         ERROR ("netapp plugin: Failed to connect to VFiler '%s' on host '%s'.",
3027                                         host->vfiler, host->host);
3028                         return (-1);
3029                 }
3030                 else {
3031                         INFO ("netapp plugin: Connected to VFiler '%s' on host '%s'.",
3032                                         host->vfiler, host->host);
3033                 }
3034         }
3035
3036         return (0);
3037 } /* }}} int cna_init_host */
3038
3039 static int cna_init (void) /* {{{ */
3040 {
3041         char err[256] = { 0 };
3042
3043         if (!na_startup(err, sizeof(err))) {
3044                 err[sizeof (err) - 1] = 0;
3045                 ERROR("netapp plugin: Error initializing netapp API: %s", err);
3046                 return 1;
3047         }
3048
3049         return (0);
3050 } /* }}} cna_init */
3051
3052 static int cna_read_internal (host_config_t *host) { /* {{{ */
3053         int status;
3054
3055         status = cna_query_wafl (host);
3056         if (status != 0)
3057                 return (status);
3058
3059         status = cna_query_disk (host);
3060         if (status != 0)
3061                 return (status);
3062
3063         status = cna_query_volume_perf (host);
3064         if (status != 0)
3065                 return (status);
3066
3067         status = cna_query_volume_usage (host);
3068         if (status != 0)
3069                 return (status);
3070
3071         status = cna_query_quota (host);
3072         if (status != 0)
3073                 return (status);
3074
3075         status = cna_query_snapvault (host);
3076         if (status != 0)
3077                 return (status);
3078
3079         status = cna_query_system (host);
3080         if (status != 0)
3081                 return (status);
3082
3083         return 0;
3084 } /* }}} int cna_read_internal */
3085
3086 static int cna_read (user_data_t *ud) { /* {{{ */
3087         host_config_t *host;
3088         int status;
3089
3090         if ((ud == NULL) || (ud->data == NULL))
3091                 return (-1);
3092
3093         host = ud->data;
3094
3095         status = cna_init_host (host);
3096         if (status != 0)
3097                 return (status);
3098
3099         status = cna_read_internal (host);
3100         if (status != 0)
3101         {
3102                 if (host->srv != NULL)
3103                         na_server_close (host->srv);
3104                 host->srv = NULL;
3105         }
3106
3107         return 0;
3108 } /* }}} int cna_read */
3109
3110 static int cna_config (oconfig_item_t *ci) { /* {{{ */
3111         oconfig_item_t *item;
3112
3113         for (int i = 0; i < ci->children_num; ++i) {
3114                 item = ci->children + i;
3115
3116                 if (strcasecmp(item->key, "Host") == 0)
3117                 {
3118                         host_config_t *host;
3119
3120                         host = cna_alloc_host ();
3121                         if (host == NULL) {
3122                                 ERROR ("netapp plugin: Failed to allocate host object.");
3123                                 continue;
3124                         }
3125
3126                         if (cna_config_host (host, item) != 0) {
3127                                 free_host_config (host);
3128                                 continue;
3129                         }
3130
3131                         cna_register_host (host);
3132                 }
3133                 else /* if (item->key != "Host") */
3134                 {
3135                         WARNING("netapp plugin: Ignoring unknown config option \"%s\".", item->key);
3136                 }
3137         }
3138         return 0;
3139 } /* }}} int cna_config */
3140
3141 static int cna_shutdown (void) /* {{{ */
3142 {
3143         /* Clean up system resources and stuff. */
3144         na_shutdown ();
3145
3146         return (0);
3147 } /* }}} int cna_shutdown */
3148
3149 void module_register(void) {
3150         plugin_register_complex_config("netapp", cna_config);
3151         plugin_register_init("netapp", cna_init);
3152         plugin_register_shutdown("netapp", cna_shutdown);
3153 }
3154
3155 /* vim: set sw=2 ts=2 noet fdm=marker : */