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