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