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