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