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