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