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