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