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