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