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