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