netapp plugin: cna_handle_volume_snap_usage: Explain what's going on in a comment.
[collectd.git] / src / netapp.c
1 /**
2  * collectd - src/netapp.c
3  * Copyright (C) 2009  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         time_t interval;
42         time_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         time_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         time_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         time_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_ALL           0x01f0
193 struct data_volume_usage_s;
194 typedef struct data_volume_usage_s data_volume_usage_t;
195 struct data_volume_usage_s {
196         char *name;
197         uint32_t flags;
198
199         na_elem_t *snap_query;
200
201         uint64_t norm_free;
202         uint64_t norm_used;
203         uint64_t snap_reserved;
204         uint64_t snap_used;
205         uint64_t sis_saved;
206
207         data_volume_usage_t *next;
208 };
209
210 typedef struct {
211         cna_interval_t interval;
212         na_elem_t *query;
213
214         ignorelist_t *il_capacity;
215         ignorelist_t *il_snapshot;
216
217         data_volume_usage_t *volumes;
218 } cfg_volume_usage_t;
219 /* }}} cfg_volume_usage_t */
220
221 /*! Data types for system statistics {{{
222  *
223  * \brief Persistent data for system performance counters
224  */
225 #define CFG_SYSTEM_CPU  0x01
226 #define CFG_SYSTEM_NET  0x02
227 #define CFG_SYSTEM_OPS  0x04
228 #define CFG_SYSTEM_DISK 0x08
229 #define CFG_SYSTEM_ALL  0x0F
230 typedef struct {
231         uint32_t flags;
232         cna_interval_t interval;
233         na_elem_t *query;
234 } cfg_system_t;
235 /* }}} cfg_system_t */
236
237 struct host_config_s {
238         char *name;
239         na_server_transport_t protocol;
240         char *host;
241         int port;
242         char *username;
243         char *password;
244         int interval;
245
246         na_server_t *srv;
247         cfg_wafl_t *cfg_wafl;
248         cfg_disk_t *cfg_disk;
249         cfg_volume_perf_t *cfg_volume_perf;
250         cfg_volume_usage_t *cfg_volume_usage;
251         cfg_system_t *cfg_system;
252
253         struct host_config_s *next;
254 };
255 #define HOST_INIT { NULL, NA_SERVER_TRANSPORT_HTTPS, NULL, 0, NULL, NULL, 0, \
256         NULL, NULL, NULL, NULL, NULL, NULL, \
257         NULL}
258
259 static host_config_t *global_host_config;
260
261 /*
262  * Free functions
263  *
264  * Used to free the various structures above.
265  */
266 static void free_disk (disk_t *disk) /* {{{ */
267 {
268         disk_t *next;
269
270         if (disk == NULL)
271                 return;
272
273         next = disk->next;
274
275         sfree (disk->name);
276         sfree (disk);
277
278         free_disk (next);
279 } /* }}} void free_disk */
280
281 static void free_cfg_wafl (cfg_wafl_t *cw) /* {{{ */
282 {
283         if (cw == NULL)
284                 return;
285
286         if (cw->query != NULL)
287                 na_elem_free (cw->query);
288
289         sfree (cw);
290 } /* }}} void free_cfg_wafl */
291
292 static void free_cfg_disk (cfg_disk_t *cfg_disk) /* {{{ */
293 {
294         if (cfg_disk == NULL)
295                 return;
296
297         if (cfg_disk->query != NULL)
298                 na_elem_free (cfg_disk->query);
299
300         free_disk (cfg_disk->disks);
301         sfree (cfg_disk);
302 } /* }}} void free_cfg_disk */
303
304 static void free_cfg_volume_perf (cfg_volume_perf_t *cvp) /* {{{ */
305 {
306         data_volume_perf_t *data;
307
308         if (cvp == NULL)
309                 return;
310
311         /* Free the ignorelists */
312         ignorelist_free (cvp->il_octets);
313         ignorelist_free (cvp->il_operations);
314         ignorelist_free (cvp->il_latency);
315
316         /* Free the linked list of volumes */
317         data = cvp->volumes;
318         while (data != NULL)
319         {
320                 data_volume_perf_t *next = data->next;
321                 sfree (data->name);
322                 sfree (data);
323                 data = next;
324         }
325
326         if (cvp->query != NULL)
327                 na_elem_free (cvp->query);
328
329         sfree (cvp);
330 } /* }}} void free_cfg_volume_perf */
331
332 static void free_cfg_volume_usage (cfg_volume_usage_t *cvu) /* {{{ */
333 {
334         data_volume_usage_t *data;
335
336         if (cvu == NULL)
337                 return;
338
339         /* Free the ignorelists */
340         ignorelist_free (cvu->il_capacity);
341         ignorelist_free (cvu->il_snapshot);
342
343         /* Free the linked list of volumes */
344         data = cvu->volumes;
345         while (data != NULL)
346         {
347                 data_volume_usage_t *next = data->next;
348                 sfree (data->name);
349                 if (data->snap_query != NULL)
350                         na_elem_free(data->snap_query);
351                 sfree (data);
352                 data = next;
353         }
354
355         if (cvu->query != NULL)
356                 na_elem_free (cvu->query);
357
358         sfree (cvu);
359 } /* }}} void free_cfg_volume_usage */
360
361 static void free_cfg_system (cfg_system_t *cs) /* {{{ */
362 {
363         if (cs == NULL)
364                 return;
365
366         if (cs->query != NULL)
367                 na_elem_free (cs->query);
368
369         sfree (cs);
370 } /* }}} void free_cfg_system */
371
372 static void free_host_config (host_config_t *hc) /* {{{ */
373 {
374         host_config_t *next;
375
376         if (hc == NULL)
377                 return;
378
379         next = hc->next;
380
381         sfree (hc->name);
382         sfree (hc->host);
383         sfree (hc->username);
384         sfree (hc->password);
385
386         free_cfg_disk (hc->cfg_disk);
387         free_cfg_wafl (hc->cfg_wafl);
388         free_cfg_volume_perf (hc->cfg_volume_perf);
389         free_cfg_volume_usage (hc->cfg_volume_usage);
390         free_cfg_system (hc->cfg_system);
391
392         if (hc->srv != NULL)
393                 na_server_close (hc->srv);
394
395         sfree (hc);
396
397         free_host_config (next);
398 } /* }}} void free_host_config */
399
400 /*
401  * Auxiliary functions
402  *
403  * Used to look up volumes and disks or to handle flags.
404  */
405 static disk_t *get_disk(cfg_disk_t *cd, const char *name) /* {{{ */
406 {
407         disk_t *d;
408
409         if ((cd == NULL) || (name == NULL))
410                 return (NULL);
411
412         for (d = cd->disks; d != NULL; d = d->next) {
413                 if (strcmp(d->name, name) == 0)
414                         return d;
415         }
416
417         d = malloc(sizeof(*d));
418         if (d == NULL)
419                 return (NULL);
420         memset (d, 0, sizeof (*d));
421         d->next = NULL;
422
423         d->name = strdup(name);
424         if (d->name == NULL) {
425                 sfree (d);
426                 return (NULL);
427         }
428
429         d->next = cd->disks;
430         cd->disks = d;
431
432         return d;
433 } /* }}} disk_t *get_disk */
434
435 static data_volume_usage_t *get_volume_usage (cfg_volume_usage_t *cvu, /* {{{ */
436                 const char *name)
437 {
438         data_volume_usage_t *last;
439         data_volume_usage_t *new;
440
441         int ignore_capacity = 0;
442         int ignore_snapshot = 0;
443
444         if ((cvu == NULL) || (name == NULL))
445                 return (NULL);
446
447         last = cvu->volumes;
448         while (last != NULL)
449         {
450                 if (strcmp (last->name, name) == 0)
451                         return (last);
452
453                 if (last->next == NULL)
454                         break;
455
456                 last = last->next;
457         }
458
459         /* Check the ignorelists. If *both* tell us to ignore a volume, return NULL. */
460         ignore_capacity = ignorelist_match (cvu->il_capacity, name);
461         ignore_snapshot = ignorelist_match (cvu->il_snapshot, name);
462         if ((ignore_capacity != 0) && (ignore_snapshot != 0))
463                 return (NULL);
464
465         /* Not found: allocate. */
466         new = malloc (sizeof (*new));
467         if (new == NULL)
468                 return (NULL);
469         memset (new, 0, sizeof (*new));
470         new->next = NULL;
471
472         new->name = strdup (name);
473         if (new->name == NULL)
474         {
475                 sfree (new);
476                 return (NULL);
477         }
478
479         if (ignore_capacity == 0)
480                 new->flags |= CFG_VOLUME_USAGE_DF;
481         if (ignore_snapshot == 0) {
482                 new->flags |= CFG_VOLUME_USAGE_SNAP;
483                 new->snap_query = na_elem_new ("snapshot-list-info");
484                 na_child_add_string(new->snap_query, "target-type", "volume");
485                 na_child_add_string(new->snap_query, "target-name", name);
486         } else {
487                 new->snap_query = NULL;
488         }
489
490         /* Add to end of list. */
491         if (last == NULL)
492                 cvu->volumes = new;
493         else
494                 last->next = new;
495
496         return (new);
497 } /* }}} data_volume_usage_t *get_volume_usage */
498
499 static data_volume_perf_t *get_volume_perf (cfg_volume_perf_t *cvp, /* {{{ */
500                 const char *name)
501 {
502         data_volume_perf_t *last;
503         data_volume_perf_t *new;
504
505         int ignore_octets = 0;
506         int ignore_operations = 0;
507         int ignore_latency = 0;
508
509         if ((cvp == NULL) || (name == NULL))
510                 return (NULL);
511
512         last = cvp->volumes;
513         while (last != NULL)
514         {
515                 if (strcmp (last->name, name) == 0)
516                         return (last);
517
518                 if (last->next == NULL)
519                         break;
520
521                 last = last->next;
522         }
523
524         /* Check the ignorelists. If *all three* tell us to ignore a volume, return
525          * NULL. */
526         ignore_octets = ignorelist_match (cvp->il_octets, name);
527         ignore_operations = ignorelist_match (cvp->il_operations, name);
528         ignore_latency = ignorelist_match (cvp->il_latency, name);
529         if ((ignore_octets != 0) || (ignore_operations != 0)
530                         || (ignore_latency != 0))
531                 return (NULL);
532
533         /* Not found: allocate. */
534         new = malloc (sizeof (*new));
535         if (new == NULL)
536                 return (NULL);
537         memset (new, 0, sizeof (*new));
538         new->next = NULL;
539
540         new->name = strdup (name);
541         if (new->name == NULL)
542         {
543                 sfree (new);
544                 return (NULL);
545         }
546
547         if (ignore_octets == 0)
548                 new->flags |= CFG_VOLUME_PERF_IO;
549         if (ignore_operations == 0)
550                 new->flags |= CFG_VOLUME_PERF_OPS;
551         if (ignore_latency == 0)
552                 new->flags |= CFG_VOLUME_PERF_LATENCY;
553
554         /* Add to end of list. */
555         if (last == NULL)
556                 cvp->volumes = new;
557         else
558                 last->next = new;
559
560         return (new);
561 } /* }}} data_volume_perf_t *get_volume_perf */
562
563 /*
564  * Various submit functions.
565  *
566  * They all eventually call "submit_values" which creates a value_list_t and
567  * dispatches it to the daemon.
568  */
569 static int submit_values (const char *host, /* {{{ */
570                 const char *plugin_inst,
571                 const char *type, const char *type_inst,
572                 value_t *values, int values_len,
573                 time_t timestamp)
574 {
575         value_list_t vl = VALUE_LIST_INIT;
576
577         vl.values = values;
578         vl.values_len = values_len;
579
580         if (timestamp > 0)
581                 vl.time = timestamp;
582
583         if (host != NULL)
584                 sstrncpy (vl.host, host, sizeof (vl.host));
585         else
586                 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
587         sstrncpy (vl.plugin, "netapp", sizeof (vl.plugin));
588         if (plugin_inst != NULL)
589                 sstrncpy (vl.plugin_instance, plugin_inst, sizeof (vl.plugin_instance));
590         sstrncpy (vl.type, type, sizeof (vl.type));
591         if (type_inst != NULL)
592                 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
593
594         return (plugin_dispatch_values (&vl));
595 } /* }}} int submit_uint64 */
596
597 static int submit_two_counters (const char *host, const char *plugin_inst, /* {{{ */
598                 const char *type, const char *type_inst, counter_t val0, counter_t val1,
599                 time_t timestamp)
600 {
601         value_t values[2];
602
603         values[0].counter = val0;
604         values[1].counter = val1;
605
606         return (submit_values (host, plugin_inst, type, type_inst,
607                                 values, 2, timestamp));
608 } /* }}} int submit_two_counters */
609
610 static int submit_counter (const char *host, const char *plugin_inst, /* {{{ */
611                 const char *type, const char *type_inst, counter_t counter, time_t timestamp)
612 {
613         value_t v;
614
615         v.counter = counter;
616
617         return (submit_values (host, plugin_inst, type, type_inst,
618                                 &v, 1, timestamp));
619 } /* }}} int submit_counter */
620
621 static int submit_two_gauge (const char *host, const char *plugin_inst, /* {{{ */
622                 const char *type, const char *type_inst, gauge_t val0, gauge_t val1,
623                 time_t timestamp)
624 {
625         value_t values[2];
626
627         values[0].gauge = val0;
628         values[1].gauge = val1;
629
630         return (submit_values (host, plugin_inst, type, type_inst,
631                                 values, 2, timestamp));
632 } /* }}} int submit_two_gauge */
633
634 static int submit_double (const char *host, const char *plugin_inst, /* {{{ */
635                 const char *type, const char *type_inst, double d, time_t timestamp)
636 {
637         value_t v;
638
639         v.gauge = (gauge_t) d;
640
641         return (submit_values (host, plugin_inst, type, type_inst,
642                                 &v, 1, timestamp));
643 } /* }}} int submit_uint64 */
644
645 /* Calculate hit ratio from old and new counters and submit the resulting
646  * percentage. Used by "submit_wafl_data". */
647 static int submit_cache_ratio (const char *host, /* {{{ */
648                 const char *plugin_inst,
649                 const char *type_inst,
650                 uint64_t new_hits,
651                 uint64_t new_misses,
652                 uint64_t old_hits,
653                 uint64_t old_misses,
654                 time_t timestamp)
655 {
656         value_t v;
657
658         if ((new_hits >= old_hits) && (new_misses >= old_misses)) {
659                 uint64_t hits;
660                 uint64_t misses;
661
662                 hits = new_hits - old_hits;
663                 misses = new_misses - old_misses;
664
665                 v.gauge = 100.0 * ((gauge_t) hits) / ((gauge_t) (hits + misses));
666         } else {
667                 v.gauge = NAN;
668         }
669
670         return (submit_values (host, plugin_inst, "cache_ratio", type_inst,
671                                 &v, 1, timestamp));
672 } /* }}} int submit_cache_ratio */
673
674 /* Submits all the caches used by WAFL. Uses "submit_cache_ratio". */
675 static int submit_wafl_data (const char *hostname, const char *instance, /* {{{ */
676                 cfg_wafl_t *old_data, const cfg_wafl_t *new_data)
677 {
678         /* Submit requested counters */
679         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_NAME_CACHE | HAVE_WAFL_NAME_CACHE)
680                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_NAME_CACHE))
681                 submit_cache_ratio (hostname, instance, "name_cache_hit",
682                                 new_data->name_cache_hit, new_data->name_cache_miss,
683                                 old_data->name_cache_hit, old_data->name_cache_miss,
684                                 new_data->timestamp);
685
686         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_DIR_CACHE | HAVE_WAFL_FIND_DIR)
687                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_FIND_DIR))
688                 submit_cache_ratio (hostname, instance, "find_dir_hit",
689                                 new_data->find_dir_hit, new_data->find_dir_miss,
690                                 old_data->find_dir_hit, old_data->find_dir_miss,
691                                 new_data->timestamp);
692
693         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_BUF_CACHE | HAVE_WAFL_BUF_HASH)
694                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_BUF_HASH))
695                 submit_cache_ratio (hostname, instance, "buf_hash_hit",
696                                 new_data->buf_hash_hit, new_data->buf_hash_miss,
697                                 old_data->buf_hash_hit, old_data->buf_hash_miss,
698                                 new_data->timestamp);
699
700         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_INODE_CACHE | HAVE_WAFL_INODE_CACHE)
701                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_INODE_CACHE))
702                 submit_cache_ratio (hostname, instance, "inode_cache_hit",
703                                 new_data->inode_cache_hit, new_data->inode_cache_miss,
704                                 old_data->inode_cache_hit, old_data->inode_cache_miss,
705                                 new_data->timestamp);
706
707         /* Clear old HAVE_* flags */
708         old_data->flags &= ~HAVE_WAFL_ALL;
709
710         /* Copy all counters */
711         old_data->timestamp        = new_data->timestamp;
712         old_data->name_cache_hit   = new_data->name_cache_hit;
713         old_data->name_cache_miss  = new_data->name_cache_miss;
714         old_data->find_dir_hit     = new_data->find_dir_hit;
715         old_data->find_dir_miss    = new_data->find_dir_miss;
716         old_data->buf_hash_hit     = new_data->buf_hash_hit;
717         old_data->buf_hash_miss    = new_data->buf_hash_miss;
718         old_data->inode_cache_hit  = new_data->inode_cache_hit;
719         old_data->inode_cache_miss = new_data->inode_cache_miss;
720
721         /* Copy HAVE_* flags */
722         old_data->flags |= (new_data->flags & HAVE_WAFL_ALL);
723
724         return (0);
725 } /* }}} int submit_wafl_data */
726
727 /* Submits volume performance data to the daemon, taking care to honor and
728  * update flags appropriately. */
729 static int submit_volume_perf_data (const char *hostname, /* {{{ */
730                 data_volume_perf_t *old_data,
731                 const data_volume_perf_t *new_data)
732 {
733         /* Check for and submit disk-octet values */
734         if (HAS_ALL_FLAGS (old_data->flags, CFG_VOLUME_PERF_IO)
735                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_BYTES_READ | HAVE_VOLUME_PERF_BYTES_WRITE))
736         {
737                 submit_two_counters (hostname, old_data->name, "disk_octets", /* type instance = */ NULL,
738                                 (counter_t) new_data->read_bytes, (counter_t) new_data->write_bytes, new_data->timestamp);
739         }
740
741         /* Check for and submit disk-operations values */
742         if (HAS_ALL_FLAGS (old_data->flags, CFG_VOLUME_PERF_OPS)
743                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE))
744         {
745                 submit_two_counters (hostname, old_data->name, "disk_ops", /* type instance = */ NULL,
746                                 (counter_t) new_data->read_ops, (counter_t) new_data->write_ops, new_data->timestamp);
747         }
748
749         /* Check for, calculate and submit disk-latency values */
750         if (HAS_ALL_FLAGS (old_data->flags, CFG_VOLUME_PERF_LATENCY
751                                 | HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
752                                 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE)
753                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
754                                 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE))
755         {
756                 gauge_t latency_per_op_read;
757                 gauge_t latency_per_op_write;
758
759                 latency_per_op_read = NAN;
760                 latency_per_op_write = NAN;
761
762                 /* Check if a counter wrapped around. */
763                 if ((new_data->read_ops > old_data->read_ops)
764                                 && (new_data->read_latency > old_data->read_latency))
765                 {
766                         uint64_t diff_ops_read;
767                         uint64_t diff_latency_read;
768
769                         diff_ops_read = new_data->read_ops - old_data->read_ops;
770                         diff_latency_read = new_data->read_latency - old_data->read_latency;
771
772                         if (diff_ops_read > 0)
773                                 latency_per_op_read = ((gauge_t) diff_latency_read) / ((gauge_t) diff_ops_read);
774                 }
775
776                 if ((new_data->write_ops > old_data->write_ops)
777                                 && (new_data->write_latency > old_data->write_latency))
778                 {
779                         uint64_t diff_ops_write;
780                         uint64_t diff_latency_write;
781
782                         diff_ops_write = new_data->write_ops - old_data->write_ops;
783                         diff_latency_write = new_data->write_latency - old_data->write_latency;
784
785                         if (diff_ops_write > 0)
786                                 latency_per_op_write = ((gauge_t) diff_latency_write) / ((gauge_t) diff_ops_write);
787                 }
788
789                 submit_two_gauge (hostname, old_data->name, "disk_latency", /* type instance = */ NULL,
790                                 latency_per_op_read, latency_per_op_write, new_data->timestamp);
791         }
792
793         /* Clear all HAVE_* flags. */
794         old_data->flags &= ~HAVE_VOLUME_PERF_ALL;
795
796         /* Copy all counters */
797         old_data->timestamp = new_data->timestamp;
798         old_data->read_bytes = new_data->read_bytes;
799         old_data->write_bytes = new_data->write_bytes;
800         old_data->read_ops = new_data->read_ops;
801         old_data->write_ops = new_data->write_ops;
802         old_data->read_latency = new_data->read_latency;
803         old_data->write_latency = new_data->write_latency;
804
805         /* Copy the HAVE_* flags */
806         old_data->flags |= (new_data->flags & HAVE_VOLUME_PERF_ALL);
807
808         return (0);
809 } /* }}} int submit_volume_perf_data */
810
811 /* 
812  * Query functions
813  *
814  * These functions are called with appropriate data returned by the libnetapp
815  * interface which is parsed and submitted with the above functions.
816  */
817 /* Data corresponding to <WAFL /> */
818 static int cna_handle_wafl_data (const char *hostname, cfg_wafl_t *cfg_wafl, /* {{{ */
819                 na_elem_t *data)
820 {
821         cfg_wafl_t perf_data;
822         const char *plugin_inst;
823
824         na_elem_t *instances;
825         na_elem_t *counter;
826         na_elem_iter_t counter_iter;
827
828         memset (&perf_data, 0, sizeof (perf_data));
829         
830         perf_data.timestamp = (time_t) na_child_get_uint64 (data, "timestamp", 0);
831
832         instances = na_elem_child(na_elem_child (data, "instances"), "instance-data");
833         if (instances == NULL)
834         {
835                 ERROR ("netapp plugin: cna_handle_wafl_data: "
836                                 "na_elem_child (\"instances\") failed.");
837                 return (-1);
838         }
839
840         plugin_inst = na_child_get_string(instances, "name");
841         if (plugin_inst == NULL)
842         {
843                 ERROR ("netapp plugin: cna_handle_wafl_data: "
844                                 "na_child_get_string (\"name\") failed.");
845                 return (-1);
846         }
847
848         /* Iterate over all counters */
849         counter_iter = na_child_iterator (na_elem_child (instances, "counters"));
850         for (counter = na_iterator_next (&counter_iter);
851                         counter != NULL;
852                         counter = na_iterator_next (&counter_iter))
853         {
854                 const char *name;
855                 uint64_t value;
856
857                 name = na_child_get_string(counter, "name");
858                 if (name == NULL)
859                         continue;
860
861                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
862                 if (value == UINT64_MAX)
863                         continue;
864
865                 if (!strcmp(name, "name_cache_hit")) {
866                         perf_data.name_cache_hit = value;
867                         perf_data.flags |= HAVE_WAFL_NAME_CACHE_HIT;
868                 } else if (!strcmp(name, "name_cache_miss")) {
869                         perf_data.name_cache_miss = value;
870                         perf_data.flags |= HAVE_WAFL_NAME_CACHE_MISS;
871                 } else if (!strcmp(name, "find_dir_hit")) {
872                         perf_data.find_dir_hit = value;
873                         perf_data.flags |= HAVE_WAFL_FIND_DIR_HIT;
874                 } else if (!strcmp(name, "find_dir_miss")) {
875                         perf_data.find_dir_miss = value;
876                         perf_data.flags |= HAVE_WAFL_FIND_DIR_MISS;
877                 } else if (!strcmp(name, "buf_hash_hit")) {
878                         perf_data.buf_hash_hit = value;
879                         perf_data.flags |= HAVE_WAFL_BUF_HASH_HIT;
880                 } else if (!strcmp(name, "buf_hash_miss")) {
881                         perf_data.buf_hash_miss = value;
882                         perf_data.flags |= HAVE_WAFL_BUF_HASH_MISS;
883                 } else if (!strcmp(name, "inode_cache_hit")) {
884                         perf_data.inode_cache_hit = value;
885                         perf_data.flags |= HAVE_WAFL_INODE_CACHE_HIT;
886                 } else if (!strcmp(name, "inode_cache_miss")) {
887                         perf_data.inode_cache_miss = value;
888                         perf_data.flags |= HAVE_WAFL_INODE_CACHE_MISS;
889                 } else {
890                         DEBUG("netapp plugin: cna_handle_wafl_data: "
891                                         "Found unexpected child: %s", name);
892                 }
893         }
894
895         return (submit_wafl_data (hostname, plugin_inst, cfg_wafl, &perf_data));
896 } /* }}} void cna_handle_wafl_data */
897
898 static int cna_setup_wafl (cfg_wafl_t *cw) /* {{{ */
899 {
900         na_elem_t *e;
901
902         if (cw == NULL)
903                 return (EINVAL);
904
905         if (cw->query != NULL)
906                 return (0);
907
908         cw->query = na_elem_new("perf-object-get-instances");
909         if (cw->query == NULL)
910         {
911                 ERROR ("netapp plugin: na_elem_new failed.");
912                 return (-1);
913         }
914         na_child_add_string (cw->query, "objectname", "wafl");
915
916         e = na_elem_new("counters");
917         if (e == NULL)
918         {
919                 na_elem_free (cw->query);
920                 cw->query = NULL;
921                 ERROR ("netapp plugin: na_elem_new failed.");
922                 return (-1);
923         }
924         na_child_add_string(e, "foo", "name_cache_hit");
925         na_child_add_string(e, "foo", "name_cache_miss");
926         na_child_add_string(e, "foo", "find_dir_hit");
927         na_child_add_string(e, "foo", "find_dir_miss");
928         na_child_add_string(e, "foo", "buf_hash_hit");
929         na_child_add_string(e, "foo", "buf_hash_miss");
930         na_child_add_string(e, "foo", "inode_cache_hit");
931         na_child_add_string(e, "foo", "inode_cache_miss");
932
933         na_child_add(cw->query, e);
934
935         return (0);
936 } /* }}} int cna_setup_wafl */
937
938 static int cna_query_wafl (host_config_t *host) /* {{{ */
939 {
940         na_elem_t *data;
941         int status;
942         time_t now;
943
944         if (host == NULL)
945                 return (EINVAL);
946
947         /* If WAFL was not configured, return without doing anything. */
948         if (host->cfg_wafl == NULL)
949                 return (0);
950
951         now = time (NULL);
952         if ((host->cfg_wafl->interval.interval + host->cfg_wafl->interval.last_read) > now)
953                 return (0);
954
955         status = cna_setup_wafl (host->cfg_wafl);
956         if (status != 0)
957                 return (status);
958         assert (host->cfg_wafl->query != NULL);
959
960         data = na_server_invoke_elem(host->srv, host->cfg_wafl->query);
961         if (na_results_status (data) != NA_OK)
962         {
963                 ERROR ("netapp plugin: cna_query_wafl: na_server_invoke_elem failed: %s",
964                                 na_results_reason (data));
965                 na_elem_free (data);
966                 return (-1);
967         }
968
969         status = cna_handle_wafl_data (host->name, host->cfg_wafl, data);
970
971         if (status == 0)
972                 host->cfg_wafl->interval.last_read = now;
973
974         na_elem_free (data);
975         return (status);
976 } /* }}} int cna_query_wafl */
977
978 /* Data corresponding to <Disks /> */
979 static int cna_handle_disk_data (const char *hostname, /* {{{ */
980                 cfg_disk_t *cfg_disk, na_elem_t *data)
981 {
982         time_t timestamp;
983         na_elem_t *instances;
984         na_elem_t *instance;
985         na_elem_iter_t instance_iter;
986         disk_t *worst_disk = NULL;
987
988         if ((cfg_disk == NULL) || (data == NULL))
989                 return (EINVAL);
990         
991         timestamp = (time_t) na_child_get_uint64(data, "timestamp", 0);
992
993         instances = na_elem_child (data, "instances");
994         if (instances == NULL)
995         {
996                 ERROR ("netapp plugin: cna_handle_disk_data: "
997                                 "na_elem_child (\"instances\") failed.");
998                 return (-1);
999         }
1000
1001         /* Iterate over all children */
1002         instance_iter = na_child_iterator (instances);
1003         for (instance = na_iterator_next (&instance_iter);
1004                         instance != NULL;
1005                         instance = na_iterator_next(&instance_iter))
1006         {
1007                 disk_t *old_data;
1008                 disk_t  new_data;
1009
1010                 na_elem_iter_t counter_iterator;
1011                 na_elem_t *counter;
1012
1013                 memset (&new_data, 0, sizeof (new_data));
1014                 new_data.timestamp = timestamp;
1015                 new_data.disk_busy_percent = NAN;
1016
1017                 old_data = get_disk(cfg_disk, na_child_get_string (instance, "name"));
1018                 if (old_data == NULL)
1019                         continue;
1020
1021                 /* Look for the "disk_busy" and "base_for_disk_busy" counters */
1022                 counter_iterator = na_child_iterator(na_elem_child(instance, "counters"));
1023                 for (counter = na_iterator_next(&counter_iterator);
1024                                 counter != NULL;
1025                                 counter = na_iterator_next(&counter_iterator))
1026                 {
1027                         const char *name;
1028                         uint64_t value;
1029
1030                         name = na_child_get_string(counter, "name");
1031                         if (name == NULL)
1032                                 continue;
1033
1034                         value = na_child_get_uint64(counter, "value", UINT64_MAX);
1035                         if (value == UINT64_MAX)
1036                                 continue;
1037
1038                         if (strcmp(name, "disk_busy") == 0)
1039                         {
1040                                 new_data.disk_busy = value;
1041                                 new_data.flags |= HAVE_DISK_BUSY;
1042                         }
1043                         else if (strcmp(name, "base_for_disk_busy") == 0)
1044                         {
1045                                 new_data.base_for_disk_busy = value;
1046                                 new_data.flags |= HAVE_DISK_BASE;
1047                         }
1048                         else
1049                         {
1050                                 DEBUG ("netapp plugin: cna_handle_disk_data: "
1051                                                 "Counter not handled: %s = %"PRIu64,
1052                                                 name, value);
1053                         }
1054                 }
1055
1056                 /* If all required counters are available and did not just wrap around,
1057                  * calculate the busy percentage. Otherwise, the value is initialized to
1058                  * NAN at the top of the for-loop. */
1059                 if (HAS_ALL_FLAGS (old_data->flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
1060                                 && HAS_ALL_FLAGS (new_data.flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
1061                                 && (new_data.disk_busy >= old_data->disk_busy)
1062                                 && (new_data.base_for_disk_busy > old_data->base_for_disk_busy))
1063                 {
1064                         uint64_t busy_diff;
1065                         uint64_t base_diff;
1066
1067                         busy_diff = new_data.disk_busy - old_data->disk_busy;
1068                         base_diff = new_data.base_for_disk_busy - old_data->base_for_disk_busy;
1069
1070                         new_data.disk_busy_percent = 100.0
1071                                 * ((gauge_t) busy_diff) / ((gauge_t) base_diff);
1072                 }
1073
1074                 /* Clear HAVE_* flags */
1075                 old_data->flags &= ~HAVE_DISK_ALL;
1076
1077                 /* Copy data */
1078                 old_data->timestamp = new_data.timestamp;
1079                 old_data->disk_busy = new_data.disk_busy;
1080                 old_data->base_for_disk_busy = new_data.base_for_disk_busy;
1081                 old_data->disk_busy_percent = new_data.disk_busy_percent;
1082
1083                 /* Copy flags */
1084                 old_data->flags |= (new_data.flags & HAVE_DISK_ALL);
1085
1086                 if ((worst_disk == NULL)
1087                                 || (worst_disk->disk_busy_percent < old_data->disk_busy_percent))
1088                         worst_disk = old_data;
1089         } /* for (all disks) */
1090
1091         if ((cfg_disk->flags & CFG_DISK_BUSIEST) && (worst_disk != NULL))
1092                 submit_double (hostname, "system", "percent", "disk_busy",
1093                                 worst_disk->disk_busy_percent, timestamp);
1094
1095         return (0);
1096 } /* }}} int cna_handle_disk_data */
1097
1098 static int cna_setup_disk (cfg_disk_t *cd) /* {{{ */
1099 {
1100         na_elem_t *e;
1101
1102         if (cd == NULL)
1103                 return (EINVAL);
1104
1105         if (cd->query != NULL)
1106                 return (0);
1107
1108         cd->query = na_elem_new ("perf-object-get-instances");
1109         if (cd->query == NULL)
1110         {
1111                 ERROR ("netapp plugin: na_elem_new failed.");
1112                 return (-1);
1113         }
1114         na_child_add_string (cd->query, "objectname", "disk");
1115
1116         e = na_elem_new("counters");
1117         if (e == NULL)
1118         {
1119                 na_elem_free (cd->query);
1120                 cd->query = NULL;
1121                 ERROR ("netapp plugin: na_elem_new failed.");
1122                 return (-1);
1123         }
1124         na_child_add_string(e, "foo", "disk_busy");
1125         na_child_add_string(e, "foo", "base_for_disk_busy");
1126         na_child_add(cd->query, e);
1127
1128         return (0);
1129 } /* }}} int cna_setup_disk */
1130
1131 static int cna_query_disk (host_config_t *host) /* {{{ */
1132 {
1133         na_elem_t *data;
1134         int status;
1135         time_t now;
1136
1137         if (host == NULL)
1138                 return (EINVAL);
1139
1140         /* If the user did not configure disk statistics, return without doing
1141          * anything. */
1142         if (host->cfg_disk == NULL)
1143                 return (0);
1144
1145         now = time (NULL);
1146         if ((host->cfg_disk->interval.interval + host->cfg_disk->interval.last_read) > now)
1147                 return (0);
1148
1149         status = cna_setup_disk (host->cfg_disk);
1150         if (status != 0)
1151                 return (status);
1152         assert (host->cfg_disk->query != NULL);
1153
1154         data = na_server_invoke_elem(host->srv, host->cfg_disk->query);
1155         if (na_results_status (data) != NA_OK)
1156         {
1157                 ERROR ("netapp plugin: cna_query_disk: na_server_invoke_elem failed: %s",
1158                                 na_results_reason (data));
1159                 na_elem_free (data);
1160                 return (-1);
1161         }
1162
1163         status = cna_handle_disk_data (host->name, host->cfg_disk, data);
1164
1165         if (status == 0)
1166                 host->cfg_disk->interval.last_read = now;
1167
1168         na_elem_free (data);
1169         return (status);
1170 } /* }}} int cna_query_disk */
1171
1172 /* Data corresponding to <VolumePerf /> */
1173 static int cna_handle_volume_perf_data (const char *hostname, /* {{{ */
1174                 cfg_volume_perf_t *cvp, na_elem_t *data)
1175 {
1176         time_t timestamp;
1177         na_elem_t *elem_instances;
1178         na_elem_iter_t iter_instances;
1179         na_elem_t *elem_instance;
1180         
1181         timestamp = (time_t) na_child_get_uint64(data, "timestamp", 0);
1182
1183         elem_instances = na_elem_child(data, "instances");
1184         if (elem_instances == NULL)
1185         {
1186                 ERROR ("netapp plugin: handle_volume_perf_data: "
1187                                 "na_elem_child (\"instances\") failed.");
1188                 return (-1);
1189         }
1190
1191         iter_instances = na_child_iterator (elem_instances);
1192         for (elem_instance = na_iterator_next(&iter_instances);
1193                         elem_instance != NULL;
1194                         elem_instance = na_iterator_next(&iter_instances))
1195         {
1196                 const char *name;
1197
1198                 data_volume_perf_t perf_data;
1199                 data_volume_perf_t *v;
1200
1201                 na_elem_t *elem_counters;
1202                 na_elem_iter_t iter_counters;
1203                 na_elem_t *elem_counter;
1204
1205                 memset (&perf_data, 0, sizeof (perf_data));
1206                 perf_data.timestamp = timestamp;
1207
1208                 name = na_child_get_string (elem_instance, "name");
1209                 if (name == NULL)
1210                         continue;
1211
1212                 /* get_volume_perf may return NULL if this volume is to be ignored. */
1213                 v = get_volume_perf (cvp, name);
1214                 if (v == NULL)
1215                         continue;
1216
1217                 elem_counters = na_elem_child (elem_instance, "counters");
1218                 if (elem_counters == NULL)
1219                         continue;
1220
1221                 iter_counters = na_child_iterator (elem_counters);
1222                 for (elem_counter = na_iterator_next(&iter_counters);
1223                                 elem_counter != NULL;
1224                                 elem_counter = na_iterator_next(&iter_counters))
1225                 {
1226                         const char *name;
1227                         uint64_t value;
1228
1229                         name = na_child_get_string (elem_counter, "name");
1230                         if (name == NULL)
1231                                 continue;
1232
1233                         value = na_child_get_uint64 (elem_counter, "value", UINT64_MAX);
1234                         if (value == UINT64_MAX)
1235                                 continue;
1236
1237                         if (!strcmp(name, "read_data")) {
1238                                 perf_data.read_bytes = value;
1239                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_READ;
1240                         } else if (!strcmp(name, "write_data")) {
1241                                 perf_data.write_bytes = value;
1242                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_WRITE;
1243                         } else if (!strcmp(name, "read_ops")) {
1244                                 perf_data.read_ops = value;
1245                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_READ;
1246                         } else if (!strcmp(name, "write_ops")) {
1247                                 perf_data.write_ops = value;
1248                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_WRITE;
1249                         } else if (!strcmp(name, "read_latency")) {
1250                                 perf_data.read_latency = value;
1251                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_READ;
1252                         } else if (!strcmp(name, "write_latency")) {
1253                                 perf_data.write_latency = value;
1254                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_WRITE;
1255                         }
1256                 } /* for (elem_counter) */
1257
1258                 submit_volume_perf_data (hostname, v, &perf_data);
1259         } /* for (volume) */
1260
1261         return (0);
1262 } /* }}} int cna_handle_volume_perf_data */
1263
1264 static int cna_setup_volume_perf (cfg_volume_perf_t *cd) /* {{{ */
1265 {
1266         na_elem_t *e;
1267
1268         if (cd == NULL)
1269                 return (EINVAL);
1270
1271         if (cd->query != NULL)
1272                 return (0);
1273
1274         cd->query = na_elem_new ("perf-object-get-instances");
1275         if (cd->query == NULL)
1276         {
1277                 ERROR ("netapp plugin: na_elem_new failed.");
1278                 return (-1);
1279         }
1280         na_child_add_string (cd->query, "objectname", "volume");
1281
1282         e = na_elem_new("counters");
1283         if (e == NULL)
1284         {
1285                 na_elem_free (cd->query);
1286                 cd->query = NULL;
1287                 ERROR ("netapp plugin: na_elem_new failed.");
1288                 return (-1);
1289         }
1290         /* "foo" means: This string has to be here but the content doesn't matter. */
1291         na_child_add_string(e, "foo", "read_ops");
1292         na_child_add_string(e, "foo", "write_ops");
1293         na_child_add_string(e, "foo", "read_data");
1294         na_child_add_string(e, "foo", "write_data");
1295         na_child_add_string(e, "foo", "read_latency");
1296         na_child_add_string(e, "foo", "write_latency");
1297         na_child_add(cd->query, e);
1298
1299         return (0);
1300 } /* }}} int cna_setup_volume_perf */
1301
1302 static int cna_query_volume_perf (host_config_t *host) /* {{{ */
1303 {
1304         na_elem_t *data;
1305         int status;
1306         time_t now;
1307
1308         if (host == NULL)
1309                 return (EINVAL);
1310
1311         /* If the user did not configure volume performance statistics, return
1312          * without doing anything. */
1313         if (host->cfg_volume_perf == NULL)
1314                 return (0);
1315
1316         now = time (NULL);
1317         if ((host->cfg_volume_perf->interval.interval + host->cfg_volume_perf->interval.last_read) > now)
1318                 return (0);
1319
1320         status = cna_setup_volume_perf (host->cfg_volume_perf);
1321         if (status != 0)
1322                 return (status);
1323         assert (host->cfg_volume_perf->query != NULL);
1324
1325         data = na_server_invoke_elem (host->srv, host->cfg_volume_perf->query);
1326         if (na_results_status (data) != NA_OK)
1327         {
1328                 ERROR ("netapp plugin: cna_query_volume_perf: na_server_invoke_elem failed: %s",
1329                                 na_results_reason (data));
1330                 na_elem_free (data);
1331                 return (-1);
1332         }
1333
1334         status = cna_handle_volume_perf_data (host->name, host->cfg_volume_perf, data);
1335
1336         if (status == 0)
1337                 host->cfg_volume_perf->interval.last_read = now;
1338
1339         na_elem_free (data);
1340         return (status);
1341 } /* }}} int cna_query_volume_perf */
1342
1343 /* Data corresponding to <VolumeUsage /> */
1344 static int cna_submit_volume_usage_data (const char *hostname, /* {{{ */
1345                 cfg_volume_usage_t *cfg_volume)
1346 {
1347         data_volume_usage_t *v;
1348
1349         for (v = cfg_volume->volumes; v != NULL; v = v->next)
1350         {
1351                 uint64_t norm_used = v->norm_used;
1352                 uint64_t norm_free = v->norm_free;
1353                 uint64_t sis_saved = v->sis_saved;
1354                 uint64_t snap_reserve_used = 0;
1355                 uint64_t snap_reserve_free = v->snap_reserved;
1356                 uint64_t snap_norm_used = v->snap_used;
1357
1358                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_USED | HAVE_VOLUME_USAGE_SNAP_RSVD)) {
1359                         if (v->snap_reserved > v->snap_used) {
1360                                 snap_reserve_free = v->snap_reserved - v->snap_used;
1361                                 snap_reserve_used = v->snap_used;
1362                                 snap_norm_used = 0;
1363                         } else {
1364                                 snap_reserve_free = 0;
1365                                 snap_reserve_used = v->snap_reserved;
1366                                 snap_norm_used = v->snap_used - v->snap_reserved;
1367                                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_NORM_USED)
1368                                                 && (norm_used >= snap_norm_used))
1369                                         norm_used -= snap_norm_used;
1370                         }
1371                 }
1372
1373                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_NORM_FREE))
1374                         submit_double (hostname, /* plugin instance = */ v->name,
1375                                         "df_complex", "free",
1376                                         (double) norm_free, /* timestamp = */ 0);
1377
1378                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SIS_SAVED))
1379                         submit_double (hostname, /* plugin instance = */ v->name,
1380                                         "df_complex", "sis_saved",
1381                                         (double) sis_saved, /* timestamp = */ 0);
1382
1383                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_NORM_USED))
1384                         submit_double (hostname, /* plugin instance = */ v->name,
1385                                         "df_complex", "used",
1386                                         (double) norm_used, /* timestamp = */ 0);
1387
1388                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_RSVD))
1389                         submit_double (hostname, /* plugin instance = */ v->name,
1390                                         "df_complex", "snap_reserved",
1391                                         (double) snap_reserve_free, /* timestamp = */ 0);
1392
1393                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_USED | HAVE_VOLUME_USAGE_SNAP_RSVD))
1394                         submit_double (hostname, /* plugin instance = */ v->name,
1395                                         "df_complex", "snap_reserve_used",
1396                                         (double) snap_reserve_used, /* timestamp = */ 0);
1397
1398                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_USED))
1399                         submit_double (hostname, /* plugin instance = */ v->name,
1400                                         "df_complex", "snap_normal_used",
1401                                         (double) snap_norm_used, /* timestamp = */ 0);
1402
1403                 /* Clear all the HAVE_* flags */
1404                 v->flags &= ~HAVE_VOLUME_USAGE_ALL;
1405         } /* for (v = cfg_volume->volumes) */
1406
1407         return (0);
1408 } /* }}} int cna_submit_volume_usage_data */
1409
1410 static void cna_handle_volume_snap_usage(const host_config_t *host, data_volume_usage_t *v)
1411 {
1412         uint64_t snap_used = 0, value;
1413         na_elem_t *data, *elem_snap, *elem_snapshots;
1414         na_elem_iter_t iter_snap;
1415
1416         data = na_server_invoke_elem(host->srv, v->snap_query);
1417         if (na_results_status(data) != NA_OK)
1418         {
1419                 if (na_results_errno(data) != EVOLUMEOFFLINE)
1420                         ERROR ("netapp plugin: cna_handle_volume_snap_usage: na_server_invoke_elem for "
1421                                         "volume \"%s\" failed with error %d: %s", v->name,
1422                                         na_results_errno(data), na_results_reason(data));
1423                 na_elem_free(data);
1424                 return;
1425         }
1426
1427         elem_snapshots = na_elem_child (data, "snapshots");
1428         if (elem_snapshots == NULL)
1429         {
1430                 ERROR ("netapp plugin: cna_handle_volume_snap_usage: "
1431                                 "na_elem_child (\"snapshots\") failed.");
1432                 na_elem_free(data);
1433                 return;
1434         }
1435
1436         iter_snap = na_child_iterator (elem_snapshots);
1437         for (elem_snap = na_iterator_next (&iter_snap);
1438                         elem_snap != NULL;
1439                         elem_snap = na_iterator_next (&iter_snap))
1440         {
1441                 value = na_child_get_uint64(elem_snap, "cumulative-total", 0);
1442                 /* "cumulative-total" is the total size of the oldest snapshot plus all
1443                  * newer ones in blocks (1KB). We therefore are looking for the highest
1444                  * number of all snapshots - that's the size required for the snapshots. */
1445                 if (value > snap_used)
1446                         snap_used = value;
1447         }
1448         na_elem_free (data);
1449         /* snap_used is in 1024 byte blocks */
1450         v->snap_used = snap_used * 1024;
1451         v->flags |= HAVE_VOLUME_USAGE_SNAP_USED;
1452 } /* }}} void cna_handle_volume_snap_usage */
1453
1454 static int cna_handle_volume_usage_data (const host_config_t *host, /* {{{ */
1455                 cfg_volume_usage_t *cfg_volume, na_elem_t *data)
1456 {
1457         na_elem_t *elem_volume;
1458         na_elem_t *elem_volumes;
1459         na_elem_iter_t iter_volume;
1460
1461         elem_volumes = na_elem_child (data, "volumes");
1462         if (elem_volumes == NULL)
1463         {
1464                 ERROR ("netapp plugin: cna_handle_volume_usage_data: "
1465                                 "na_elem_child (\"volumes\") failed.");
1466                 return (-1);
1467         }
1468
1469         iter_volume = na_child_iterator (elem_volumes);
1470         for (elem_volume = na_iterator_next (&iter_volume);
1471                         elem_volume != NULL;
1472                         elem_volume = na_iterator_next (&iter_volume))
1473         {
1474                 const char *volume_name, *state;
1475
1476                 data_volume_usage_t *v;
1477                 uint64_t value;
1478
1479                 na_elem_t *sis;
1480                 const char *sis_state;
1481                 uint64_t sis_saved_reported;
1482
1483                 volume_name = na_child_get_string (elem_volume, "name");
1484                 if (volume_name == NULL)
1485                         continue;
1486
1487                 state = na_child_get_string (elem_volume, "state");
1488                 if ((state == NULL) || (strcmp(state, "online") != 0))
1489                         continue;
1490
1491                 /* get_volume_usage may return NULL if the volume is to be ignored. */
1492                 v = get_volume_usage (cfg_volume, volume_name);
1493                 if (v == NULL)
1494                         continue;
1495
1496                 if ((v->flags & CFG_VOLUME_USAGE_SNAP) != 0)
1497                         cna_handle_volume_snap_usage(host, v);
1498                 
1499                 if ((v->flags & CFG_VOLUME_USAGE_DF) == 0)
1500                         continue;
1501
1502                 /* 2^4 exa-bytes? This will take a while ;) */
1503                 value = na_child_get_uint64(elem_volume, "size-available", UINT64_MAX);
1504                 if (value != UINT64_MAX) {
1505                         v->norm_free = value;
1506                         v->flags |= HAVE_VOLUME_USAGE_NORM_FREE;
1507                 }
1508
1509                 value = na_child_get_uint64(elem_volume, "size-used", UINT64_MAX);
1510                 if (value != UINT64_MAX) {
1511                         v->norm_used = value;
1512                         v->flags |= HAVE_VOLUME_USAGE_NORM_USED;
1513                 }
1514
1515                 value = na_child_get_uint64(elem_volume, "snapshot-blocks-reserved", UINT64_MAX);
1516                 if (value != UINT64_MAX) {
1517                         /* 1 block == 1024 bytes  as per API docs */
1518                         v->snap_reserved = 1024 * value;
1519                         v->flags |= HAVE_VOLUME_USAGE_SNAP_RSVD;
1520                 }
1521
1522                 sis = na_elem_child(elem_volume, "sis");
1523                 if (sis == NULL)
1524                         continue;
1525
1526                 sis_state = na_child_get_string(sis, "state");
1527                 if (sis_state == NULL)
1528                         continue;
1529
1530                 /* If SIS is not enabled, there's nothing left to do for this volume. */
1531                 if (strcmp ("enabled", sis_state) != 0)
1532                         continue;
1533
1534                 sis_saved_reported = na_child_get_uint64(sis, "size-saved", UINT64_MAX);
1535                 if (sis_saved_reported == UINT64_MAX)
1536                         continue;
1537
1538                 /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
1539                 if ((sis_saved_reported >> 32) != 0) {
1540                         /* In case they ever fix this bug. */
1541                         v->sis_saved = sis_saved_reported;
1542                         v->flags |= HAVE_VOLUME_USAGE_SIS_SAVED;
1543                 } else { /* really hacky work-around code. {{{ */
1544                         uint64_t sis_saved_percent;
1545                         uint64_t sis_saved_guess;
1546                         uint64_t overflow_guess;
1547                         uint64_t guess1, guess2, guess3;
1548
1549                         /* Check if we have v->norm_used. Without it, we cannot calculate
1550                          * sis_saved_guess. */
1551                         if ((v->flags & HAVE_VOLUME_USAGE_NORM_USED) == 0)
1552                                 continue;
1553
1554                         sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
1555                         if (sis_saved_percent > 100)
1556                                 continue;
1557
1558                         /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
1559                          * will hopefully be fixed in later versions. To work around the bug, try
1560                          * to figure out how often the 32bit integer wrapped around by using the
1561                          * "percentage-saved" value. Because the percentage is in the range
1562                          * [0-100], this should work as long as the saved space does not exceed
1563                          * 400 GBytes. */
1564                         /* percentage-saved = size-saved / (size-saved + size-used) */
1565                         if (sis_saved_percent < 100)
1566                                 sis_saved_guess = v->norm_used * sis_saved_percent / (100 - sis_saved_percent);
1567                         else
1568                                 sis_saved_guess = v->norm_used;
1569
1570                         overflow_guess = sis_saved_guess >> 32;
1571                         guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
1572                         guess2 = (overflow_guess << 32) + sis_saved_reported;
1573                         guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
1574
1575                         if (sis_saved_guess < guess2) {
1576                                 if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
1577                                         v->sis_saved = guess1;
1578                                 else
1579                                         v->sis_saved = guess2;
1580                         } else {
1581                                 if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
1582                                         v->sis_saved = guess2;
1583                                 else
1584                                         v->sis_saved = guess3;
1585                         }
1586                         v->flags |= HAVE_VOLUME_USAGE_SIS_SAVED;
1587                 } /* }}} end of 32-bit workaround */
1588         } /* for (elem_volume) */
1589
1590         return (cna_submit_volume_usage_data (host->name, cfg_volume));
1591 } /* }}} int cna_handle_volume_usage_data */
1592
1593 static int cna_setup_volume_usage (cfg_volume_usage_t *cvu) /* {{{ */
1594 {
1595         if (cvu == NULL)
1596                 return (EINVAL);
1597
1598         if (cvu->query != NULL)
1599                 return (0);
1600
1601         cvu->query = na_elem_new ("volume-list-info");
1602         if (cvu->query == NULL)
1603         {
1604                 ERROR ("netapp plugin: na_elem_new failed.");
1605                 return (-1);
1606         }
1607
1608         return (0);
1609 } /* }}} int cna_setup_volume_usage */
1610
1611 static int cna_query_volume_usage (host_config_t *host) /* {{{ */
1612 {
1613         na_elem_t *data;
1614         int status;
1615         time_t now;
1616
1617         if (host == NULL)
1618                 return (EINVAL);
1619
1620         /* If the user did not configure volume_usage statistics, return without
1621          * doing anything. */
1622         if (host->cfg_volume_usage == NULL)
1623                 return (0);
1624
1625         now = time (NULL);
1626         if ((host->cfg_volume_usage->interval.interval + host->cfg_volume_usage->interval.last_read) > now)
1627                 return (0);
1628
1629         status = cna_setup_volume_usage (host->cfg_volume_usage);
1630         if (status != 0)
1631                 return (status);
1632         assert (host->cfg_volume_usage->query != NULL);
1633
1634         data = na_server_invoke_elem(host->srv, host->cfg_volume_usage->query);
1635         if (na_results_status (data) != NA_OK)
1636         {
1637                 ERROR ("netapp plugin: cna_query_volume_usage: na_server_invoke_elem failed: %s",
1638                                 na_results_reason (data));
1639                 na_elem_free (data);
1640                 return (-1);
1641         }
1642
1643         status = cna_handle_volume_usage_data (host, host->cfg_volume_usage, data);
1644
1645         if (status == 0)
1646                 host->cfg_volume_usage->interval.last_read = now;
1647
1648         na_elem_free (data);
1649         return (status);
1650 } /* }}} int cna_query_volume_usage */
1651
1652 /* Data corresponding to <System /> */
1653 static int cna_handle_system_data (const char *hostname, /* {{{ */
1654                 cfg_system_t *cfg_system, na_elem_t *data)
1655 {
1656         na_elem_t *instances;
1657         na_elem_t *counter;
1658         na_elem_iter_t counter_iter;
1659
1660         counter_t disk_read = 0, disk_written = 0;
1661         counter_t net_recv = 0, net_sent = 0;
1662         counter_t cpu_busy = 0, cpu_total = 0;
1663         uint32_t counter_flags = 0;
1664
1665         const char *instance;
1666         time_t timestamp;
1667         
1668         timestamp = (time_t) na_child_get_uint64 (data, "timestamp", 0);
1669
1670         instances = na_elem_child(na_elem_child (data, "instances"), "instance-data");
1671         if (instances == NULL)
1672         {
1673                 ERROR ("netapp plugin: cna_handle_system_data: "
1674                                 "na_elem_child (\"instances\") failed.");
1675                 return (-1);
1676         }
1677
1678         instance = na_child_get_string (instances, "name");
1679         if (instance == NULL)
1680         {
1681                 ERROR ("netapp plugin: cna_handle_system_data: "
1682                                 "na_child_get_string (\"name\") failed.");
1683                 return (-1);
1684         }
1685
1686         counter_iter = na_child_iterator (na_elem_child (instances, "counters"));
1687         for (counter = na_iterator_next (&counter_iter);
1688                         counter != NULL;
1689                         counter = na_iterator_next (&counter_iter))
1690         {
1691                 const char *name;
1692                 uint64_t value;
1693
1694                 name = na_child_get_string(counter, "name");
1695                 if (name == NULL)
1696                         continue;
1697
1698                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
1699                 if (value == UINT64_MAX)
1700                         continue;
1701
1702                 if (!strcmp(name, "disk_data_read")) {
1703                         disk_read = (counter_t) (value * 1024);
1704                         counter_flags |= 0x01;
1705                 } else if (!strcmp(name, "disk_data_written")) {
1706                         disk_written = (counter_t) (value * 1024);
1707                         counter_flags |= 0x02;
1708                 } else if (!strcmp(name, "net_data_recv")) {
1709                         net_recv = (counter_t) (value * 1024);
1710                         counter_flags |= 0x04;
1711                 } else if (!strcmp(name, "net_data_sent")) {
1712                         net_sent = (counter_t) (value * 1024);
1713                         counter_flags |= 0x08;
1714                 } else if (!strcmp(name, "cpu_busy")) {
1715                         cpu_busy = (counter_t) value;
1716                         counter_flags |= 0x10;
1717                 } else if (!strcmp(name, "cpu_elapsed_time")) {
1718                         cpu_total = (counter_t) value;
1719                         counter_flags |= 0x20;
1720                 } else if ((cfg_system->flags & CFG_SYSTEM_OPS)
1721                                 && (value > 0) && (strlen(name) > 4)
1722                                 && (!strcmp(name + strlen(name) - 4, "_ops"))) {
1723                         submit_counter (hostname, instance, "disk_ops_complex", name,
1724                                         (counter_t) value, timestamp);
1725                 }
1726         } /* for (counter) */
1727
1728         if ((cfg_system->flags & CFG_SYSTEM_DISK)
1729                         && (HAS_ALL_FLAGS (counter_flags, 0x01 | 0x02)))
1730                 submit_two_counters (hostname, instance, "disk_octets", NULL,
1731                                 disk_read, disk_written, timestamp);
1732                                 
1733         if ((cfg_system->flags & CFG_SYSTEM_NET)
1734                         && (HAS_ALL_FLAGS (counter_flags, 0x04 | 0x08)))
1735                 submit_two_counters (hostname, instance, "if_octets", NULL,
1736                                 net_recv, net_sent, timestamp);
1737
1738         if ((cfg_system->flags & CFG_SYSTEM_CPU)
1739                         && (HAS_ALL_FLAGS (counter_flags, 0x10 | 0x20)))
1740         {
1741                 submit_counter (hostname, instance, "cpu", "system",
1742                                 cpu_busy, timestamp);
1743                 submit_counter (hostname, instance, "cpu", "idle",
1744                                 cpu_total - cpu_busy, timestamp);
1745         }
1746
1747         return (0);
1748 } /* }}} int cna_handle_system_data */
1749
1750 static int cna_setup_system (cfg_system_t *cs) /* {{{ */
1751 {
1752         if (cs == NULL)
1753                 return (EINVAL);
1754
1755         if (cs->query != NULL)
1756                 return (0);
1757
1758         cs->query = na_elem_new ("perf-object-get-instances");
1759         if (cs->query == NULL)
1760         {
1761                 ERROR ("netapp plugin: na_elem_new failed.");
1762                 return (-1);
1763         }
1764         na_child_add_string (cs->query, "objectname", "system");
1765
1766         return (0);
1767 } /* }}} int cna_setup_system */
1768
1769 static int cna_query_system (host_config_t *host) /* {{{ */
1770 {
1771         na_elem_t *data;
1772         int status;
1773         time_t now;
1774
1775         if (host == NULL)
1776                 return (EINVAL);
1777
1778         /* If system statistics were not configured, return without doing anything. */
1779         if (host->cfg_system == NULL)
1780                 return (0);
1781
1782         now = time (NULL);
1783         if ((host->cfg_system->interval.interval + host->cfg_system->interval.last_read) > now)
1784                 return (0);
1785
1786         status = cna_setup_system (host->cfg_system);
1787         if (status != 0)
1788                 return (status);
1789         assert (host->cfg_system->query != NULL);
1790
1791         data = na_server_invoke_elem(host->srv, host->cfg_system->query);
1792         if (na_results_status (data) != NA_OK)
1793         {
1794                 ERROR ("netapp plugin: cna_query_system: na_server_invoke_elem failed: %s",
1795                                 na_results_reason (data));
1796                 na_elem_free (data);
1797                 return (-1);
1798         }
1799
1800         status = cna_handle_system_data (host->name, host->cfg_system, data);
1801
1802         if (status == 0)
1803                 host->cfg_system->interval.last_read = now;
1804
1805         na_elem_free (data);
1806         return (status);
1807 } /* }}} int cna_query_system */
1808
1809 /*
1810  * Configuration handling
1811  */
1812 /* Sets a given flag if the boolean argument is true and unsets the flag if it
1813  * is false. On error, the flag-field is not changed. */
1814 static int cna_config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */
1815                 uint32_t *flags, uint32_t flag)
1816 {
1817         if ((ci == NULL) || (flags == NULL))
1818                 return (EINVAL);
1819
1820         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1821         {
1822                 WARNING ("netapp plugin: The %s option needs exactly one boolean argument.",
1823                                 ci->key);
1824                 return (-1);
1825         }
1826
1827         if (ci->values[0].value.boolean)
1828                 *flags |= flag;
1829         else
1830                 *flags &= ~flag;
1831
1832         return (0);
1833 } /* }}} int cna_config_bool_to_flag */
1834
1835 /* Handling of the "Interval" option which is allowed in every block. */
1836 static int cna_config_get_interval (const oconfig_item_t *ci, /* {{{ */
1837                 cna_interval_t *out_interval)
1838 {
1839         time_t tmp;
1840
1841         if ((ci == NULL) || (out_interval == NULL))
1842                 return (EINVAL);
1843
1844         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1845         {
1846                 WARNING ("netapp plugin: The `Interval' option needs exactly one numeric argument.");
1847                 return (-1);
1848         }
1849
1850         tmp = (time_t) (ci->values[0].value.number + .5);
1851         if (tmp < 1)
1852         {
1853                 WARNING ("netapp plugin: The `Interval' option needs a positive integer argument.");
1854                 return (-1);
1855         }
1856
1857         out_interval->interval = tmp;
1858         out_interval->last_read = 0;
1859
1860         return (0);
1861 } /* }}} int cna_config_get_interval */
1862
1863 /* Handling of the "GetIO", "GetOps" and "GetLatency" options within a
1864  * <VolumePerf /> block. */
1865 static void cna_config_volume_perf_option (cfg_volume_perf_t *cvp, /* {{{ */
1866                 const oconfig_item_t *ci)
1867 {
1868         char *name;
1869         ignorelist_t * il;
1870
1871         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1872         {
1873                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
1874                                 ci->key);
1875                 return;
1876         }
1877
1878         name = ci->values[0].value.string;
1879
1880         if (strcasecmp ("GetIO", ci->key) == 0)
1881                 il = cvp->il_octets;
1882         else if (strcasecmp ("GetOps", ci->key) == 0)
1883                 il = cvp->il_operations;
1884         else if (strcasecmp ("GetLatency", ci->key) == 0)
1885                 il = cvp->il_latency;
1886         else
1887                 return;
1888
1889         ignorelist_add (il, name);
1890 } /* }}} void cna_config_volume_perf_option */
1891
1892 /* Handling of the "IgnoreSelectedIO", "IgnoreSelectedOps" and
1893  * "IgnoreSelectedLatency" options within a <VolumePerf /> block. */
1894 static void cna_config_volume_perf_default (cfg_volume_perf_t *cvp, /* {{{ */
1895                 const oconfig_item_t *ci)
1896 {
1897         ignorelist_t *il;
1898
1899         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1900         {
1901                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
1902                                 ci->key);
1903                 return;
1904         }
1905
1906         if (strcasecmp ("IgnoreSelectedIO", ci->key) == 0)
1907                 il = cvp->il_octets;
1908         else if (strcasecmp ("IgnoreSelectedOps", ci->key) == 0)
1909                 il = cvp->il_operations;
1910         else if (strcasecmp ("IgnoreSelectedLatency", ci->key) == 0)
1911                 il = cvp->il_latency;
1912         else
1913                 return;
1914
1915         if (ci->values[0].value.boolean)
1916                 ignorelist_set_invert (il, /* invert = */ 0);
1917         else
1918                 ignorelist_set_invert (il, /* invert = */ 1);
1919 } /* }}} void cna_config_volume_perf_default */
1920
1921 /* Corresponds to a <Disks /> block */
1922 /*
1923  * <VolumePerf>
1924  *   GetIO "vol0"
1925  *   GetIO "vol1"
1926  *   IgnoreSelectedIO false
1927  *
1928  *   GetOps "vol0"
1929  *   GetOps "vol2"
1930  *   IgnoreSelectedOps false
1931  *
1932  *   GetLatency "vol2"
1933  *   GetLatency "vol3"
1934  *   IgnoreSelectedLatency false
1935  * </VolumePerf>
1936  */
1937 /* Corresponds to a <VolumePerf /> block */
1938 static int cna_config_volume_performance (host_config_t *host, /* {{{ */
1939                 const oconfig_item_t *ci)
1940 {
1941         cfg_volume_perf_t *cfg_volume_perf;
1942         int i;
1943
1944         if ((host == NULL) || (ci == NULL))
1945                 return (EINVAL);
1946
1947         if (host->cfg_volume_perf == NULL)
1948         {
1949                 cfg_volume_perf = malloc (sizeof (*cfg_volume_perf));
1950                 if (cfg_volume_perf == NULL)
1951                         return (ENOMEM);
1952                 memset (cfg_volume_perf, 0, sizeof (*cfg_volume_perf));
1953
1954                 /* Set default flags */
1955                 cfg_volume_perf->query = NULL;
1956                 cfg_volume_perf->volumes = NULL;
1957
1958                 cfg_volume_perf->il_octets = ignorelist_create (/* invert = */ 1);
1959                 if (cfg_volume_perf->il_octets == NULL)
1960                 {
1961                         sfree (cfg_volume_perf);
1962                         return (ENOMEM);
1963                 }
1964
1965                 cfg_volume_perf->il_operations = ignorelist_create (/* invert = */ 1);
1966                 if (cfg_volume_perf->il_operations == NULL)
1967                 {
1968                         ignorelist_free (cfg_volume_perf->il_octets);
1969                         sfree (cfg_volume_perf);
1970                         return (ENOMEM);
1971                 }
1972
1973                 cfg_volume_perf->il_latency = ignorelist_create (/* invert = */ 1);
1974                 if (cfg_volume_perf->il_latency == NULL)
1975                 {
1976                         ignorelist_free (cfg_volume_perf->il_octets);
1977                         ignorelist_free (cfg_volume_perf->il_operations);
1978                         sfree (cfg_volume_perf);
1979                         return (ENOMEM);
1980                 }
1981
1982                 host->cfg_volume_perf = cfg_volume_perf;
1983         }
1984         cfg_volume_perf = host->cfg_volume_perf;
1985         
1986         for (i = 0; i < ci->children_num; ++i) {
1987                 oconfig_item_t *item = ci->children + i;
1988                 
1989                 /* if (!item || !item->key || !*item->key) continue; */
1990                 if (strcasecmp(item->key, "Interval") == 0)
1991                         cna_config_get_interval (item, &cfg_volume_perf->interval);
1992                 else if (!strcasecmp(item->key, "GetIO"))
1993                         cna_config_volume_perf_option (cfg_volume_perf, item);
1994                 else if (!strcasecmp(item->key, "GetOps"))
1995                         cna_config_volume_perf_option (cfg_volume_perf, item);
1996                 else if (!strcasecmp(item->key, "GetLatency"))
1997                         cna_config_volume_perf_option (cfg_volume_perf, item);
1998                 else if (!strcasecmp(item->key, "IgnoreSelectedIO"))
1999                         cna_config_volume_perf_default (cfg_volume_perf, item);
2000                 else if (!strcasecmp(item->key, "IgnoreSelectedOps"))
2001                         cna_config_volume_perf_default (cfg_volume_perf, item);
2002                 else if (!strcasecmp(item->key, "IgnoreSelectedLatency"))
2003                         cna_config_volume_perf_default (cfg_volume_perf, item);
2004                 else
2005                         WARNING ("netapp plugin: The option %s is not allowed within "
2006                                         "`VolumePerf' blocks.", item->key);
2007         }
2008
2009         return (0);
2010 } /* }}} int cna_config_volume_performance */
2011
2012 /* Handling of the "GetCapacity" and "GetSnapshot" options within a
2013  * <VolumeUsage /> block. */
2014 static void cna_config_volume_usage_option (cfg_volume_usage_t *cvu, /* {{{ */
2015                 const oconfig_item_t *ci)
2016 {
2017         char *name;
2018         ignorelist_t * il;
2019
2020         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2021         {
2022                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2023                                 ci->key);
2024                 return;
2025         }
2026
2027         name = ci->values[0].value.string;
2028
2029         if (strcasecmp ("GetCapacity", ci->key) == 0)
2030                 il = cvu->il_capacity;
2031         else if (strcasecmp ("GetSnapshot", ci->key) == 0)
2032                 il = cvu->il_snapshot;
2033         else
2034                 return;
2035
2036         ignorelist_add (il, name);
2037 } /* }}} void cna_config_volume_usage_option */
2038
2039 /* Handling of the "IgnoreSelectedCapacity" and "IgnoreSelectedSnapshot"
2040  * options within a <VolumeUsage /> block. */
2041 static void cna_config_volume_usage_default (cfg_volume_usage_t *cvu, /* {{{ */
2042                 const oconfig_item_t *ci)
2043 {
2044         ignorelist_t *il;
2045
2046         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
2047         {
2048                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2049                                 ci->key);
2050                 return;
2051         }
2052
2053         if (strcasecmp ("IgnoreSelectedCapacity", ci->key) == 0)
2054                 il = cvu->il_capacity;
2055         else if (strcasecmp ("IgnoreSelectedSnapshot", ci->key) == 0)
2056                 il = cvu->il_snapshot;
2057         else
2058                 return;
2059
2060         if (ci->values[0].value.boolean)
2061                 ignorelist_set_invert (il, /* invert = */ 0);
2062         else
2063                 ignorelist_set_invert (il, /* invert = */ 1);
2064 } /* }}} void cna_config_volume_usage_default */
2065
2066 /* Corresponds to a <Disks /> block */
2067 static int cna_config_disk(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
2068         cfg_disk_t *cfg_disk;
2069         int i;
2070
2071         if ((host == NULL) || (ci == NULL))
2072                 return (EINVAL);
2073
2074         if (host->cfg_disk == NULL)
2075         {
2076                 cfg_disk = malloc (sizeof (*cfg_disk));
2077                 if (cfg_disk == NULL)
2078                         return (ENOMEM);
2079                 memset (cfg_disk, 0, sizeof (*cfg_disk));
2080
2081                 /* Set default flags */
2082                 cfg_disk->flags = CFG_DISK_ALL;
2083                 cfg_disk->query = NULL;
2084                 cfg_disk->disks = NULL;
2085
2086                 host->cfg_disk = cfg_disk;
2087         }
2088         cfg_disk = host->cfg_disk;
2089         
2090         for (i = 0; i < ci->children_num; ++i) {
2091                 oconfig_item_t *item = ci->children + i;
2092                 
2093                 /* if (!item || !item->key || !*item->key) continue; */
2094                 if (strcasecmp(item->key, "Interval") == 0)
2095                         cna_config_get_interval (item, &cfg_disk->interval);
2096                 else if (strcasecmp(item->key, "GetBusy") == 0)
2097                         cna_config_bool_to_flag (item, &cfg_disk->flags, CFG_DISK_BUSIEST);
2098         }
2099
2100         if ((cfg_disk->flags & CFG_DISK_ALL) == 0)
2101         {
2102                 NOTICE ("netapp plugin: All disk related values have been disabled. "
2103                                 "Collection of per-disk data will be disabled entirely.");
2104                 free_cfg_disk (host->cfg_disk);
2105                 host->cfg_disk = NULL;
2106         }
2107
2108         return (0);
2109 } /* }}} int cna_config_disk */
2110
2111 /* Corresponds to a <WAFL /> block */
2112 static int cna_config_wafl(host_config_t *host, oconfig_item_t *ci) /* {{{ */
2113 {
2114         cfg_wafl_t *cfg_wafl;
2115         int i;
2116
2117         if ((host == NULL) || (ci == NULL))
2118                 return (EINVAL);
2119
2120         if (host->cfg_wafl == NULL)
2121         {
2122                 cfg_wafl = malloc (sizeof (*cfg_wafl));
2123                 if (cfg_wafl == NULL)
2124                         return (ENOMEM);
2125                 memset (cfg_wafl, 0, sizeof (*cfg_wafl));
2126
2127                 /* Set default flags */
2128                 cfg_wafl->flags = CFG_WAFL_ALL;
2129
2130                 host->cfg_wafl = cfg_wafl;
2131         }
2132         cfg_wafl = host->cfg_wafl;
2133
2134         for (i = 0; i < ci->children_num; ++i) {
2135                 oconfig_item_t *item = ci->children + i;
2136                 
2137                 if (strcasecmp(item->key, "Interval") == 0)
2138                         cna_config_get_interval (item, &cfg_wafl->interval);
2139                 else if (!strcasecmp(item->key, "GetNameCache"))
2140                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_NAME_CACHE);
2141                 else if (!strcasecmp(item->key, "GetDirCache"))
2142                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_DIR_CACHE);
2143                 else if (!strcasecmp(item->key, "GetBufferCache"))
2144                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_BUF_CACHE);
2145                 else if (!strcasecmp(item->key, "GetInodeCache"))
2146                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_INODE_CACHE);
2147                 else
2148                         WARNING ("netapp plugin: The %s config option is not allowed within "
2149                                         "`WAFL' blocks.", item->key);
2150         }
2151
2152         if ((cfg_wafl->flags & CFG_WAFL_ALL) == 0)
2153         {
2154                 NOTICE ("netapp plugin: All WAFL related values have been disabled. "
2155                                 "Collection of WAFL data will be disabled entirely.");
2156                 free_cfg_wafl (host->cfg_wafl);
2157                 host->cfg_wafl = NULL;
2158         }
2159
2160         return (0);
2161 } /* }}} int cna_config_wafl */
2162
2163 /*
2164  * <VolumeUsage>
2165  *   GetCapacity "vol0"
2166  *   GetCapacity "vol1"
2167  *   GetCapacity "vol2"
2168  *   GetCapacity "vol3"
2169  *   GetCapacity "vol4"
2170  *   IgnoreSelectedCapacity false
2171  *
2172  *   GetSnapshot "vol0"
2173  *   GetSnapshot "vol3"
2174  *   GetSnapshot "vol4"
2175  *   GetSnapshot "vol7"
2176  *   IgnoreSelectedSnapshot false
2177  * </VolumeUsage>
2178  */
2179 /* Corresponds to a <VolumeUsage /> block */
2180 static int cna_config_volume_usage(host_config_t *host, /* {{{ */
2181                 const oconfig_item_t *ci)
2182 {
2183         cfg_volume_usage_t *cfg_volume_usage;
2184         int i;
2185
2186         if ((host == NULL) || (ci == NULL))
2187                 return (EINVAL);
2188
2189         if (host->cfg_volume_usage == NULL)
2190         {
2191                 cfg_volume_usage = malloc (sizeof (*cfg_volume_usage));
2192                 if (cfg_volume_usage == NULL)
2193                         return (ENOMEM);
2194                 memset (cfg_volume_usage, 0, sizeof (*cfg_volume_usage));
2195
2196                 /* Set default flags */
2197                 cfg_volume_usage->query = NULL;
2198                 cfg_volume_usage->volumes = NULL;
2199
2200                 cfg_volume_usage->il_capacity = ignorelist_create (/* invert = */ 1);
2201                 if (cfg_volume_usage->il_capacity == NULL)
2202                 {
2203                         sfree (cfg_volume_usage);
2204                         return (ENOMEM);
2205                 }
2206
2207                 cfg_volume_usage->il_snapshot = ignorelist_create (/* invert = */ 1);
2208                 if (cfg_volume_usage->il_snapshot == NULL)
2209                 {
2210                         ignorelist_free (cfg_volume_usage->il_capacity);
2211                         sfree (cfg_volume_usage);
2212                         return (ENOMEM);
2213                 }
2214
2215                 host->cfg_volume_usage = cfg_volume_usage;
2216         }
2217         cfg_volume_usage = host->cfg_volume_usage;
2218         
2219         for (i = 0; i < ci->children_num; ++i) {
2220                 oconfig_item_t *item = ci->children + i;
2221                 
2222                 /* if (!item || !item->key || !*item->key) continue; */
2223                 if (strcasecmp(item->key, "Interval") == 0)
2224                         cna_config_get_interval (item, &cfg_volume_usage->interval);
2225                 else if (!strcasecmp(item->key, "GetCapacity"))
2226                         cna_config_volume_usage_option (cfg_volume_usage, item);
2227                 else if (!strcasecmp(item->key, "GetSnapshot"))
2228                         cna_config_volume_usage_option (cfg_volume_usage, item);
2229                 else if (!strcasecmp(item->key, "IgnoreSelectedCapacity"))
2230                         cna_config_volume_usage_default (cfg_volume_usage, item);
2231                 else if (!strcasecmp(item->key, "IgnoreSelectedSnapshot"))
2232                         cna_config_volume_usage_default (cfg_volume_usage, item);
2233                 else
2234                         WARNING ("netapp plugin: The option %s is not allowed within "
2235                                         "`VolumeUsage' blocks.", item->key);
2236         }
2237
2238         return (0);
2239 } /* }}} int cna_config_volume_usage */
2240
2241 /* Corresponds to a <System /> block */
2242 static int cna_config_system (host_config_t *host, /* {{{ */
2243                 oconfig_item_t *ci)
2244 {
2245         cfg_system_t *cfg_system;
2246         int i;
2247         
2248         if ((host == NULL) || (ci == NULL))
2249                 return (EINVAL);
2250
2251         if (host->cfg_system == NULL)
2252         {
2253                 cfg_system = malloc (sizeof (*cfg_system));
2254                 if (cfg_system == NULL)
2255                         return (ENOMEM);
2256                 memset (cfg_system, 0, sizeof (*cfg_system));
2257
2258                 /* Set default flags */
2259                 cfg_system->flags = CFG_SYSTEM_ALL;
2260                 cfg_system->query = NULL;
2261
2262                 host->cfg_system = cfg_system;
2263         }
2264         cfg_system = host->cfg_system;
2265
2266         for (i = 0; i < ci->children_num; ++i) {
2267                 oconfig_item_t *item = ci->children + i;
2268
2269                 if (strcasecmp(item->key, "Interval") == 0) {
2270                         cna_config_get_interval (item, &cfg_system->interval);
2271                 } else if (!strcasecmp(item->key, "GetCPULoad")) {
2272                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_CPU);
2273                 } else if (!strcasecmp(item->key, "GetInterfaces")) {
2274                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_NET);
2275                 } else if (!strcasecmp(item->key, "GetDiskOps")) {
2276                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_OPS);
2277                 } else if (!strcasecmp(item->key, "GetDiskIO")) {
2278                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_DISK);
2279                 } else {
2280                         WARNING ("netapp plugin: The %s config option is not allowed within "
2281                                         "`System' blocks.", item->key);
2282                 }
2283         }
2284
2285         if ((cfg_system->flags & CFG_SYSTEM_ALL) == 0)
2286         {
2287                 NOTICE ("netapp plugin: All system related values have been disabled. "
2288                                 "Collection of system data will be disabled entirely.");
2289                 free_cfg_system (host->cfg_system);
2290                 host->cfg_system = NULL;
2291         }
2292
2293         return (0);
2294 } /* }}} int cna_config_system */
2295
2296 /* Corresponds to a <Host /> block. */
2297 static host_config_t *cna_config_host (const oconfig_item_t *ci, /* {{{ */
2298                 const host_config_t *default_host)
2299 {
2300         oconfig_item_t *item;
2301         host_config_t *host;
2302         int status;
2303         int i;
2304         
2305         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2306                 WARNING("netapp plugin: \"Host\" needs exactly one string argument. Ignoring host block.");
2307                 return 0;
2308         }
2309
2310         host = malloc(sizeof(*host));
2311         memcpy (host, default_host, sizeof (*host));
2312
2313         status = cf_util_get_string (ci, &host->name);
2314         if (status != 0)
2315         {
2316                 sfree (host);
2317                 return (NULL);
2318         }
2319
2320         for (i = 0; i < ci->children_num; ++i) {
2321                 item = ci->children + i;
2322
2323                 status = 0;
2324
2325                 if (!strcasecmp(item->key, "Address")) {
2326                         status = cf_util_get_string (item, &host->host);
2327                 } else if (!strcasecmp(item->key, "Port")) {
2328                         int tmp;
2329
2330                         tmp = cf_util_get_port_number (item);
2331                         if (tmp > 0)
2332                                 host->port = tmp;
2333                 } else if (!strcasecmp(item->key, "Protocol")) {
2334                         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"))) {
2335                                 WARNING("netapp plugin: \"Protocol\" needs to be either \"http\" or \"https\". Ignoring host block \"%s\".", ci->values[0].value.string);
2336                                 return 0;
2337                         }
2338                         if (!strcasecmp(item->values[0].value.string, "http")) host->protocol = NA_SERVER_TRANSPORT_HTTP;
2339                         else host->protocol = NA_SERVER_TRANSPORT_HTTPS;
2340                 } else if (!strcasecmp(item->key, "User")) {
2341                         status = cf_util_get_string (item, &host->username);
2342                 } else if (!strcasecmp(item->key, "Password")) {
2343                         status = cf_util_get_string (item, &host->password);
2344                 } else if (!strcasecmp(item->key, "Interval")) {
2345                         if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_NUMBER || item->values[0].value.number != (int) item->values[0].value.number || item->values[0].value.number < 2) {
2346                                 WARNING("netapp plugin: \"Interval\" of host %s needs exactly one integer argument.", ci->values[0].value.string);
2347                                 continue;
2348                         }
2349                         host->interval = item->values[0].value.number;
2350                 } else if (!strcasecmp(item->key, "WAFL")) {
2351                         cna_config_wafl(host, item);
2352                 } else if (!strcasecmp(item->key, "Disks")) {
2353                         cna_config_disk(host, item);
2354                 } else if (!strcasecmp(item->key, "VolumePerf")) {
2355                         cna_config_volume_performance(host, item);
2356                 } else if (!strcasecmp(item->key, "VolumeUsage")) {
2357                         cna_config_volume_usage(host, item);
2358                 } else if (!strcasecmp(item->key, "System")) {
2359                         cna_config_system(host, item);
2360                 } else {
2361                         WARNING("netapp plugin: Ignoring unknown config option \"%s\" in host block \"%s\".",
2362                                         item->key, ci->values[0].value.string);
2363                 }
2364
2365                 if (status != 0)
2366                         break;
2367         }
2368
2369         if (host->host == NULL)
2370                 host->host = strdup (host->name);
2371
2372         if (host->host == NULL)
2373                 status = -1;
2374
2375         if (host->port <= 0)
2376                 host->port = (host->protocol == NA_SERVER_TRANSPORT_HTTP) ? 80 : 443;
2377
2378         if ((host->username == NULL) || (host->password == NULL)) {
2379                 WARNING("netapp plugin: Please supply login information for host \"%s\". "
2380                                 "Ignoring host block.", host->name);
2381                 status = -1;
2382         }
2383
2384         if (status != 0)
2385         {
2386                 free_host_config (host);
2387                 return (NULL);
2388         }
2389
2390         return host;
2391 } /* }}} host_config_t *cna_config_host */
2392
2393 /*
2394  * Callbacks registered with the daemon
2395  *
2396  * Pretty standard stuff here.
2397  */
2398 static int cna_init(void) { /* {{{ */
2399         char err[256];
2400         host_config_t *host;
2401         
2402         if (!global_host_config) {
2403                 WARNING("netapp plugin: Plugin loaded but no hosts defined.");
2404                 return 1;
2405         }
2406
2407         memset (err, 0, sizeof (err));
2408         if (!na_startup(err, sizeof(err))) {
2409                 err[sizeof (err) - 1] = 0;
2410                 ERROR("netapp plugin: Error initializing netapp API: %s", err);
2411                 return 1;
2412         }
2413
2414         for (host = global_host_config; host; host = host->next) {
2415                 /* Request version 1.1 of the ONTAP API */
2416                 host->srv = na_server_open(host->host,
2417                                 /* major version = */ 1, /* minor version = */ 1); 
2418                 if (host->srv == NULL) {
2419                         ERROR ("netapp plugin: na_server_open (%s) failed.", host->host);
2420                         continue;
2421                 }
2422
2423                 if (host->interval < interval_g)
2424                         host->interval = interval_g;
2425
2426                 na_server_set_transport_type(host->srv, host->protocol,
2427                                 /* transportarg = */ NULL);
2428                 na_server_set_port(host->srv, host->port);
2429                 na_server_style(host->srv, NA_STYLE_LOGIN_PASSWORD);
2430                 na_server_adminuser(host->srv, host->username, host->password);
2431                 na_server_set_timeout(host->srv, 5 /* seconds */);
2432         }
2433         return 0;
2434 } /* }}} int cna_init */
2435
2436 static int cna_config (oconfig_item_t *ci) { /* {{{ */
2437         int i;
2438         oconfig_item_t *item;
2439         host_config_t default_host = HOST_INIT;
2440         
2441         for (i = 0; i < ci->children_num; ++i) {
2442                 item = ci->children + i;
2443
2444                 if (!strcasecmp(item->key, "Host")) {
2445                         host_config_t *host;
2446                         host_config_t *tmp;
2447
2448                         host = cna_config_host(item, &default_host);
2449                         if (host == NULL)
2450                                 continue;
2451
2452                         for (tmp = global_host_config; tmp != NULL; tmp = tmp->next)
2453                         {
2454                                 if (strcasecmp (host->name, tmp->name) == 0)
2455                                         WARNING ("netapp plugin: Duplicate definition of host `%s'. "
2456                                                         "This is probably a bad idea.",
2457                                                         host->name);
2458
2459                                 if (tmp->next == NULL)
2460                                         break;
2461                         }
2462
2463                         host->next = NULL;
2464                         if (tmp == NULL)
2465                                 global_host_config = host;
2466                         else
2467                                 tmp->next = host;
2468                 } else {
2469                         WARNING("netapp plugin: Ignoring unknown config option \"%s\".", item->key);
2470                 }
2471         }
2472         return 0;
2473 } /* }}} int cna_config */
2474
2475 static int cna_read (void) { /* {{{ */
2476         host_config_t *host;
2477         
2478         for (host = global_host_config; host; host = host->next) {
2479                 cna_query_wafl (host);
2480                 cna_query_disk (host);
2481                 cna_query_volume_perf (host);
2482                 cna_query_volume_usage (host);
2483                 cna_query_system (host);
2484         }
2485         return 0;
2486 } /* }}} int cna_read */
2487
2488 static int cna_shutdown (void) /* {{{ */
2489 {
2490         free_host_config (global_host_config);
2491         global_host_config = NULL;
2492
2493         return (0);
2494 } /* }}} int cna_shutdown */
2495
2496 void module_register(void) {
2497         plugin_register_complex_config("netapp", cna_config);
2498         plugin_register_init("netapp", cna_init);
2499         plugin_register_read("netapp", cna_read);
2500         plugin_register_shutdown("netapp", cna_shutdown);
2501 }
2502
2503 /* vim: set sw=2 ts=2 noet fdm=marker : */