Merge remote-tracking branch 'github/pr/2058'
[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         sstrncpy (vl.plugin, "netapp", sizeof (vl.plugin));
637         if (plugin_inst != NULL)
638                 sstrncpy (vl.plugin_instance, plugin_inst, sizeof (vl.plugin_instance));
639         sstrncpy (vl.type, type, sizeof (vl.type));
640         if (type_inst != NULL)
641                 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
642
643         return (plugin_dispatch_values (&vl));
644 } /* }}} int submit_uint64 */
645
646 static int submit_two_derive (const char *host, const char *plugin_inst, /* {{{ */
647                 const char *type, const char *type_inst, derive_t val0, derive_t val1,
648                 cdtime_t timestamp, cdtime_t interval)
649 {
650         value_t values[] = {
651                 { .derive = val0 },
652                 { .derive = val1 },
653         };
654
655         return (submit_values (host, plugin_inst, type, type_inst,
656                                 values, STATIC_ARRAY_SIZE (values), timestamp, interval));
657 } /* }}} int submit_two_derive */
658
659 static int submit_derive (const char *host, const char *plugin_inst, /* {{{ */
660                 const char *type, const char *type_inst, derive_t counter,
661                 cdtime_t timestamp, cdtime_t interval)
662 {
663         return (submit_values (host, plugin_inst, type, type_inst,
664                                 &(value_t) { .derive = counter }, 1, timestamp, interval));
665 } /* }}} int submit_derive */
666
667 static int submit_two_gauge (const char *host, const char *plugin_inst, /* {{{ */
668                 const char *type, const char *type_inst, gauge_t val0, gauge_t val1,
669                 cdtime_t timestamp, cdtime_t interval)
670 {
671         value_t values[] = {
672                 { .gauge = val0 },
673                 { .gauge = val1 },
674         };
675
676         return (submit_values (host, plugin_inst, type, type_inst,
677                                 values, STATIC_ARRAY_SIZE (values), timestamp, interval));
678 } /* }}} int submit_two_gauge */
679
680 static int submit_double (const char *host, const char *plugin_inst, /* {{{ */
681                 const char *type, const char *type_inst, double d,
682                 cdtime_t timestamp, cdtime_t interval)
683 {
684         return (submit_values (host, plugin_inst, type, type_inst,
685                                 &(value_t) { .gauge = counter }, 1, timestamp, interval));
686 } /* }}} int submit_uint64 */
687
688 /* Calculate hit ratio from old and new counters and submit the resulting
689  * percentage. Used by "submit_wafl_data". */
690 static int submit_cache_ratio (const char *host, /* {{{ */
691                 const char *plugin_inst,
692                 const char *type_inst,
693                 uint64_t new_hits,
694                 uint64_t new_misses,
695                 uint64_t old_hits,
696                 uint64_t old_misses,
697                 cdtime_t timestamp,
698                 cdtime_t interval)
699 {
700         value_t v = { .gauge = NAN };
701
702         if ((new_hits >= old_hits) && (new_misses >= old_misses)) {
703                 uint64_t hits;
704                 uint64_t misses;
705
706                 hits = new_hits - old_hits;
707                 misses = new_misses - old_misses;
708
709                 v.gauge = 100.0 * ((gauge_t) hits) / ((gauge_t) (hits + misses));
710         }
711
712         return (submit_values (host, plugin_inst, "cache_ratio", type_inst,
713                                 &v, 1, timestamp, interval));
714 } /* }}} int submit_cache_ratio */
715
716 /* Submits all the caches used by WAFL. Uses "submit_cache_ratio". */
717 static int submit_wafl_data (const char *hostname, const char *instance, /* {{{ */
718                 cfg_wafl_t *old_data, const cfg_wafl_t *new_data, cdtime_t interval)
719 {
720         /* Submit requested counters */
721         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_NAME_CACHE | HAVE_WAFL_NAME_CACHE)
722                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_NAME_CACHE))
723                 submit_cache_ratio (hostname, instance, "name_cache_hit",
724                                 new_data->name_cache_hit, new_data->name_cache_miss,
725                                 old_data->name_cache_hit, old_data->name_cache_miss,
726                                 new_data->timestamp, interval);
727
728         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_DIR_CACHE | HAVE_WAFL_FIND_DIR)
729                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_FIND_DIR))
730                 submit_cache_ratio (hostname, instance, "find_dir_hit",
731                                 new_data->find_dir_hit, new_data->find_dir_miss,
732                                 old_data->find_dir_hit, old_data->find_dir_miss,
733                                 new_data->timestamp, interval);
734
735         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_BUF_CACHE | HAVE_WAFL_BUF_HASH)
736                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_BUF_HASH))
737                 submit_cache_ratio (hostname, instance, "buf_hash_hit",
738                                 new_data->buf_hash_hit, new_data->buf_hash_miss,
739                                 old_data->buf_hash_hit, old_data->buf_hash_miss,
740                                 new_data->timestamp, interval);
741
742         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_INODE_CACHE | HAVE_WAFL_INODE_CACHE)
743                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_INODE_CACHE))
744                 submit_cache_ratio (hostname, instance, "inode_cache_hit",
745                                 new_data->inode_cache_hit, new_data->inode_cache_miss,
746                                 old_data->inode_cache_hit, old_data->inode_cache_miss,
747                                 new_data->timestamp, interval);
748
749         /* Clear old HAVE_* flags */
750         old_data->flags &= ~HAVE_WAFL_ALL;
751
752         /* Copy all counters */
753         old_data->timestamp        = new_data->timestamp;
754         old_data->name_cache_hit   = new_data->name_cache_hit;
755         old_data->name_cache_miss  = new_data->name_cache_miss;
756         old_data->find_dir_hit     = new_data->find_dir_hit;
757         old_data->find_dir_miss    = new_data->find_dir_miss;
758         old_data->buf_hash_hit     = new_data->buf_hash_hit;
759         old_data->buf_hash_miss    = new_data->buf_hash_miss;
760         old_data->inode_cache_hit  = new_data->inode_cache_hit;
761         old_data->inode_cache_miss = new_data->inode_cache_miss;
762
763         /* Copy HAVE_* flags */
764         old_data->flags |= (new_data->flags & HAVE_WAFL_ALL);
765
766         return (0);
767 } /* }}} int submit_wafl_data */
768
769 /* Submits volume performance data to the daemon, taking care to honor and
770  * update flags appropriately. */
771 static int submit_volume_perf_data (const char *hostname, /* {{{ */
772                 data_volume_perf_t *old_data,
773                 const data_volume_perf_t *new_data, int interval)
774 {
775         char plugin_instance[DATA_MAX_NAME_LEN];
776
777         if ((hostname == NULL) || (old_data == NULL) || (new_data == NULL))
778                 return (-1);
779
780         ssnprintf (plugin_instance, sizeof (plugin_instance),
781                         "volume-%s", old_data->name);
782
783         /* Check for and submit disk-octet values */
784         if (HAS_ALL_FLAGS (old_data->flags, CFG_VOLUME_PERF_IO)
785                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_BYTES_READ | HAVE_VOLUME_PERF_BYTES_WRITE))
786         {
787                 submit_two_derive (hostname, plugin_instance, "disk_octets", /* type instance = */ NULL,
788                                 (derive_t) new_data->read_bytes, (derive_t) new_data->write_bytes, new_data->timestamp, interval);
789         }
790
791         /* Check for and submit disk-operations values */
792         if (HAS_ALL_FLAGS (old_data->flags, CFG_VOLUME_PERF_OPS)
793                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE))
794         {
795                 submit_two_derive (hostname, plugin_instance, "disk_ops", /* type instance = */ NULL,
796                                 (derive_t) new_data->read_ops, (derive_t) new_data->write_ops, new_data->timestamp, interval);
797         }
798
799         /* Check for, calculate and submit disk-latency values */
800         if (HAS_ALL_FLAGS (old_data->flags, CFG_VOLUME_PERF_LATENCY
801                                 | HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
802                                 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE)
803                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
804                                 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE))
805         {
806                 gauge_t latency_per_op_read;
807                 gauge_t latency_per_op_write;
808
809                 latency_per_op_read = NAN;
810                 latency_per_op_write = NAN;
811
812                 /* Check if a counter wrapped around. */
813                 if ((new_data->read_ops > old_data->read_ops)
814                                 && (new_data->read_latency > old_data->read_latency))
815                 {
816                         uint64_t diff_ops_read;
817                         uint64_t diff_latency_read;
818
819                         diff_ops_read = new_data->read_ops - old_data->read_ops;
820                         diff_latency_read = new_data->read_latency - old_data->read_latency;
821
822                         if (diff_ops_read > 0)
823                                 latency_per_op_read = ((gauge_t) diff_latency_read) / ((gauge_t) diff_ops_read);
824                 }
825
826                 if ((new_data->write_ops > old_data->write_ops)
827                                 && (new_data->write_latency > old_data->write_latency))
828                 {
829                         uint64_t diff_ops_write;
830                         uint64_t diff_latency_write;
831
832                         diff_ops_write = new_data->write_ops - old_data->write_ops;
833                         diff_latency_write = new_data->write_latency - old_data->write_latency;
834
835                         if (diff_ops_write > 0)
836                                 latency_per_op_write = ((gauge_t) diff_latency_write) / ((gauge_t) diff_ops_write);
837                 }
838
839                 submit_two_gauge (hostname, plugin_instance, "disk_latency", /* type instance = */ NULL,
840                                 latency_per_op_read, latency_per_op_write, new_data->timestamp, interval);
841         }
842
843         /* Clear all HAVE_* flags. */
844         old_data->flags &= ~HAVE_VOLUME_PERF_ALL;
845
846         /* Copy all counters */
847         old_data->timestamp = new_data->timestamp;
848         old_data->read_bytes = new_data->read_bytes;
849         old_data->write_bytes = new_data->write_bytes;
850         old_data->read_ops = new_data->read_ops;
851         old_data->write_ops = new_data->write_ops;
852         old_data->read_latency = new_data->read_latency;
853         old_data->write_latency = new_data->write_latency;
854
855         /* Copy the HAVE_* flags */
856         old_data->flags |= (new_data->flags & HAVE_VOLUME_PERF_ALL);
857
858         return (0);
859 } /* }}} int submit_volume_perf_data */
860
861 static cdtime_t cna_child_get_cdtime (na_elem_t *data) /* {{{ */
862 {
863         time_t t;
864
865         t = (time_t) na_child_get_uint64 (data, "timestamp", /* default = */ 0);
866
867         return (TIME_T_TO_CDTIME_T (t));
868 } /* }}} cdtime_t cna_child_get_cdtime */
869
870
871 /*
872  * Query functions
873  *
874  * These functions are called with appropriate data returned by the libnetapp
875  * interface which is parsed and submitted with the above functions.
876  */
877 /* Data corresponding to <WAFL /> */
878 static int cna_handle_wafl_data (const char *hostname, cfg_wafl_t *cfg_wafl, /* {{{ */
879                 na_elem_t *data, cdtime_t interval)
880 {
881         cfg_wafl_t perf_data = { 0 };
882         const char *plugin_inst;
883
884         na_elem_t *instances;
885         na_elem_iter_t counter_iter;
886
887         perf_data.timestamp = cna_child_get_cdtime (data);
888
889         instances = na_elem_child(na_elem_child (data, "instances"), "instance-data");
890         if (instances == NULL)
891         {
892                 ERROR ("netapp plugin: cna_handle_wafl_data: "
893                                 "na_elem_child (\"instances\") failed "
894                                 "for host %s.", hostname);
895                 return (-1);
896         }
897
898         plugin_inst = na_child_get_string(instances, "name");
899         if (plugin_inst == NULL)
900         {
901                 ERROR ("netapp plugin: cna_handle_wafl_data: "
902                                 "na_child_get_string (\"name\") failed "
903                                 "for host %s.", hostname);
904                 return (-1);
905         }
906
907         /* Iterate over all counters */
908         counter_iter = na_child_iterator (na_elem_child (instances, "counters"));
909         for (na_elem_t *counter = na_iterator_next (&counter_iter);
910                         counter != NULL;
911                         counter = na_iterator_next (&counter_iter))
912         {
913                 const char *name;
914                 uint64_t value;
915
916                 name = na_child_get_string(counter, "name");
917                 if (name == NULL)
918                         continue;
919
920                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
921                 if (value == UINT64_MAX)
922                         continue;
923
924                 if (!strcmp(name, "name_cache_hit")) {
925                         perf_data.name_cache_hit = value;
926                         perf_data.flags |= HAVE_WAFL_NAME_CACHE_HIT;
927                 } else if (!strcmp(name, "name_cache_miss")) {
928                         perf_data.name_cache_miss = value;
929                         perf_data.flags |= HAVE_WAFL_NAME_CACHE_MISS;
930                 } else if (!strcmp(name, "find_dir_hit")) {
931                         perf_data.find_dir_hit = value;
932                         perf_data.flags |= HAVE_WAFL_FIND_DIR_HIT;
933                 } else if (!strcmp(name, "find_dir_miss")) {
934                         perf_data.find_dir_miss = value;
935                         perf_data.flags |= HAVE_WAFL_FIND_DIR_MISS;
936                 } else if (!strcmp(name, "buf_hash_hit")) {
937                         perf_data.buf_hash_hit = value;
938                         perf_data.flags |= HAVE_WAFL_BUF_HASH_HIT;
939                 } else if (!strcmp(name, "buf_hash_miss")) {
940                         perf_data.buf_hash_miss = value;
941                         perf_data.flags |= HAVE_WAFL_BUF_HASH_MISS;
942                 } else if (!strcmp(name, "inode_cache_hit")) {
943                         perf_data.inode_cache_hit = value;
944                         perf_data.flags |= HAVE_WAFL_INODE_CACHE_HIT;
945                 } else if (!strcmp(name, "inode_cache_miss")) {
946                         perf_data.inode_cache_miss = value;
947                         perf_data.flags |= HAVE_WAFL_INODE_CACHE_MISS;
948                 } else {
949                         DEBUG("netapp plugin: cna_handle_wafl_data: "
950                                         "Found unexpected child: %s "
951                                         "for host %s.", name, hostname);
952                 }
953         }
954
955         return (submit_wafl_data (hostname, plugin_inst, cfg_wafl, &perf_data, interval));
956 } /* }}} void cna_handle_wafl_data */
957
958 static int cna_setup_wafl (cfg_wafl_t *cw) /* {{{ */
959 {
960         na_elem_t *e;
961
962         if (cw == NULL)
963                 return (EINVAL);
964
965         if (cw->query != NULL)
966                 return (0);
967
968         cw->query = na_elem_new("perf-object-get-instances");
969         if (cw->query == NULL)
970         {
971                 ERROR ("netapp plugin: na_elem_new failed.");
972                 return (-1);
973         }
974         na_child_add_string (cw->query, "objectname", "wafl");
975
976         e = na_elem_new("counters");
977         if (e == NULL)
978         {
979                 na_elem_free (cw->query);
980                 cw->query = NULL;
981                 ERROR ("netapp plugin: na_elem_new failed.");
982                 return (-1);
983         }
984         na_child_add_string(e, "counter", "name_cache_hit");
985         na_child_add_string(e, "counter", "name_cache_miss");
986         na_child_add_string(e, "counter", "find_dir_hit");
987         na_child_add_string(e, "counter", "find_dir_miss");
988         na_child_add_string(e, "counter", "buf_hash_hit");
989         na_child_add_string(e, "counter", "buf_hash_miss");
990         na_child_add_string(e, "counter", "inode_cache_hit");
991         na_child_add_string(e, "counter", "inode_cache_miss");
992
993         na_child_add(cw->query, e);
994
995         return (0);
996 } /* }}} int cna_setup_wafl */
997
998 static int cna_query_wafl (host_config_t *host) /* {{{ */
999 {
1000         na_elem_t *data;
1001         int status;
1002         cdtime_t now;
1003
1004         if (host == NULL)
1005                 return (EINVAL);
1006
1007         /* If WAFL was not configured, return without doing anything. */
1008         if (host->cfg_wafl == NULL)
1009                 return (0);
1010
1011         now = cdtime ();
1012         if ((host->cfg_wafl->interval.interval + host->cfg_wafl->interval.last_read) > now)
1013                 return (0);
1014
1015         status = cna_setup_wafl (host->cfg_wafl);
1016         if (status != 0)
1017                 return (status);
1018         assert (host->cfg_wafl->query != NULL);
1019
1020         data = na_server_invoke_elem(host->srv, host->cfg_wafl->query);
1021         if (na_results_status (data) != NA_OK)
1022         {
1023                 ERROR ("netapp plugin: cna_query_wafl: na_server_invoke_elem failed for host %s: %s",
1024                                 host->name, na_results_reason (data));
1025                 na_elem_free (data);
1026                 return (-1);
1027         }
1028
1029         status = cna_handle_wafl_data (host->name, host->cfg_wafl, data,
1030                         host->cfg_wafl->interval.interval);
1031
1032         if (status == 0)
1033                 host->cfg_wafl->interval.last_read = now;
1034
1035         na_elem_free (data);
1036         return (status);
1037 } /* }}} int cna_query_wafl */
1038
1039 /* Data corresponding to <Disks /> */
1040 static int cna_handle_disk_data (const char *hostname, /* {{{ */
1041                 cfg_disk_t *cfg_disk, na_elem_t *data, cdtime_t interval)
1042 {
1043         cdtime_t timestamp;
1044         na_elem_t *instances;
1045         na_elem_iter_t instance_iter;
1046         disk_t *worst_disk = NULL;
1047
1048         if ((cfg_disk == NULL) || (data == NULL))
1049                 return (EINVAL);
1050
1051         timestamp = cna_child_get_cdtime (data);
1052
1053         instances = na_elem_child (data, "instances");
1054         if (instances == NULL)
1055         {
1056                 ERROR ("netapp plugin: cna_handle_disk_data: "
1057                                 "na_elem_child (\"instances\") failed "
1058                                 "for host %s.", hostname);
1059                 return (-1);
1060         }
1061
1062         /* Iterate over all children */
1063         instance_iter = na_child_iterator (instances);
1064         for (na_elem_t *instance = na_iterator_next (&instance_iter);
1065                         instance != NULL;
1066                         instance = na_iterator_next(&instance_iter))
1067         {
1068                 disk_t *old_data;
1069                 disk_t  new_data = { 0 };
1070
1071                 na_elem_iter_t counter_iterator;
1072
1073                 new_data.timestamp = timestamp;
1074                 new_data.disk_busy_percent = NAN;
1075
1076                 old_data = get_disk(cfg_disk, na_child_get_string (instance, "name"));
1077                 if (old_data == NULL)
1078                         continue;
1079
1080                 /* Look for the "disk_busy" and "base_for_disk_busy" counters */
1081                 counter_iterator = na_child_iterator(na_elem_child(instance, "counters"));
1082                 for (na_elem_t *counter = na_iterator_next(&counter_iterator);
1083                                 counter != NULL;
1084                                 counter = na_iterator_next(&counter_iterator))
1085                 {
1086                         const char *name;
1087                         uint64_t value;
1088
1089                         name = na_child_get_string(counter, "name");
1090                         if (name == NULL)
1091                                 continue;
1092
1093                         value = na_child_get_uint64(counter, "value", UINT64_MAX);
1094                         if (value == UINT64_MAX)
1095                                 continue;
1096
1097                         if (strcmp(name, "disk_busy") == 0)
1098                         {
1099                                 new_data.disk_busy = value;
1100                                 new_data.flags |= HAVE_DISK_BUSY;
1101                         }
1102                         else if (strcmp(name, "base_for_disk_busy") == 0)
1103                         {
1104                                 new_data.base_for_disk_busy = value;
1105                                 new_data.flags |= HAVE_DISK_BASE;
1106                         }
1107                         else
1108                         {
1109                                 DEBUG ("netapp plugin: cna_handle_disk_data: "
1110                                                 "Counter not handled: %s = %"PRIu64,
1111                                                 name, value);
1112                         }
1113                 }
1114
1115                 /* If all required counters are available and did not just wrap around,
1116                  * calculate the busy percentage. Otherwise, the value is initialized to
1117                  * NAN at the top of the for-loop. */
1118                 if (HAS_ALL_FLAGS (old_data->flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
1119                                 && HAS_ALL_FLAGS (new_data.flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
1120                                 && (new_data.disk_busy >= old_data->disk_busy)
1121                                 && (new_data.base_for_disk_busy > old_data->base_for_disk_busy))
1122                 {
1123                         uint64_t busy_diff;
1124                         uint64_t base_diff;
1125
1126                         busy_diff = new_data.disk_busy - old_data->disk_busy;
1127                         base_diff = new_data.base_for_disk_busy - old_data->base_for_disk_busy;
1128
1129                         new_data.disk_busy_percent = 100.0
1130                                 * ((gauge_t) busy_diff) / ((gauge_t) base_diff);
1131                 }
1132
1133                 /* Clear HAVE_* flags */
1134                 old_data->flags &= ~HAVE_DISK_ALL;
1135
1136                 /* Copy data */
1137                 old_data->timestamp = new_data.timestamp;
1138                 old_data->disk_busy = new_data.disk_busy;
1139                 old_data->base_for_disk_busy = new_data.base_for_disk_busy;
1140                 old_data->disk_busy_percent = new_data.disk_busy_percent;
1141
1142                 /* Copy flags */
1143                 old_data->flags |= (new_data.flags & HAVE_DISK_ALL);
1144
1145                 if ((worst_disk == NULL)
1146                                 || (worst_disk->disk_busy_percent < old_data->disk_busy_percent))
1147                         worst_disk = old_data;
1148         } /* for (all disks) */
1149
1150         if ((cfg_disk->flags & CFG_DISK_BUSIEST) && (worst_disk != NULL))
1151                 submit_double (hostname, "system", "percent", "disk_busy",
1152                                 worst_disk->disk_busy_percent, timestamp, interval);
1153
1154         return (0);
1155 } /* }}} int cna_handle_disk_data */
1156
1157 static int cna_setup_disk (cfg_disk_t *cd) /* {{{ */
1158 {
1159         na_elem_t *e;
1160
1161         if (cd == NULL)
1162                 return (EINVAL);
1163
1164         if (cd->query != NULL)
1165                 return (0);
1166
1167         cd->query = na_elem_new ("perf-object-get-instances");
1168         if (cd->query == NULL)
1169         {
1170                 ERROR ("netapp plugin: na_elem_new failed.");
1171                 return (-1);
1172         }
1173         na_child_add_string (cd->query, "objectname", "disk");
1174
1175         e = na_elem_new("counters");
1176         if (e == NULL)
1177         {
1178                 na_elem_free (cd->query);
1179                 cd->query = NULL;
1180                 ERROR ("netapp plugin: na_elem_new failed.");
1181                 return (-1);
1182         }
1183         na_child_add_string(e, "counter", "disk_busy");
1184         na_child_add_string(e, "counter", "base_for_disk_busy");
1185         na_child_add(cd->query, e);
1186
1187         return (0);
1188 } /* }}} int cna_setup_disk */
1189
1190 static int cna_query_disk (host_config_t *host) /* {{{ */
1191 {
1192         na_elem_t *data;
1193         int status;
1194         cdtime_t now;
1195
1196         if (host == NULL)
1197                 return (EINVAL);
1198
1199         /* If the user did not configure disk statistics, return without doing
1200          * anything. */
1201         if (host->cfg_disk == NULL)
1202                 return (0);
1203
1204         now = cdtime ();
1205         if ((host->cfg_disk->interval.interval + host->cfg_disk->interval.last_read) > now)
1206                 return (0);
1207
1208         status = cna_setup_disk (host->cfg_disk);
1209         if (status != 0)
1210                 return (status);
1211         assert (host->cfg_disk->query != NULL);
1212
1213         data = na_server_invoke_elem(host->srv, host->cfg_disk->query);
1214         if (na_results_status (data) != NA_OK)
1215         {
1216                 ERROR ("netapp plugin: cna_query_disk: na_server_invoke_elem failed for host %s: %s",
1217                                 host->name, na_results_reason (data));
1218                 na_elem_free (data);
1219                 return (-1);
1220         }
1221
1222         status = cna_handle_disk_data (host->name, host->cfg_disk, data,
1223                         host->cfg_disk->interval.interval);
1224
1225         if (status == 0)
1226                 host->cfg_disk->interval.last_read = now;
1227
1228         na_elem_free (data);
1229         return (status);
1230 } /* }}} int cna_query_disk */
1231
1232 /* Data corresponding to <VolumePerf /> */
1233 static int cna_handle_volume_perf_data (const char *hostname, /* {{{ */
1234                 cfg_volume_perf_t *cvp, na_elem_t *data, cdtime_t interval)
1235 {
1236         cdtime_t timestamp;
1237         na_elem_t *elem_instances;
1238         na_elem_iter_t iter_instances;
1239
1240         timestamp = cna_child_get_cdtime (data);
1241
1242         elem_instances = na_elem_child(data, "instances");
1243         if (elem_instances == NULL)
1244         {
1245                 ERROR ("netapp plugin: handle_volume_perf_data: "
1246                                 "na_elem_child (\"instances\") failed "
1247                                 "for host %s.", hostname);
1248                 return (-1);
1249         }
1250
1251         iter_instances = na_child_iterator (elem_instances);
1252         for (na_elem_t *elem_instance = na_iterator_next(&iter_instances);
1253                         elem_instance != NULL;
1254                         elem_instance = na_iterator_next(&iter_instances))
1255         {
1256                 const char *name;
1257
1258                 data_volume_perf_t perf_data = { 0 };
1259                 data_volume_perf_t *v;
1260
1261                 na_elem_t *elem_counters;
1262                 na_elem_iter_t iter_counters;
1263
1264                 perf_data.timestamp = timestamp;
1265
1266                 name = na_child_get_string (elem_instance, "name");
1267                 if (name == NULL)
1268                         continue;
1269
1270                 /* get_volume_perf may return NULL if this volume is to be ignored. */
1271                 v = get_volume_perf (cvp, name);
1272                 if (v == NULL)
1273                         continue;
1274
1275                 elem_counters = na_elem_child (elem_instance, "counters");
1276                 if (elem_counters == NULL)
1277                         continue;
1278
1279                 iter_counters = na_child_iterator (elem_counters);
1280                 for (na_elem_t *elem_counter = na_iterator_next(&iter_counters);
1281                                 elem_counter != NULL;
1282                                 elem_counter = na_iterator_next(&iter_counters))
1283                 {
1284                         const char *name;
1285                         uint64_t value;
1286
1287                         name = na_child_get_string (elem_counter, "name");
1288                         if (name == NULL)
1289                                 continue;
1290
1291                         value = na_child_get_uint64 (elem_counter, "value", UINT64_MAX);
1292                         if (value == UINT64_MAX)
1293                                 continue;
1294
1295                         if (!strcmp(name, "read_data")) {
1296                                 perf_data.read_bytes = value;
1297                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_READ;
1298                         } else if (!strcmp(name, "write_data")) {
1299                                 perf_data.write_bytes = value;
1300                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_WRITE;
1301                         } else if (!strcmp(name, "read_ops")) {
1302                                 perf_data.read_ops = value;
1303                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_READ;
1304                         } else if (!strcmp(name, "write_ops")) {
1305                                 perf_data.write_ops = value;
1306                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_WRITE;
1307                         } else if (!strcmp(name, "read_latency")) {
1308                                 perf_data.read_latency = value;
1309                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_READ;
1310                         } else if (!strcmp(name, "write_latency")) {
1311                                 perf_data.write_latency = value;
1312                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_WRITE;
1313                         }
1314                 } /* for (elem_counter) */
1315
1316                 submit_volume_perf_data (hostname, v, &perf_data, interval);
1317         } /* for (volume) */
1318
1319         return (0);
1320 } /* }}} int cna_handle_volume_perf_data */
1321
1322 static int cna_setup_volume_perf (cfg_volume_perf_t *cd) /* {{{ */
1323 {
1324         na_elem_t *e;
1325
1326         if (cd == NULL)
1327                 return (EINVAL);
1328
1329         if (cd->query != NULL)
1330                 return (0);
1331
1332         cd->query = na_elem_new ("perf-object-get-instances");
1333         if (cd->query == NULL)
1334         {
1335                 ERROR ("netapp plugin: na_elem_new failed.");
1336                 return (-1);
1337         }
1338         na_child_add_string (cd->query, "objectname", "volume");
1339
1340         e = na_elem_new("counters");
1341         if (e == NULL)
1342         {
1343                 na_elem_free (cd->query);
1344                 cd->query = NULL;
1345                 ERROR ("netapp plugin: na_elem_new failed.");
1346                 return (-1);
1347         }
1348         na_child_add_string(e, "counter", "read_ops");
1349         na_child_add_string(e, "counter", "write_ops");
1350         na_child_add_string(e, "counter", "read_data");
1351         na_child_add_string(e, "counter", "write_data");
1352         na_child_add_string(e, "counter", "read_latency");
1353         na_child_add_string(e, "counter", "write_latency");
1354         na_child_add(cd->query, e);
1355
1356         return (0);
1357 } /* }}} int cna_setup_volume_perf */
1358
1359 static int cna_query_volume_perf (host_config_t *host) /* {{{ */
1360 {
1361         na_elem_t *data;
1362         int status;
1363         cdtime_t now;
1364
1365         if (host == NULL)
1366                 return (EINVAL);
1367
1368         /* If the user did not configure volume performance statistics, return
1369          * without doing anything. */
1370         if (host->cfg_volume_perf == NULL)
1371                 return (0);
1372
1373         now = cdtime ();
1374         if ((host->cfg_volume_perf->interval.interval + host->cfg_volume_perf->interval.last_read) > now)
1375                 return (0);
1376
1377         status = cna_setup_volume_perf (host->cfg_volume_perf);
1378         if (status != 0)
1379                 return (status);
1380         assert (host->cfg_volume_perf->query != NULL);
1381
1382         data = na_server_invoke_elem (host->srv, host->cfg_volume_perf->query);
1383         if (na_results_status (data) != NA_OK)
1384         {
1385                 ERROR ("netapp plugin: cna_query_volume_perf: na_server_invoke_elem failed for host %s: %s",
1386                                 host->name, na_results_reason (data));
1387                 na_elem_free (data);
1388                 return (-1);
1389         }
1390
1391         status = cna_handle_volume_perf_data (host->name, host->cfg_volume_perf, data,
1392                         host->cfg_volume_perf->interval.interval);
1393
1394         if (status == 0)
1395                 host->cfg_volume_perf->interval.last_read = now;
1396
1397         na_elem_free (data);
1398         return (status);
1399 } /* }}} int cna_query_volume_perf */
1400
1401 /* Data corresponding to <VolumeUsage /> */
1402 static int cna_submit_volume_usage_data (const char *hostname, /* {{{ */
1403                 cfg_volume_usage_t *cfg_volume, int interval)
1404 {
1405         for (data_volume_usage_t *v = cfg_volume->volumes; v != NULL; v = v->next)
1406         {
1407                 char plugin_instance[DATA_MAX_NAME_LEN];
1408
1409                 uint64_t norm_used = v->norm_used;
1410                 uint64_t norm_free = v->norm_free;
1411                 uint64_t sis_saved = v->sis_saved;
1412                 uint64_t compress_saved = v->compress_saved;
1413                 uint64_t dedup_saved = v->dedup_saved;
1414                 uint64_t snap_reserve_used = 0;
1415                 uint64_t snap_reserve_free = v->snap_reserved;
1416                 uint64_t snap_norm_used = v->snap_used;
1417
1418                 ssnprintf (plugin_instance, sizeof (plugin_instance),
1419                                 "volume-%s", v->name);
1420
1421                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_USED | HAVE_VOLUME_USAGE_SNAP_RSVD)) {
1422                         if (v->snap_reserved > v->snap_used) {
1423                                 snap_reserve_free = v->snap_reserved - v->snap_used;
1424                                 snap_reserve_used = v->snap_used;
1425                                 snap_norm_used = 0;
1426                         } else {
1427                                 snap_reserve_free = 0;
1428                                 snap_reserve_used = v->snap_reserved;
1429                                 snap_norm_used = v->snap_used - v->snap_reserved;
1430                         }
1431                 }
1432
1433                 /* The space used by snapshots but not reserved for them is included in
1434                  * both, norm_used and snap_norm_used. If possible, subtract this here. */
1435                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_NORM_USED | HAVE_VOLUME_USAGE_SNAP_USED))
1436                 {
1437                         if (norm_used >= snap_norm_used)
1438                                 norm_used -= snap_norm_used;
1439                         else
1440                         {
1441                                 ERROR ("netapp plugin: (norm_used = %"PRIu64") < (snap_norm_used = "
1442                                                 "%"PRIu64") for host %s. Invalidating both.",
1443                                                 norm_used, snap_norm_used, hostname);
1444                                 v->flags &= ~(HAVE_VOLUME_USAGE_NORM_USED | HAVE_VOLUME_USAGE_SNAP_USED);
1445                         }
1446                 }
1447
1448                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_NORM_FREE))
1449                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1450                                         "df_complex", "free",
1451                                         (double) norm_free, /* timestamp = */ 0, interval);
1452
1453                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SIS_SAVED))
1454                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1455                                         "df_complex", "sis_saved",
1456                                         (double) sis_saved, /* timestamp = */ 0, interval);
1457
1458                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_COMPRESS_SAVED))
1459                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1460                                         "df_complex", "compression_saved",
1461                                         (double) compress_saved, /* timestamp = */ 0, interval);
1462
1463                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_DEDUP_SAVED))
1464                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1465                                         "df_complex", "dedup_saved",
1466                                         (double) dedup_saved, /* timestamp = */ 0, interval);
1467
1468                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_NORM_USED))
1469                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1470                                         "df_complex", "used",
1471                                         (double) norm_used, /* timestamp = */ 0, interval);
1472
1473                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_RSVD))
1474                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1475                                         "df_complex", "snap_reserved",
1476                                         (double) snap_reserve_free, /* timestamp = */ 0, interval);
1477
1478                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_USED | HAVE_VOLUME_USAGE_SNAP_RSVD))
1479                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1480                                         "df_complex", "snap_reserve_used",
1481                                         (double) snap_reserve_used, /* timestamp = */ 0, interval);
1482
1483                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_USED))
1484                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1485                                         "df_complex", "snap_normal_used",
1486                                         (double) snap_norm_used, /* timestamp = */ 0, interval);
1487
1488                 /* Clear all the HAVE_* flags */
1489                 v->flags &= ~HAVE_VOLUME_USAGE_ALL;
1490         } /* for (v = cfg_volume->volumes) */
1491
1492         return (0);
1493 } /* }}} int cna_submit_volume_usage_data */
1494
1495 /* Switch the state of a volume between online and offline and send out a
1496  * notification. */
1497 static int cna_change_volume_status (const char *hostname, /* {{{ */
1498                 data_volume_usage_t *v)
1499 {
1500         notification_t n = { 0 };
1501
1502         n.time = cdtime ();
1503         sstrncpy (n.host, hostname, sizeof (n.host));
1504         sstrncpy (n.plugin, "netapp", sizeof (n.plugin));
1505         sstrncpy (n.plugin_instance, v->name, sizeof (n.plugin_instance));
1506
1507         if ((v->flags & IS_VOLUME_USAGE_OFFLINE) != 0) {
1508                 n.severity = NOTIF_OKAY;
1509                 ssnprintf (n.message, sizeof (n.message),
1510                                 "Volume %s is now online.", v->name);
1511                 v->flags &= ~IS_VOLUME_USAGE_OFFLINE;
1512         } else {
1513                 n.severity = NOTIF_WARNING;
1514                 ssnprintf (n.message, sizeof (n.message),
1515                                 "Volume %s is now offline.", v->name);
1516                 v->flags |= IS_VOLUME_USAGE_OFFLINE;
1517         }
1518
1519         return (plugin_dispatch_notification (&n));
1520 } /* }}} int cna_change_volume_status */
1521
1522 static void cna_handle_volume_snap_usage(const host_config_t *host, /* {{{ */
1523                 data_volume_usage_t *v)
1524 {
1525         uint64_t snap_used = 0, value;
1526         na_elem_t *data, *elem_snapshots;
1527         na_elem_iter_t iter_snap;
1528
1529         data = na_server_invoke_elem(host->srv, v->snap_query);
1530         if (na_results_status(data) != NA_OK)
1531         {
1532                 if (na_results_errno(data) == EVOLUMEOFFLINE) {
1533                         if ((v->flags & IS_VOLUME_USAGE_OFFLINE) == 0)
1534                                 cna_change_volume_status (host->name, v);
1535                 } else {
1536                         ERROR ("netapp plugin: cna_handle_volume_snap_usage: na_server_invoke_elem for "
1537                                         "volume \"%s\" on host %s failed with error %d: %s", v->name,
1538                                         host->name, na_results_errno(data), na_results_reason(data));
1539                 }
1540                 na_elem_free(data);
1541                 return;
1542         }
1543
1544         if ((v->flags & IS_VOLUME_USAGE_OFFLINE) != 0)
1545                 cna_change_volume_status (host->name, v);
1546
1547         elem_snapshots = na_elem_child (data, "snapshots");
1548         if (elem_snapshots == NULL)
1549         {
1550                 ERROR ("netapp plugin: cna_handle_volume_snap_usage: "
1551                                 "na_elem_child (\"snapshots\") failed "
1552                                 "for host %s.", host->name);
1553                 na_elem_free(data);
1554                 return;
1555         }
1556
1557         iter_snap = na_child_iterator (elem_snapshots);
1558         for (na_elem_t *elem_snap = na_iterator_next (&iter_snap);
1559                         elem_snap != NULL;
1560                         elem_snap = na_iterator_next (&iter_snap))
1561         {
1562                 value = na_child_get_uint64(elem_snap, "cumulative-total", 0);
1563                 /* "cumulative-total" is the total size of the oldest snapshot plus all
1564                  * newer ones in blocks (1KB). We therefore are looking for the highest
1565                  * number of all snapshots - that's the size required for the snapshots. */
1566                 if (value > snap_used)
1567                         snap_used = value;
1568         }
1569         na_elem_free (data);
1570         /* snap_used is in 1024 byte blocks */
1571         v->snap_used = snap_used * 1024;
1572         v->flags |= HAVE_VOLUME_USAGE_SNAP_USED;
1573 } /* }}} void cna_handle_volume_snap_usage */
1574
1575 static void cna_handle_volume_sis_data (const host_config_t *host, /* {{{ */
1576                 data_volume_usage_t *v, na_elem_t *sis)
1577 {
1578         const char *sis_state;
1579         uint64_t sis_saved_reported;
1580
1581         if (na_elem_child(sis, "sis-info"))
1582                 sis = na_elem_child(sis, "sis-info");
1583
1584         sis_state = na_child_get_string(sis, "state");
1585         if (sis_state == NULL)
1586                 return;
1587
1588         /* If SIS is not enabled, there's nothing left to do for this volume. */
1589         if (strcmp ("enabled", sis_state) != 0)
1590                 return;
1591
1592         sis_saved_reported = na_child_get_uint64(sis, "size-saved", UINT64_MAX);
1593         if (sis_saved_reported == UINT64_MAX)
1594                 return;
1595
1596         /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
1597         if ((sis_saved_reported >> 32) != 0) {
1598                 /* In case they ever fix this bug. */
1599                 v->sis_saved = sis_saved_reported;
1600                 v->flags |= HAVE_VOLUME_USAGE_SIS_SAVED;
1601         } else { /* really hacky work-around code. {{{ */
1602                 uint64_t sis_saved_percent;
1603                 uint64_t sis_saved_guess;
1604                 uint64_t overflow_guess;
1605                 uint64_t guess1, guess2, guess3;
1606
1607                 /* Check if we have v->norm_used. Without it, we cannot calculate
1608                  * sis_saved_guess. */
1609                 if ((v->flags & HAVE_VOLUME_USAGE_NORM_USED) == 0)
1610                         return;
1611
1612                 sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
1613                 if (sis_saved_percent > 100)
1614                         return;
1615
1616                 /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
1617                  * will hopefully be fixed in later versions. To work around the bug, try
1618                  * to figure out how often the 32bit integer wrapped around by using the
1619                  * "percentage-saved" value. Because the percentage is in the range
1620                  * [0-100], this should work as long as the saved space does not exceed
1621                  * 400 GBytes. */
1622                 /* percentage-saved = size-saved / (size-saved + size-used) */
1623                 if (sis_saved_percent < 100)
1624                         sis_saved_guess = v->norm_used * sis_saved_percent / (100 - sis_saved_percent);
1625                 else
1626                         sis_saved_guess = v->norm_used;
1627
1628                 overflow_guess = sis_saved_guess >> 32;
1629                 guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
1630                 guess2 = (overflow_guess << 32) + sis_saved_reported;
1631                 guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
1632
1633                 if (sis_saved_guess < guess2) {
1634                         if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
1635                                 v->sis_saved = guess1;
1636                         else
1637                                 v->sis_saved = guess2;
1638                 } else {
1639                         if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
1640                                 v->sis_saved = guess2;
1641                         else
1642                                 v->sis_saved = guess3;
1643                 }
1644                 v->flags |= HAVE_VOLUME_USAGE_SIS_SAVED;
1645         } /* }}} end of 32-bit workaround */
1646 } /* }}} void cna_handle_volume_sis_data */
1647
1648 /* ONTAP >= 8.1 uses SIS for managing dedup and compression */
1649 static void cna_handle_volume_sis_saved (const host_config_t *host, /* {{{ */
1650                 data_volume_usage_t *v, na_elem_t *sis)
1651 {
1652         uint64_t saved;
1653
1654         if (na_elem_child(sis, "sis-info"))
1655                 sis = na_elem_child(sis, "sis-info");
1656
1657         saved = na_child_get_uint64(sis, "compress-saved", UINT64_MAX);
1658         if (saved != UINT64_MAX) {
1659                 v->compress_saved = saved;
1660                 v->flags |= HAVE_VOLUME_USAGE_COMPRESS_SAVED;
1661         }
1662
1663         saved = na_child_get_uint64(sis, "dedup-saved", UINT64_MAX);
1664         if (saved != UINT64_MAX) {
1665                 v->dedup_saved = saved;
1666                 v->flags |= HAVE_VOLUME_USAGE_DEDUP_SAVED;
1667         }
1668 } /* }}} void cna_handle_volume_sis_saved */
1669
1670 static int cna_handle_volume_usage_data (const host_config_t *host, /* {{{ */
1671                 cfg_volume_usage_t *cfg_volume, na_elem_t *data)
1672 {
1673         na_elem_t *elem_volumes;
1674         na_elem_iter_t iter_volume;
1675
1676         elem_volumes = na_elem_child (data, "volumes");
1677         if (elem_volumes == NULL)
1678         {
1679                 ERROR ("netapp plugin: cna_handle_volume_usage_data: "
1680                                 "na_elem_child (\"volumes\") failed "
1681                                 "for host %s.", host->name);
1682                 return (-1);
1683         }
1684
1685         iter_volume = na_child_iterator (elem_volumes);
1686         for (na_elem_t *elem_volume = na_iterator_next (&iter_volume);
1687                         elem_volume != NULL;
1688                         elem_volume = na_iterator_next (&iter_volume))
1689         {
1690                 const char *volume_name, *state;
1691
1692                 data_volume_usage_t *v;
1693                 uint64_t value;
1694
1695                 na_elem_t *sis;
1696
1697                 volume_name = na_child_get_string (elem_volume, "name");
1698                 if (volume_name == NULL)
1699                         continue;
1700
1701                 state = na_child_get_string (elem_volume, "state");
1702                 if ((state == NULL) || (strcmp(state, "online") != 0))
1703                         continue;
1704
1705                 /* get_volume_usage may return NULL if the volume is to be ignored. */
1706                 v = get_volume_usage (cfg_volume, volume_name);
1707                 if (v == NULL)
1708                         continue;
1709
1710                 if ((v->flags & CFG_VOLUME_USAGE_SNAP) != 0)
1711                         cna_handle_volume_snap_usage(host, v);
1712
1713                 if ((v->flags & CFG_VOLUME_USAGE_DF) == 0)
1714                         continue;
1715
1716                 /* 2^4 exa-bytes? This will take a while ;) */
1717                 value = na_child_get_uint64(elem_volume, "size-available", UINT64_MAX);
1718                 if (value != UINT64_MAX) {
1719                         v->norm_free = value;
1720                         v->flags |= HAVE_VOLUME_USAGE_NORM_FREE;
1721                 }
1722
1723                 value = na_child_get_uint64(elem_volume, "size-used", UINT64_MAX);
1724                 if (value != UINT64_MAX) {
1725                         v->norm_used = value;
1726                         v->flags |= HAVE_VOLUME_USAGE_NORM_USED;
1727                 }
1728
1729                 value = na_child_get_uint64(elem_volume, "snapshot-blocks-reserved", UINT64_MAX);
1730                 if (value != UINT64_MAX) {
1731                         /* 1 block == 1024 bytes  as per API docs */
1732                         v->snap_reserved = 1024 * value;
1733                         v->flags |= HAVE_VOLUME_USAGE_SNAP_RSVD;
1734                 }
1735
1736                 sis = na_elem_child(elem_volume, "sis");
1737                 if (sis != NULL) {
1738                         cna_handle_volume_sis_data (host, v, sis);
1739                         cna_handle_volume_sis_saved (host, v, sis);
1740                 }
1741         } /* for (elem_volume) */
1742
1743         return (cna_submit_volume_usage_data (host->name, cfg_volume,
1744                                 host->cfg_volume_usage->interval.interval));
1745 } /* }}} int cna_handle_volume_usage_data */
1746
1747 static int cna_setup_volume_usage (cfg_volume_usage_t *cvu) /* {{{ */
1748 {
1749         if (cvu == NULL)
1750                 return (EINVAL);
1751
1752         if (cvu->query != NULL)
1753                 return (0);
1754
1755         cvu->query = na_elem_new ("volume-list-info");
1756         if (cvu->query == NULL)
1757         {
1758                 ERROR ("netapp plugin: na_elem_new failed.");
1759                 return (-1);
1760         }
1761
1762         return (0);
1763 } /* }}} int cna_setup_volume_usage */
1764
1765 static int cna_query_volume_usage (host_config_t *host) /* {{{ */
1766 {
1767         na_elem_t *data;
1768         int status;
1769         cdtime_t now;
1770
1771         if (host == NULL)
1772                 return (EINVAL);
1773
1774         /* If the user did not configure volume_usage statistics, return without
1775          * doing anything. */
1776         if (host->cfg_volume_usage == NULL)
1777                 return (0);
1778
1779         now = cdtime ();
1780         if ((host->cfg_volume_usage->interval.interval + host->cfg_volume_usage->interval.last_read) > now)
1781                 return (0);
1782
1783         status = cna_setup_volume_usage (host->cfg_volume_usage);
1784         if (status != 0)
1785                 return (status);
1786         assert (host->cfg_volume_usage->query != NULL);
1787
1788         data = na_server_invoke_elem(host->srv, host->cfg_volume_usage->query);
1789         if (na_results_status (data) != NA_OK)
1790         {
1791                 ERROR ("netapp plugin: cna_query_volume_usage: na_server_invoke_elem failed for host %s: %s",
1792                                 host->name, na_results_reason (data));
1793                 na_elem_free (data);
1794                 return (-1);
1795         }
1796
1797         status = cna_handle_volume_usage_data (host, host->cfg_volume_usage, data);
1798
1799         if (status == 0)
1800                 host->cfg_volume_usage->interval.last_read = now;
1801
1802         na_elem_free (data);
1803         return (status);
1804 } /* }}} int cna_query_volume_usage */
1805
1806 /* Data corresponding to <Quota /> */
1807 static int cna_handle_quota_data (const host_config_t *host, /* {{{ */
1808                 cfg_quota_t *cfg_quota, na_elem_t *data)
1809 {
1810         na_elem_t *elem_quotas;
1811         na_elem_iter_t iter_quota;
1812
1813         elem_quotas = na_elem_child (data, "quotas");
1814         if (elem_quotas == NULL)
1815         {
1816                 ERROR ("netapp plugin: cna_handle_quota_data: "
1817                                 "na_elem_child (\"quotas\") failed "
1818                                 "for host %s.", host->name);
1819                 return (-1);
1820         }
1821
1822         iter_quota = na_child_iterator (elem_quotas);
1823         for (na_elem_t *elem_quota = na_iterator_next (&iter_quota);
1824                         elem_quota != NULL;
1825                         elem_quota = na_iterator_next (&iter_quota))
1826         {
1827                 const char *quota_type, *volume_name, *tree_name;
1828                 uint64_t value;
1829
1830                 char plugin_instance[DATA_MAX_NAME_LEN];
1831
1832                 quota_type = na_child_get_string (elem_quota, "quota-type");
1833                 if (quota_type == NULL)
1834                         continue;
1835
1836                 /* possible TODO: support other types as well */
1837                 if (strcmp (quota_type, "tree") != 0)
1838                         continue;
1839
1840                 tree_name = na_child_get_string (elem_quota, "tree");
1841                 if ((tree_name == NULL) || (*tree_name == '\0'))
1842                         continue;
1843
1844                 volume_name = na_child_get_string (elem_quota, "volume");
1845                 if (volume_name == NULL)
1846                         continue;
1847
1848                 ssnprintf (plugin_instance, sizeof (plugin_instance),
1849                                 "quota-%s-%s", volume_name, tree_name);
1850
1851                 value = na_child_get_uint64 (elem_quota, "disk-used", UINT64_MAX);
1852                 if (value != UINT64_MAX) {
1853                         value *= 1024; /* disk-used reports kilobytes */
1854                         submit_double (host->name, plugin_instance,
1855                                         /* type = */ "df_complex", /* type instance = */ NULL,
1856                                         (double)value, /* timestamp = */ 0,
1857                                         host->cfg_quota->interval.interval);
1858                 }
1859
1860                 value = na_child_get_uint64 (elem_quota, "files-used", UINT64_MAX);
1861                 if (value != UINT64_MAX) {
1862                         submit_double (host->name, plugin_instance,
1863                                         /* type = */ "files", /* type instance = */ NULL,
1864                                         (double)value, /* timestamp = */ 0,
1865                                         host->cfg_quota->interval.interval);
1866                 }
1867         } /* for (elem_quota) */
1868
1869         return (0);
1870 } /* }}} int cna_handle_volume_usage_data */
1871
1872 static int cna_setup_quota (cfg_quota_t *cq) /* {{{ */
1873 {
1874         if (cq == NULL)
1875                 return (EINVAL);
1876
1877         if (cq->query != NULL)
1878                 return (0);
1879
1880         cq->query = na_elem_new ("quota-report");
1881         if (cq->query == NULL)
1882         {
1883                 ERROR ("netapp plugin: na_elem_new failed.");
1884                 return (-1);
1885         }
1886
1887         return (0);
1888 } /* }}} int cna_setup_quota */
1889
1890 static int cna_query_quota (host_config_t *host) /* {{{ */
1891 {
1892         na_elem_t *data;
1893         int status;
1894         cdtime_t now;
1895
1896         if (host == NULL)
1897                 return (EINVAL);
1898
1899         /* If the user did not configure quota statistics, return without
1900          * doing anything. */
1901         if (host->cfg_quota == NULL)
1902                 return (0);
1903
1904         now = cdtime ();
1905         if ((host->cfg_quota->interval.interval + host->cfg_quota->interval.last_read) > now)
1906                 return (0);
1907
1908         status = cna_setup_quota (host->cfg_quota);
1909         if (status != 0)
1910                 return (status);
1911         assert (host->cfg_quota->query != NULL);
1912
1913         data = na_server_invoke_elem (host->srv, host->cfg_quota->query);
1914         if (na_results_status (data) != NA_OK)
1915         {
1916                 ERROR ("netapp plugin: cna_query_quota: na_server_invoke_elem failed for host %s: %s",
1917                                 host->name, na_results_reason (data));
1918                 na_elem_free (data);
1919                 return (-1);
1920         }
1921
1922         status = cna_handle_quota_data (host, host->cfg_quota, data);
1923
1924         if (status == 0)
1925                 host->cfg_quota->interval.last_read = now;
1926
1927         na_elem_free (data);
1928         return (status);
1929 } /* }}} int cna_query_quota */
1930
1931 /* Data corresponding to <SnapVault /> */
1932 static int cna_handle_snapvault_data (const char *hostname, /* {{{ */
1933                 cfg_snapvault_t *cfg_snapvault, na_elem_t *data, cdtime_t interval)
1934 {
1935         na_elem_iter_t status_iter;
1936
1937         status = na_elem_child (data, "status-list");
1938         if (! status) {
1939                 ERROR ("netapp plugin: SnapVault status record missing status-list");
1940                 return (0);
1941         }
1942
1943         status_iter = na_child_iterator (status);
1944         for (na_elem_t *status = na_iterator_next (&status_iter);
1945                         status != NULL;
1946                         status = na_iterator_next (&status_iter))
1947         {
1948                 const char *dest_sys, *dest_path, *src_sys, *src_path;
1949                 char plugin_instance[DATA_MAX_NAME_LEN];
1950                 uint64_t value;
1951
1952                 dest_sys  = na_child_get_string (status, "destination-system");
1953                 dest_path = na_child_get_string (status, "destination-path");
1954                 src_sys   = na_child_get_string (status, "source-system");
1955                 src_path  = na_child_get_string (status, "source-path");
1956
1957                 if ((! dest_sys) || (! dest_path) || (! src_sys) || (! src_path))
1958                         continue;
1959
1960                 value = na_child_get_uint64 (status, "lag-time", UINT64_MAX);
1961                 if (value == UINT64_MAX) /* no successful baseline transfer yet */
1962                         continue;
1963
1964                 /* possible TODO: make plugin instance configurable */
1965                 ssnprintf (plugin_instance, sizeof (plugin_instance),
1966                                 "snapvault-%s", dest_path);
1967                 submit_double (hostname, plugin_instance, /* type = */ "delay", NULL,
1968                                 (double)value, /* timestamp = */ 0, interval);
1969
1970                 value = na_child_get_uint64 (status, "last-transfer-duration", UINT64_MAX);
1971                 if (value != UINT64_MAX)
1972                         submit_double (hostname, plugin_instance, /* type = */ "duration", "last_transfer",
1973                                         (double)value, /* timestamp = */ 0, interval);
1974
1975                 value = na_child_get_uint64 (status, "transfer-progress", UINT64_MAX);
1976                 if (value == UINT64_MAX)
1977                         value = na_child_get_uint64 (status, "last-transfer-size", UINT64_MAX);
1978                 if (value != UINT64_MAX) {
1979                         value *= 1024; /* this is kilobytes */
1980                         submit_derive (hostname, plugin_instance, /* type = */ "if_rx_octets", "transferred",
1981                                         value, /* timestamp = */ 0, interval);
1982                 }
1983         } /* for (status) */
1984
1985         return (0);
1986 } /* }}} int cna_handle_snapvault_data */
1987
1988 static int cna_handle_snapvault_iter (host_config_t *host, /* {{{ */
1989                 na_elem_t *data)
1990 {
1991         const char *tag;
1992
1993         uint32_t records_count;
1994
1995         records_count = na_child_get_uint32 (data, "records", UINT32_MAX);
1996         if (records_count == UINT32_MAX)
1997                 return 0;
1998
1999         tag = na_child_get_string (data, "tag");
2000         if (! tag)
2001                 return 0;
2002
2003         DEBUG ("netapp plugin: Iterating %u SV records (tag = %s)", records_count, tag);
2004
2005         for (uint32_t i = 0; i < records_count; ++i) {
2006                 na_elem_t *elem;
2007
2008                 elem = na_server_invoke (host->srv,
2009                                 "snapvault-secondary-relationship-status-list-iter-next",
2010                                 "maximum", "1", "tag", tag, NULL);
2011
2012                 if (na_results_status (elem) != NA_OK)
2013                 {
2014                         ERROR ("netapp plugin: cna_handle_snapvault_iter: "
2015                                         "na_server_invoke failed for host %s: %s",
2016                                         host->name, na_results_reason (data));
2017                         na_elem_free (elem);
2018                         return (-1);
2019                 }
2020
2021                 cna_handle_snapvault_data (host->name, host->cfg_snapvault, elem,
2022                                 host->cfg_snapvault->interval.interval);
2023                 na_elem_free (elem);
2024         }
2025
2026         na_elem_free (na_server_invoke (host->srv,
2027                         "snapvault-secondary-relationship-status-list-iter-end",
2028                         "tag", tag, NULL));
2029         return (0);
2030 } /* }}} int cna_handle_snapvault_iter */
2031
2032 static int cna_setup_snapvault (cfg_snapvault_t *sv) /* {{{ */
2033 {
2034         if (sv == NULL)
2035                 return (EINVAL);
2036
2037         if (sv->query != NULL)
2038                 return (0);
2039
2040         sv->query = na_elem_new ("snapvault-secondary-relationship-status-list-iter-start");
2041         if (sv->query == NULL)
2042         {
2043                 ERROR ("netapp plugin: na_elem_new failed.");
2044                 return (-1);
2045         }
2046
2047         return (0);
2048 } /* }}} int cna_setup_snapvault */
2049
2050 static int cna_query_snapvault (host_config_t *host) /* {{{ */
2051 {
2052         na_elem_t *data;
2053         int status;
2054         cdtime_t now;
2055
2056         if (host == NULL)
2057                 return EINVAL;
2058
2059         if (host->cfg_snapvault == NULL)
2060                 return 0;
2061
2062         now = cdtime ();
2063         if ((host->cfg_snapvault->interval.interval + host->cfg_snapvault->interval.last_read) > now)
2064                 return (0);
2065
2066         status = cna_setup_snapvault (host->cfg_snapvault);
2067         if (status != 0)
2068                 return (status);
2069         assert (host->cfg_snapvault->query != NULL);
2070
2071         data = na_server_invoke_elem (host->srv, host->cfg_snapvault->query);
2072         if (na_results_status (data) != NA_OK)
2073         {
2074                 ERROR ("netapp plugin: cna_query_snapvault: na_server_invoke_elem failed for host %s: %s",
2075                                 host->name, na_results_reason (data));
2076                 na_elem_free (data);
2077                 return (-1);
2078         }
2079
2080         status = cna_handle_snapvault_iter (host, data);
2081
2082         if (status == 0)
2083                 host->cfg_snapvault->interval.last_read = now;
2084
2085         na_elem_free (data);
2086         return (status);
2087 } /* }}} int cna_query_snapvault */
2088
2089 /* Data corresponding to <System /> */
2090 static int cna_handle_system_data (const char *hostname, /* {{{ */
2091                 cfg_system_t *cfg_system, na_elem_t *data, int interval)
2092 {
2093         na_elem_t *instances;
2094         na_elem_iter_t counter_iter;
2095
2096         derive_t disk_read = 0, disk_written = 0;
2097         derive_t net_recv = 0, net_sent = 0;
2098         derive_t cpu_busy = 0, cpu_total = 0;
2099         uint32_t counter_flags = 0;
2100
2101         const char *instance;
2102         cdtime_t timestamp;
2103
2104         timestamp = cna_child_get_cdtime (data);
2105
2106         instances = na_elem_child(na_elem_child (data, "instances"), "instance-data");
2107         if (instances == NULL)
2108         {
2109                 ERROR ("netapp plugin: cna_handle_system_data: "
2110                                 "na_elem_child (\"instances\") failed "
2111                                 "for host %s.", hostname);
2112                 return (-1);
2113         }
2114
2115         instance = na_child_get_string (instances, "name");
2116         if (instance == NULL)
2117         {
2118                 ERROR ("netapp plugin: cna_handle_system_data: "
2119                                 "na_child_get_string (\"name\") failed "
2120                                 "for host %s.", hostname);
2121                 return (-1);
2122         }
2123
2124         counter_iter = na_child_iterator (na_elem_child (instances, "counters"));
2125         for (na_elem_t *counter = na_iterator_next (&counter_iter);
2126                         counter != NULL;
2127                         counter = na_iterator_next (&counter_iter))
2128         {
2129                 const char *name;
2130                 uint64_t value;
2131
2132                 name = na_child_get_string(counter, "name");
2133                 if (name == NULL)
2134                         continue;
2135
2136                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
2137                 if (value == UINT64_MAX)
2138                         continue;
2139
2140                 if (!strcmp(name, "disk_data_read")) {
2141                         disk_read = (derive_t) (value * 1024);
2142                         counter_flags |= 0x01;
2143                 } else if (!strcmp(name, "disk_data_written")) {
2144                         disk_written = (derive_t) (value * 1024);
2145                         counter_flags |= 0x02;
2146                 } else if (!strcmp(name, "net_data_recv")) {
2147                         net_recv = (derive_t) (value * 1024);
2148                         counter_flags |= 0x04;
2149                 } else if (!strcmp(name, "net_data_sent")) {
2150                         net_sent = (derive_t) (value * 1024);
2151                         counter_flags |= 0x08;
2152                 } else if (!strcmp(name, "cpu_busy")) {
2153                         cpu_busy = (derive_t) value;
2154                         counter_flags |= 0x10;
2155                 } else if (!strcmp(name, "cpu_elapsed_time")) {
2156                         cpu_total = (derive_t) value;
2157                         counter_flags |= 0x20;
2158                 } else if ((cfg_system->flags & CFG_SYSTEM_OPS)
2159                                 && (value > 0) && (strlen(name) > 4)
2160                                 && (!strcmp(name + strlen(name) - 4, "_ops"))) {
2161                         submit_derive (hostname, instance, "disk_ops_complex", name,
2162                                         (derive_t) value, timestamp, interval);
2163                 }
2164         } /* for (counter) */
2165
2166         if ((cfg_system->flags & CFG_SYSTEM_DISK)
2167                         && (HAS_ALL_FLAGS (counter_flags, 0x01 | 0x02)))
2168                 submit_two_derive (hostname, instance, "disk_octets", NULL,
2169                                 disk_read, disk_written, timestamp, interval);
2170
2171         if ((cfg_system->flags & CFG_SYSTEM_NET)
2172                         && (HAS_ALL_FLAGS (counter_flags, 0x04 | 0x08)))
2173                 submit_two_derive (hostname, instance, "if_octets", NULL,
2174                                 net_recv, net_sent, timestamp, interval);
2175
2176         if ((cfg_system->flags & CFG_SYSTEM_CPU)
2177                         && (HAS_ALL_FLAGS (counter_flags, 0x10 | 0x20)))
2178         {
2179                 submit_derive (hostname, instance, "cpu", "system",
2180                                 cpu_busy, timestamp, interval);
2181                 submit_derive (hostname, instance, "cpu", "idle",
2182                                 cpu_total - cpu_busy, timestamp, interval);
2183         }
2184
2185         return (0);
2186 } /* }}} int cna_handle_system_data */
2187
2188 static int cna_setup_system (cfg_system_t *cs) /* {{{ */
2189 {
2190         if (cs == NULL)
2191                 return (EINVAL);
2192
2193         if (cs->query != NULL)
2194                 return (0);
2195
2196         cs->query = na_elem_new ("perf-object-get-instances");
2197         if (cs->query == NULL)
2198         {
2199                 ERROR ("netapp plugin: na_elem_new failed.");
2200                 return (-1);
2201         }
2202         na_child_add_string (cs->query, "objectname", "system");
2203
2204         return (0);
2205 } /* }}} int cna_setup_system */
2206
2207 static int cna_query_system (host_config_t *host) /* {{{ */
2208 {
2209         na_elem_t *data;
2210         int status;
2211         cdtime_t now;
2212
2213         if (host == NULL)
2214                 return (EINVAL);
2215
2216         /* If system statistics were not configured, return without doing anything. */
2217         if (host->cfg_system == NULL)
2218                 return (0);
2219
2220         now = cdtime ();
2221         if ((host->cfg_system->interval.interval + host->cfg_system->interval.last_read) > now)
2222                 return (0);
2223
2224         status = cna_setup_system (host->cfg_system);
2225         if (status != 0)
2226                 return (status);
2227         assert (host->cfg_system->query != NULL);
2228
2229         data = na_server_invoke_elem(host->srv, host->cfg_system->query);
2230         if (na_results_status (data) != NA_OK)
2231         {
2232                 ERROR ("netapp plugin: cna_query_system: na_server_invoke_elem failed for host %s: %s",
2233                                 host->name, na_results_reason (data));
2234                 na_elem_free (data);
2235                 return (-1);
2236         }
2237
2238         status = cna_handle_system_data (host->name, host->cfg_system, data,
2239                         host->cfg_system->interval.interval);
2240
2241         if (status == 0)
2242                 host->cfg_system->interval.last_read = now;
2243
2244         na_elem_free (data);
2245         return (status);
2246 } /* }}} int cna_query_system */
2247
2248 /*
2249  * Configuration handling
2250  */
2251 /* Sets a given flag if the boolean argument is true and unsets the flag if it
2252  * is false. On error, the flag-field is not changed. */
2253 static int cna_config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */
2254                 uint32_t *flags, uint32_t flag)
2255 {
2256         if ((ci == NULL) || (flags == NULL))
2257                 return (EINVAL);
2258
2259         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
2260         {
2261                 WARNING ("netapp plugin: The %s option needs exactly one boolean argument.",
2262                                 ci->key);
2263                 return (-1);
2264         }
2265
2266         if (ci->values[0].value.boolean)
2267                 *flags |= flag;
2268         else
2269                 *flags &= ~flag;
2270
2271         return (0);
2272 } /* }}} int cna_config_bool_to_flag */
2273
2274 /* Handling of the "Interval" option which is allowed in every block. */
2275 static int cna_config_get_interval (const oconfig_item_t *ci, /* {{{ */
2276                 cna_interval_t *out_interval)
2277 {
2278         cdtime_t tmp = 0;
2279         int status;
2280
2281         status = cf_util_get_cdtime (ci, &tmp);
2282         if (status != 0)
2283                 return (status);
2284
2285         out_interval->interval = tmp;
2286         out_interval->last_read = 0;
2287
2288         return (0);
2289 } /* }}} int cna_config_get_interval */
2290
2291 /* Handling of the "GetIO", "GetOps" and "GetLatency" options within a
2292  * <VolumePerf /> block. */
2293 static void cna_config_volume_perf_option (cfg_volume_perf_t *cvp, /* {{{ */
2294                 const oconfig_item_t *ci)
2295 {
2296         char *name;
2297         ignorelist_t * il;
2298
2299         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2300         {
2301                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2302                                 ci->key);
2303                 return;
2304         }
2305
2306         name = ci->values[0].value.string;
2307
2308         if (strcasecmp ("GetIO", ci->key) == 0)
2309                 il = cvp->il_octets;
2310         else if (strcasecmp ("GetOps", ci->key) == 0)
2311                 il = cvp->il_operations;
2312         else if (strcasecmp ("GetLatency", ci->key) == 0)
2313                 il = cvp->il_latency;
2314         else
2315                 return;
2316
2317         ignorelist_add (il, name);
2318 } /* }}} void cna_config_volume_perf_option */
2319
2320 /* Handling of the "IgnoreSelectedIO", "IgnoreSelectedOps" and
2321  * "IgnoreSelectedLatency" options within a <VolumePerf /> block. */
2322 static void cna_config_volume_perf_default (cfg_volume_perf_t *cvp, /* {{{ */
2323                 const oconfig_item_t *ci)
2324 {
2325         ignorelist_t *il;
2326
2327         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
2328         {
2329                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2330                                 ci->key);
2331                 return;
2332         }
2333
2334         if (strcasecmp ("IgnoreSelectedIO", ci->key) == 0)
2335                 il = cvp->il_octets;
2336         else if (strcasecmp ("IgnoreSelectedOps", ci->key) == 0)
2337                 il = cvp->il_operations;
2338         else if (strcasecmp ("IgnoreSelectedLatency", ci->key) == 0)
2339                 il = cvp->il_latency;
2340         else
2341                 return;
2342
2343         if (ci->values[0].value.boolean)
2344                 ignorelist_set_invert (il, /* invert = */ 0);
2345         else
2346                 ignorelist_set_invert (il, /* invert = */ 1);
2347 } /* }}} void cna_config_volume_perf_default */
2348
2349 /* Corresponds to a <Disks /> block */
2350 /*
2351  * <VolumePerf>
2352  *   GetIO "vol0"
2353  *   GetIO "vol1"
2354  *   IgnoreSelectedIO false
2355  *
2356  *   GetOps "vol0"
2357  *   GetOps "vol2"
2358  *   IgnoreSelectedOps false
2359  *
2360  *   GetLatency "vol2"
2361  *   GetLatency "vol3"
2362  *   IgnoreSelectedLatency false
2363  * </VolumePerf>
2364  */
2365 /* Corresponds to a <VolumePerf /> block */
2366 static int cna_config_volume_performance (host_config_t *host, /* {{{ */
2367                 const oconfig_item_t *ci)
2368 {
2369         cfg_volume_perf_t *cfg_volume_perf;
2370
2371         if ((host == NULL) || (ci == NULL))
2372                 return (EINVAL);
2373
2374         if (host->cfg_volume_perf == NULL)
2375         {
2376                 cfg_volume_perf = calloc (1, sizeof (*cfg_volume_perf));
2377                 if (cfg_volume_perf == NULL)
2378                         return (ENOMEM);
2379
2380                 /* Set default flags */
2381                 cfg_volume_perf->query = NULL;
2382                 cfg_volume_perf->volumes = NULL;
2383
2384                 cfg_volume_perf->il_octets = ignorelist_create (/* invert = */ 1);
2385                 if (cfg_volume_perf->il_octets == NULL)
2386                 {
2387                         sfree (cfg_volume_perf);
2388                         return (ENOMEM);
2389                 }
2390
2391                 cfg_volume_perf->il_operations = ignorelist_create (/* invert = */ 1);
2392                 if (cfg_volume_perf->il_operations == NULL)
2393                 {
2394                         ignorelist_free (cfg_volume_perf->il_octets);
2395                         sfree (cfg_volume_perf);
2396                         return (ENOMEM);
2397                 }
2398
2399                 cfg_volume_perf->il_latency = ignorelist_create (/* invert = */ 1);
2400                 if (cfg_volume_perf->il_latency == NULL)
2401                 {
2402                         ignorelist_free (cfg_volume_perf->il_octets);
2403                         ignorelist_free (cfg_volume_perf->il_operations);
2404                         sfree (cfg_volume_perf);
2405                         return (ENOMEM);
2406                 }
2407
2408                 host->cfg_volume_perf = cfg_volume_perf;
2409         }
2410         cfg_volume_perf = host->cfg_volume_perf;
2411
2412         for (int i = 0; i < ci->children_num; ++i) {
2413                 oconfig_item_t *item = ci->children + i;
2414
2415                 /* if (!item || !item->key || !*item->key) continue; */
2416                 if (strcasecmp(item->key, "Interval") == 0)
2417                         cna_config_get_interval (item, &cfg_volume_perf->interval);
2418                 else if (!strcasecmp(item->key, "GetIO"))
2419                         cna_config_volume_perf_option (cfg_volume_perf, item);
2420                 else if (!strcasecmp(item->key, "GetOps"))
2421                         cna_config_volume_perf_option (cfg_volume_perf, item);
2422                 else if (!strcasecmp(item->key, "GetLatency"))
2423                         cna_config_volume_perf_option (cfg_volume_perf, item);
2424                 else if (!strcasecmp(item->key, "IgnoreSelectedIO"))
2425                         cna_config_volume_perf_default (cfg_volume_perf, item);
2426                 else if (!strcasecmp(item->key, "IgnoreSelectedOps"))
2427                         cna_config_volume_perf_default (cfg_volume_perf, item);
2428                 else if (!strcasecmp(item->key, "IgnoreSelectedLatency"))
2429                         cna_config_volume_perf_default (cfg_volume_perf, item);
2430                 else
2431                         WARNING ("netapp plugin: The option %s is not allowed within "
2432                                         "`VolumePerf' blocks.", item->key);
2433         }
2434
2435         return (0);
2436 } /* }}} int cna_config_volume_performance */
2437
2438 /* Handling of the "GetCapacity" and "GetSnapshot" options within a
2439  * <VolumeUsage /> block. */
2440 static void cna_config_volume_usage_option (cfg_volume_usage_t *cvu, /* {{{ */
2441                 const oconfig_item_t *ci)
2442 {
2443         char *name;
2444         ignorelist_t * il;
2445
2446         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2447         {
2448                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2449                                 ci->key);
2450                 return;
2451         }
2452
2453         name = ci->values[0].value.string;
2454
2455         if (strcasecmp ("GetCapacity", ci->key) == 0)
2456                 il = cvu->il_capacity;
2457         else if (strcasecmp ("GetSnapshot", ci->key) == 0)
2458                 il = cvu->il_snapshot;
2459         else
2460                 return;
2461
2462         ignorelist_add (il, name);
2463 } /* }}} void cna_config_volume_usage_option */
2464
2465 /* Handling of the "IgnoreSelectedCapacity" and "IgnoreSelectedSnapshot"
2466  * options within a <VolumeUsage /> block. */
2467 static void cna_config_volume_usage_default (cfg_volume_usage_t *cvu, /* {{{ */
2468                 const oconfig_item_t *ci)
2469 {
2470         ignorelist_t *il;
2471
2472         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
2473         {
2474                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2475                                 ci->key);
2476                 return;
2477         }
2478
2479         if (strcasecmp ("IgnoreSelectedCapacity", ci->key) == 0)
2480                 il = cvu->il_capacity;
2481         else if (strcasecmp ("IgnoreSelectedSnapshot", ci->key) == 0)
2482                 il = cvu->il_snapshot;
2483         else
2484                 return;
2485
2486         if (ci->values[0].value.boolean)
2487                 ignorelist_set_invert (il, /* invert = */ 0);
2488         else
2489                 ignorelist_set_invert (il, /* invert = */ 1);
2490 } /* }}} void cna_config_volume_usage_default */
2491
2492 /* Corresponds to a <Quota /> block */
2493 static int cna_config_quota (host_config_t *host, oconfig_item_t *ci) /* {{{ */
2494 {
2495         cfg_quota_t *cfg_quota;
2496
2497         if ((host == NULL) || (ci == NULL))
2498                 return (EINVAL);
2499
2500         if (host->cfg_quota == NULL)
2501         {
2502                 cfg_quota = calloc (1, sizeof (*cfg_quota));
2503                 if (cfg_quota == NULL)
2504                         return (ENOMEM);
2505                 cfg_quota->query = NULL;
2506
2507                 host->cfg_quota = cfg_quota;
2508         }
2509         cfg_quota = host->cfg_quota;
2510
2511         for (int i = 0; i < ci->children_num; ++i) {
2512                 oconfig_item_t *item = ci->children + i;
2513
2514                 if (strcasecmp (item->key, "Interval") == 0)
2515                         cna_config_get_interval (item, &cfg_quota->interval);
2516                 else
2517                         WARNING ("netapp plugin: The option %s is not allowed within "
2518                                         "`Quota' blocks.", item->key);
2519         }
2520
2521         return (0);
2522 } /* }}} int cna_config_quota */
2523
2524 /* Corresponds to a <Disks /> block */
2525 static int cna_config_disk(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
2526         cfg_disk_t *cfg_disk;
2527
2528         if ((host == NULL) || (ci == NULL))
2529                 return (EINVAL);
2530
2531         if (host->cfg_disk == NULL)
2532         {
2533                 cfg_disk = calloc (1, sizeof (*cfg_disk));
2534                 if (cfg_disk == NULL)
2535                         return (ENOMEM);
2536
2537                 /* Set default flags */
2538                 cfg_disk->flags = CFG_DISK_ALL;
2539                 cfg_disk->query = NULL;
2540                 cfg_disk->disks = NULL;
2541
2542                 host->cfg_disk = cfg_disk;
2543         }
2544         cfg_disk = host->cfg_disk;
2545
2546         for (int i = 0; i < ci->children_num; ++i) {
2547                 oconfig_item_t *item = ci->children + i;
2548
2549                 /* if (!item || !item->key || !*item->key) continue; */
2550                 if (strcasecmp(item->key, "Interval") == 0)
2551                         cna_config_get_interval (item, &cfg_disk->interval);
2552                 else if (strcasecmp(item->key, "GetBusy") == 0)
2553                         cna_config_bool_to_flag (item, &cfg_disk->flags, CFG_DISK_BUSIEST);
2554         }
2555
2556         if ((cfg_disk->flags & CFG_DISK_ALL) == 0)
2557         {
2558                 NOTICE ("netapp plugin: All disk related values have been disabled. "
2559                                 "Collection of per-disk data will be disabled entirely.");
2560                 free_cfg_disk (host->cfg_disk);
2561                 host->cfg_disk = NULL;
2562         }
2563
2564         return (0);
2565 } /* }}} int cna_config_disk */
2566
2567 /* Corresponds to a <WAFL /> block */
2568 static int cna_config_wafl(host_config_t *host, oconfig_item_t *ci) /* {{{ */
2569 {
2570         cfg_wafl_t *cfg_wafl;
2571
2572         if ((host == NULL) || (ci == NULL))
2573                 return (EINVAL);
2574
2575         if (host->cfg_wafl == NULL)
2576         {
2577                 cfg_wafl = calloc (1, sizeof (*cfg_wafl));
2578                 if (cfg_wafl == NULL)
2579                         return (ENOMEM);
2580
2581                 /* Set default flags */
2582                 cfg_wafl->flags = CFG_WAFL_ALL;
2583
2584                 host->cfg_wafl = cfg_wafl;
2585         }
2586         cfg_wafl = host->cfg_wafl;
2587
2588         for (int i = 0; i < ci->children_num; ++i) {
2589                 oconfig_item_t *item = ci->children + i;
2590
2591                 if (strcasecmp(item->key, "Interval") == 0)
2592                         cna_config_get_interval (item, &cfg_wafl->interval);
2593                 else if (!strcasecmp(item->key, "GetNameCache"))
2594                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_NAME_CACHE);
2595                 else if (!strcasecmp(item->key, "GetDirCache"))
2596                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_DIR_CACHE);
2597                 else if (!strcasecmp(item->key, "GetBufferCache"))
2598                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_BUF_CACHE);
2599                 else if (!strcasecmp(item->key, "GetInodeCache"))
2600                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_INODE_CACHE);
2601                 else
2602                         WARNING ("netapp plugin: The %s config option is not allowed within "
2603                                         "`WAFL' blocks.", item->key);
2604         }
2605
2606         if ((cfg_wafl->flags & CFG_WAFL_ALL) == 0)
2607         {
2608                 NOTICE ("netapp plugin: All WAFL related values have been disabled. "
2609                                 "Collection of WAFL data will be disabled entirely.");
2610                 free_cfg_wafl (host->cfg_wafl);
2611                 host->cfg_wafl = NULL;
2612         }
2613
2614         return (0);
2615 } /* }}} int cna_config_wafl */
2616
2617 /*
2618  * <VolumeUsage>
2619  *   GetCapacity "vol0"
2620  *   GetCapacity "vol1"
2621  *   GetCapacity "vol2"
2622  *   GetCapacity "vol3"
2623  *   GetCapacity "vol4"
2624  *   IgnoreSelectedCapacity false
2625  *
2626  *   GetSnapshot "vol0"
2627  *   GetSnapshot "vol3"
2628  *   GetSnapshot "vol4"
2629  *   GetSnapshot "vol7"
2630  *   IgnoreSelectedSnapshot false
2631  * </VolumeUsage>
2632  */
2633 /* Corresponds to a <VolumeUsage /> block */
2634 static int cna_config_volume_usage(host_config_t *host, /* {{{ */
2635                 const oconfig_item_t *ci)
2636 {
2637         cfg_volume_usage_t *cfg_volume_usage;
2638
2639         if ((host == NULL) || (ci == NULL))
2640                 return (EINVAL);
2641
2642         if (host->cfg_volume_usage == NULL)
2643         {
2644                 cfg_volume_usage = calloc (1, sizeof (*cfg_volume_usage));
2645                 if (cfg_volume_usage == NULL)
2646                         return (ENOMEM);
2647
2648                 /* Set default flags */
2649                 cfg_volume_usage->query = NULL;
2650                 cfg_volume_usage->volumes = NULL;
2651
2652                 cfg_volume_usage->il_capacity = ignorelist_create (/* invert = */ 1);
2653                 if (cfg_volume_usage->il_capacity == NULL)
2654                 {
2655                         sfree (cfg_volume_usage);
2656                         return (ENOMEM);
2657                 }
2658
2659                 cfg_volume_usage->il_snapshot = ignorelist_create (/* invert = */ 1);
2660                 if (cfg_volume_usage->il_snapshot == NULL)
2661                 {
2662                         ignorelist_free (cfg_volume_usage->il_capacity);
2663                         sfree (cfg_volume_usage);
2664                         return (ENOMEM);
2665                 }
2666
2667                 host->cfg_volume_usage = cfg_volume_usage;
2668         }
2669         cfg_volume_usage = host->cfg_volume_usage;
2670
2671         for (int i = 0; i < ci->children_num; ++i) {
2672                 oconfig_item_t *item = ci->children + i;
2673
2674                 /* if (!item || !item->key || !*item->key) continue; */
2675                 if (strcasecmp(item->key, "Interval") == 0)
2676                         cna_config_get_interval (item, &cfg_volume_usage->interval);
2677                 else if (!strcasecmp(item->key, "GetCapacity"))
2678                         cna_config_volume_usage_option (cfg_volume_usage, item);
2679                 else if (!strcasecmp(item->key, "GetSnapshot"))
2680                         cna_config_volume_usage_option (cfg_volume_usage, item);
2681                 else if (!strcasecmp(item->key, "IgnoreSelectedCapacity"))
2682                         cna_config_volume_usage_default (cfg_volume_usage, item);
2683                 else if (!strcasecmp(item->key, "IgnoreSelectedSnapshot"))
2684                         cna_config_volume_usage_default (cfg_volume_usage, item);
2685                 else
2686                         WARNING ("netapp plugin: The option %s is not allowed within "
2687                                         "`VolumeUsage' blocks.", item->key);
2688         }
2689
2690         return (0);
2691 } /* }}} int cna_config_volume_usage */
2692
2693 /* Corresponds to a <SnapVault /> block */
2694 static int cna_config_snapvault (host_config_t *host, /* {{{ */
2695                 const oconfig_item_t *ci)
2696 {
2697         cfg_snapvault_t *cfg_snapvault;
2698
2699         if ((host == NULL) || (ci == NULL))
2700                 return EINVAL;
2701
2702         if (host->cfg_snapvault == NULL)
2703         {
2704                 cfg_snapvault = calloc (1, sizeof (*cfg_snapvault));
2705                 if (cfg_snapvault == NULL)
2706                         return ENOMEM;
2707                 cfg_snapvault->query = NULL;
2708
2709                 host->cfg_snapvault = cfg_snapvault;
2710         }
2711
2712         cfg_snapvault = host->cfg_snapvault;
2713
2714         for (int i = 0; i < ci->children_num; ++i) {
2715                 oconfig_item_t *item = ci->children + i;
2716
2717                 if (strcasecmp (item->key, "Interval") == 0)
2718                         cna_config_get_interval (item, &cfg_snapvault->interval);
2719                 else
2720                         WARNING ("netapp plugin: The option %s is not allowed within "
2721                                         "`SnapVault' blocks.", item->key);
2722         }
2723
2724         return 0;
2725 } /* }}} int cna_config_snapvault */
2726
2727 /* Corresponds to a <System /> block */
2728 static int cna_config_system (host_config_t *host, /* {{{ */
2729                 oconfig_item_t *ci)
2730 {
2731         cfg_system_t *cfg_system;
2732
2733         if ((host == NULL) || (ci == NULL))
2734                 return (EINVAL);
2735
2736         if (host->cfg_system == NULL)
2737         {
2738                 cfg_system = calloc (1, sizeof (*cfg_system));
2739                 if (cfg_system == NULL)
2740                         return (ENOMEM);
2741
2742                 /* Set default flags */
2743                 cfg_system->flags = CFG_SYSTEM_ALL;
2744                 cfg_system->query = NULL;
2745
2746                 host->cfg_system = cfg_system;
2747         }
2748         cfg_system = host->cfg_system;
2749
2750         for (int i = 0; i < ci->children_num; ++i) {
2751                 oconfig_item_t *item = ci->children + i;
2752
2753                 if (strcasecmp(item->key, "Interval") == 0) {
2754                         cna_config_get_interval (item, &cfg_system->interval);
2755                 } else if (!strcasecmp(item->key, "GetCPULoad")) {
2756                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_CPU);
2757                 } else if (!strcasecmp(item->key, "GetInterfaces")) {
2758                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_NET);
2759                 } else if (!strcasecmp(item->key, "GetDiskOps")) {
2760                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_OPS);
2761                 } else if (!strcasecmp(item->key, "GetDiskIO")) {
2762                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_DISK);
2763                 } else {
2764                         WARNING ("netapp plugin: The %s config option is not allowed within "
2765                                         "`System' blocks.", item->key);
2766                 }
2767         }
2768
2769         if ((cfg_system->flags & CFG_SYSTEM_ALL) == 0)
2770         {
2771                 NOTICE ("netapp plugin: All system related values have been disabled. "
2772                                 "Collection of system data will be disabled entirely.");
2773                 free_cfg_system (host->cfg_system);
2774                 host->cfg_system = NULL;
2775         }
2776
2777         return (0);
2778 } /* }}} int cna_config_system */
2779
2780 /* Corresponds to a <Host /> block. */
2781 static host_config_t *cna_alloc_host (void) /* {{{ */
2782 {
2783         host_config_t *host;
2784
2785         host = calloc (1, sizeof (*host));
2786         if (host == NULL)
2787                 return (NULL);
2788
2789         host->name = NULL;
2790         host->protocol = NA_SERVER_TRANSPORT_HTTPS;
2791         host->host = NULL;
2792         host->username = NULL;
2793         host->password = NULL;
2794         host->vfiler = NULL;
2795         host->srv = NULL;
2796         host->cfg_wafl = NULL;
2797         host->cfg_disk = NULL;
2798         host->cfg_volume_perf = NULL;
2799         host->cfg_volume_usage = NULL;
2800         host->cfg_quota = NULL;
2801         host->cfg_snapvault = NULL;
2802         host->cfg_system = NULL;
2803
2804         return (host);
2805 } /* }}} host_config_t *cna_alloc_host */
2806
2807 static host_config_t *cna_shallow_clone_host (host_config_t *host) /* {{{ */
2808 {
2809         host_config_t *clone;
2810
2811         if (host == NULL)
2812                 return (NULL);
2813
2814         clone = cna_alloc_host ();
2815         if (clone == NULL)
2816                 return (NULL);
2817
2818         if (host->name != NULL) {
2819                 clone->name = strdup (host->name);
2820                 if (clone->name == NULL) {
2821                         free_host_config (clone);
2822                         return NULL;
2823                 }
2824         }
2825
2826         clone->protocol = host->protocol;
2827
2828         if (host->host != NULL) {
2829                 clone->host = strdup (host->host);
2830                 if (clone->host == NULL) {
2831                         free_host_config (clone);
2832                         return NULL;
2833                 }
2834         }
2835
2836         clone->port = host->port;
2837
2838         if (host->username != NULL) {
2839                 clone->username = strdup (host->username);
2840                 if (clone->username == NULL) {
2841                         free_host_config (clone);
2842                         return NULL;
2843                 }
2844         }
2845         if (host->password != NULL) {
2846                 clone->password = strdup (host->password);
2847                 if (clone->password == NULL) {
2848                         free_host_config (clone);
2849                         return NULL;
2850                 }
2851         }
2852
2853         clone->interval = host->interval;
2854
2855         return (clone);
2856 } /* }}} host_config_t *cna_shallow_clone_host */
2857
2858 static int cna_read (user_data_t *ud);
2859
2860 static int cna_register_host (host_config_t *host) /* {{{ */
2861 {
2862         char cb_name[256];
2863
2864         if (host->vfiler)
2865                 ssnprintf (cb_name, sizeof (cb_name), "netapp-%s-%s",
2866                                 host->name, host->vfiler);
2867         else
2868                 ssnprintf (cb_name, sizeof (cb_name), "netapp-%s", host->name);
2869
2870         plugin_register_complex_read (/* group = */ NULL, cb_name,
2871                         /* callback  = */ cna_read,
2872                         /* interval  = */ host->interval,
2873                         &(user_data_t) {
2874                                 .data = host,
2875                                 .free_func = (void *) free_host_config,
2876                         });
2877
2878         return (0);
2879 } /* }}} int cna_register_host */
2880
2881 static int cna_config_host (host_config_t *host, /* {{{ */
2882                 const oconfig_item_t *ci)
2883 {
2884         oconfig_item_t *item;
2885         _Bool is_vfiler = 0;
2886         int status;
2887
2888         if (! strcasecmp (ci->key, "VFiler"))
2889                 is_vfiler = 1;
2890
2891         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2892                 WARNING ("netapp plugin: \"%s\" needs exactly one string argument. Ignoring host block.", ci->key);
2893                 return (1);
2894         }
2895
2896         status = cf_util_get_string (ci, &host->name);
2897         if (status != 0)
2898                 return (1);
2899
2900         for (int i = 0; i < ci->children_num; ++i) {
2901                 item = ci->children + i;
2902
2903                 status = 0;
2904
2905                 if (!strcasecmp(item->key, "Address")) {
2906                         status = cf_util_get_string (item, &host->host);
2907                 } else if (!strcasecmp(item->key, "Port")) {
2908                         int tmp;
2909
2910                         tmp = cf_util_get_port_number (item);
2911                         if (tmp > 0)
2912                                 host->port = tmp;
2913                 } else if (!strcasecmp(item->key, "Protocol")) {
2914                         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"))) {
2915                                 WARNING("netapp plugin: \"Protocol\" needs to be either \"http\" or \"https\". Ignoring host block \"%s\".", ci->values[0].value.string);
2916                                 return (1);
2917                         }
2918                         if (!strcasecmp(item->values[0].value.string, "http")) host->protocol = NA_SERVER_TRANSPORT_HTTP;
2919                         else host->protocol = NA_SERVER_TRANSPORT_HTTPS;
2920                 } else if (!strcasecmp(item->key, "User")) {
2921                         status = cf_util_get_string (item, &host->username);
2922                 } else if (!strcasecmp(item->key, "Password")) {
2923                         status = cf_util_get_string (item, &host->password);
2924                 } else if (!strcasecmp(item->key, "Interval")) {
2925                         status = cf_util_get_cdtime (item, &host->interval);
2926                 } else if (!strcasecmp(item->key, "WAFL")) {
2927                         cna_config_wafl(host, item);
2928                 } else if (!strcasecmp(item->key, "Disks")) {
2929                         cna_config_disk(host, item);
2930                 } else if (!strcasecmp(item->key, "VolumePerf")) {
2931                         cna_config_volume_performance(host, item);
2932                 } else if (!strcasecmp(item->key, "VolumeUsage")) {
2933                         cna_config_volume_usage(host, item);
2934                 } else if (!strcasecmp(item->key, "Quota")) {
2935                         cna_config_quota(host, item);
2936                 } else if (!strcasecmp(item->key, "SnapVault")) {
2937                         cna_config_snapvault(host, item);
2938                 } else if (!strcasecmp(item->key, "System")) {
2939                         cna_config_system(host, item);
2940                 } else if ((!strcasecmp(item->key, "VFiler")) && (! is_vfiler)) {
2941                         host_config_t *vfiler;
2942
2943                         vfiler = cna_shallow_clone_host (host);
2944                         if (! vfiler) {
2945                                 ERROR ("netapp plugin: Failed to allocate host object for vfiler.");
2946                                 continue;
2947                         }
2948
2949                         if (cna_config_host (vfiler, item)) {
2950                                 free_host_config (vfiler);
2951                                 continue;
2952                         }
2953
2954                         cna_register_host (vfiler);
2955                 } else if ((!strcasecmp(item->key, "VFilerName")) && is_vfiler) {
2956                         status = cf_util_get_string (item, &host->vfiler);
2957                 } else {
2958                         WARNING ("netapp plugin: Ignoring unknown config option \"%s\" in %s block \"%s\".",
2959                                         item->key, is_vfiler ? "vfiler" : "host", ci->values[0].value.string);
2960                 }
2961
2962                 if (status != 0)
2963                         break;
2964         }
2965
2966         if (host->host == NULL)
2967                 host->host = strdup (host->name);
2968
2969         if (is_vfiler && (! host->vfiler))
2970                 host->vfiler = strdup (host->name);
2971
2972         if (host->host == NULL)
2973                 status = -1;
2974
2975         if (host->port <= 0)
2976                 host->port = (host->protocol == NA_SERVER_TRANSPORT_HTTP) ? 80 : 443;
2977
2978         if ((host->username == NULL) || (host->password == NULL)) {
2979                 WARNING("netapp plugin: Please supply login information for host \"%s\". "
2980                                 "Ignoring host block.", host->name);
2981                 status = -1;
2982         }
2983
2984         if (status != 0)
2985                 return status;
2986
2987         return (0);
2988 } /* }}} host_config_t *cna_config_host */
2989
2990 /*
2991  * Callbacks registered with the daemon
2992  *
2993  * Pretty standard stuff here.
2994  */
2995 static int cna_init_host (host_config_t *host) /* {{{ */
2996 {
2997         /* Request version 1.1 of the ONTAP API */
2998         int major_version = 1, minor_version = 1;
2999
3000         if (host == NULL)
3001                 return (EINVAL);
3002
3003         if (host->srv != NULL)
3004                 return (0);
3005
3006         if (host->vfiler != NULL) /* Request version 1.7 of the ONTAP API */
3007                 minor_version = 7;
3008
3009         host->srv = na_server_open (host->host, major_version, minor_version);
3010         if (host->srv == NULL) {
3011                 ERROR ("netapp plugin: na_server_open (%s) failed.", host->host);
3012                 return (-1);
3013         }
3014
3015         na_server_set_transport_type(host->srv, host->protocol,
3016                         /* transportarg = */ NULL);
3017         na_server_set_port(host->srv, host->port);
3018         na_server_style(host->srv, NA_STYLE_LOGIN_PASSWORD);
3019         na_server_adminuser(host->srv, host->username, host->password);
3020         na_server_set_timeout(host->srv, 5 /* seconds */);
3021
3022         if (host->vfiler != NULL) {
3023                 if (! na_server_set_vfiler (host->srv, host->vfiler)) {
3024                         ERROR ("netapp plugin: Failed to connect to VFiler '%s' on host '%s'.",
3025                                         host->vfiler, host->host);
3026                         return (-1);
3027                 }
3028                 else {
3029                         INFO ("netapp plugin: Connected to VFiler '%s' on host '%s'.",
3030                                         host->vfiler, host->host);
3031                 }
3032         }
3033
3034         return (0);
3035 } /* }}} int cna_init_host */
3036
3037 static int cna_init (void) /* {{{ */
3038 {
3039         char err[256] = { 0 };
3040
3041         if (!na_startup(err, sizeof(err))) {
3042                 err[sizeof (err) - 1] = 0;
3043                 ERROR("netapp plugin: Error initializing netapp API: %s", err);
3044                 return 1;
3045         }
3046
3047         return (0);
3048 } /* }}} cna_init */
3049
3050 static int cna_read_internal (host_config_t *host) { /* {{{ */
3051         int status;
3052
3053         status = cna_query_wafl (host);
3054         if (status != 0)
3055                 return (status);
3056
3057         status = cna_query_disk (host);
3058         if (status != 0)
3059                 return (status);
3060
3061         status = cna_query_volume_perf (host);
3062         if (status != 0)
3063                 return (status);
3064
3065         status = cna_query_volume_usage (host);
3066         if (status != 0)
3067                 return (status);
3068
3069         status = cna_query_quota (host);
3070         if (status != 0)
3071                 return (status);
3072
3073         status = cna_query_snapvault (host);
3074         if (status != 0)
3075                 return (status);
3076
3077         status = cna_query_system (host);
3078         if (status != 0)
3079                 return (status);
3080
3081         return 0;
3082 } /* }}} int cna_read_internal */
3083
3084 static int cna_read (user_data_t *ud) { /* {{{ */
3085         host_config_t *host;
3086         int status;
3087
3088         if ((ud == NULL) || (ud->data == NULL))
3089                 return (-1);
3090
3091         host = ud->data;
3092
3093         status = cna_init_host (host);
3094         if (status != 0)
3095                 return (status);
3096
3097         status = cna_read_internal (host);
3098         if (status != 0)
3099         {
3100                 if (host->srv != NULL)
3101                         na_server_close (host->srv);
3102                 host->srv = NULL;
3103         }
3104
3105         return 0;
3106 } /* }}} int cna_read */
3107
3108 static int cna_config (oconfig_item_t *ci) { /* {{{ */
3109         oconfig_item_t *item;
3110
3111         for (int i = 0; i < ci->children_num; ++i) {
3112                 item = ci->children + i;
3113
3114                 if (strcasecmp(item->key, "Host") == 0)
3115                 {
3116                         host_config_t *host;
3117
3118                         host = cna_alloc_host ();
3119                         if (host == NULL) {
3120                                 ERROR ("netapp plugin: Failed to allocate host object.");
3121                                 continue;
3122                         }
3123
3124                         if (cna_config_host (host, item) != 0) {
3125                                 free_host_config (host);
3126                                 continue;
3127                         }
3128
3129                         cna_register_host (host);
3130                 }
3131                 else /* if (item->key != "Host") */
3132                 {
3133                         WARNING("netapp plugin: Ignoring unknown config option \"%s\".", item->key);
3134                 }
3135         }
3136         return 0;
3137 } /* }}} int cna_config */
3138
3139 static int cna_shutdown (void) /* {{{ */
3140 {
3141         /* Clean up system resources and stuff. */
3142         na_shutdown ();
3143
3144         return (0);
3145 } /* }}} int cna_shutdown */
3146
3147 void module_register(void) {
3148         plugin_register_complex_config("netapp", cna_config);
3149         plugin_register_init("netapp", cna_init);
3150         plugin_register_shutdown("netapp", cna_shutdown);
3151 }
3152
3153 /* vim: set sw=2 ts=2 noet fdm=marker : */