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