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