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