netapp plugin: Subtract snap_norm_used from norm_used in any case.
[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         char plugin_instance[DATA_MAX_NAME_LEN];
735
736         if ((hostname == NULL) || (old_data == NULL) || (new_data == NULL))
737                 return (-1);
738
739         ssnprintf (plugin_instance, sizeof (plugin_instance),
740                         "volume-%s", old_data->name);
741
742         /* Check for and submit disk-octet values */
743         if (HAS_ALL_FLAGS (old_data->flags, CFG_VOLUME_PERF_IO)
744                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_BYTES_READ | HAVE_VOLUME_PERF_BYTES_WRITE))
745         {
746                 submit_two_counters (hostname, plugin_instance, "disk_octets", /* type instance = */ NULL,
747                                 (counter_t) new_data->read_bytes, (counter_t) new_data->write_bytes, new_data->timestamp);
748         }
749
750         /* Check for and submit disk-operations values */
751         if (HAS_ALL_FLAGS (old_data->flags, CFG_VOLUME_PERF_OPS)
752                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE))
753         {
754                 submit_two_counters (hostname, plugin_instance, "disk_ops", /* type instance = */ NULL,
755                                 (counter_t) new_data->read_ops, (counter_t) new_data->write_ops, new_data->timestamp);
756         }
757
758         /* Check for, calculate and submit disk-latency values */
759         if (HAS_ALL_FLAGS (old_data->flags, CFG_VOLUME_PERF_LATENCY
760                                 | HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
761                                 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE)
762                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
763                                 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE))
764         {
765                 gauge_t latency_per_op_read;
766                 gauge_t latency_per_op_write;
767
768                 latency_per_op_read = NAN;
769                 latency_per_op_write = NAN;
770
771                 /* Check if a counter wrapped around. */
772                 if ((new_data->read_ops > old_data->read_ops)
773                                 && (new_data->read_latency > old_data->read_latency))
774                 {
775                         uint64_t diff_ops_read;
776                         uint64_t diff_latency_read;
777
778                         diff_ops_read = new_data->read_ops - old_data->read_ops;
779                         diff_latency_read = new_data->read_latency - old_data->read_latency;
780
781                         if (diff_ops_read > 0)
782                                 latency_per_op_read = ((gauge_t) diff_latency_read) / ((gauge_t) diff_ops_read);
783                 }
784
785                 if ((new_data->write_ops > old_data->write_ops)
786                                 && (new_data->write_latency > old_data->write_latency))
787                 {
788                         uint64_t diff_ops_write;
789                         uint64_t diff_latency_write;
790
791                         diff_ops_write = new_data->write_ops - old_data->write_ops;
792                         diff_latency_write = new_data->write_latency - old_data->write_latency;
793
794                         if (diff_ops_write > 0)
795                                 latency_per_op_write = ((gauge_t) diff_latency_write) / ((gauge_t) diff_ops_write);
796                 }
797
798                 submit_two_gauge (hostname, plugin_instance, "disk_latency", /* type instance = */ NULL,
799                                 latency_per_op_read, latency_per_op_write, new_data->timestamp);
800         }
801
802         /* Clear all HAVE_* flags. */
803         old_data->flags &= ~HAVE_VOLUME_PERF_ALL;
804
805         /* Copy all counters */
806         old_data->timestamp = new_data->timestamp;
807         old_data->read_bytes = new_data->read_bytes;
808         old_data->write_bytes = new_data->write_bytes;
809         old_data->read_ops = new_data->read_ops;
810         old_data->write_ops = new_data->write_ops;
811         old_data->read_latency = new_data->read_latency;
812         old_data->write_latency = new_data->write_latency;
813
814         /* Copy the HAVE_* flags */
815         old_data->flags |= (new_data->flags & HAVE_VOLUME_PERF_ALL);
816
817         return (0);
818 } /* }}} int submit_volume_perf_data */
819
820 /* 
821  * Query functions
822  *
823  * These functions are called with appropriate data returned by the libnetapp
824  * interface which is parsed and submitted with the above functions.
825  */
826 /* Data corresponding to <WAFL /> */
827 static int cna_handle_wafl_data (const char *hostname, cfg_wafl_t *cfg_wafl, /* {{{ */
828                 na_elem_t *data)
829 {
830         cfg_wafl_t perf_data;
831         const char *plugin_inst;
832
833         na_elem_t *instances;
834         na_elem_t *counter;
835         na_elem_iter_t counter_iter;
836
837         memset (&perf_data, 0, sizeof (perf_data));
838         
839         perf_data.timestamp = (time_t) na_child_get_uint64 (data, "timestamp", 0);
840
841         instances = na_elem_child(na_elem_child (data, "instances"), "instance-data");
842         if (instances == NULL)
843         {
844                 ERROR ("netapp plugin: cna_handle_wafl_data: "
845                                 "na_elem_child (\"instances\") failed.");
846                 return (-1);
847         }
848
849         plugin_inst = na_child_get_string(instances, "name");
850         if (plugin_inst == NULL)
851         {
852                 ERROR ("netapp plugin: cna_handle_wafl_data: "
853                                 "na_child_get_string (\"name\") failed.");
854                 return (-1);
855         }
856
857         /* Iterate over all counters */
858         counter_iter = na_child_iterator (na_elem_child (instances, "counters"));
859         for (counter = na_iterator_next (&counter_iter);
860                         counter != NULL;
861                         counter = na_iterator_next (&counter_iter))
862         {
863                 const char *name;
864                 uint64_t value;
865
866                 name = na_child_get_string(counter, "name");
867                 if (name == NULL)
868                         continue;
869
870                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
871                 if (value == UINT64_MAX)
872                         continue;
873
874                 if (!strcmp(name, "name_cache_hit")) {
875                         perf_data.name_cache_hit = value;
876                         perf_data.flags |= HAVE_WAFL_NAME_CACHE_HIT;
877                 } else if (!strcmp(name, "name_cache_miss")) {
878                         perf_data.name_cache_miss = value;
879                         perf_data.flags |= HAVE_WAFL_NAME_CACHE_MISS;
880                 } else if (!strcmp(name, "find_dir_hit")) {
881                         perf_data.find_dir_hit = value;
882                         perf_data.flags |= HAVE_WAFL_FIND_DIR_HIT;
883                 } else if (!strcmp(name, "find_dir_miss")) {
884                         perf_data.find_dir_miss = value;
885                         perf_data.flags |= HAVE_WAFL_FIND_DIR_MISS;
886                 } else if (!strcmp(name, "buf_hash_hit")) {
887                         perf_data.buf_hash_hit = value;
888                         perf_data.flags |= HAVE_WAFL_BUF_HASH_HIT;
889                 } else if (!strcmp(name, "buf_hash_miss")) {
890                         perf_data.buf_hash_miss = value;
891                         perf_data.flags |= HAVE_WAFL_BUF_HASH_MISS;
892                 } else if (!strcmp(name, "inode_cache_hit")) {
893                         perf_data.inode_cache_hit = value;
894                         perf_data.flags |= HAVE_WAFL_INODE_CACHE_HIT;
895                 } else if (!strcmp(name, "inode_cache_miss")) {
896                         perf_data.inode_cache_miss = value;
897                         perf_data.flags |= HAVE_WAFL_INODE_CACHE_MISS;
898                 } else {
899                         DEBUG("netapp plugin: cna_handle_wafl_data: "
900                                         "Found unexpected child: %s", name);
901                 }
902         }
903
904         return (submit_wafl_data (hostname, plugin_inst, cfg_wafl, &perf_data));
905 } /* }}} void cna_handle_wafl_data */
906
907 static int cna_setup_wafl (cfg_wafl_t *cw) /* {{{ */
908 {
909         na_elem_t *e;
910
911         if (cw == NULL)
912                 return (EINVAL);
913
914         if (cw->query != NULL)
915                 return (0);
916
917         cw->query = na_elem_new("perf-object-get-instances");
918         if (cw->query == NULL)
919         {
920                 ERROR ("netapp plugin: na_elem_new failed.");
921                 return (-1);
922         }
923         na_child_add_string (cw->query, "objectname", "wafl");
924
925         e = na_elem_new("counters");
926         if (e == NULL)
927         {
928                 na_elem_free (cw->query);
929                 cw->query = NULL;
930                 ERROR ("netapp plugin: na_elem_new failed.");
931                 return (-1);
932         }
933         na_child_add_string(e, "foo", "name_cache_hit");
934         na_child_add_string(e, "foo", "name_cache_miss");
935         na_child_add_string(e, "foo", "find_dir_hit");
936         na_child_add_string(e, "foo", "find_dir_miss");
937         na_child_add_string(e, "foo", "buf_hash_hit");
938         na_child_add_string(e, "foo", "buf_hash_miss");
939         na_child_add_string(e, "foo", "inode_cache_hit");
940         na_child_add_string(e, "foo", "inode_cache_miss");
941
942         na_child_add(cw->query, e);
943
944         return (0);
945 } /* }}} int cna_setup_wafl */
946
947 static int cna_query_wafl (host_config_t *host) /* {{{ */
948 {
949         na_elem_t *data;
950         int status;
951         time_t now;
952
953         if (host == NULL)
954                 return (EINVAL);
955
956         /* If WAFL was not configured, return without doing anything. */
957         if (host->cfg_wafl == NULL)
958                 return (0);
959
960         now = time (NULL);
961         if ((host->cfg_wafl->interval.interval + host->cfg_wafl->interval.last_read) > now)
962                 return (0);
963
964         status = cna_setup_wafl (host->cfg_wafl);
965         if (status != 0)
966                 return (status);
967         assert (host->cfg_wafl->query != NULL);
968
969         data = na_server_invoke_elem(host->srv, host->cfg_wafl->query);
970         if (na_results_status (data) != NA_OK)
971         {
972                 ERROR ("netapp plugin: cna_query_wafl: na_server_invoke_elem failed: %s",
973                                 na_results_reason (data));
974                 na_elem_free (data);
975                 return (-1);
976         }
977
978         status = cna_handle_wafl_data (host->name, host->cfg_wafl, data);
979
980         if (status == 0)
981                 host->cfg_wafl->interval.last_read = now;
982
983         na_elem_free (data);
984         return (status);
985 } /* }}} int cna_query_wafl */
986
987 /* Data corresponding to <Disks /> */
988 static int cna_handle_disk_data (const char *hostname, /* {{{ */
989                 cfg_disk_t *cfg_disk, na_elem_t *data)
990 {
991         time_t timestamp;
992         na_elem_t *instances;
993         na_elem_t *instance;
994         na_elem_iter_t instance_iter;
995         disk_t *worst_disk = NULL;
996
997         if ((cfg_disk == NULL) || (data == NULL))
998                 return (EINVAL);
999         
1000         timestamp = (time_t) na_child_get_uint64(data, "timestamp", 0);
1001
1002         instances = na_elem_child (data, "instances");
1003         if (instances == NULL)
1004         {
1005                 ERROR ("netapp plugin: cna_handle_disk_data: "
1006                                 "na_elem_child (\"instances\") failed.");
1007                 return (-1);
1008         }
1009
1010         /* Iterate over all children */
1011         instance_iter = na_child_iterator (instances);
1012         for (instance = na_iterator_next (&instance_iter);
1013                         instance != NULL;
1014                         instance = na_iterator_next(&instance_iter))
1015         {
1016                 disk_t *old_data;
1017                 disk_t  new_data;
1018
1019                 na_elem_iter_t counter_iterator;
1020                 na_elem_t *counter;
1021
1022                 memset (&new_data, 0, sizeof (new_data));
1023                 new_data.timestamp = timestamp;
1024                 new_data.disk_busy_percent = NAN;
1025
1026                 old_data = get_disk(cfg_disk, na_child_get_string (instance, "name"));
1027                 if (old_data == NULL)
1028                         continue;
1029
1030                 /* Look for the "disk_busy" and "base_for_disk_busy" counters */
1031                 counter_iterator = na_child_iterator(na_elem_child(instance, "counters"));
1032                 for (counter = na_iterator_next(&counter_iterator);
1033                                 counter != NULL;
1034                                 counter = na_iterator_next(&counter_iterator))
1035                 {
1036                         const char *name;
1037                         uint64_t value;
1038
1039                         name = na_child_get_string(counter, "name");
1040                         if (name == NULL)
1041                                 continue;
1042
1043                         value = na_child_get_uint64(counter, "value", UINT64_MAX);
1044                         if (value == UINT64_MAX)
1045                                 continue;
1046
1047                         if (strcmp(name, "disk_busy") == 0)
1048                         {
1049                                 new_data.disk_busy = value;
1050                                 new_data.flags |= HAVE_DISK_BUSY;
1051                         }
1052                         else if (strcmp(name, "base_for_disk_busy") == 0)
1053                         {
1054                                 new_data.base_for_disk_busy = value;
1055                                 new_data.flags |= HAVE_DISK_BASE;
1056                         }
1057                         else
1058                         {
1059                                 DEBUG ("netapp plugin: cna_handle_disk_data: "
1060                                                 "Counter not handled: %s = %"PRIu64,
1061                                                 name, value);
1062                         }
1063                 }
1064
1065                 /* If all required counters are available and did not just wrap around,
1066                  * calculate the busy percentage. Otherwise, the value is initialized to
1067                  * NAN at the top of the for-loop. */
1068                 if (HAS_ALL_FLAGS (old_data->flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
1069                                 && HAS_ALL_FLAGS (new_data.flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
1070                                 && (new_data.disk_busy >= old_data->disk_busy)
1071                                 && (new_data.base_for_disk_busy > old_data->base_for_disk_busy))
1072                 {
1073                         uint64_t busy_diff;
1074                         uint64_t base_diff;
1075
1076                         busy_diff = new_data.disk_busy - old_data->disk_busy;
1077                         base_diff = new_data.base_for_disk_busy - old_data->base_for_disk_busy;
1078
1079                         new_data.disk_busy_percent = 100.0
1080                                 * ((gauge_t) busy_diff) / ((gauge_t) base_diff);
1081                 }
1082
1083                 /* Clear HAVE_* flags */
1084                 old_data->flags &= ~HAVE_DISK_ALL;
1085
1086                 /* Copy data */
1087                 old_data->timestamp = new_data.timestamp;
1088                 old_data->disk_busy = new_data.disk_busy;
1089                 old_data->base_for_disk_busy = new_data.base_for_disk_busy;
1090                 old_data->disk_busy_percent = new_data.disk_busy_percent;
1091
1092                 /* Copy flags */
1093                 old_data->flags |= (new_data.flags & HAVE_DISK_ALL);
1094
1095                 if ((worst_disk == NULL)
1096                                 || (worst_disk->disk_busy_percent < old_data->disk_busy_percent))
1097                         worst_disk = old_data;
1098         } /* for (all disks) */
1099
1100         if ((cfg_disk->flags & CFG_DISK_BUSIEST) && (worst_disk != NULL))
1101                 submit_double (hostname, "system", "percent", "disk_busy",
1102                                 worst_disk->disk_busy_percent, timestamp);
1103
1104         return (0);
1105 } /* }}} int cna_handle_disk_data */
1106
1107 static int cna_setup_disk (cfg_disk_t *cd) /* {{{ */
1108 {
1109         na_elem_t *e;
1110
1111         if (cd == NULL)
1112                 return (EINVAL);
1113
1114         if (cd->query != NULL)
1115                 return (0);
1116
1117         cd->query = na_elem_new ("perf-object-get-instances");
1118         if (cd->query == NULL)
1119         {
1120                 ERROR ("netapp plugin: na_elem_new failed.");
1121                 return (-1);
1122         }
1123         na_child_add_string (cd->query, "objectname", "disk");
1124
1125         e = na_elem_new("counters");
1126         if (e == NULL)
1127         {
1128                 na_elem_free (cd->query);
1129                 cd->query = NULL;
1130                 ERROR ("netapp plugin: na_elem_new failed.");
1131                 return (-1);
1132         }
1133         na_child_add_string(e, "foo", "disk_busy");
1134         na_child_add_string(e, "foo", "base_for_disk_busy");
1135         na_child_add(cd->query, e);
1136
1137         return (0);
1138 } /* }}} int cna_setup_disk */
1139
1140 static int cna_query_disk (host_config_t *host) /* {{{ */
1141 {
1142         na_elem_t *data;
1143         int status;
1144         time_t now;
1145
1146         if (host == NULL)
1147                 return (EINVAL);
1148
1149         /* If the user did not configure disk statistics, return without doing
1150          * anything. */
1151         if (host->cfg_disk == NULL)
1152                 return (0);
1153
1154         now = time (NULL);
1155         if ((host->cfg_disk->interval.interval + host->cfg_disk->interval.last_read) > now)
1156                 return (0);
1157
1158         status = cna_setup_disk (host->cfg_disk);
1159         if (status != 0)
1160                 return (status);
1161         assert (host->cfg_disk->query != NULL);
1162
1163         data = na_server_invoke_elem(host->srv, host->cfg_disk->query);
1164         if (na_results_status (data) != NA_OK)
1165         {
1166                 ERROR ("netapp plugin: cna_query_disk: na_server_invoke_elem failed: %s",
1167                                 na_results_reason (data));
1168                 na_elem_free (data);
1169                 return (-1);
1170         }
1171
1172         status = cna_handle_disk_data (host->name, host->cfg_disk, data);
1173
1174         if (status == 0)
1175                 host->cfg_disk->interval.last_read = now;
1176
1177         na_elem_free (data);
1178         return (status);
1179 } /* }}} int cna_query_disk */
1180
1181 /* Data corresponding to <VolumePerf /> */
1182 static int cna_handle_volume_perf_data (const char *hostname, /* {{{ */
1183                 cfg_volume_perf_t *cvp, na_elem_t *data)
1184 {
1185         time_t timestamp;
1186         na_elem_t *elem_instances;
1187         na_elem_iter_t iter_instances;
1188         na_elem_t *elem_instance;
1189         
1190         timestamp = (time_t) na_child_get_uint64(data, "timestamp", 0);
1191
1192         elem_instances = na_elem_child(data, "instances");
1193         if (elem_instances == NULL)
1194         {
1195                 ERROR ("netapp plugin: handle_volume_perf_data: "
1196                                 "na_elem_child (\"instances\") failed.");
1197                 return (-1);
1198         }
1199
1200         iter_instances = na_child_iterator (elem_instances);
1201         for (elem_instance = na_iterator_next(&iter_instances);
1202                         elem_instance != NULL;
1203                         elem_instance = na_iterator_next(&iter_instances))
1204         {
1205                 const char *name;
1206
1207                 data_volume_perf_t perf_data;
1208                 data_volume_perf_t *v;
1209
1210                 na_elem_t *elem_counters;
1211                 na_elem_iter_t iter_counters;
1212                 na_elem_t *elem_counter;
1213
1214                 memset (&perf_data, 0, sizeof (perf_data));
1215                 perf_data.timestamp = timestamp;
1216
1217                 name = na_child_get_string (elem_instance, "name");
1218                 if (name == NULL)
1219                         continue;
1220
1221                 /* get_volume_perf may return NULL if this volume is to be ignored. */
1222                 v = get_volume_perf (cvp, name);
1223                 if (v == NULL)
1224                         continue;
1225
1226                 elem_counters = na_elem_child (elem_instance, "counters");
1227                 if (elem_counters == NULL)
1228                         continue;
1229
1230                 iter_counters = na_child_iterator (elem_counters);
1231                 for (elem_counter = na_iterator_next(&iter_counters);
1232                                 elem_counter != NULL;
1233                                 elem_counter = na_iterator_next(&iter_counters))
1234                 {
1235                         const char *name;
1236                         uint64_t value;
1237
1238                         name = na_child_get_string (elem_counter, "name");
1239                         if (name == NULL)
1240                                 continue;
1241
1242                         value = na_child_get_uint64 (elem_counter, "value", UINT64_MAX);
1243                         if (value == UINT64_MAX)
1244                                 continue;
1245
1246                         if (!strcmp(name, "read_data")) {
1247                                 perf_data.read_bytes = value;
1248                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_READ;
1249                         } else if (!strcmp(name, "write_data")) {
1250                                 perf_data.write_bytes = value;
1251                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_WRITE;
1252                         } else if (!strcmp(name, "read_ops")) {
1253                                 perf_data.read_ops = value;
1254                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_READ;
1255                         } else if (!strcmp(name, "write_ops")) {
1256                                 perf_data.write_ops = value;
1257                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_WRITE;
1258                         } else if (!strcmp(name, "read_latency")) {
1259                                 perf_data.read_latency = value;
1260                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_READ;
1261                         } else if (!strcmp(name, "write_latency")) {
1262                                 perf_data.write_latency = value;
1263                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_WRITE;
1264                         }
1265                 } /* for (elem_counter) */
1266
1267                 submit_volume_perf_data (hostname, v, &perf_data);
1268         } /* for (volume) */
1269
1270         return (0);
1271 } /* }}} int cna_handle_volume_perf_data */
1272
1273 static int cna_setup_volume_perf (cfg_volume_perf_t *cd) /* {{{ */
1274 {
1275         na_elem_t *e;
1276
1277         if (cd == NULL)
1278                 return (EINVAL);
1279
1280         if (cd->query != NULL)
1281                 return (0);
1282
1283         cd->query = na_elem_new ("perf-object-get-instances");
1284         if (cd->query == NULL)
1285         {
1286                 ERROR ("netapp plugin: na_elem_new failed.");
1287                 return (-1);
1288         }
1289         na_child_add_string (cd->query, "objectname", "volume");
1290
1291         e = na_elem_new("counters");
1292         if (e == NULL)
1293         {
1294                 na_elem_free (cd->query);
1295                 cd->query = NULL;
1296                 ERROR ("netapp plugin: na_elem_new failed.");
1297                 return (-1);
1298         }
1299         /* "foo" means: This string has to be here but the content doesn't matter. */
1300         na_child_add_string(e, "foo", "read_ops");
1301         na_child_add_string(e, "foo", "write_ops");
1302         na_child_add_string(e, "foo", "read_data");
1303         na_child_add_string(e, "foo", "write_data");
1304         na_child_add_string(e, "foo", "read_latency");
1305         na_child_add_string(e, "foo", "write_latency");
1306         na_child_add(cd->query, e);
1307
1308         return (0);
1309 } /* }}} int cna_setup_volume_perf */
1310
1311 static int cna_query_volume_perf (host_config_t *host) /* {{{ */
1312 {
1313         na_elem_t *data;
1314         int status;
1315         time_t now;
1316
1317         if (host == NULL)
1318                 return (EINVAL);
1319
1320         /* If the user did not configure volume performance statistics, return
1321          * without doing anything. */
1322         if (host->cfg_volume_perf == NULL)
1323                 return (0);
1324
1325         now = time (NULL);
1326         if ((host->cfg_volume_perf->interval.interval + host->cfg_volume_perf->interval.last_read) > now)
1327                 return (0);
1328
1329         status = cna_setup_volume_perf (host->cfg_volume_perf);
1330         if (status != 0)
1331                 return (status);
1332         assert (host->cfg_volume_perf->query != NULL);
1333
1334         data = na_server_invoke_elem (host->srv, host->cfg_volume_perf->query);
1335         if (na_results_status (data) != NA_OK)
1336         {
1337                 ERROR ("netapp plugin: cna_query_volume_perf: na_server_invoke_elem failed: %s",
1338                                 na_results_reason (data));
1339                 na_elem_free (data);
1340                 return (-1);
1341         }
1342
1343         status = cna_handle_volume_perf_data (host->name, host->cfg_volume_perf, data);
1344
1345         if (status == 0)
1346                 host->cfg_volume_perf->interval.last_read = now;
1347
1348         na_elem_free (data);
1349         return (status);
1350 } /* }}} int cna_query_volume_perf */
1351
1352 /* Data corresponding to <VolumeUsage /> */
1353 static int cna_submit_volume_usage_data (const char *hostname, /* {{{ */
1354                 cfg_volume_usage_t *cfg_volume)
1355 {
1356         data_volume_usage_t *v;
1357
1358         for (v = cfg_volume->volumes; v != NULL; v = v->next)
1359         {
1360                 char plugin_instance[DATA_MAX_NAME_LEN];
1361
1362                 uint64_t norm_used = v->norm_used;
1363                 uint64_t norm_free = v->norm_free;
1364                 uint64_t sis_saved = v->sis_saved;
1365                 uint64_t snap_reserve_used = 0;
1366                 uint64_t snap_reserve_free = v->snap_reserved;
1367                 uint64_t snap_norm_used = v->snap_used;
1368
1369                 ssnprintf (plugin_instance, sizeof (plugin_instance),
1370                                 "volume-%s", v->name);
1371
1372                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_USED | HAVE_VOLUME_USAGE_SNAP_RSVD)) {
1373                         if (v->snap_reserved > v->snap_used) {
1374                                 snap_reserve_free = v->snap_reserved - v->snap_used;
1375                                 snap_reserve_used = v->snap_used;
1376                                 snap_norm_used = 0;
1377                         } else {
1378                                 snap_reserve_free = 0;
1379                                 snap_reserve_used = v->snap_reserved;
1380                                 snap_norm_used = v->snap_used - v->snap_reserved;
1381                         }
1382                 }
1383
1384                 /* The space used by snapshots but not reserved for them is included in
1385                  * both, norm_used and snap_norm_used. If possible, subtract this here. */
1386                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_NORM_USED | HAVE_VOLUME_USAGE_SNAP_USED))
1387                 {
1388                         if (norm_used >= snap_norm_used)
1389                                 norm_used -= snap_norm_used;
1390                         else
1391                         {
1392                                 ERROR ("netapp plugin: (norm_used = %"PRIu64") < (snap_norm_used = "
1393                                                 "%"PRIu64"). Invalidating both.",
1394                                                 norm_used, snap_norm_used);
1395                                 v->flags &= ~(HAVE_VOLUME_USAGE_NORM_USED | HAVE_VOLUME_USAGE_SNAP_USED);
1396                         }
1397                 }
1398
1399                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_NORM_FREE))
1400                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1401                                         "df_complex", "free",
1402                                         (double) norm_free, /* timestamp = */ 0);
1403
1404                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SIS_SAVED))
1405                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1406                                         "df_complex", "sis_saved",
1407                                         (double) sis_saved, /* timestamp = */ 0);
1408
1409                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_NORM_USED))
1410                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1411                                         "df_complex", "used",
1412                                         (double) norm_used, /* timestamp = */ 0);
1413
1414                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_RSVD))
1415                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1416                                         "df_complex", "snap_reserved",
1417                                         (double) snap_reserve_free, /* timestamp = */ 0);
1418
1419                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_USED | HAVE_VOLUME_USAGE_SNAP_RSVD))
1420                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1421                                         "df_complex", "snap_reserve_used",
1422                                         (double) snap_reserve_used, /* timestamp = */ 0);
1423
1424                 if (HAS_ALL_FLAGS (v->flags, HAVE_VOLUME_USAGE_SNAP_USED))
1425                         submit_double (hostname, /* plugin instance = */ plugin_instance,
1426                                         "df_complex", "snap_normal_used",
1427                                         (double) snap_norm_used, /* timestamp = */ 0);
1428
1429                 /* Clear all the HAVE_* flags */
1430                 v->flags &= ~HAVE_VOLUME_USAGE_ALL;
1431         } /* for (v = cfg_volume->volumes) */
1432
1433         return (0);
1434 } /* }}} int cna_submit_volume_usage_data */
1435
1436 /* Switch the state of a volume between online and offline and send out a
1437  * notification. */
1438 static int cna_change_volume_status (const char *hostname, /* {{{ */
1439                 data_volume_usage_t *v)
1440 {
1441         notification_t n;
1442
1443         memset (&n, 0, sizeof (&n));
1444         n.time = time (NULL);
1445         sstrncpy (n.host, hostname, sizeof (n.host));
1446         sstrncpy (n.plugin, "netapp", sizeof (n.plugin));
1447         sstrncpy (n.plugin_instance, v->name, sizeof (n.plugin_instance));
1448
1449         if ((v->flags & IS_VOLUME_USAGE_OFFLINE) != 0) {
1450                 n.severity = NOTIF_OKAY;
1451                 ssnprintf (n.message, sizeof (n.message),
1452                                 "Volume %s is now online.", v->name);
1453                 v->flags &= ~IS_VOLUME_USAGE_OFFLINE;
1454         } else {
1455                 n.severity = NOTIF_WARNING;
1456                 ssnprintf (n.message, sizeof (n.message),
1457                                 "Volume %s is now offline.", v->name);
1458                 v->flags |= IS_VOLUME_USAGE_OFFLINE;
1459         }
1460
1461         return (plugin_dispatch_notification (&n));
1462 } /* }}} int cna_change_volume_status */
1463
1464 static void cna_handle_volume_snap_usage(const host_config_t *host, /* {{{ */
1465                 data_volume_usage_t *v)
1466 {
1467         uint64_t snap_used = 0, value;
1468         na_elem_t *data, *elem_snap, *elem_snapshots;
1469         na_elem_iter_t iter_snap;
1470
1471         data = na_server_invoke_elem(host->srv, v->snap_query);
1472         if (na_results_status(data) != NA_OK)
1473         {
1474                 if (na_results_errno(data) == EVOLUMEOFFLINE) {
1475                         if ((v->flags & IS_VOLUME_USAGE_OFFLINE) == 0)
1476                                 cna_change_volume_status (host->name, v);
1477                 } else {
1478                         ERROR ("netapp plugin: cna_handle_volume_snap_usage: na_server_invoke_elem for "
1479                                         "volume \"%s\" failed with error %d: %s", v->name,
1480                                         na_results_errno(data), na_results_reason(data));
1481                 }
1482                 na_elem_free(data);
1483                 return;
1484         }
1485
1486         if ((v->flags & IS_VOLUME_USAGE_OFFLINE) != 0)
1487                 cna_change_volume_status (host->name, v);
1488
1489         elem_snapshots = na_elem_child (data, "snapshots");
1490         if (elem_snapshots == NULL)
1491         {
1492                 ERROR ("netapp plugin: cna_handle_volume_snap_usage: "
1493                                 "na_elem_child (\"snapshots\") failed.");
1494                 na_elem_free(data);
1495                 return;
1496         }
1497
1498         iter_snap = na_child_iterator (elem_snapshots);
1499         for (elem_snap = na_iterator_next (&iter_snap);
1500                         elem_snap != NULL;
1501                         elem_snap = na_iterator_next (&iter_snap))
1502         {
1503                 value = na_child_get_uint64(elem_snap, "cumulative-total", 0);
1504                 /* "cumulative-total" is the total size of the oldest snapshot plus all
1505                  * newer ones in blocks (1KB). We therefore are looking for the highest
1506                  * number of all snapshots - that's the size required for the snapshots. */
1507                 if (value > snap_used)
1508                         snap_used = value;
1509         }
1510         na_elem_free (data);
1511         /* snap_used is in 1024 byte blocks */
1512         v->snap_used = snap_used * 1024;
1513         v->flags |= HAVE_VOLUME_USAGE_SNAP_USED;
1514 } /* }}} void cna_handle_volume_snap_usage */
1515
1516 static int cna_handle_volume_usage_data (const host_config_t *host, /* {{{ */
1517                 cfg_volume_usage_t *cfg_volume, na_elem_t *data)
1518 {
1519         na_elem_t *elem_volume;
1520         na_elem_t *elem_volumes;
1521         na_elem_iter_t iter_volume;
1522
1523         elem_volumes = na_elem_child (data, "volumes");
1524         if (elem_volumes == NULL)
1525         {
1526                 ERROR ("netapp plugin: cna_handle_volume_usage_data: "
1527                                 "na_elem_child (\"volumes\") failed.");
1528                 return (-1);
1529         }
1530
1531         iter_volume = na_child_iterator (elem_volumes);
1532         for (elem_volume = na_iterator_next (&iter_volume);
1533                         elem_volume != NULL;
1534                         elem_volume = na_iterator_next (&iter_volume))
1535         {
1536                 const char *volume_name, *state;
1537
1538                 data_volume_usage_t *v;
1539                 uint64_t value;
1540
1541                 na_elem_t *sis;
1542                 const char *sis_state;
1543                 uint64_t sis_saved_reported;
1544
1545                 volume_name = na_child_get_string (elem_volume, "name");
1546                 if (volume_name == NULL)
1547                         continue;
1548
1549                 state = na_child_get_string (elem_volume, "state");
1550                 if ((state == NULL) || (strcmp(state, "online") != 0))
1551                         continue;
1552
1553                 /* get_volume_usage may return NULL if the volume is to be ignored. */
1554                 v = get_volume_usage (cfg_volume, volume_name);
1555                 if (v == NULL)
1556                         continue;
1557
1558                 if ((v->flags & CFG_VOLUME_USAGE_SNAP) != 0)
1559                         cna_handle_volume_snap_usage(host, v);
1560                 
1561                 if ((v->flags & CFG_VOLUME_USAGE_DF) == 0)
1562                         continue;
1563
1564                 /* 2^4 exa-bytes? This will take a while ;) */
1565                 value = na_child_get_uint64(elem_volume, "size-available", UINT64_MAX);
1566                 if (value != UINT64_MAX) {
1567                         v->norm_free = value;
1568                         v->flags |= HAVE_VOLUME_USAGE_NORM_FREE;
1569                 }
1570
1571                 value = na_child_get_uint64(elem_volume, "size-used", UINT64_MAX);
1572                 if (value != UINT64_MAX) {
1573                         v->norm_used = value;
1574                         v->flags |= HAVE_VOLUME_USAGE_NORM_USED;
1575                 }
1576
1577                 value = na_child_get_uint64(elem_volume, "snapshot-blocks-reserved", UINT64_MAX);
1578                 if (value != UINT64_MAX) {
1579                         /* 1 block == 1024 bytes  as per API docs */
1580                         v->snap_reserved = 1024 * value;
1581                         v->flags |= HAVE_VOLUME_USAGE_SNAP_RSVD;
1582                 }
1583
1584                 sis = na_elem_child(elem_volume, "sis");
1585                 if (sis == NULL)
1586                         continue;
1587
1588                 sis_state = na_child_get_string(sis, "state");
1589                 if (sis_state == NULL)
1590                         continue;
1591
1592                 /* If SIS is not enabled, there's nothing left to do for this volume. */
1593                 if (strcmp ("enabled", sis_state) != 0)
1594                         continue;
1595
1596                 sis_saved_reported = na_child_get_uint64(sis, "size-saved", UINT64_MAX);
1597                 if (sis_saved_reported == UINT64_MAX)
1598                         continue;
1599
1600                 /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
1601                 if ((sis_saved_reported >> 32) != 0) {
1602                         /* In case they ever fix this bug. */
1603                         v->sis_saved = sis_saved_reported;
1604                         v->flags |= HAVE_VOLUME_USAGE_SIS_SAVED;
1605                 } else { /* really hacky work-around code. {{{ */
1606                         uint64_t sis_saved_percent;
1607                         uint64_t sis_saved_guess;
1608                         uint64_t overflow_guess;
1609                         uint64_t guess1, guess2, guess3;
1610
1611                         /* Check if we have v->norm_used. Without it, we cannot calculate
1612                          * sis_saved_guess. */
1613                         if ((v->flags & HAVE_VOLUME_USAGE_NORM_USED) == 0)
1614                                 continue;
1615
1616                         sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
1617                         if (sis_saved_percent > 100)
1618                                 continue;
1619
1620                         /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
1621                          * will hopefully be fixed in later versions. To work around the bug, try
1622                          * to figure out how often the 32bit integer wrapped around by using the
1623                          * "percentage-saved" value. Because the percentage is in the range
1624                          * [0-100], this should work as long as the saved space does not exceed
1625                          * 400 GBytes. */
1626                         /* percentage-saved = size-saved / (size-saved + size-used) */
1627                         if (sis_saved_percent < 100)
1628                                 sis_saved_guess = v->norm_used * sis_saved_percent / (100 - sis_saved_percent);
1629                         else
1630                                 sis_saved_guess = v->norm_used;
1631
1632                         overflow_guess = sis_saved_guess >> 32;
1633                         guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
1634                         guess2 = (overflow_guess << 32) + sis_saved_reported;
1635                         guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
1636
1637                         if (sis_saved_guess < guess2) {
1638                                 if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
1639                                         v->sis_saved = guess1;
1640                                 else
1641                                         v->sis_saved = guess2;
1642                         } else {
1643                                 if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
1644                                         v->sis_saved = guess2;
1645                                 else
1646                                         v->sis_saved = guess3;
1647                         }
1648                         v->flags |= HAVE_VOLUME_USAGE_SIS_SAVED;
1649                 } /* }}} end of 32-bit workaround */
1650         } /* for (elem_volume) */
1651
1652         return (cna_submit_volume_usage_data (host->name, cfg_volume));
1653 } /* }}} int cna_handle_volume_usage_data */
1654
1655 static int cna_setup_volume_usage (cfg_volume_usage_t *cvu) /* {{{ */
1656 {
1657         if (cvu == NULL)
1658                 return (EINVAL);
1659
1660         if (cvu->query != NULL)
1661                 return (0);
1662
1663         cvu->query = na_elem_new ("volume-list-info");
1664         if (cvu->query == NULL)
1665         {
1666                 ERROR ("netapp plugin: na_elem_new failed.");
1667                 return (-1);
1668         }
1669
1670         return (0);
1671 } /* }}} int cna_setup_volume_usage */
1672
1673 static int cna_query_volume_usage (host_config_t *host) /* {{{ */
1674 {
1675         na_elem_t *data;
1676         int status;
1677         time_t now;
1678
1679         if (host == NULL)
1680                 return (EINVAL);
1681
1682         /* If the user did not configure volume_usage statistics, return without
1683          * doing anything. */
1684         if (host->cfg_volume_usage == NULL)
1685                 return (0);
1686
1687         now = time (NULL);
1688         if ((host->cfg_volume_usage->interval.interval + host->cfg_volume_usage->interval.last_read) > now)
1689                 return (0);
1690
1691         status = cna_setup_volume_usage (host->cfg_volume_usage);
1692         if (status != 0)
1693                 return (status);
1694         assert (host->cfg_volume_usage->query != NULL);
1695
1696         data = na_server_invoke_elem(host->srv, host->cfg_volume_usage->query);
1697         if (na_results_status (data) != NA_OK)
1698         {
1699                 ERROR ("netapp plugin: cna_query_volume_usage: na_server_invoke_elem failed: %s",
1700                                 na_results_reason (data));
1701                 na_elem_free (data);
1702                 return (-1);
1703         }
1704
1705         status = cna_handle_volume_usage_data (host, host->cfg_volume_usage, data);
1706
1707         if (status == 0)
1708                 host->cfg_volume_usage->interval.last_read = now;
1709
1710         na_elem_free (data);
1711         return (status);
1712 } /* }}} int cna_query_volume_usage */
1713
1714 /* Data corresponding to <System /> */
1715 static int cna_handle_system_data (const char *hostname, /* {{{ */
1716                 cfg_system_t *cfg_system, na_elem_t *data)
1717 {
1718         na_elem_t *instances;
1719         na_elem_t *counter;
1720         na_elem_iter_t counter_iter;
1721
1722         counter_t disk_read = 0, disk_written = 0;
1723         counter_t net_recv = 0, net_sent = 0;
1724         counter_t cpu_busy = 0, cpu_total = 0;
1725         uint32_t counter_flags = 0;
1726
1727         const char *instance;
1728         time_t timestamp;
1729         
1730         timestamp = (time_t) na_child_get_uint64 (data, "timestamp", 0);
1731
1732         instances = na_elem_child(na_elem_child (data, "instances"), "instance-data");
1733         if (instances == NULL)
1734         {
1735                 ERROR ("netapp plugin: cna_handle_system_data: "
1736                                 "na_elem_child (\"instances\") failed.");
1737                 return (-1);
1738         }
1739
1740         instance = na_child_get_string (instances, "name");
1741         if (instance == NULL)
1742         {
1743                 ERROR ("netapp plugin: cna_handle_system_data: "
1744                                 "na_child_get_string (\"name\") failed.");
1745                 return (-1);
1746         }
1747
1748         counter_iter = na_child_iterator (na_elem_child (instances, "counters"));
1749         for (counter = na_iterator_next (&counter_iter);
1750                         counter != NULL;
1751                         counter = na_iterator_next (&counter_iter))
1752         {
1753                 const char *name;
1754                 uint64_t value;
1755
1756                 name = na_child_get_string(counter, "name");
1757                 if (name == NULL)
1758                         continue;
1759
1760                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
1761                 if (value == UINT64_MAX)
1762                         continue;
1763
1764                 if (!strcmp(name, "disk_data_read")) {
1765                         disk_read = (counter_t) (value * 1024);
1766                         counter_flags |= 0x01;
1767                 } else if (!strcmp(name, "disk_data_written")) {
1768                         disk_written = (counter_t) (value * 1024);
1769                         counter_flags |= 0x02;
1770                 } else if (!strcmp(name, "net_data_recv")) {
1771                         net_recv = (counter_t) (value * 1024);
1772                         counter_flags |= 0x04;
1773                 } else if (!strcmp(name, "net_data_sent")) {
1774                         net_sent = (counter_t) (value * 1024);
1775                         counter_flags |= 0x08;
1776                 } else if (!strcmp(name, "cpu_busy")) {
1777                         cpu_busy = (counter_t) value;
1778                         counter_flags |= 0x10;
1779                 } else if (!strcmp(name, "cpu_elapsed_time")) {
1780                         cpu_total = (counter_t) value;
1781                         counter_flags |= 0x20;
1782                 } else if ((cfg_system->flags & CFG_SYSTEM_OPS)
1783                                 && (value > 0) && (strlen(name) > 4)
1784                                 && (!strcmp(name + strlen(name) - 4, "_ops"))) {
1785                         submit_counter (hostname, instance, "disk_ops_complex", name,
1786                                         (counter_t) value, timestamp);
1787                 }
1788         } /* for (counter) */
1789
1790         if ((cfg_system->flags & CFG_SYSTEM_DISK)
1791                         && (HAS_ALL_FLAGS (counter_flags, 0x01 | 0x02)))
1792                 submit_two_counters (hostname, instance, "disk_octets", NULL,
1793                                 disk_read, disk_written, timestamp);
1794                                 
1795         if ((cfg_system->flags & CFG_SYSTEM_NET)
1796                         && (HAS_ALL_FLAGS (counter_flags, 0x04 | 0x08)))
1797                 submit_two_counters (hostname, instance, "if_octets", NULL,
1798                                 net_recv, net_sent, timestamp);
1799
1800         if ((cfg_system->flags & CFG_SYSTEM_CPU)
1801                         && (HAS_ALL_FLAGS (counter_flags, 0x10 | 0x20)))
1802         {
1803                 submit_counter (hostname, instance, "cpu", "system",
1804                                 cpu_busy, timestamp);
1805                 submit_counter (hostname, instance, "cpu", "idle",
1806                                 cpu_total - cpu_busy, timestamp);
1807         }
1808
1809         return (0);
1810 } /* }}} int cna_handle_system_data */
1811
1812 static int cna_setup_system (cfg_system_t *cs) /* {{{ */
1813 {
1814         if (cs == NULL)
1815                 return (EINVAL);
1816
1817         if (cs->query != NULL)
1818                 return (0);
1819
1820         cs->query = na_elem_new ("perf-object-get-instances");
1821         if (cs->query == NULL)
1822         {
1823                 ERROR ("netapp plugin: na_elem_new failed.");
1824                 return (-1);
1825         }
1826         na_child_add_string (cs->query, "objectname", "system");
1827
1828         return (0);
1829 } /* }}} int cna_setup_system */
1830
1831 static int cna_query_system (host_config_t *host) /* {{{ */
1832 {
1833         na_elem_t *data;
1834         int status;
1835         time_t now;
1836
1837         if (host == NULL)
1838                 return (EINVAL);
1839
1840         /* If system statistics were not configured, return without doing anything. */
1841         if (host->cfg_system == NULL)
1842                 return (0);
1843
1844         now = time (NULL);
1845         if ((host->cfg_system->interval.interval + host->cfg_system->interval.last_read) > now)
1846                 return (0);
1847
1848         status = cna_setup_system (host->cfg_system);
1849         if (status != 0)
1850                 return (status);
1851         assert (host->cfg_system->query != NULL);
1852
1853         data = na_server_invoke_elem(host->srv, host->cfg_system->query);
1854         if (na_results_status (data) != NA_OK)
1855         {
1856                 ERROR ("netapp plugin: cna_query_system: na_server_invoke_elem failed: %s",
1857                                 na_results_reason (data));
1858                 na_elem_free (data);
1859                 return (-1);
1860         }
1861
1862         status = cna_handle_system_data (host->name, host->cfg_system, data);
1863
1864         if (status == 0)
1865                 host->cfg_system->interval.last_read = now;
1866
1867         na_elem_free (data);
1868         return (status);
1869 } /* }}} int cna_query_system */
1870
1871 /*
1872  * Configuration handling
1873  */
1874 /* Sets a given flag if the boolean argument is true and unsets the flag if it
1875  * is false. On error, the flag-field is not changed. */
1876 static int cna_config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */
1877                 uint32_t *flags, uint32_t flag)
1878 {
1879         if ((ci == NULL) || (flags == NULL))
1880                 return (EINVAL);
1881
1882         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1883         {
1884                 WARNING ("netapp plugin: The %s option needs exactly one boolean argument.",
1885                                 ci->key);
1886                 return (-1);
1887         }
1888
1889         if (ci->values[0].value.boolean)
1890                 *flags |= flag;
1891         else
1892                 *flags &= ~flag;
1893
1894         return (0);
1895 } /* }}} int cna_config_bool_to_flag */
1896
1897 /* Handling of the "Interval" option which is allowed in every block. */
1898 static int cna_config_get_interval (const oconfig_item_t *ci, /* {{{ */
1899                 cna_interval_t *out_interval)
1900 {
1901         time_t tmp;
1902
1903         if ((ci == NULL) || (out_interval == NULL))
1904                 return (EINVAL);
1905
1906         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1907         {
1908                 WARNING ("netapp plugin: The `Interval' option needs exactly one numeric argument.");
1909                 return (-1);
1910         }
1911
1912         tmp = (time_t) (ci->values[0].value.number + .5);
1913         if (tmp < 1)
1914         {
1915                 WARNING ("netapp plugin: The `Interval' option needs a positive integer argument.");
1916                 return (-1);
1917         }
1918
1919         out_interval->interval = tmp;
1920         out_interval->last_read = 0;
1921
1922         return (0);
1923 } /* }}} int cna_config_get_interval */
1924
1925 /* Handling of the "GetIO", "GetOps" and "GetLatency" options within a
1926  * <VolumePerf /> block. */
1927 static void cna_config_volume_perf_option (cfg_volume_perf_t *cvp, /* {{{ */
1928                 const oconfig_item_t *ci)
1929 {
1930         char *name;
1931         ignorelist_t * il;
1932
1933         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1934         {
1935                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
1936                                 ci->key);
1937                 return;
1938         }
1939
1940         name = ci->values[0].value.string;
1941
1942         if (strcasecmp ("GetIO", ci->key) == 0)
1943                 il = cvp->il_octets;
1944         else if (strcasecmp ("GetOps", ci->key) == 0)
1945                 il = cvp->il_operations;
1946         else if (strcasecmp ("GetLatency", ci->key) == 0)
1947                 il = cvp->il_latency;
1948         else
1949                 return;
1950
1951         ignorelist_add (il, name);
1952 } /* }}} void cna_config_volume_perf_option */
1953
1954 /* Handling of the "IgnoreSelectedIO", "IgnoreSelectedOps" and
1955  * "IgnoreSelectedLatency" options within a <VolumePerf /> block. */
1956 static void cna_config_volume_perf_default (cfg_volume_perf_t *cvp, /* {{{ */
1957                 const oconfig_item_t *ci)
1958 {
1959         ignorelist_t *il;
1960
1961         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1962         {
1963                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
1964                                 ci->key);
1965                 return;
1966         }
1967
1968         if (strcasecmp ("IgnoreSelectedIO", ci->key) == 0)
1969                 il = cvp->il_octets;
1970         else if (strcasecmp ("IgnoreSelectedOps", ci->key) == 0)
1971                 il = cvp->il_operations;
1972         else if (strcasecmp ("IgnoreSelectedLatency", ci->key) == 0)
1973                 il = cvp->il_latency;
1974         else
1975                 return;
1976
1977         if (ci->values[0].value.boolean)
1978                 ignorelist_set_invert (il, /* invert = */ 0);
1979         else
1980                 ignorelist_set_invert (il, /* invert = */ 1);
1981 } /* }}} void cna_config_volume_perf_default */
1982
1983 /* Corresponds to a <Disks /> block */
1984 /*
1985  * <VolumePerf>
1986  *   GetIO "vol0"
1987  *   GetIO "vol1"
1988  *   IgnoreSelectedIO false
1989  *
1990  *   GetOps "vol0"
1991  *   GetOps "vol2"
1992  *   IgnoreSelectedOps false
1993  *
1994  *   GetLatency "vol2"
1995  *   GetLatency "vol3"
1996  *   IgnoreSelectedLatency false
1997  * </VolumePerf>
1998  */
1999 /* Corresponds to a <VolumePerf /> block */
2000 static int cna_config_volume_performance (host_config_t *host, /* {{{ */
2001                 const oconfig_item_t *ci)
2002 {
2003         cfg_volume_perf_t *cfg_volume_perf;
2004         int i;
2005
2006         if ((host == NULL) || (ci == NULL))
2007                 return (EINVAL);
2008
2009         if (host->cfg_volume_perf == NULL)
2010         {
2011                 cfg_volume_perf = malloc (sizeof (*cfg_volume_perf));
2012                 if (cfg_volume_perf == NULL)
2013                         return (ENOMEM);
2014                 memset (cfg_volume_perf, 0, sizeof (*cfg_volume_perf));
2015
2016                 /* Set default flags */
2017                 cfg_volume_perf->query = NULL;
2018                 cfg_volume_perf->volumes = NULL;
2019
2020                 cfg_volume_perf->il_octets = ignorelist_create (/* invert = */ 1);
2021                 if (cfg_volume_perf->il_octets == NULL)
2022                 {
2023                         sfree (cfg_volume_perf);
2024                         return (ENOMEM);
2025                 }
2026
2027                 cfg_volume_perf->il_operations = ignorelist_create (/* invert = */ 1);
2028                 if (cfg_volume_perf->il_operations == NULL)
2029                 {
2030                         ignorelist_free (cfg_volume_perf->il_octets);
2031                         sfree (cfg_volume_perf);
2032                         return (ENOMEM);
2033                 }
2034
2035                 cfg_volume_perf->il_latency = ignorelist_create (/* invert = */ 1);
2036                 if (cfg_volume_perf->il_latency == NULL)
2037                 {
2038                         ignorelist_free (cfg_volume_perf->il_octets);
2039                         ignorelist_free (cfg_volume_perf->il_operations);
2040                         sfree (cfg_volume_perf);
2041                         return (ENOMEM);
2042                 }
2043
2044                 host->cfg_volume_perf = cfg_volume_perf;
2045         }
2046         cfg_volume_perf = host->cfg_volume_perf;
2047         
2048         for (i = 0; i < ci->children_num; ++i) {
2049                 oconfig_item_t *item = ci->children + i;
2050                 
2051                 /* if (!item || !item->key || !*item->key) continue; */
2052                 if (strcasecmp(item->key, "Interval") == 0)
2053                         cna_config_get_interval (item, &cfg_volume_perf->interval);
2054                 else if (!strcasecmp(item->key, "GetIO"))
2055                         cna_config_volume_perf_option (cfg_volume_perf, item);
2056                 else if (!strcasecmp(item->key, "GetOps"))
2057                         cna_config_volume_perf_option (cfg_volume_perf, item);
2058                 else if (!strcasecmp(item->key, "GetLatency"))
2059                         cna_config_volume_perf_option (cfg_volume_perf, item);
2060                 else if (!strcasecmp(item->key, "IgnoreSelectedIO"))
2061                         cna_config_volume_perf_default (cfg_volume_perf, item);
2062                 else if (!strcasecmp(item->key, "IgnoreSelectedOps"))
2063                         cna_config_volume_perf_default (cfg_volume_perf, item);
2064                 else if (!strcasecmp(item->key, "IgnoreSelectedLatency"))
2065                         cna_config_volume_perf_default (cfg_volume_perf, item);
2066                 else
2067                         WARNING ("netapp plugin: The option %s is not allowed within "
2068                                         "`VolumePerf' blocks.", item->key);
2069         }
2070
2071         return (0);
2072 } /* }}} int cna_config_volume_performance */
2073
2074 /* Handling of the "GetCapacity" and "GetSnapshot" options within a
2075  * <VolumeUsage /> block. */
2076 static void cna_config_volume_usage_option (cfg_volume_usage_t *cvu, /* {{{ */
2077                 const oconfig_item_t *ci)
2078 {
2079         char *name;
2080         ignorelist_t * il;
2081
2082         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2083         {
2084                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2085                                 ci->key);
2086                 return;
2087         }
2088
2089         name = ci->values[0].value.string;
2090
2091         if (strcasecmp ("GetCapacity", ci->key) == 0)
2092                 il = cvu->il_capacity;
2093         else if (strcasecmp ("GetSnapshot", ci->key) == 0)
2094                 il = cvu->il_snapshot;
2095         else
2096                 return;
2097
2098         ignorelist_add (il, name);
2099 } /* }}} void cna_config_volume_usage_option */
2100
2101 /* Handling of the "IgnoreSelectedCapacity" and "IgnoreSelectedSnapshot"
2102  * options within a <VolumeUsage /> block. */
2103 static void cna_config_volume_usage_default (cfg_volume_usage_t *cvu, /* {{{ */
2104                 const oconfig_item_t *ci)
2105 {
2106         ignorelist_t *il;
2107
2108         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
2109         {
2110                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
2111                                 ci->key);
2112                 return;
2113         }
2114
2115         if (strcasecmp ("IgnoreSelectedCapacity", ci->key) == 0)
2116                 il = cvu->il_capacity;
2117         else if (strcasecmp ("IgnoreSelectedSnapshot", ci->key) == 0)
2118                 il = cvu->il_snapshot;
2119         else
2120                 return;
2121
2122         if (ci->values[0].value.boolean)
2123                 ignorelist_set_invert (il, /* invert = */ 0);
2124         else
2125                 ignorelist_set_invert (il, /* invert = */ 1);
2126 } /* }}} void cna_config_volume_usage_default */
2127
2128 /* Corresponds to a <Disks /> block */
2129 static int cna_config_disk(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
2130         cfg_disk_t *cfg_disk;
2131         int i;
2132
2133         if ((host == NULL) || (ci == NULL))
2134                 return (EINVAL);
2135
2136         if (host->cfg_disk == NULL)
2137         {
2138                 cfg_disk = malloc (sizeof (*cfg_disk));
2139                 if (cfg_disk == NULL)
2140                         return (ENOMEM);
2141                 memset (cfg_disk, 0, sizeof (*cfg_disk));
2142
2143                 /* Set default flags */
2144                 cfg_disk->flags = CFG_DISK_ALL;
2145                 cfg_disk->query = NULL;
2146                 cfg_disk->disks = NULL;
2147
2148                 host->cfg_disk = cfg_disk;
2149         }
2150         cfg_disk = host->cfg_disk;
2151         
2152         for (i = 0; i < ci->children_num; ++i) {
2153                 oconfig_item_t *item = ci->children + i;
2154                 
2155                 /* if (!item || !item->key || !*item->key) continue; */
2156                 if (strcasecmp(item->key, "Interval") == 0)
2157                         cna_config_get_interval (item, &cfg_disk->interval);
2158                 else if (strcasecmp(item->key, "GetBusy") == 0)
2159                         cna_config_bool_to_flag (item, &cfg_disk->flags, CFG_DISK_BUSIEST);
2160         }
2161
2162         if ((cfg_disk->flags & CFG_DISK_ALL) == 0)
2163         {
2164                 NOTICE ("netapp plugin: All disk related values have been disabled. "
2165                                 "Collection of per-disk data will be disabled entirely.");
2166                 free_cfg_disk (host->cfg_disk);
2167                 host->cfg_disk = NULL;
2168         }
2169
2170         return (0);
2171 } /* }}} int cna_config_disk */
2172
2173 /* Corresponds to a <WAFL /> block */
2174 static int cna_config_wafl(host_config_t *host, oconfig_item_t *ci) /* {{{ */
2175 {
2176         cfg_wafl_t *cfg_wafl;
2177         int i;
2178
2179         if ((host == NULL) || (ci == NULL))
2180                 return (EINVAL);
2181
2182         if (host->cfg_wafl == NULL)
2183         {
2184                 cfg_wafl = malloc (sizeof (*cfg_wafl));
2185                 if (cfg_wafl == NULL)
2186                         return (ENOMEM);
2187                 memset (cfg_wafl, 0, sizeof (*cfg_wafl));
2188
2189                 /* Set default flags */
2190                 cfg_wafl->flags = CFG_WAFL_ALL;
2191
2192                 host->cfg_wafl = cfg_wafl;
2193         }
2194         cfg_wafl = host->cfg_wafl;
2195
2196         for (i = 0; i < ci->children_num; ++i) {
2197                 oconfig_item_t *item = ci->children + i;
2198                 
2199                 if (strcasecmp(item->key, "Interval") == 0)
2200                         cna_config_get_interval (item, &cfg_wafl->interval);
2201                 else if (!strcasecmp(item->key, "GetNameCache"))
2202                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_NAME_CACHE);
2203                 else if (!strcasecmp(item->key, "GetDirCache"))
2204                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_DIR_CACHE);
2205                 else if (!strcasecmp(item->key, "GetBufferCache"))
2206                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_BUF_CACHE);
2207                 else if (!strcasecmp(item->key, "GetInodeCache"))
2208                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_INODE_CACHE);
2209                 else
2210                         WARNING ("netapp plugin: The %s config option is not allowed within "
2211                                         "`WAFL' blocks.", item->key);
2212         }
2213
2214         if ((cfg_wafl->flags & CFG_WAFL_ALL) == 0)
2215         {
2216                 NOTICE ("netapp plugin: All WAFL related values have been disabled. "
2217                                 "Collection of WAFL data will be disabled entirely.");
2218                 free_cfg_wafl (host->cfg_wafl);
2219                 host->cfg_wafl = NULL;
2220         }
2221
2222         return (0);
2223 } /* }}} int cna_config_wafl */
2224
2225 /*
2226  * <VolumeUsage>
2227  *   GetCapacity "vol0"
2228  *   GetCapacity "vol1"
2229  *   GetCapacity "vol2"
2230  *   GetCapacity "vol3"
2231  *   GetCapacity "vol4"
2232  *   IgnoreSelectedCapacity false
2233  *
2234  *   GetSnapshot "vol0"
2235  *   GetSnapshot "vol3"
2236  *   GetSnapshot "vol4"
2237  *   GetSnapshot "vol7"
2238  *   IgnoreSelectedSnapshot false
2239  * </VolumeUsage>
2240  */
2241 /* Corresponds to a <VolumeUsage /> block */
2242 static int cna_config_volume_usage(host_config_t *host, /* {{{ */
2243                 const oconfig_item_t *ci)
2244 {
2245         cfg_volume_usage_t *cfg_volume_usage;
2246         int i;
2247
2248         if ((host == NULL) || (ci == NULL))
2249                 return (EINVAL);
2250
2251         if (host->cfg_volume_usage == NULL)
2252         {
2253                 cfg_volume_usage = malloc (sizeof (*cfg_volume_usage));
2254                 if (cfg_volume_usage == NULL)
2255                         return (ENOMEM);
2256                 memset (cfg_volume_usage, 0, sizeof (*cfg_volume_usage));
2257
2258                 /* Set default flags */
2259                 cfg_volume_usage->query = NULL;
2260                 cfg_volume_usage->volumes = NULL;
2261
2262                 cfg_volume_usage->il_capacity = ignorelist_create (/* invert = */ 1);
2263                 if (cfg_volume_usage->il_capacity == NULL)
2264                 {
2265                         sfree (cfg_volume_usage);
2266                         return (ENOMEM);
2267                 }
2268
2269                 cfg_volume_usage->il_snapshot = ignorelist_create (/* invert = */ 1);
2270                 if (cfg_volume_usage->il_snapshot == NULL)
2271                 {
2272                         ignorelist_free (cfg_volume_usage->il_capacity);
2273                         sfree (cfg_volume_usage);
2274                         return (ENOMEM);
2275                 }
2276
2277                 host->cfg_volume_usage = cfg_volume_usage;
2278         }
2279         cfg_volume_usage = host->cfg_volume_usage;
2280         
2281         for (i = 0; i < ci->children_num; ++i) {
2282                 oconfig_item_t *item = ci->children + i;
2283                 
2284                 /* if (!item || !item->key || !*item->key) continue; */
2285                 if (strcasecmp(item->key, "Interval") == 0)
2286                         cna_config_get_interval (item, &cfg_volume_usage->interval);
2287                 else if (!strcasecmp(item->key, "GetCapacity"))
2288                         cna_config_volume_usage_option (cfg_volume_usage, item);
2289                 else if (!strcasecmp(item->key, "GetSnapshot"))
2290                         cna_config_volume_usage_option (cfg_volume_usage, item);
2291                 else if (!strcasecmp(item->key, "IgnoreSelectedCapacity"))
2292                         cna_config_volume_usage_default (cfg_volume_usage, item);
2293                 else if (!strcasecmp(item->key, "IgnoreSelectedSnapshot"))
2294                         cna_config_volume_usage_default (cfg_volume_usage, item);
2295                 else
2296                         WARNING ("netapp plugin: The option %s is not allowed within "
2297                                         "`VolumeUsage' blocks.", item->key);
2298         }
2299
2300         return (0);
2301 } /* }}} int cna_config_volume_usage */
2302
2303 /* Corresponds to a <System /> block */
2304 static int cna_config_system (host_config_t *host, /* {{{ */
2305                 oconfig_item_t *ci)
2306 {
2307         cfg_system_t *cfg_system;
2308         int i;
2309         
2310         if ((host == NULL) || (ci == NULL))
2311                 return (EINVAL);
2312
2313         if (host->cfg_system == NULL)
2314         {
2315                 cfg_system = malloc (sizeof (*cfg_system));
2316                 if (cfg_system == NULL)
2317                         return (ENOMEM);
2318                 memset (cfg_system, 0, sizeof (*cfg_system));
2319
2320                 /* Set default flags */
2321                 cfg_system->flags = CFG_SYSTEM_ALL;
2322                 cfg_system->query = NULL;
2323
2324                 host->cfg_system = cfg_system;
2325         }
2326         cfg_system = host->cfg_system;
2327
2328         for (i = 0; i < ci->children_num; ++i) {
2329                 oconfig_item_t *item = ci->children + i;
2330
2331                 if (strcasecmp(item->key, "Interval") == 0) {
2332                         cna_config_get_interval (item, &cfg_system->interval);
2333                 } else if (!strcasecmp(item->key, "GetCPULoad")) {
2334                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_CPU);
2335                 } else if (!strcasecmp(item->key, "GetInterfaces")) {
2336                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_NET);
2337                 } else if (!strcasecmp(item->key, "GetDiskOps")) {
2338                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_OPS);
2339                 } else if (!strcasecmp(item->key, "GetDiskIO")) {
2340                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_DISK);
2341                 } else {
2342                         WARNING ("netapp plugin: The %s config option is not allowed within "
2343                                         "`System' blocks.", item->key);
2344                 }
2345         }
2346
2347         if ((cfg_system->flags & CFG_SYSTEM_ALL) == 0)
2348         {
2349                 NOTICE ("netapp plugin: All system related values have been disabled. "
2350                                 "Collection of system data will be disabled entirely.");
2351                 free_cfg_system (host->cfg_system);
2352                 host->cfg_system = NULL;
2353         }
2354
2355         return (0);
2356 } /* }}} int cna_config_system */
2357
2358 /* Corresponds to a <Host /> block. */
2359 static host_config_t *cna_config_host (const oconfig_item_t *ci, /* {{{ */
2360                 const host_config_t *default_host)
2361 {
2362         oconfig_item_t *item;
2363         host_config_t *host;
2364         int status;
2365         int i;
2366         
2367         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2368                 WARNING("netapp plugin: \"Host\" needs exactly one string argument. Ignoring host block.");
2369                 return 0;
2370         }
2371
2372         host = malloc(sizeof(*host));
2373         memcpy (host, default_host, sizeof (*host));
2374
2375         status = cf_util_get_string (ci, &host->name);
2376         if (status != 0)
2377         {
2378                 sfree (host);
2379                 return (NULL);
2380         }
2381
2382         for (i = 0; i < ci->children_num; ++i) {
2383                 item = ci->children + i;
2384
2385                 status = 0;
2386
2387                 if (!strcasecmp(item->key, "Address")) {
2388                         status = cf_util_get_string (item, &host->host);
2389                 } else if (!strcasecmp(item->key, "Port")) {
2390                         int tmp;
2391
2392                         tmp = cf_util_get_port_number (item);
2393                         if (tmp > 0)
2394                                 host->port = tmp;
2395                 } else if (!strcasecmp(item->key, "Protocol")) {
2396                         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"))) {
2397                                 WARNING("netapp plugin: \"Protocol\" needs to be either \"http\" or \"https\". Ignoring host block \"%s\".", ci->values[0].value.string);
2398                                 return 0;
2399                         }
2400                         if (!strcasecmp(item->values[0].value.string, "http")) host->protocol = NA_SERVER_TRANSPORT_HTTP;
2401                         else host->protocol = NA_SERVER_TRANSPORT_HTTPS;
2402                 } else if (!strcasecmp(item->key, "User")) {
2403                         status = cf_util_get_string (item, &host->username);
2404                 } else if (!strcasecmp(item->key, "Password")) {
2405                         status = cf_util_get_string (item, &host->password);
2406                 } else if (!strcasecmp(item->key, "Interval")) {
2407                         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) {
2408                                 WARNING("netapp plugin: \"Interval\" of host %s needs exactly one integer argument.", ci->values[0].value.string);
2409                                 continue;
2410                         }
2411                         host->interval = item->values[0].value.number;
2412                 } else if (!strcasecmp(item->key, "WAFL")) {
2413                         cna_config_wafl(host, item);
2414                 } else if (!strcasecmp(item->key, "Disks")) {
2415                         cna_config_disk(host, item);
2416                 } else if (!strcasecmp(item->key, "VolumePerf")) {
2417                         cna_config_volume_performance(host, item);
2418                 } else if (!strcasecmp(item->key, "VolumeUsage")) {
2419                         cna_config_volume_usage(host, item);
2420                 } else if (!strcasecmp(item->key, "System")) {
2421                         cna_config_system(host, item);
2422                 } else {
2423                         WARNING("netapp plugin: Ignoring unknown config option \"%s\" in host block \"%s\".",
2424                                         item->key, ci->values[0].value.string);
2425                 }
2426
2427                 if (status != 0)
2428                         break;
2429         }
2430
2431         if (host->host == NULL)
2432                 host->host = strdup (host->name);
2433
2434         if (host->host == NULL)
2435                 status = -1;
2436
2437         if (host->port <= 0)
2438                 host->port = (host->protocol == NA_SERVER_TRANSPORT_HTTP) ? 80 : 443;
2439
2440         if ((host->username == NULL) || (host->password == NULL)) {
2441                 WARNING("netapp plugin: Please supply login information for host \"%s\". "
2442                                 "Ignoring host block.", host->name);
2443                 status = -1;
2444         }
2445
2446         if (status != 0)
2447         {
2448                 free_host_config (host);
2449                 return (NULL);
2450         }
2451
2452         return host;
2453 } /* }}} host_config_t *cna_config_host */
2454
2455 /*
2456  * Callbacks registered with the daemon
2457  *
2458  * Pretty standard stuff here.
2459  */
2460 static int cna_init(void) { /* {{{ */
2461         char err[256];
2462         host_config_t *host;
2463         
2464         if (!global_host_config) {
2465                 WARNING("netapp plugin: Plugin loaded but no hosts defined.");
2466                 return 1;
2467         }
2468
2469         memset (err, 0, sizeof (err));
2470         if (!na_startup(err, sizeof(err))) {
2471                 err[sizeof (err) - 1] = 0;
2472                 ERROR("netapp plugin: Error initializing netapp API: %s", err);
2473                 return 1;
2474         }
2475
2476         for (host = global_host_config; host; host = host->next) {
2477                 /* Request version 1.1 of the ONTAP API */
2478                 host->srv = na_server_open(host->host,
2479                                 /* major version = */ 1, /* minor version = */ 1); 
2480                 if (host->srv == NULL) {
2481                         ERROR ("netapp plugin: na_server_open (%s) failed.", host->host);
2482                         continue;
2483                 }
2484
2485                 if (host->interval < interval_g)
2486                         host->interval = interval_g;
2487
2488                 na_server_set_transport_type(host->srv, host->protocol,
2489                                 /* transportarg = */ NULL);
2490                 na_server_set_port(host->srv, host->port);
2491                 na_server_style(host->srv, NA_STYLE_LOGIN_PASSWORD);
2492                 na_server_adminuser(host->srv, host->username, host->password);
2493                 na_server_set_timeout(host->srv, 5 /* seconds */);
2494         }
2495         return 0;
2496 } /* }}} int cna_init */
2497
2498 static int cna_config (oconfig_item_t *ci) { /* {{{ */
2499         int i;
2500         oconfig_item_t *item;
2501         host_config_t default_host = HOST_INIT;
2502         
2503         for (i = 0; i < ci->children_num; ++i) {
2504                 item = ci->children + i;
2505
2506                 if (!strcasecmp(item->key, "Host")) {
2507                         host_config_t *host;
2508                         host_config_t *tmp;
2509
2510                         host = cna_config_host(item, &default_host);
2511                         if (host == NULL)
2512                                 continue;
2513
2514                         for (tmp = global_host_config; tmp != NULL; tmp = tmp->next)
2515                         {
2516                                 if (strcasecmp (host->name, tmp->name) == 0)
2517                                         WARNING ("netapp plugin: Duplicate definition of host `%s'. "
2518                                                         "This is probably a bad idea.",
2519                                                         host->name);
2520
2521                                 if (tmp->next == NULL)
2522                                         break;
2523                         }
2524
2525                         host->next = NULL;
2526                         if (tmp == NULL)
2527                                 global_host_config = host;
2528                         else
2529                                 tmp->next = host;
2530                 } else {
2531                         WARNING("netapp plugin: Ignoring unknown config option \"%s\".", item->key);
2532                 }
2533         }
2534         return 0;
2535 } /* }}} int cna_config */
2536
2537 static int cna_read (void) { /* {{{ */
2538         host_config_t *host;
2539         
2540         for (host = global_host_config; host; host = host->next) {
2541                 cna_query_wafl (host);
2542                 cna_query_disk (host);
2543                 cna_query_volume_perf (host);
2544                 cna_query_volume_usage (host);
2545                 cna_query_system (host);
2546         }
2547         return 0;
2548 } /* }}} int cna_read */
2549
2550 static int cna_shutdown (void) /* {{{ */
2551 {
2552         free_host_config (global_host_config);
2553         global_host_config = NULL;
2554
2555         return (0);
2556 } /* }}} int cna_shutdown */
2557
2558 void module_register(void) {
2559         plugin_register_complex_config("netapp", cna_config);
2560         plugin_register_init("netapp", cna_init);
2561         plugin_register_read("netapp", cna_read);
2562         plugin_register_shutdown("netapp", cna_shutdown);
2563 }
2564
2565 /* vim: set sw=2 ts=2 noet fdm=marker : */