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