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