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