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