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