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