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