Fixed VolumePerf data collection.
[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                 if (value > snap_used)
1443                         snap_used = value;
1444         }
1445         na_elem_free (data);
1446         /* snap_used is the total size of the oldest snapshot plus all
1447          * newer ones in blocks (1KB). */
1448         v->snap_used = snap_used * 1024;
1449         v->flags |= HAVE_VOLUME_USAGE_SNAP_USED;
1450 }
1451
1452 static int cna_handle_volume_usage_data (const host_config_t *host, /* {{{ */
1453                 cfg_volume_usage_t *cfg_volume, na_elem_t *data)
1454 {
1455         na_elem_t *elem_volume;
1456         na_elem_t *elem_volumes;
1457         na_elem_iter_t iter_volume;
1458
1459         elem_volumes = na_elem_child (data, "volumes");
1460         if (elem_volumes == NULL)
1461         {
1462                 ERROR ("netapp plugin: cna_handle_volume_usage_data: "
1463                                 "na_elem_child (\"volumes\") failed.");
1464                 return (-1);
1465         }
1466
1467         iter_volume = na_child_iterator (elem_volumes);
1468         for (elem_volume = na_iterator_next (&iter_volume);
1469                         elem_volume != NULL;
1470                         elem_volume = na_iterator_next (&iter_volume))
1471         {
1472                 const char *volume_name, *state;
1473
1474                 data_volume_usage_t *v;
1475                 uint64_t value;
1476
1477                 na_elem_t *sis;
1478                 const char *sis_state;
1479                 uint64_t sis_saved_reported;
1480
1481                 volume_name = na_child_get_string (elem_volume, "name");
1482                 if (volume_name == NULL)
1483                         continue;
1484
1485                 state = na_child_get_string (elem_volume, "state");
1486                 if ((state == NULL) || (strcmp(state, "online") != 0))
1487                         continue;
1488
1489                 /* get_volume_usage may return NULL if the volume is to be ignored. */
1490                 v = get_volume_usage (cfg_volume, volume_name);
1491                 if (v == NULL)
1492                         continue;
1493
1494                 if ((v->flags & CFG_VOLUME_USAGE_SNAP) != 0)
1495                         cna_handle_volume_snap_usage(host, v);
1496                 
1497                 if ((v->flags & CFG_VOLUME_USAGE_DF) == 0)
1498                         continue;
1499
1500                 /* 2^4 exa-bytes? This will take a while ;) */
1501                 value = na_child_get_uint64(elem_volume, "size-available", UINT64_MAX);
1502                 if (value != UINT64_MAX) {
1503                         v->norm_free = value;
1504                         v->flags |= HAVE_VOLUME_USAGE_NORM_FREE;
1505                 }
1506
1507                 value = na_child_get_uint64(elem_volume, "size-used", UINT64_MAX);
1508                 if (value != UINT64_MAX) {
1509                         v->norm_used = value;
1510                         v->flags |= HAVE_VOLUME_USAGE_NORM_USED;
1511                 }
1512
1513                 value = na_child_get_uint64(elem_volume, "snapshot-blocks-reserved", UINT64_MAX);
1514                 if (value != UINT64_MAX) {
1515                         /* 1 block == 1024 bytes  as per API docs */
1516                         v->snap_reserved = 1024 * value;
1517                         v->flags |= HAVE_VOLUME_USAGE_SNAP_RSVD;
1518                 }
1519
1520                 sis = na_elem_child(elem_volume, "sis");
1521                 if (sis == NULL)
1522                         continue;
1523
1524                 sis_state = na_child_get_string(sis, "state");
1525                 if (sis_state == NULL)
1526                         continue;
1527
1528                 /* If SIS is not enabled, there's nothing left to do for this volume. */
1529                 if (strcmp ("enabled", sis_state) != 0)
1530                         continue;
1531
1532                 sis_saved_reported = na_child_get_uint64(sis, "size-saved", UINT64_MAX);
1533                 if (sis_saved_reported == UINT64_MAX)
1534                         continue;
1535
1536                 /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
1537                 if ((sis_saved_reported >> 32) != 0) {
1538                         /* In case they ever fix this bug. */
1539                         v->sis_saved = sis_saved_reported;
1540                         v->flags |= HAVE_VOLUME_USAGE_SIS_SAVED;
1541                 } else { /* really hacky work-around code. {{{ */
1542                         uint64_t sis_saved_percent;
1543                         uint64_t sis_saved_guess;
1544                         uint64_t overflow_guess;
1545                         uint64_t guess1, guess2, guess3;
1546
1547                         /* Check if we have v->norm_used. Without it, we cannot calculate
1548                          * sis_saved_guess. */
1549                         if ((v->flags & HAVE_VOLUME_USAGE_NORM_USED) == 0)
1550                                 continue;
1551
1552                         sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
1553                         if (sis_saved_percent > 100)
1554                                 continue;
1555
1556                         /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
1557                          * will hopefully be fixed in later versions. To work around the bug, try
1558                          * to figure out how often the 32bit integer wrapped around by using the
1559                          * "percentage-saved" value. Because the percentage is in the range
1560                          * [0-100], this should work as long as the saved space does not exceed
1561                          * 400 GBytes. */
1562                         /* percentage-saved = size-saved / (size-saved + size-used) */
1563                         if (sis_saved_percent < 100)
1564                                 sis_saved_guess = v->norm_used * sis_saved_percent / (100 - sis_saved_percent);
1565                         else
1566                                 sis_saved_guess = v->norm_used;
1567
1568                         overflow_guess = sis_saved_guess >> 32;
1569                         guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
1570                         guess2 = (overflow_guess << 32) + sis_saved_reported;
1571                         guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
1572
1573                         if (sis_saved_guess < guess2) {
1574                                 if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
1575                                         v->sis_saved = guess1;
1576                                 else
1577                                         v->sis_saved = guess2;
1578                         } else {
1579                                 if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
1580                                         v->sis_saved = guess2;
1581                                 else
1582                                         v->sis_saved = guess3;
1583                         }
1584                         v->flags |= HAVE_VOLUME_USAGE_SIS_SAVED;
1585                 } /* }}} end of 32-bit workaround */
1586         } /* for (elem_volume) */
1587
1588         return (cna_submit_volume_usage_data (host->name, cfg_volume));
1589 } /* }}} int cna_handle_volume_usage_data */
1590
1591 static int cna_setup_volume_usage (cfg_volume_usage_t *cvu) /* {{{ */
1592 {
1593         if (cvu == NULL)
1594                 return (EINVAL);
1595
1596         if (cvu->query != NULL)
1597                 return (0);
1598
1599         cvu->query = na_elem_new ("volume-list-info");
1600         if (cvu->query == NULL)
1601         {
1602                 ERROR ("netapp plugin: na_elem_new failed.");
1603                 return (-1);
1604         }
1605
1606         return (0);
1607 } /* }}} int cna_setup_volume_usage */
1608
1609 static int cna_query_volume_usage (host_config_t *host) /* {{{ */
1610 {
1611         na_elem_t *data;
1612         int status;
1613         time_t now;
1614
1615         if (host == NULL)
1616                 return (EINVAL);
1617
1618         /* If the user did not configure volume_usage statistics, return without
1619          * doing anything. */
1620         if (host->cfg_volume_usage == NULL)
1621                 return (0);
1622
1623         now = time (NULL);
1624         if ((host->cfg_volume_usage->interval.interval + host->cfg_volume_usage->interval.last_read) > now)
1625                 return (0);
1626
1627         status = cna_setup_volume_usage (host->cfg_volume_usage);
1628         if (status != 0)
1629                 return (status);
1630         assert (host->cfg_volume_usage->query != NULL);
1631
1632         data = na_server_invoke_elem(host->srv, host->cfg_volume_usage->query);
1633         if (na_results_status (data) != NA_OK)
1634         {
1635                 ERROR ("netapp plugin: cna_query_volume_usage: na_server_invoke_elem failed: %s",
1636                                 na_results_reason (data));
1637                 na_elem_free (data);
1638                 return (-1);
1639         }
1640
1641         status = cna_handle_volume_usage_data (host, host->cfg_volume_usage, data);
1642
1643         if (status == 0)
1644                 host->cfg_volume_usage->interval.last_read = now;
1645
1646         na_elem_free (data);
1647         return (status);
1648 } /* }}} int cna_query_volume_usage */
1649
1650 /* Data corresponding to <System /> */
1651 static int cna_handle_system_data (const char *hostname, /* {{{ */
1652                 cfg_system_t *cfg_system, na_elem_t *data)
1653 {
1654         na_elem_t *instances;
1655         na_elem_t *counter;
1656         na_elem_iter_t counter_iter;
1657
1658         counter_t disk_read = 0, disk_written = 0;
1659         counter_t net_recv = 0, net_sent = 0;
1660         counter_t cpu_busy = 0, cpu_total = 0;
1661         uint32_t counter_flags = 0;
1662
1663         const char *instance;
1664         time_t timestamp;
1665         
1666         timestamp = (time_t) na_child_get_uint64 (data, "timestamp", 0);
1667
1668         instances = na_elem_child(na_elem_child (data, "instances"), "instance-data");
1669         if (instances == NULL)
1670         {
1671                 ERROR ("netapp plugin: cna_handle_system_data: "
1672                                 "na_elem_child (\"instances\") failed.");
1673                 return (-1);
1674         }
1675
1676         instance = na_child_get_string (instances, "name");
1677         if (instance == NULL)
1678         {
1679                 ERROR ("netapp plugin: cna_handle_system_data: "
1680                                 "na_child_get_string (\"name\") failed.");
1681                 return (-1);
1682         }
1683
1684         counter_iter = na_child_iterator (na_elem_child (instances, "counters"));
1685         for (counter = na_iterator_next (&counter_iter);
1686                         counter != NULL;
1687                         counter = na_iterator_next (&counter_iter))
1688         {
1689                 const char *name;
1690                 uint64_t value;
1691
1692                 name = na_child_get_string(counter, "name");
1693                 if (name == NULL)
1694                         continue;
1695
1696                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
1697                 if (value == UINT64_MAX)
1698                         continue;
1699
1700                 if (!strcmp(name, "disk_data_read")) {
1701                         disk_read = (counter_t) (value * 1024);
1702                         counter_flags |= 0x01;
1703                 } else if (!strcmp(name, "disk_data_written")) {
1704                         disk_written = (counter_t) (value * 1024);
1705                         counter_flags |= 0x02;
1706                 } else if (!strcmp(name, "net_data_recv")) {
1707                         net_recv = (counter_t) (value * 1024);
1708                         counter_flags |= 0x04;
1709                 } else if (!strcmp(name, "net_data_sent")) {
1710                         net_sent = (counter_t) (value * 1024);
1711                         counter_flags |= 0x08;
1712                 } else if (!strcmp(name, "cpu_busy")) {
1713                         cpu_busy = (counter_t) value;
1714                         counter_flags |= 0x10;
1715                 } else if (!strcmp(name, "cpu_elapsed_time")) {
1716                         cpu_total = (counter_t) value;
1717                         counter_flags |= 0x20;
1718                 } else if ((cfg_system->flags & CFG_SYSTEM_OPS)
1719                                 && (value > 0) && (strlen(name) > 4)
1720                                 && (!strcmp(name + strlen(name) - 4, "_ops"))) {
1721                         submit_counter (hostname, instance, "disk_ops_complex", name,
1722                                         (counter_t) value, timestamp);
1723                 }
1724         } /* for (counter) */
1725
1726         if ((cfg_system->flags & CFG_SYSTEM_DISK)
1727                         && (HAS_ALL_FLAGS (counter_flags, 0x01 | 0x02)))
1728                 submit_two_counters (hostname, instance, "disk_octets", NULL,
1729                                 disk_read, disk_written, timestamp);
1730                                 
1731         if ((cfg_system->flags & CFG_SYSTEM_NET)
1732                         && (HAS_ALL_FLAGS (counter_flags, 0x04 | 0x08)))
1733                 submit_two_counters (hostname, instance, "if_octets", NULL,
1734                                 net_recv, net_sent, timestamp);
1735
1736         if ((cfg_system->flags & CFG_SYSTEM_CPU)
1737                         && (HAS_ALL_FLAGS (counter_flags, 0x10 | 0x20)))
1738         {
1739                 submit_counter (hostname, instance, "cpu", "system",
1740                                 cpu_busy, timestamp);
1741                 submit_counter (hostname, instance, "cpu", "idle",
1742                                 cpu_total - cpu_busy, timestamp);
1743         }
1744
1745         return (0);
1746 } /* }}} int cna_handle_system_data */
1747
1748 static int cna_setup_system (cfg_system_t *cs) /* {{{ */
1749 {
1750         if (cs == NULL)
1751                 return (EINVAL);
1752
1753         if (cs->query != NULL)
1754                 return (0);
1755
1756         cs->query = na_elem_new ("perf-object-get-instances");
1757         if (cs->query == NULL)
1758         {
1759                 ERROR ("netapp plugin: na_elem_new failed.");
1760                 return (-1);
1761         }
1762         na_child_add_string (cs->query, "objectname", "system");
1763
1764         return (0);
1765 } /* }}} int cna_setup_system */
1766
1767 static int cna_query_system (host_config_t *host) /* {{{ */
1768 {
1769         na_elem_t *data;
1770         int status;
1771         time_t now;
1772
1773         if (host == NULL)
1774                 return (EINVAL);
1775
1776         /* If system statistics were not configured, return without doing anything. */
1777         if (host->cfg_system == NULL)
1778                 return (0);
1779
1780         now = time (NULL);
1781         if ((host->cfg_system->interval.interval + host->cfg_system->interval.last_read) > now)
1782                 return (0);
1783
1784         status = cna_setup_system (host->cfg_system);
1785         if (status != 0)
1786                 return (status);
1787         assert (host->cfg_system->query != NULL);
1788
1789         data = na_server_invoke_elem(host->srv, host->cfg_system->query);
1790         if (na_results_status (data) != NA_OK)
1791         {
1792                 ERROR ("netapp plugin: cna_query_system: na_server_invoke_elem failed: %s",
1793                                 na_results_reason (data));
1794                 na_elem_free (data);
1795                 return (-1);
1796         }
1797
1798         status = cna_handle_system_data (host->name, host->cfg_system, data);
1799
1800         if (status == 0)
1801                 host->cfg_system->interval.last_read = now;
1802
1803         na_elem_free (data);
1804         return (status);
1805 } /* }}} int cna_query_system */
1806
1807 /*
1808  * Configuration handling
1809  */
1810 /* Sets a given flag if the boolean argument is true and unsets the flag if it
1811  * is false. On error, the flag-field is not changed. */
1812 static int cna_config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */
1813                 uint32_t *flags, uint32_t flag)
1814 {
1815         if ((ci == NULL) || (flags == NULL))
1816                 return (EINVAL);
1817
1818         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1819         {
1820                 WARNING ("netapp plugin: The %s option needs exactly one boolean argument.",
1821                                 ci->key);
1822                 return (-1);
1823         }
1824
1825         if (ci->values[0].value.boolean)
1826                 *flags |= flag;
1827         else
1828                 *flags &= ~flag;
1829
1830         return (0);
1831 } /* }}} int cna_config_bool_to_flag */
1832
1833 /* Handling of the "Interval" option which is allowed in every block. */
1834 static int cna_config_get_interval (const oconfig_item_t *ci, /* {{{ */
1835                 cna_interval_t *out_interval)
1836 {
1837         time_t tmp;
1838
1839         if ((ci == NULL) || (out_interval == NULL))
1840                 return (EINVAL);
1841
1842         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1843         {
1844                 WARNING ("netapp plugin: The `Interval' option needs exactly one numeric argument.");
1845                 return (-1);
1846         }
1847
1848         tmp = (time_t) (ci->values[0].value.number + .5);
1849         if (tmp < 1)
1850         {
1851                 WARNING ("netapp plugin: The `Interval' option needs a positive integer argument.");
1852                 return (-1);
1853         }
1854
1855         out_interval->interval = tmp;
1856         out_interval->last_read = 0;
1857
1858         return (0);
1859 } /* }}} int cna_config_get_interval */
1860
1861 /* Handling of the "GetIO", "GetOps" and "GetLatency" options within a
1862  * <VolumePerf /> block. */
1863 static void cna_config_volume_perf_option (cfg_volume_perf_t *cvp, /* {{{ */
1864                 const oconfig_item_t *ci)
1865 {
1866         char *name;
1867         ignorelist_t * il;
1868
1869         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1870         {
1871                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
1872                                 ci->key);
1873                 return;
1874         }
1875
1876         name = ci->values[0].value.string;
1877
1878         if (strcasecmp ("GetIO", ci->key) == 0)
1879                 il = cvp->il_octets;
1880         else if (strcasecmp ("GetOps", ci->key) == 0)
1881                 il = cvp->il_operations;
1882         else if (strcasecmp ("GetLatency", ci->key) == 0)
1883                 il = cvp->il_latency;
1884         else
1885                 return;
1886
1887         ignorelist_add (il, name);
1888 } /* }}} void cna_config_volume_perf_option */
1889
1890 /* Handling of the "IgnoreSelectedIO", "IgnoreSelectedOps" and
1891  * "IgnoreSelectedLatency" options within a <VolumePerf /> block. */
1892 static void cna_config_volume_perf_default (cfg_volume_perf_t *cvp, /* {{{ */
1893                 const oconfig_item_t *ci)
1894 {
1895         ignorelist_t *il;
1896
1897         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1898         {
1899                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
1900                                 ci->key);
1901                 return;
1902         }
1903
1904         if (strcasecmp ("IgnoreSelectedIO", ci->key) == 0)
1905                 il = cvp->il_octets;
1906         else if (strcasecmp ("IgnoreSelectedOps", ci->key) == 0)
1907                 il = cvp->il_operations;
1908         else if (strcasecmp ("IgnoreSelectedLatency", ci->key) == 0)
1909                 il = cvp->il_latency;
1910         else
1911                 return;
1912
1913         if (ci->values[0].value.boolean)
1914                 ignorelist_set_invert (il, /* invert = */ 0);
1915         else
1916                 ignorelist_set_invert (il, /* invert = */ 1);
1917 } /* }}} void cna_config_volume_perf_default */
1918
1919 /* Corresponds to a <Disks /> block */
1920 /*
1921  * <VolumePerf>
1922  *   GetIO "vol0"
1923  *   GetIO "vol1"
1924  *   IgnoreSelectedIO false
1925  *
1926  *   GetOps "vol0"
1927  *   GetOps "vol2"
1928  *   IgnoreSelectedOps false
1929  *
1930  *   GetLatency "vol2"
1931  *   GetLatency "vol3"
1932  *   IgnoreSelectedLatency false
1933  * </VolumePerf>
1934  */
1935 /* Corresponds to a <VolumePerf /> block */
1936 static int cna_config_volume_performance (host_config_t *host, /* {{{ */
1937                 const oconfig_item_t *ci)
1938 {
1939         cfg_volume_perf_t *cfg_volume_perf;
1940         int i;
1941
1942         if ((host == NULL) || (ci == NULL))
1943                 return (EINVAL);
1944
1945         if (host->cfg_volume_perf == NULL)
1946         {
1947                 cfg_volume_perf = malloc (sizeof (*cfg_volume_perf));
1948                 if (cfg_volume_perf == NULL)
1949                         return (ENOMEM);
1950                 memset (cfg_volume_perf, 0, sizeof (*cfg_volume_perf));
1951
1952                 /* Set default flags */
1953                 cfg_volume_perf->query = NULL;
1954                 cfg_volume_perf->volumes = NULL;
1955
1956                 cfg_volume_perf->il_octets = ignorelist_create (/* invert = */ 1);
1957                 if (cfg_volume_perf->il_octets == NULL)
1958                 {
1959                         sfree (cfg_volume_perf);
1960                         return (ENOMEM);
1961                 }
1962
1963                 cfg_volume_perf->il_operations = ignorelist_create (/* invert = */ 1);
1964                 if (cfg_volume_perf->il_operations == NULL)
1965                 {
1966                         ignorelist_free (cfg_volume_perf->il_octets);
1967                         sfree (cfg_volume_perf);
1968                         return (ENOMEM);
1969                 }
1970
1971                 cfg_volume_perf->il_latency = ignorelist_create (/* invert = */ 1);
1972                 if (cfg_volume_perf->il_latency == NULL)
1973                 {
1974                         ignorelist_free (cfg_volume_perf->il_octets);
1975                         ignorelist_free (cfg_volume_perf->il_operations);
1976                         sfree (cfg_volume_perf);
1977                         return (ENOMEM);
1978                 }
1979
1980                 host->cfg_volume_perf = cfg_volume_perf;
1981         }
1982         cfg_volume_perf = host->cfg_volume_perf;
1983         
1984         for (i = 0; i < ci->children_num; ++i) {
1985                 oconfig_item_t *item = ci->children + i;
1986                 
1987                 /* if (!item || !item->key || !*item->key) continue; */
1988                 if (strcasecmp(item->key, "Interval") == 0)
1989                         cna_config_get_interval (item, &cfg_volume_perf->interval);
1990                 else if (!strcasecmp(item->key, "GetIO"))
1991                         cna_config_volume_perf_option (cfg_volume_perf, item);
1992                 else if (!strcasecmp(item->key, "GetOps"))
1993                         cna_config_volume_perf_option (cfg_volume_perf, item);
1994                 else if (!strcasecmp(item->key, "GetLatency"))
1995                         cna_config_volume_perf_option (cfg_volume_perf, item);
1996                 else if (!strcasecmp(item->key, "IgnoreSelectedIO"))
1997                         cna_config_volume_perf_default (cfg_volume_perf, item);
1998                 else if (!strcasecmp(item->key, "IgnoreSelectedOps"))
1999                         cna_config_volume_perf_default (cfg_volume_perf, item);
2000                 else if (!strcasecmp(item->key, "IgnoreSelectedLatency"))
2001                         cna_config_volume_perf_default (cfg_volume_perf, item);
2002                 else
2003                         WARNING ("netapp plugin: The option %s is not allowed within "
2004                                         "`VolumePerf' blocks.", item->key);
2005         }
2006
2007         return (0);
2008 } /* }}} int cna_config_volume_performance */
2009
2010 /* Handling of the "GetCapacity" and "GetSnapshot" options within a
2011  * <VolumeUsage /> block. */
2012 static void cna_config_volume_usage_option (cfg_volume_usage_t *cvu, /* {{{ */
2013                 const oconfig_item_t *ci)
2014 {
2015         char *name;
2016         ignorelist_t * il;
2017
2018         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2019         {
2020                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2021                                 ci->key);
2022                 return;
2023         }
2024
2025         name = ci->values[0].value.string;
2026
2027         if (strcasecmp ("GetCapacity", ci->key) == 0)
2028                 il = cvu->il_capacity;
2029         else if (strcasecmp ("GetSnapshot", ci->key) == 0)
2030                 il = cvu->il_snapshot;
2031         else
2032                 return;
2033
2034         ignorelist_add (il, name);
2035 } /* }}} void cna_config_volume_usage_option */
2036
2037 /* Handling of the "IgnoreSelectedCapacity" and "IgnoreSelectedSnapshot"
2038  * options within a <VolumeUsage /> block. */
2039 static void cna_config_volume_usage_default (cfg_volume_usage_t *cvu, /* {{{ */
2040                 const oconfig_item_t *ci)
2041 {
2042         ignorelist_t *il;
2043
2044         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
2045         {
2046                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2047                                 ci->key);
2048                 return;
2049         }
2050
2051         if (strcasecmp ("IgnoreSelectedCapacity", ci->key) == 0)
2052                 il = cvu->il_capacity;
2053         else if (strcasecmp ("IgnoreSelectedSnapshot", ci->key) == 0)
2054                 il = cvu->il_snapshot;
2055         else
2056                 return;
2057
2058         if (ci->values[0].value.boolean)
2059                 ignorelist_set_invert (il, /* invert = */ 0);
2060         else
2061                 ignorelist_set_invert (il, /* invert = */ 1);
2062 } /* }}} void cna_config_volume_usage_default */
2063
2064 /* Corresponds to a <Disks /> block */
2065 static int cna_config_disk(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
2066         cfg_disk_t *cfg_disk;
2067         int i;
2068
2069         if ((host == NULL) || (ci == NULL))
2070                 return (EINVAL);
2071
2072         if (host->cfg_disk == NULL)
2073         {
2074                 cfg_disk = malloc (sizeof (*cfg_disk));
2075                 if (cfg_disk == NULL)
2076                         return (ENOMEM);
2077                 memset (cfg_disk, 0, sizeof (*cfg_disk));
2078
2079                 /* Set default flags */
2080                 cfg_disk->flags = CFG_DISK_ALL;
2081                 cfg_disk->query = NULL;
2082                 cfg_disk->disks = NULL;
2083
2084                 host->cfg_disk = cfg_disk;
2085         }
2086         cfg_disk = host->cfg_disk;
2087         
2088         for (i = 0; i < ci->children_num; ++i) {
2089                 oconfig_item_t *item = ci->children + i;
2090                 
2091                 /* if (!item || !item->key || !*item->key) continue; */
2092                 if (strcasecmp(item->key, "Interval") == 0)
2093                         cna_config_get_interval (item, &cfg_disk->interval);
2094                 else if (strcasecmp(item->key, "GetBusy") == 0)
2095                         cna_config_bool_to_flag (item, &cfg_disk->flags, CFG_DISK_BUSIEST);
2096         }
2097
2098         if ((cfg_disk->flags & CFG_DISK_ALL) == 0)
2099         {
2100                 NOTICE ("netapp plugin: All disk related values have been disabled. "
2101                                 "Collection of per-disk data will be disabled entirely.");
2102                 free_cfg_disk (host->cfg_disk);
2103                 host->cfg_disk = NULL;
2104         }
2105
2106         return (0);
2107 } /* }}} int cna_config_disk */
2108
2109 /* Corresponds to a <WAFL /> block */
2110 static int cna_config_wafl(host_config_t *host, oconfig_item_t *ci) /* {{{ */
2111 {
2112         cfg_wafl_t *cfg_wafl;
2113         int i;
2114
2115         if ((host == NULL) || (ci == NULL))
2116                 return (EINVAL);
2117
2118         if (host->cfg_wafl == NULL)
2119         {
2120                 cfg_wafl = malloc (sizeof (*cfg_wafl));
2121                 if (cfg_wafl == NULL)
2122                         return (ENOMEM);
2123                 memset (cfg_wafl, 0, sizeof (*cfg_wafl));
2124
2125                 /* Set default flags */
2126                 cfg_wafl->flags = CFG_WAFL_ALL;
2127
2128                 host->cfg_wafl = cfg_wafl;
2129         }
2130         cfg_wafl = host->cfg_wafl;
2131
2132         for (i = 0; i < ci->children_num; ++i) {
2133                 oconfig_item_t *item = ci->children + i;
2134                 
2135                 if (strcasecmp(item->key, "Interval") == 0)
2136                         cna_config_get_interval (item, &cfg_wafl->interval);
2137                 else if (!strcasecmp(item->key, "GetNameCache"))
2138                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_NAME_CACHE);
2139                 else if (!strcasecmp(item->key, "GetDirCache"))
2140                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_DIR_CACHE);
2141                 else if (!strcasecmp(item->key, "GetBufferCache"))
2142                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_BUF_CACHE);
2143                 else if (!strcasecmp(item->key, "GetInodeCache"))
2144                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_INODE_CACHE);
2145                 else
2146                         WARNING ("netapp plugin: The %s config option is not allowed within "
2147                                         "`WAFL' blocks.", item->key);
2148         }
2149
2150         if ((cfg_wafl->flags & CFG_WAFL_ALL) == 0)
2151         {
2152                 NOTICE ("netapp plugin: All WAFL related values have been disabled. "
2153                                 "Collection of WAFL data will be disabled entirely.");
2154                 free_cfg_wafl (host->cfg_wafl);
2155                 host->cfg_wafl = NULL;
2156         }
2157
2158         return (0);
2159 } /* }}} int cna_config_wafl */
2160
2161 /*
2162  * <VolumeUsage>
2163  *   GetCapacity "vol0"
2164  *   GetCapacity "vol1"
2165  *   GetCapacity "vol2"
2166  *   GetCapacity "vol3"
2167  *   GetCapacity "vol4"
2168  *   IgnoreSelectedCapacity false
2169  *
2170  *   GetSnapshot "vol0"
2171  *   GetSnapshot "vol3"
2172  *   GetSnapshot "vol4"
2173  *   GetSnapshot "vol7"
2174  *   IgnoreSelectedSnapshot false
2175  * </VolumeUsage>
2176  */
2177 /* Corresponds to a <VolumeUsage /> block */
2178 static int cna_config_volume_usage(host_config_t *host, /* {{{ */
2179                 const oconfig_item_t *ci)
2180 {
2181         cfg_volume_usage_t *cfg_volume_usage;
2182         int i;
2183
2184         if ((host == NULL) || (ci == NULL))
2185                 return (EINVAL);
2186
2187         if (host->cfg_volume_usage == NULL)
2188         {
2189                 cfg_volume_usage = malloc (sizeof (*cfg_volume_usage));
2190                 if (cfg_volume_usage == NULL)
2191                         return (ENOMEM);
2192                 memset (cfg_volume_usage, 0, sizeof (*cfg_volume_usage));
2193
2194                 /* Set default flags */
2195                 cfg_volume_usage->query = NULL;
2196                 cfg_volume_usage->volumes = NULL;
2197
2198                 cfg_volume_usage->il_capacity = ignorelist_create (/* invert = */ 1);
2199                 if (cfg_volume_usage->il_capacity == NULL)
2200                 {
2201                         sfree (cfg_volume_usage);
2202                         return (ENOMEM);
2203                 }
2204
2205                 cfg_volume_usage->il_snapshot = ignorelist_create (/* invert = */ 1);
2206                 if (cfg_volume_usage->il_snapshot == NULL)
2207                 {
2208                         ignorelist_free (cfg_volume_usage->il_capacity);
2209                         sfree (cfg_volume_usage);
2210                         return (ENOMEM);
2211                 }
2212
2213                 host->cfg_volume_usage = cfg_volume_usage;
2214         }
2215         cfg_volume_usage = host->cfg_volume_usage;
2216         
2217         for (i = 0; i < ci->children_num; ++i) {
2218                 oconfig_item_t *item = ci->children + i;
2219                 
2220                 /* if (!item || !item->key || !*item->key) continue; */
2221                 if (strcasecmp(item->key, "Interval") == 0)
2222                         cna_config_get_interval (item, &cfg_volume_usage->interval);
2223                 else if (!strcasecmp(item->key, "GetCapacity"))
2224                         cna_config_volume_usage_option (cfg_volume_usage, item);
2225                 else if (!strcasecmp(item->key, "GetSnapshot"))
2226                         cna_config_volume_usage_option (cfg_volume_usage, item);
2227                 else if (!strcasecmp(item->key, "IgnoreSelectedCapacity"))
2228                         cna_config_volume_usage_default (cfg_volume_usage, item);
2229                 else if (!strcasecmp(item->key, "IgnoreSelectedSnapshot"))
2230                         cna_config_volume_usage_default (cfg_volume_usage, item);
2231                 else
2232                         WARNING ("netapp plugin: The option %s is not allowed within "
2233                                         "`VolumeUsage' blocks.", item->key);
2234         }
2235
2236         return (0);
2237 } /* }}} int cna_config_volume_usage */
2238
2239 /* Corresponds to a <System /> block */
2240 static int cna_config_system (host_config_t *host, /* {{{ */
2241                 oconfig_item_t *ci)
2242 {
2243         cfg_system_t *cfg_system;
2244         int i;
2245         
2246         if ((host == NULL) || (ci == NULL))
2247                 return (EINVAL);
2248
2249         if (host->cfg_system == NULL)
2250         {
2251                 cfg_system = malloc (sizeof (*cfg_system));
2252                 if (cfg_system == NULL)
2253                         return (ENOMEM);
2254                 memset (cfg_system, 0, sizeof (*cfg_system));
2255
2256                 /* Set default flags */
2257                 cfg_system->flags = CFG_SYSTEM_ALL;
2258                 cfg_system->query = NULL;
2259
2260                 host->cfg_system = cfg_system;
2261         }
2262         cfg_system = host->cfg_system;
2263
2264         for (i = 0; i < ci->children_num; ++i) {
2265                 oconfig_item_t *item = ci->children + i;
2266
2267                 if (strcasecmp(item->key, "Interval") == 0) {
2268                         cna_config_get_interval (item, &cfg_system->interval);
2269                 } else if (!strcasecmp(item->key, "GetCPULoad")) {
2270                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_CPU);
2271                 } else if (!strcasecmp(item->key, "GetInterfaces")) {
2272                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_NET);
2273                 } else if (!strcasecmp(item->key, "GetDiskOps")) {
2274                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_OPS);
2275                 } else if (!strcasecmp(item->key, "GetDiskIO")) {
2276                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_DISK);
2277                 } else {
2278                         WARNING ("netapp plugin: The %s config option is not allowed within "
2279                                         "`System' blocks.", item->key);
2280                 }
2281         }
2282
2283         if ((cfg_system->flags & CFG_SYSTEM_ALL) == 0)
2284         {
2285                 NOTICE ("netapp plugin: All system related values have been disabled. "
2286                                 "Collection of system data will be disabled entirely.");
2287                 free_cfg_system (host->cfg_system);
2288                 host->cfg_system = NULL;
2289         }
2290
2291         return (0);
2292 } /* }}} int cna_config_system */
2293
2294 /* Corresponds to a <Host /> block. */
2295 static host_config_t *cna_config_host (const oconfig_item_t *ci, /* {{{ */
2296                 const host_config_t *default_host)
2297 {
2298         oconfig_item_t *item;
2299         host_config_t *host;
2300         int status;
2301         int i;
2302         
2303         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2304                 WARNING("netapp plugin: \"Host\" needs exactly one string argument. Ignoring host block.");
2305                 return 0;
2306         }
2307
2308         host = malloc(sizeof(*host));
2309         memcpy (host, default_host, sizeof (*host));
2310
2311         status = cf_util_get_string (ci, &host->name);
2312         if (status != 0)
2313         {
2314                 sfree (host);
2315                 return (NULL);
2316         }
2317
2318         for (i = 0; i < ci->children_num; ++i) {
2319                 item = ci->children + i;
2320
2321                 status = 0;
2322
2323                 if (!strcasecmp(item->key, "Address")) {
2324                         status = cf_util_get_string (item, &host->host);
2325                 } else if (!strcasecmp(item->key, "Port")) {
2326                         int tmp;
2327
2328                         tmp = cf_util_get_port_number (item);
2329                         if (tmp > 0)
2330                                 host->port = tmp;
2331                 } else if (!strcasecmp(item->key, "Protocol")) {
2332                         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"))) {
2333                                 WARNING("netapp plugin: \"Protocol\" needs to be either \"http\" or \"https\". Ignoring host block \"%s\".", ci->values[0].value.string);
2334                                 return 0;
2335                         }
2336                         if (!strcasecmp(item->values[0].value.string, "http")) host->protocol = NA_SERVER_TRANSPORT_HTTP;
2337                         else host->protocol = NA_SERVER_TRANSPORT_HTTPS;
2338                 } else if (!strcasecmp(item->key, "User")) {
2339                         status = cf_util_get_string (item, &host->username);
2340                 } else if (!strcasecmp(item->key, "Password")) {
2341                         status = cf_util_get_string (item, &host->password);
2342                 } else if (!strcasecmp(item->key, "Interval")) {
2343                         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) {
2344                                 WARNING("netapp plugin: \"Interval\" of host %s needs exactly one integer argument.", ci->values[0].value.string);
2345                                 continue;
2346                         }
2347                         host->interval = item->values[0].value.number;
2348                 } else if (!strcasecmp(item->key, "WAFL")) {
2349                         cna_config_wafl(host, item);
2350                 } else if (!strcasecmp(item->key, "Disks")) {
2351                         cna_config_disk(host, item);
2352                 } else if (!strcasecmp(item->key, "VolumePerf")) {
2353                         cna_config_volume_performance(host, item);
2354                 } else if (!strcasecmp(item->key, "VolumeUsage")) {
2355                         cna_config_volume_usage(host, item);
2356                 } else if (!strcasecmp(item->key, "System")) {
2357                         cna_config_system(host, item);
2358                 } else {
2359                         WARNING("netapp plugin: Ignoring unknown config option \"%s\" in host block \"%s\".",
2360                                         item->key, ci->values[0].value.string);
2361                 }
2362
2363                 if (status != 0)
2364                         break;
2365         }
2366
2367         if (host->host == NULL)
2368                 host->host = strdup (host->name);
2369
2370         if (host->host == NULL)
2371                 status = -1;
2372
2373         if (host->port <= 0)
2374                 host->port = (host->protocol == NA_SERVER_TRANSPORT_HTTP) ? 80 : 443;
2375
2376         if ((host->username == NULL) || (host->password == NULL)) {
2377                 WARNING("netapp plugin: Please supply login information for host \"%s\". "
2378                                 "Ignoring host block.", host->name);
2379                 status = -1;
2380         }
2381
2382         if (status != 0)
2383         {
2384                 free_host_config (host);
2385                 return (NULL);
2386         }
2387
2388         return host;
2389 } /* }}} host_config_t *cna_config_host */
2390
2391 /*
2392  * Callbacks registered with the daemon
2393  *
2394  * Pretty standard stuff here.
2395  */
2396 static int cna_init(void) { /* {{{ */
2397         char err[256];
2398         host_config_t *host;
2399         
2400         if (!global_host_config) {
2401                 WARNING("netapp plugin: Plugin loaded but no hosts defined.");
2402                 return 1;
2403         }
2404
2405         memset (err, 0, sizeof (err));
2406         if (!na_startup(err, sizeof(err))) {
2407                 err[sizeof (err) - 1] = 0;
2408                 ERROR("netapp plugin: Error initializing netapp API: %s", err);
2409                 return 1;
2410         }
2411
2412         for (host = global_host_config; host; host = host->next) {
2413                 /* Request version 1.1 of the ONTAP API */
2414                 host->srv = na_server_open(host->host,
2415                                 /* major version = */ 1, /* minor version = */ 1); 
2416                 if (host->srv == NULL) {
2417                         ERROR ("netapp plugin: na_server_open (%s) failed.", host->host);
2418                         continue;
2419                 }
2420
2421                 if (host->interval < interval_g)
2422                         host->interval = interval_g;
2423
2424                 na_server_set_transport_type(host->srv, host->protocol,
2425                                 /* transportarg = */ NULL);
2426                 na_server_set_port(host->srv, host->port);
2427                 na_server_style(host->srv, NA_STYLE_LOGIN_PASSWORD);
2428                 na_server_adminuser(host->srv, host->username, host->password);
2429                 na_server_set_timeout(host->srv, 5 /* seconds */);
2430         }
2431         return 0;
2432 } /* }}} int cna_init */
2433
2434 static int cna_config (oconfig_item_t *ci) { /* {{{ */
2435         int i;
2436         oconfig_item_t *item;
2437         host_config_t default_host = HOST_INIT;
2438         
2439         for (i = 0; i < ci->children_num; ++i) {
2440                 item = ci->children + i;
2441
2442                 if (!strcasecmp(item->key, "Host")) {
2443                         host_config_t *host;
2444                         host_config_t *tmp;
2445
2446                         host = cna_config_host(item, &default_host);
2447                         if (host == NULL)
2448                                 continue;
2449
2450                         for (tmp = global_host_config; tmp != NULL; tmp = tmp->next)
2451                         {
2452                                 if (strcasecmp (host->name, tmp->name) == 0)
2453                                         WARNING ("netapp plugin: Duplicate definition of host `%s'. "
2454                                                         "This is probably a bad idea.",
2455                                                         host->name);
2456
2457                                 if (tmp->next == NULL)
2458                                         break;
2459                         }
2460
2461                         host->next = NULL;
2462                         if (tmp == NULL)
2463                                 global_host_config = host;
2464                         else
2465                                 tmp->next = host;
2466                 } else {
2467                         WARNING("netapp plugin: Ignoring unknown config option \"%s\".", item->key);
2468                 }
2469         }
2470         return 0;
2471 } /* }}} int cna_config */
2472
2473 static int cna_read (void) { /* {{{ */
2474         host_config_t *host;
2475         
2476         for (host = global_host_config; host; host = host->next) {
2477                 cna_query_wafl (host);
2478                 cna_query_disk (host);
2479                 cna_query_volume_perf (host);
2480                 cna_query_volume_usage (host);
2481                 cna_query_system (host);
2482         }
2483         return 0;
2484 } /* }}} int cna_read */
2485
2486 static int cna_shutdown (void) /* {{{ */
2487 {
2488         free_host_config (global_host_config);
2489         global_host_config = NULL;
2490
2491         return (0);
2492 } /* }}} int cna_shutdown */
2493
2494 void module_register(void) {
2495         plugin_register_complex_config("netapp", cna_config);
2496         plugin_register_init("netapp", cna_init);
2497         plugin_register_read("netapp", cna_read);
2498         plugin_register_shutdown("netapp", cna_shutdown);
2499 }
2500
2501 /* vim: set sw=2 ts=2 noet fdm=marker : */