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