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