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