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