netapp plugin: Document the graceful return if statistics are not wanted.
[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 WAFL was not configured, return without doing anything. */
810         if (host->cfg_wafl == NULL)
811                 return (0);
812
813         now = time (NULL);
814         if ((host->cfg_wafl->interval.interval + host->cfg_wafl->interval.last_read) > now)
815                 return (0);
816
817         status = cna_setup_wafl (host->cfg_wafl);
818         if (status != 0)
819                 return (status);
820         assert (host->cfg_wafl->query != NULL);
821
822         data = na_server_invoke_elem(host->srv, host->cfg_wafl->query);
823         if (na_results_status (data) != NA_OK)
824         {
825                 ERROR ("netapp plugin: cna_query_wafl: na_server_invoke_elem failed: %s",
826                                 na_results_reason (data));
827                 na_elem_free (data);
828                 return (-1);
829         }
830
831         status = cna_handle_wafl_data (host->name, host->cfg_wafl, data);
832
833         if (status == 0)
834                 host->cfg_wafl->interval.last_read = now;
835
836         na_elem_free (data);
837         return (status);
838 } /* }}} int cna_query_wafl */
839
840 /* Data corresponding to <Disks /> */
841 static int cna_handle_disk_data (const char *hostname, /* {{{ */
842                 cfg_disk_t *cfg_disk, na_elem_t *data)
843 {
844         time_t timestamp;
845         na_elem_t *instances;
846         na_elem_t *instance;
847         na_elem_iter_t instance_iter;
848         disk_t *worst_disk = NULL;
849
850         if ((cfg_disk == NULL) || (data == NULL))
851                 return (EINVAL);
852         
853         timestamp = (time_t) na_child_get_uint64(data, "timestamp", 0);
854
855         instances = na_elem_child (data, "instances");
856         if (instances == NULL)
857         {
858                 ERROR ("netapp plugin: cna_handle_disk_data: "
859                                 "na_elem_child (\"instances\") failed.");
860                 return (-1);
861         }
862
863         /* Iterate over all children */
864         instance_iter = na_child_iterator (instances);
865         for (instance = na_iterator_next (&instance_iter);
866                         instance != NULL;
867                         instance = na_iterator_next(&instance_iter))
868         {
869                 disk_t *old_data;
870                 disk_t  new_data;
871
872                 na_elem_iter_t counter_iterator;
873                 na_elem_t *counter;
874
875                 memset (&new_data, 0, sizeof (new_data));
876                 new_data.timestamp = timestamp;
877                 new_data.disk_busy_percent = NAN;
878
879                 old_data = get_disk(cfg_disk, na_child_get_string (instance, "name"));
880                 if (old_data == NULL)
881                         continue;
882
883                 /* Look for the "disk_busy" and "base_for_disk_busy" counters */
884                 counter_iterator = na_child_iterator(na_elem_child(instance, "counters"));
885                 for (counter = na_iterator_next(&counter_iterator);
886                                 counter != NULL;
887                                 counter = na_iterator_next(&counter_iterator))
888                 {
889                         const char *name;
890                         uint64_t value;
891
892                         name = na_child_get_string(counter, "name");
893                         if (name == NULL)
894                                 continue;
895
896                         value = na_child_get_uint64(counter, "value", UINT64_MAX);
897                         if (value == UINT64_MAX)
898                                 continue;
899
900                         if (strcmp(name, "disk_busy") == 0)
901                         {
902                                 new_data.disk_busy = value;
903                                 new_data.flags |= HAVE_DISK_BUSY;
904                         }
905                         else if (strcmp(name, "base_for_disk_busy") == 0)
906                         {
907                                 new_data.base_for_disk_busy = value;
908                                 new_data.flags |= HAVE_DISK_BASE;
909                         }
910                         else
911                         {
912                                 DEBUG ("netapp plugin: cna_handle_disk_data: "
913                                                 "Counter not handled: %s = %"PRIu64,
914                                                 name, value);
915                         }
916                 }
917
918                 /* If all required counters are available and did not just wrap around,
919                  * calculate the busy percentage. Otherwise, the value is initialized to
920                  * NAN at the top of the for-loop. */
921                 if (HAS_ALL_FLAGS (old_data->flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
922                                 && HAS_ALL_FLAGS (new_data.flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
923                                 && (new_data.disk_busy >= old_data->disk_busy)
924                                 && (new_data.base_for_disk_busy > old_data->base_for_disk_busy))
925                 {
926                         uint64_t busy_diff;
927                         uint64_t base_diff;
928
929                         busy_diff = new_data.disk_busy - old_data->disk_busy;
930                         base_diff = new_data.base_for_disk_busy - old_data->base_for_disk_busy;
931
932                         new_data.disk_busy_percent = 100.0
933                                 * ((gauge_t) busy_diff) / ((gauge_t) base_diff);
934                 }
935
936                 /* Clear HAVE_* flags */
937                 old_data->flags &= ~HAVE_DISK_ALL;
938
939                 /* Copy data */
940                 old_data->timestamp = new_data.timestamp;
941                 old_data->disk_busy = new_data.disk_busy;
942                 old_data->base_for_disk_busy = new_data.base_for_disk_busy;
943                 old_data->disk_busy_percent = new_data.disk_busy_percent;
944
945                 /* Copy flags */
946                 old_data->flags |= (new_data.flags & HAVE_DISK_ALL);
947
948                 if ((worst_disk == NULL)
949                                 || (worst_disk->disk_busy_percent < old_data->disk_busy_percent))
950                         worst_disk = old_data;
951         } /* for (all disks) */
952
953         if ((cfg_disk->flags & CFG_DISK_BUSIEST) && (worst_disk != NULL))
954                 submit_double (hostname, "system", "percent", "disk_busy",
955                                 worst_disk->disk_busy_percent, timestamp);
956
957         return (0);
958 } /* }}} int cna_handle_disk_data */
959
960 static int cna_setup_disk (cfg_disk_t *cd) /* {{{ */
961 {
962         na_elem_t *e;
963
964         if (cd == NULL)
965                 return (EINVAL);
966
967         if (cd->query != NULL)
968                 return (0);
969
970         cd->query = na_elem_new ("perf-object-get-instances");
971         if (cd->query == NULL)
972         {
973                 ERROR ("netapp plugin: na_elem_new failed.");
974                 return (-1);
975         }
976         na_child_add_string (cd->query, "objectname", "disk");
977
978         e = na_elem_new("counters");
979         if (e == NULL)
980         {
981                 na_elem_free (cd->query);
982                 cd->query = NULL;
983                 ERROR ("netapp plugin: na_elem_new failed.");
984                 return (-1);
985         }
986         na_child_add_string(e, "foo", "disk_busy");
987         na_child_add_string(e, "foo", "base_for_disk_busy");
988         na_child_add(cd->query, e);
989
990         return (0);
991 } /* }}} int cna_setup_disk */
992
993 static int cna_query_disk (host_config_t *host) /* {{{ */
994 {
995         na_elem_t *data;
996         int status;
997         time_t now;
998
999         if (host == NULL)
1000                 return (EINVAL);
1001
1002         /* If the user did not configure disk statistics, return without doing
1003          * anything. */
1004         if (host->cfg_disk == NULL)
1005                 return (0);
1006
1007         now = time (NULL);
1008         if ((host->cfg_disk->interval.interval + host->cfg_disk->interval.last_read) > now)
1009                 return (0);
1010
1011         status = cna_setup_disk (host->cfg_disk);
1012         if (status != 0)
1013                 return (status);
1014         assert (host->cfg_disk->query != NULL);
1015
1016         data = na_server_invoke_elem(host->srv, host->cfg_disk->query);
1017         if (na_results_status (data) != NA_OK)
1018         {
1019                 ERROR ("netapp plugin: cna_query_disk: na_server_invoke_elem failed: %s",
1020                                 na_results_reason (data));
1021                 na_elem_free (data);
1022                 return (-1);
1023         }
1024
1025         status = cna_handle_disk_data (host->name, host->cfg_disk, data);
1026
1027         if (status == 0)
1028                 host->cfg_disk->interval.last_read = now;
1029
1030         na_elem_free (data);
1031         return (status);
1032 } /* }}} int cna_query_disk */
1033
1034 /* Data corresponding to <GetVolumeData /> */
1035 static void collect_volume_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
1036         na_elem_t *inst;
1037         volume_t *volume;
1038         cfg_volume_usage_t *cfg_volume_data = data;
1039
1040         out = na_elem_child(out, "volumes");
1041         na_elem_iter_t inst_iter = na_child_iterator(out);
1042         for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
1043                 uint64_t size_free = 0, size_used = 0, snap_reserved = 0;
1044
1045                 na_elem_t *sis;
1046                 const char *sis_state;
1047                 uint64_t sis_saved_reported;
1048                 uint64_t sis_saved;
1049
1050                 volume = get_volume(host, na_child_get_string(inst, "name"),
1051                                 cfg_volume_data->flags, /* perf_flags = */ 0);
1052                 if (volume == NULL)
1053                         continue;
1054
1055                 if (!(volume->cfg_volume_usage.flags & CFG_VOLUME_USAGE_DF))
1056                         continue;
1057
1058                 /* 2^4 exa-bytes? This will take a while ;) */
1059                 size_free = na_child_get_uint64(inst, "size-available", UINT64_MAX);
1060                 if (size_free != UINT64_MAX)
1061                         submit_double (host->name, volume->name, "df_complex", "free",
1062                                         (double) size_free, /* time = */ 0);
1063
1064                 size_used = na_child_get_uint64(inst, "size-used", UINT64_MAX);
1065                 if (size_used != UINT64_MAX) {
1066                         if ((volume->cfg_volume_usage.flags & HAVE_VOLUME_USAGE_SNAP)
1067                                         && (size_used >= volume->cfg_volume_usage.snap_used))
1068                                 size_used -= volume->cfg_volume_usage.snap_used;
1069                         submit_double (host->name, volume->name, "df_complex", "used",
1070                                         (double) size_used, /* time = */ 0);
1071                 }
1072
1073                 snap_reserved = na_child_get_uint64(inst, "snapshot-blocks-reserved", UINT64_MAX);
1074                 if (!(volume->cfg_volume_usage.flags & HAVE_VOLUME_USAGE_SNAP) && (snap_reserved != UINT64_MAX))
1075                         /* If we have snap usage data this value has already been submitted. */
1076                         /* 1 block == 1024 bytes  as per API docs */
1077                         submit_double (host->name, volume->name, "df_complex", "snap_reserved",
1078                                         (double) (1024 * snap_reserved), /* time = */ 0);
1079
1080                 sis = na_elem_child(inst, "sis");
1081                 if (sis == NULL)
1082                         continue;
1083
1084                 sis_state = na_child_get_string(sis, "state");
1085                 if ((sis_state == NULL)
1086                                 || (strcmp ("enabled", sis_state) != 0))
1087                         continue;
1088
1089                 sis_saved_reported = na_child_get_uint64(sis, "size-saved", UINT64_MAX);
1090                 if (sis_saved_reported == UINT64_MAX)
1091                         continue;
1092
1093                 /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
1094                 if ((sis_saved_reported >> 32) != 0) {
1095                         /* In case they ever fix this bug. */
1096                         sis_saved = sis_saved_reported;
1097                 } else {
1098                         uint64_t sis_saved_percent;
1099                         uint64_t sis_saved_guess;
1100                         uint64_t overflow_guess;
1101                         uint64_t guess1, guess2, guess3;
1102
1103                         sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
1104                         if (sis_saved_percent > 100)
1105                                 continue;
1106
1107                         /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
1108                          * will hopefully be fixed in later versions. To work around the bug, try
1109                          * to figure out how often the 32bit integer wrapped around by using the
1110                          * "percentage-saved" value. Because the percentage is in the range
1111                          * [0-100], this should work as long as the saved space does not exceed
1112                          * 400 GBytes. */
1113                         /* percentage-saved = size-saved / (size-saved + size-used) */
1114                         if (sis_saved_percent < 100)
1115                                 sis_saved_guess = size_used * sis_saved_percent / (100 - sis_saved_percent);
1116                         else
1117                                 sis_saved_guess = size_used;
1118
1119                         overflow_guess = sis_saved_guess >> 32;
1120                         guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
1121                         guess2 = (overflow_guess << 32) + sis_saved_reported;
1122                         guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
1123
1124                         if (sis_saved_guess < guess2) {
1125                                 if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
1126                                         sis_saved = guess1;
1127                                 else
1128                                         sis_saved = guess2;
1129                         } else {
1130                                 if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
1131                                         sis_saved = guess2;
1132                                 else
1133                                         sis_saved = guess3;
1134                         }
1135                 } /* end of 32-bit workaround */
1136
1137                 submit_double (host->name, volume->name, "df_complex", "sis_saved",
1138                                 (double) sis_saved, /* time = */ 0);
1139         }
1140 } /* }}} void collect_volume_data */
1141
1142 /* Data corresponding to <GetVolumePerfData /> */
1143 static void query_volume_perf_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
1144         cfg_volume_perf_t *cfg_volume_perf = data;
1145         time_t timestamp;
1146         na_elem_t *counter, *inst;
1147         
1148         timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
1149
1150         out = na_elem_child(out, "instances");
1151         na_elem_iter_t inst_iter = na_child_iterator(out);
1152         for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
1153                 data_volume_perf_t perf_data;
1154                 volume_t *volume;
1155
1156                 memset (&perf_data, 0, sizeof (perf_data));
1157                 perf_data.timestamp = timestamp;
1158
1159                 volume = get_volume(host, na_child_get_string(inst, "name"),
1160                                 /* data_flags = */ 0, cfg_volume_perf->flags);
1161                 if (volume == NULL)
1162                         continue;
1163
1164                 na_elem_iter_t count_iter = na_child_iterator(na_elem_child(inst, "counters"));
1165                 for (counter = na_iterator_next(&count_iter); counter; counter = na_iterator_next(&count_iter)) {
1166                         const char *name;
1167                         uint64_t value;
1168
1169                         name = na_child_get_string(counter, "name");
1170                         if (name == NULL)
1171                                 continue;
1172
1173                         value = na_child_get_uint64(counter, "value", UINT64_MAX);
1174                         if (value == UINT64_MAX)
1175                                 continue;
1176
1177                         if (!strcmp(name, "read_data")) {
1178                                 perf_data.read_bytes = value;
1179                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_READ;
1180                         } else if (!strcmp(name, "write_data")) {
1181                                 perf_data.write_bytes = value;
1182                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_WRITE;
1183                         } else if (!strcmp(name, "read_ops")) {
1184                                 perf_data.read_ops = value;
1185                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_READ;
1186                         } else if (!strcmp(name, "write_ops")) {
1187                                 perf_data.write_ops = value;
1188                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_WRITE;
1189                         } else if (!strcmp(name, "read_latency")) {
1190                                 perf_data.read_latency = value;
1191                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_READ;
1192                         } else if (!strcmp(name, "write_latency")) {
1193                                 perf_data.write_latency = value;
1194                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_WRITE;
1195                         }
1196                 }
1197
1198                 submit_volume_perf_data (host, volume, &perf_data);
1199         } /* for (volume) */
1200 } /* }}} void query_volume_perf_data */
1201
1202 /* Data corresponding to <System /> */
1203 static int cna_handle_system_data (const char *hostname, /* {{{ */
1204                 cfg_system_t *cfg_system, na_elem_t *data)
1205 {
1206         na_elem_t *instances;
1207         na_elem_t *counter;
1208         na_elem_iter_t counter_iter;
1209
1210         counter_t disk_read = 0, disk_written = 0;
1211         counter_t net_recv = 0, net_sent = 0;
1212         counter_t cpu_busy = 0, cpu_total = 0;
1213         uint32_t counter_flags = 0;
1214
1215         const char *instance;
1216         time_t timestamp;
1217         
1218         timestamp = (time_t) na_child_get_uint64 (data, "timestamp", 0);
1219
1220         instances = na_elem_child(na_elem_child (data, "instances"), "instance-data");
1221         if (instances == NULL)
1222         {
1223                 ERROR ("netapp plugin: cna_handle_system_data: "
1224                                 "na_elem_child (\"instances\") failed.");
1225                 return (-1);
1226         }
1227
1228         instance = na_child_get_string (instances, "name");
1229         if (instance == NULL)
1230         {
1231                 ERROR ("netapp plugin: cna_handle_system_data: "
1232                                 "na_child_get_string (\"name\") failed.");
1233                 return (-1);
1234         }
1235
1236         counter_iter = na_child_iterator (na_elem_child (instances, "counters"));
1237         for (counter = na_iterator_next (&counter_iter);
1238                         counter != NULL;
1239                         counter = na_iterator_next (&counter_iter))
1240         {
1241                 const char *name;
1242                 uint64_t value;
1243
1244                 name = na_child_get_string(counter, "name");
1245                 if (name == NULL)
1246                         continue;
1247
1248                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
1249                 if (value == UINT64_MAX)
1250                         continue;
1251
1252                 if (!strcmp(name, "disk_data_read")) {
1253                         disk_read = (counter_t) (value * 1024);
1254                         counter_flags |= 0x01;
1255                 } else if (!strcmp(name, "disk_data_written")) {
1256                         disk_written = (counter_t) (value * 1024);
1257                         counter_flags |= 0x02;
1258                 } else if (!strcmp(name, "net_data_recv")) {
1259                         net_recv = (counter_t) (value * 1024);
1260                         counter_flags |= 0x04;
1261                 } else if (!strcmp(name, "net_data_sent")) {
1262                         net_sent = (counter_t) (value * 1024);
1263                         counter_flags |= 0x08;
1264                 } else if (!strcmp(name, "cpu_busy")) {
1265                         cpu_busy = (counter_t) value;
1266                         counter_flags |= 0x10;
1267                 } else if (!strcmp(name, "cpu_elapsed_time")) {
1268                         cpu_total = (counter_t) value;
1269                         counter_flags |= 0x20;
1270                 } else if ((cfg_system->flags & CFG_SYSTEM_OPS)
1271                                 && (value > 0) && (strlen(name) > 4)
1272                                 && (!strcmp(name + strlen(name) - 4, "_ops"))) {
1273                         submit_counter (hostname, instance, "disk_ops_complex", name,
1274                                         (counter_t) value, timestamp);
1275                 }
1276         } /* for (counter) */
1277
1278         if ((cfg_system->flags & CFG_SYSTEM_DISK)
1279                         && (HAS_ALL_FLAGS (counter_flags, 0x01 | 0x02)))
1280                 submit_two_counters (hostname, instance, "disk_octets", NULL,
1281                                 disk_read, disk_written, timestamp);
1282                                 
1283         if ((cfg_system->flags & CFG_SYSTEM_NET)
1284                         && (HAS_ALL_FLAGS (counter_flags, 0x04 | 0x08)))
1285                 submit_two_counters (hostname, instance, "if_octets", NULL,
1286                                 net_recv, net_sent, timestamp);
1287
1288         if ((cfg_system->flags & CFG_SYSTEM_CPU)
1289                         && (HAS_ALL_FLAGS (counter_flags, 0x10 | 0x20)))
1290         {
1291                 submit_counter (hostname, instance, "cpu", "system",
1292                                 cpu_busy, timestamp);
1293                 submit_counter (hostname, instance, "cpu", "idle",
1294                                 cpu_total - cpu_busy, timestamp);
1295         }
1296
1297         return (0);
1298 } /* }}} int cna_handle_system_data */
1299
1300 static int cna_setup_system (cfg_system_t *cs) /* {{{ */
1301 {
1302         if (cs == NULL)
1303                 return (EINVAL);
1304
1305         if (cs->query != NULL)
1306                 return (0);
1307
1308         cs->query = na_elem_new ("perf-object-get-instances");
1309         if (cs->query == NULL)
1310         {
1311                 ERROR ("netapp plugin: na_elem_new failed.");
1312                 return (-1);
1313         }
1314         na_child_add_string (cs->query, "objectname", "system");
1315
1316         return (0);
1317 } /* }}} int cna_setup_system */
1318
1319 static int cna_query_system (host_config_t *host) /* {{{ */
1320 {
1321         na_elem_t *data;
1322         int status;
1323         time_t now;
1324
1325         if (host == NULL)
1326                 return (EINVAL);
1327
1328         /* If system statistics were not configured, return without doing anything. */
1329         if (host->cfg_system == NULL)
1330                 return (0);
1331
1332         now = time (NULL);
1333         if ((host->cfg_system->interval.interval + host->cfg_system->interval.last_read) > now)
1334                 return (0);
1335
1336         status = cna_setup_system (host->cfg_system);
1337         if (status != 0)
1338                 return (status);
1339         assert (host->cfg_system->query != NULL);
1340
1341         data = na_server_invoke_elem(host->srv, host->cfg_system->query);
1342         if (na_results_status (data) != NA_OK)
1343         {
1344                 ERROR ("netapp plugin: cna_query_system: na_server_invoke_elem failed: %s",
1345                                 na_results_reason (data));
1346                 na_elem_free (data);
1347                 return (-1);
1348         }
1349
1350         status = cna_handle_system_data (host->name, host->cfg_system, data);
1351
1352         if (status == 0)
1353                 host->cfg_system->interval.last_read = now;
1354
1355         na_elem_free (data);
1356         return (status);
1357 } /* }}} int cna_query_system */
1358
1359 /*
1360  * Configuration handling
1361  */
1362 /* Sets a given flag if the boolean argument is true and unsets the flag if it
1363  * is false. On error, the flag-field is not changed. */
1364 static int cna_config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */
1365                 uint32_t *flags, uint32_t flag)
1366 {
1367         if ((ci == NULL) || (flags == NULL))
1368                 return (EINVAL);
1369
1370         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1371         {
1372                 WARNING ("netapp plugin: The %s option needs exactly one boolean argument.",
1373                                 ci->key);
1374                 return (-1);
1375         }
1376
1377         if (ci->values[0].value.boolean)
1378                 *flags |= flag;
1379         else
1380                 *flags &= ~flag;
1381
1382         return (0);
1383 } /* }}} int cna_config_bool_to_flag */
1384
1385 /* Handling of the "Multiplier" option which is allowed in every block. */
1386 static int cna_config_get_multiplier (const oconfig_item_t *ci, /* {{{ */
1387                 cfg_service_t *service)
1388 {
1389         int tmp;
1390
1391         if ((ci == NULL) || (service == NULL))
1392                 return (EINVAL);
1393
1394         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1395         {
1396                 WARNING ("netapp plugin: The `Multiplier' option needs exactly one numeric argument.");
1397                 return (-1);
1398         }
1399
1400         tmp = (int) (ci->values[0].value.number + .5);
1401         if (tmp < 1)
1402         {
1403                 WARNING ("netapp plugin: The `Multiplier' option needs a positive integer argument.");
1404                 return (-1);
1405         }
1406
1407         service->multiplier = tmp;
1408         service->skip_countdown = tmp;
1409
1410         return (0);
1411 } /* }}} int cna_config_get_multiplier */
1412
1413 /* Handling of the "Interval" option which is allowed in every block. */
1414 static int cna_config_get_interval (const oconfig_item_t *ci, /* {{{ */
1415                 cna_interval_t *out_interval)
1416 {
1417         time_t tmp;
1418
1419         if ((ci == NULL) || (out_interval == NULL))
1420                 return (EINVAL);
1421
1422         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1423         {
1424                 WARNING ("netapp plugin: The `Multiplier' option needs exactly one numeric argument.");
1425                 return (-1);
1426         }
1427
1428         tmp = (time_t) (ci->values[0].value.number + .5);
1429         if (tmp < 1)
1430         {
1431                 WARNING ("netapp plugin: The `Multiplier' option needs a positive integer argument.");
1432                 return (-1);
1433         }
1434
1435         out_interval->interval = tmp;
1436         out_interval->last_read = 0;
1437
1438         return (0);
1439 } /* }}} int cna_config_get_interval */
1440
1441 /* Handling of the "GetIO", "GetOps" and "GetLatency" options within a
1442  * <GetVolumePerfData /> block. */
1443 static void cna_config_volume_performance_option (host_config_t *host, /* {{{ */
1444                 cfg_volume_perf_t *perf_volume, const oconfig_item_t *item,
1445                 uint32_t flag)
1446 {
1447         int i;
1448         
1449         for (i = 0; i < item->values_num; ++i) {
1450                 const char *name;
1451                 volume_t *v;
1452                 _Bool set = true;
1453
1454                 if (item->values[i].type != OCONFIG_TYPE_STRING) {
1455                         WARNING("netapp plugin: Ignoring non-string argument in "
1456                                         "\"GetVolumePerfData\" block for host %s", host->name);
1457                         continue;
1458                 }
1459
1460                 name = item->values[i].value.string;
1461                 if (name[0] == '+') {
1462                         set = true;
1463                         ++name;
1464                 } else if (name[0] == '-') {
1465                         set = false;
1466                         ++name;
1467                 }
1468
1469                 if (!name[0]) {
1470                         if (set)
1471                                 perf_volume->flags |= flag;
1472                         else /* if (!set) */
1473                                 perf_volume->flags &= ~flag;
1474
1475                         host_set_all_perf_data_flags(host, flag, set);
1476                         continue;
1477                 }
1478
1479                 v = get_volume (host, name, /* data_flags = */ 0, perf_volume->flags);
1480                 if (v == NULL)
1481                         continue;
1482
1483                 if (set)
1484                         v->perf_data.flags |= flag;
1485                 else /* if (!set) */
1486                         v->perf_data.flags &= ~flag;
1487         } /* for (i = 0 .. item->values_num) */
1488 } /* }}} void cna_config_volume_performance_option */
1489
1490 /* Corresponds to a <GetVolumePerfData /> block */
1491 static void cna_config_volume_performance(host_config_t *host, const oconfig_item_t *ci) { /* {{{ */
1492         int i, had_io = 0, had_ops = 0, had_latency = 0;
1493         cfg_service_t *service;
1494         cfg_volume_perf_t *perf_volume;
1495         
1496         service = malloc(sizeof(*service));
1497         service->query = 0;
1498         service->handler = query_volume_perf_data;
1499         perf_volume = service->data = malloc(sizeof(*perf_volume));
1500         perf_volume->flags = CFG_VOLUME_PERF_INIT;
1501         service->next = host->services;
1502         host->services = service;
1503         for (i = 0; i < ci->children_num; ++i) {
1504                 oconfig_item_t *item = ci->children + i;
1505                 
1506                 /* if (!item || !item->key || !*item->key) continue; */
1507                 if (!strcasecmp(item->key, "Multiplier")) {
1508                         cna_config_get_multiplier (item, service);
1509                 } else if (!strcasecmp(item->key, "GetIO")) {
1510                         had_io = 1;
1511                         cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_IO);
1512                 } else if (!strcasecmp(item->key, "GetOps")) {
1513                         had_ops = 1;
1514                         cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_OPS);
1515                 } else if (!strcasecmp(item->key, "GetLatency")) {
1516                         had_latency = 1;
1517                         cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_LATENCY);
1518                 }
1519         }
1520         if (!had_io) {
1521                 perf_volume->flags |= CFG_VOLUME_PERF_IO;
1522                 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_IO, /* set = */ true);
1523         }
1524         if (!had_ops) {
1525                 perf_volume->flags |= CFG_VOLUME_PERF_OPS;
1526                 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_OPS, /* set = */ true);
1527         }
1528         if (!had_latency) {
1529                 perf_volume->flags |= CFG_VOLUME_PERF_LATENCY;
1530                 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_LATENCY, /* set = */ true);
1531         }
1532 } /* }}} void cna_config_volume_performance */
1533
1534 /* Handling of the "GetDiskUtil" option within a <GetVolumeData /> block. */
1535 static void cna_config_volume_usage_option (host_config_t *host, /* {{{ */
1536                 cfg_volume_usage_t *cfg_volume_data, const oconfig_item_t *item, uint32_t flag)
1537 {
1538         int i;
1539         
1540         for (i = 0; i < item->values_num; ++i) {
1541                 const char *name;
1542                 volume_t *v;
1543                 _Bool set = true;
1544
1545                 if (item->values[i].type != OCONFIG_TYPE_STRING) {
1546                         WARNING("netapp plugin: Ignoring non-string argument in \"GetVolData\""
1547                                         "block for host %s", host->name);
1548                         continue;
1549                 }
1550
1551                 name = item->values[i].value.string;
1552                 if (name[0] == '+') {
1553                         set = true;
1554                         ++name;
1555                 } else if (name[0] == '-') {
1556                         set = false;
1557                         ++name;
1558                 }
1559
1560                 if (!name[0]) {
1561                         if (set)
1562                                 cfg_volume_data->flags |= flag;
1563                         else /* if (!set) */
1564                                 cfg_volume_data->flags &= ~flag;
1565
1566                         host_set_all_cfg_volume_usage_flags(host, flag, set);
1567                         continue;
1568                 }
1569
1570                 v = get_volume(host, name, cfg_volume_data->flags, /* perf_flags = */ 0);
1571                 if (v == NULL)
1572                         continue;
1573
1574                 if (!v->cfg_volume_usage.flags)
1575                         v->cfg_volume_usage.flags = cfg_volume_data->flags;
1576
1577                 if (set)
1578                         v->cfg_volume_usage.flags |= flag;
1579                 else /* if (!set) */
1580                         v->cfg_volume_usage.flags &= ~flag;
1581         }
1582 } /* }}} void cna_config_volume_usage_option */
1583
1584 /* Corresponds to a <GetVolumeData /> block */
1585 static void cna_config_volume_usage(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
1586         int i, had_df = 0;
1587         cfg_service_t *service;
1588         cfg_volume_usage_t *cfg_volume_data;
1589         
1590         service = malloc(sizeof(*service));
1591         service->query = 0;
1592         service->handler = collect_volume_data;
1593         cfg_volume_data = service->data = malloc(sizeof(*cfg_volume_data));
1594         cfg_volume_data->flags = CFG_VOLUME_USAGE_INIT;
1595         service->next = host->services;
1596         host->services = service;
1597         for (i = 0; i < ci->children_num; ++i) {
1598                 oconfig_item_t *item = ci->children + i;
1599                 
1600                 /* if (!item || !item->key || !*item->key) continue; */
1601                 if (!strcasecmp(item->key, "Multiplier")) {
1602                         cna_config_get_multiplier (item, service);
1603                 } else if (!strcasecmp(item->key, "GetDiskUtil")) {
1604                         had_df = 1;
1605                         cna_config_volume_usage_option(host, cfg_volume_data, item, CFG_VOLUME_USAGE_DF);
1606                 } else if (!strcasecmp(item->key, "GetSnapUtil")) {
1607                         had_df = 1;
1608                         cna_config_volume_usage_option(host, cfg_volume_data, item, CFG_VOLUME_USAGE_SNAP);
1609                 }
1610         }
1611         if (!had_df) {
1612                 cfg_volume_data->flags |= CFG_VOLUME_USAGE_DF;
1613                 host_set_all_cfg_volume_usage_flags(host, CFG_VOLUME_USAGE_DF, /* set = */ true);
1614         }
1615         if (cfg_volume_data->flags & CFG_VOLUME_USAGE_SNAP) {
1616                 WARNING("netapp plugin: The \"GetSnapUtil\" option does not support the \"+\" wildcard.");
1617         }
1618 } /* }}} void cna_config_volume_usage */
1619
1620 /* Corresponds to a <Disks /> block */
1621 static int cna_config_disk(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
1622         cfg_disk_t *cfg_disk;
1623         int i;
1624
1625         if ((host == NULL) || (ci == NULL))
1626                 return (EINVAL);
1627
1628         if (host->cfg_disk == NULL)
1629         {
1630                 cfg_disk = malloc (sizeof (*cfg_disk));
1631                 if (cfg_disk == NULL)
1632                         return (ENOMEM);
1633                 memset (cfg_disk, 0, sizeof (*cfg_disk));
1634
1635                 /* Set default flags */
1636                 cfg_disk->flags = CFG_DISK_ALL;
1637                 cfg_disk->query = NULL;
1638                 cfg_disk->disks = NULL;
1639
1640                 host->cfg_disk = cfg_disk;
1641         }
1642         cfg_disk = host->cfg_disk;
1643         
1644         for (i = 0; i < ci->children_num; ++i) {
1645                 oconfig_item_t *item = ci->children + i;
1646                 
1647                 /* if (!item || !item->key || !*item->key) continue; */
1648                 if (strcasecmp(item->key, "Interval") == 0)
1649                         cna_config_get_interval (item, &cfg_disk->interval);
1650                 else if (strcasecmp(item->key, "GetBusy") == 0)
1651                         cna_config_bool_to_flag (item, &cfg_disk->flags, CFG_DISK_BUSIEST);
1652         }
1653
1654         return (0);
1655 } /* }}} int cna_config_disk */
1656
1657 /* Corresponds to a <WAFL /> block */
1658 static int cna_config_wafl(host_config_t *host, oconfig_item_t *ci) /* {{{ */
1659 {
1660         cfg_wafl_t *cfg_wafl;
1661         int i;
1662
1663         if ((host == NULL) || (ci == NULL))
1664                 return (EINVAL);
1665
1666         if (host->cfg_wafl == NULL)
1667         {
1668                 cfg_wafl = malloc (sizeof (*cfg_wafl));
1669                 if (cfg_wafl == NULL)
1670                         return (ENOMEM);
1671                 memset (cfg_wafl, 0, sizeof (*cfg_wafl));
1672
1673                 /* Set default flags */
1674                 cfg_wafl->flags = CFG_WAFL_ALL;
1675
1676                 host->cfg_wafl = cfg_wafl;
1677         }
1678         cfg_wafl = host->cfg_wafl;
1679
1680         for (i = 0; i < ci->children_num; ++i) {
1681                 oconfig_item_t *item = ci->children + i;
1682                 
1683                 if (strcasecmp(item->key, "Interval") == 0)
1684                         cna_config_get_interval (item, &cfg_wafl->interval);
1685                 else if (!strcasecmp(item->key, "GetNameCache"))
1686                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_NAME_CACHE);
1687                 else if (!strcasecmp(item->key, "GetDirCache"))
1688                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_DIR_CACHE);
1689                 else if (!strcasecmp(item->key, "GetBufferCache"))
1690                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_BUF_CACHE);
1691                 else if (!strcasecmp(item->key, "GetInodeCache"))
1692                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_INODE_CACHE);
1693                 else
1694                         WARNING ("netapp plugin: The %s config option is not allowed within "
1695                                         "`WAFL' blocks.", item->key);
1696         }
1697
1698         return (0);
1699 } /* }}} int cna_config_wafl */
1700
1701 /* Corresponds to a <System /> block */
1702 static int cna_config_system (host_config_t *host, /* {{{ */
1703                 oconfig_item_t *ci, const cfg_service_t *default_service)
1704 {
1705         cfg_system_t *cfg_system;
1706         int i;
1707         
1708         if ((host == NULL) || (ci == NULL))
1709                 return (EINVAL);
1710
1711         if (host->cfg_system == NULL)
1712         {
1713                 cfg_system = malloc (sizeof (*cfg_system));
1714                 if (cfg_system == NULL)
1715                         return (ENOMEM);
1716                 memset (cfg_system, 0, sizeof (*cfg_system));
1717
1718                 /* Set default flags */
1719                 cfg_system->flags = CFG_SYSTEM_ALL;
1720                 cfg_system->query = NULL;
1721
1722                 host->cfg_system = cfg_system;
1723         }
1724         cfg_system = host->cfg_system;
1725
1726         for (i = 0; i < ci->children_num; ++i) {
1727                 oconfig_item_t *item = ci->children + i;
1728
1729                 if (strcasecmp(item->key, "Interval") == 0) {
1730                         cna_config_get_interval (item, &cfg_system->interval);
1731                 } else if (!strcasecmp(item->key, "GetCPULoad")) {
1732                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_CPU);
1733                 } else if (!strcasecmp(item->key, "GetInterfaces")) {
1734                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_NET);
1735                 } else if (!strcasecmp(item->key, "GetDiskOps")) {
1736                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_OPS);
1737                 } else if (!strcasecmp(item->key, "GetDiskIO")) {
1738                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_DISK);
1739                 } else {
1740                         WARNING ("netapp plugin: The %s config option is not allowed within "
1741                                         "`System' blocks.", item->key);
1742                 }
1743         }
1744
1745         return (0);
1746 } /* }}} int cna_config_system */
1747
1748 /* Corresponds to a <Host /> block. */
1749 static host_config_t *cna_config_host (const oconfig_item_t *ci, /* {{{ */
1750                 const host_config_t *default_host, const cfg_service_t *def_def_service)
1751 {
1752         oconfig_item_t *item;
1753         host_config_t *host;
1754         cfg_service_t default_service = *def_def_service;
1755         int status;
1756         int i;
1757         
1758         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1759                 WARNING("netapp plugin: \"Host\" needs exactly one string argument. Ignoring host block.");
1760                 return 0;
1761         }
1762
1763         host = malloc(sizeof(*host));
1764         memcpy (host, default_host, sizeof (*host));
1765
1766         status = cf_util_get_string (ci, &host->name);
1767         if (status != 0)
1768         {
1769                 sfree (host);
1770                 return (NULL);
1771         }
1772
1773         for (i = 0; i < ci->children_num; ++i) {
1774                 item = ci->children + i;
1775
1776                 status = 0;
1777
1778                 if (!strcasecmp(item->key, "Address")) {
1779                         status = cf_util_get_string (item, &host->host);
1780                 } else if (!strcasecmp(item->key, "Port")) {
1781                         int tmp;
1782
1783                         tmp = cf_util_get_port_number (item);
1784                         if (tmp > 0)
1785                                 host->port = tmp;
1786                 } else if (!strcasecmp(item->key, "Protocol")) {
1787                         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"))) {
1788                                 WARNING("netapp plugin: \"Protocol\" needs to be either \"http\" or \"https\". Ignoring host block \"%s\".", ci->values[0].value.string);
1789                                 return 0;
1790                         }
1791                         if (!strcasecmp(item->values[0].value.string, "http")) host->protocol = NA_SERVER_TRANSPORT_HTTP;
1792                         else host->protocol = NA_SERVER_TRANSPORT_HTTPS;
1793                 } else if (!strcasecmp(item->key, "User")) {
1794                         status = cf_util_get_string (item, &host->username);
1795                 } else if (!strcasecmp(item->key, "Password")) {
1796                         status = cf_util_get_string (item, &host->password);
1797                 } else if (!strcasecmp(item->key, "Interval")) {
1798                         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) {
1799                                 WARNING("netapp plugin: \"Interval\" of host %s needs exactly one integer argument.", ci->values[0].value.string);
1800                                 continue;
1801                         }
1802                         host->interval = item->values[0].value.number;
1803                 } else if (!strcasecmp(item->key, "GetVolumePerfData")) {
1804                         cna_config_volume_performance(host, item);
1805                 } else if (!strcasecmp(item->key, "System")) {
1806                         cna_config_system(host, item, &default_service);
1807                 } else if (!strcasecmp(item->key, "WAFL")) {
1808                         cna_config_wafl(host, item);
1809                 } else if (!strcasecmp(item->key, "Disks")) {
1810                         cna_config_disk(host, item);
1811                 } else if (!strcasecmp(item->key, "GetVolumeData")) {
1812                         cna_config_volume_usage(host, item);
1813                 } else {
1814                         WARNING("netapp plugin: Ignoring unknown config option \"%s\" in host block \"%s\".",
1815                                         item->key, ci->values[0].value.string);
1816                 }
1817
1818                 if (status != 0)
1819                         break;
1820         }
1821
1822         if (host->host == NULL)
1823                 host->host = strdup (host->name);
1824
1825         if (host->host == NULL)
1826                 status = -1;
1827
1828         if (host->port <= 0)
1829                 host->port = (host->protocol == NA_SERVER_TRANSPORT_HTTP) ? 80 : 443;
1830
1831         if ((host->username == NULL) || (host->password == NULL)) {
1832                 WARNING("netapp plugin: Please supply login information for host \"%s\". "
1833                                 "Ignoring host block.", host->name);
1834                 status = -1;
1835         }
1836
1837         if (status != 0)
1838         {
1839                 free_host_config (host);
1840                 return (NULL);
1841         }
1842
1843         return host;
1844 } /* }}} host_config_t *cna_config_host */
1845
1846 /*
1847  * Callbacks registered with the daemon
1848  *
1849  * Pretty standard stuff here.
1850  */
1851 static int cna_init(void) { /* {{{ */
1852         char err[256];
1853         na_elem_t *e;
1854         host_config_t *host;
1855         cfg_service_t *service;
1856         
1857         if (!global_host_config) {
1858                 WARNING("netapp plugin: Plugin loaded but no hosts defined.");
1859                 return 1;
1860         }
1861
1862         memset (err, 0, sizeof (err));
1863         if (!na_startup(err, sizeof(err))) {
1864                 err[sizeof (err) - 1] = 0;
1865                 ERROR("netapp plugin: Error initializing netapp API: %s", err);
1866                 return 1;
1867         }
1868
1869         for (host = global_host_config; host; host = host->next) {
1870                 /* Request version 1.1 of the ONTAP API */
1871                 host->srv = na_server_open(host->host,
1872                                 /* major version = */ 1, /* minor version = */ 1); 
1873                 if (host->srv == NULL) {
1874                         ERROR ("netapp plugin: na_server_open (%s) failed.", host->host);
1875                         continue;
1876                 }
1877
1878                 if (host->interval < interval_g)
1879                         host->interval = interval_g;
1880
1881                 na_server_set_transport_type(host->srv, host->protocol,
1882                                 /* transportarg = */ NULL);
1883                 na_server_set_port(host->srv, host->port);
1884                 na_server_style(host->srv, NA_STYLE_LOGIN_PASSWORD);
1885                 na_server_adminuser(host->srv, host->username, host->password);
1886                 na_server_set_timeout(host->srv, 5 /* seconds */);
1887
1888                 for (service = host->services; service; service = service->next) {
1889                         service->interval = host->interval * service->multiplier;
1890
1891                         if (service->handler == query_volume_perf_data) {
1892                                 service->query = na_elem_new("perf-object-get-instances");
1893                                 na_child_add_string(service->query, "objectname", "volume");
1894                                 e = na_elem_new("counters");
1895                                 /* "foo" means: This string has to be here but
1896                                    the content doesn't matter. */
1897                                 na_child_add_string(e, "foo", "read_ops");
1898                                 na_child_add_string(e, "foo", "write_ops");
1899                                 na_child_add_string(e, "foo", "read_data");
1900                                 na_child_add_string(e, "foo", "write_data");
1901                                 na_child_add_string(e, "foo", "read_latency");
1902                                 na_child_add_string(e, "foo", "write_latency");
1903                                 na_child_add(service->query, e);
1904                         } else if (service->handler == collect_volume_data) {
1905                                 service->query = na_elem_new("volume-list-info");
1906                                 /* na_child_add_string(service->query, "objectname", "volume"); */
1907                                 /* } else if (service->handler == collect_snapshot_data) { */
1908                                 /* service->query = na_elem_new("snapshot-list-info"); */
1909                         }
1910                 } /* for (host->services) */
1911         }
1912         return 0;
1913 } /* }}} int cna_init */
1914
1915 static int cna_config (oconfig_item_t *ci) { /* {{{ */
1916         int i;
1917         oconfig_item_t *item;
1918         host_config_t default_host = HOST_INIT;
1919         cfg_service_t default_service = SERVICE_INIT;
1920         
1921         for (i = 0; i < ci->children_num; ++i) {
1922                 item = ci->children + i;
1923
1924                 if (!strcasecmp(item->key, "Host")) {
1925                         host_config_t *host;
1926                         host_config_t *tmp;
1927
1928                         host = cna_config_host(item, &default_host, &default_service);
1929                         if (host == NULL)
1930                                 continue;
1931
1932                         for (tmp = global_host_config; tmp != NULL; tmp = tmp->next)
1933                         {
1934                                 if (strcasecmp (host->name, tmp->name) == 0)
1935                                         WARNING ("netapp plugin: Duplicate definition of host `%s'. "
1936                                                         "This is probably a bad idea.",
1937                                                         host->name);
1938
1939                                 if (tmp->next == NULL)
1940                                         break;
1941                         }
1942
1943                         host->next = NULL;
1944                         if (tmp == NULL)
1945                                 global_host_config = host;
1946                         else
1947                                 tmp->next = host;
1948                 } else {
1949                         WARNING("netapp plugin: Ignoring unknown config option \"%s\".", item->key);
1950                 }
1951         }
1952         return 0;
1953 } /* }}} int cna_config */
1954
1955 static int cna_read(void) { /* {{{ */
1956         na_elem_t *out;
1957         host_config_t *host;
1958         cfg_service_t *service;
1959         
1960         for (host = global_host_config; host; host = host->next) {
1961                 for (service = host->services; service; service = service->next) {
1962                         if (--service->skip_countdown > 0) continue;
1963                         service->skip_countdown = service->multiplier;
1964                         out = na_server_invoke_elem(host->srv, service->query);
1965                         if (na_results_status(out) != NA_OK) {
1966                                 int netapp_errno = na_results_errno(out);
1967                                 ERROR("netapp plugin: Error %d from host %s: %s", netapp_errno, host->name, na_results_reason(out));
1968                                 na_elem_free(out);
1969                                 if (netapp_errno == EIO || netapp_errno == ETIMEDOUT) {
1970                                         /* Network problems. Just give up on all other services on this host. */
1971                                         break;
1972                                 }
1973                                 continue;
1974                         }
1975                         service->handler(host, out, service->data);
1976                         na_elem_free(out);
1977                 } /* for (host->services) */
1978
1979                 cna_query_wafl (host);
1980                 cna_query_disk (host);
1981                 cna_query_system (host);
1982         }
1983         return 0;
1984 } /* }}} int cna_read */
1985
1986 void module_register(void) {
1987         plugin_register_complex_config("netapp", cna_config);
1988         plugin_register_init("netapp", cna_init);
1989         plugin_register_read("netapp", cna_read);
1990 }
1991
1992 /* vim: set sw=2 ts=2 noet fdm=marker : */