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