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