57fb59de5c5ef0be861729bcc24c400ca0d818ed
[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                 } else { /* really hacky work-around code. {{{ */
1297                         uint64_t sis_saved_percent;
1298                         uint64_t sis_saved_guess;
1299                         uint64_t overflow_guess;
1300                         uint64_t guess1, guess2, guess3;
1301
1302                         /* Check if we have v->norm_used. Without it, we cannot calculate
1303                          * sis_saved_guess. */
1304                         if ((v->flags & HAVE_VOLUME_USAGE_NORM_USED) == 0)
1305                                 continue;
1306
1307                         sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
1308                         if (sis_saved_percent > 100)
1309                                 continue;
1310
1311                         /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
1312                          * will hopefully be fixed in later versions. To work around the bug, try
1313                          * to figure out how often the 32bit integer wrapped around by using the
1314                          * "percentage-saved" value. Because the percentage is in the range
1315                          * [0-100], this should work as long as the saved space does not exceed
1316                          * 400 GBytes. */
1317                         /* percentage-saved = size-saved / (size-saved + size-used) */
1318                         if (sis_saved_percent < 100)
1319                                 sis_saved_guess = v->norm_used * sis_saved_percent / (100 - sis_saved_percent);
1320                         else
1321                                 sis_saved_guess = v->norm_used;
1322
1323                         overflow_guess = sis_saved_guess >> 32;
1324                         guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
1325                         guess2 = (overflow_guess << 32) + sis_saved_reported;
1326                         guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
1327
1328                         if (sis_saved_guess < guess2) {
1329                                 if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
1330                                         v->sis_saved = guess1;
1331                                 else
1332                                         v->sis_saved = guess2;
1333                         } else {
1334                                 if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
1335                                         v->sis_saved = guess2;
1336                                 else
1337                                         v->sis_saved = guess3;
1338                         }
1339                 } /* }}} end of 32-bit workaround */
1340         } /* for (elem_volume) */
1341
1342         return (cna_submit_volume_usage_data (hostname, cfg_volume));
1343 } /* }}} int cna_handle_volume_usage_data */
1344
1345 static int cna_setup_volume_usage (cfg_volume_usage_t *cvu) /* {{{ */
1346 {
1347         if (cvu == NULL)
1348                 return (EINVAL);
1349
1350         if (cvu->query != NULL)
1351                 return (0);
1352
1353         cvu->query = na_elem_new ("volume-list-info");
1354         if (cvu->query == NULL)
1355         {
1356                 ERROR ("netapp plugin: na_elem_new failed.");
1357                 return (-1);
1358         }
1359
1360         /* TODO: cvu->snap_query = na_elem_new("snapshot-list-info"); */
1361
1362         return (0);
1363 } /* }}} int cna_setup_volume_usage */
1364
1365 static int cna_query_volume_usage (host_config_t *host) /* {{{ */
1366 {
1367         na_elem_t *data;
1368         int status;
1369         time_t now;
1370
1371         if (host == NULL)
1372                 return (EINVAL);
1373
1374         /* If the user did not configure volume_usage statistics, return without
1375          * doing anything. */
1376         if (host->cfg_volume_usage == NULL)
1377                 return (0);
1378
1379         now = time (NULL);
1380         if ((host->cfg_volume_usage->interval.interval + host->cfg_volume_usage->interval.last_read) > now)
1381                 return (0);
1382
1383         status = cna_setup_volume_usage (host->cfg_volume_usage);
1384         if (status != 0)
1385                 return (status);
1386         assert (host->cfg_volume_usage->query != NULL);
1387
1388         data = na_server_invoke_elem(host->srv, host->cfg_volume_usage->query);
1389         if (na_results_status (data) != NA_OK)
1390         {
1391                 ERROR ("netapp plugin: cna_query_volume_usage: na_server_invoke_elem failed: %s",
1392                                 na_results_reason (data));
1393                 na_elem_free (data);
1394                 return (-1);
1395         }
1396
1397         status = cna_handle_volume_usage_data (host->name, host->cfg_volume_usage, data);
1398
1399         if (status == 0)
1400                 host->cfg_volume_usage->interval.last_read = now;
1401
1402         na_elem_free (data);
1403         return (status);
1404 } /* }}} int cna_query_volume_usage */
1405
1406 /* Data corresponding to <GetVolumePerfData /> */
1407 static void query_volume_perf_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
1408         cfg_volume_perf_t *cfg_volume_perf = data;
1409         time_t timestamp;
1410         na_elem_t *counter, *inst;
1411         
1412         timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
1413
1414         out = na_elem_child(out, "instances");
1415         na_elem_iter_t inst_iter = na_child_iterator(out);
1416         for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
1417                 data_volume_perf_t perf_data;
1418                 volume_t *volume;
1419
1420                 memset (&perf_data, 0, sizeof (perf_data));
1421                 perf_data.timestamp = timestamp;
1422
1423                 volume = get_volume(host, na_child_get_string(inst, "name"),
1424                                 cfg_volume_perf->flags);
1425                 if (volume == NULL)
1426                         continue;
1427
1428                 na_elem_iter_t count_iter = na_child_iterator(na_elem_child(inst, "counters"));
1429                 for (counter = na_iterator_next(&count_iter); counter; counter = na_iterator_next(&count_iter)) {
1430                         const char *name;
1431                         uint64_t value;
1432
1433                         name = na_child_get_string(counter, "name");
1434                         if (name == NULL)
1435                                 continue;
1436
1437                         value = na_child_get_uint64(counter, "value", UINT64_MAX);
1438                         if (value == UINT64_MAX)
1439                                 continue;
1440
1441                         if (!strcmp(name, "read_data")) {
1442                                 perf_data.read_bytes = value;
1443                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_READ;
1444                         } else if (!strcmp(name, "write_data")) {
1445                                 perf_data.write_bytes = value;
1446                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_WRITE;
1447                         } else if (!strcmp(name, "read_ops")) {
1448                                 perf_data.read_ops = value;
1449                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_READ;
1450                         } else if (!strcmp(name, "write_ops")) {
1451                                 perf_data.write_ops = value;
1452                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_WRITE;
1453                         } else if (!strcmp(name, "read_latency")) {
1454                                 perf_data.read_latency = value;
1455                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_READ;
1456                         } else if (!strcmp(name, "write_latency")) {
1457                                 perf_data.write_latency = value;
1458                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_WRITE;
1459                         }
1460                 }
1461
1462                 submit_volume_perf_data (host, volume, &perf_data);
1463         } /* for (volume) */
1464 } /* }}} void query_volume_perf_data */
1465
1466 /* Data corresponding to <System /> */
1467 static int cna_handle_system_data (const char *hostname, /* {{{ */
1468                 cfg_system_t *cfg_system, na_elem_t *data)
1469 {
1470         na_elem_t *instances;
1471         na_elem_t *counter;
1472         na_elem_iter_t counter_iter;
1473
1474         counter_t disk_read = 0, disk_written = 0;
1475         counter_t net_recv = 0, net_sent = 0;
1476         counter_t cpu_busy = 0, cpu_total = 0;
1477         uint32_t counter_flags = 0;
1478
1479         const char *instance;
1480         time_t timestamp;
1481         
1482         timestamp = (time_t) na_child_get_uint64 (data, "timestamp", 0);
1483
1484         instances = na_elem_child(na_elem_child (data, "instances"), "instance-data");
1485         if (instances == NULL)
1486         {
1487                 ERROR ("netapp plugin: cna_handle_system_data: "
1488                                 "na_elem_child (\"instances\") failed.");
1489                 return (-1);
1490         }
1491
1492         instance = na_child_get_string (instances, "name");
1493         if (instance == NULL)
1494         {
1495                 ERROR ("netapp plugin: cna_handle_system_data: "
1496                                 "na_child_get_string (\"name\") failed.");
1497                 return (-1);
1498         }
1499
1500         counter_iter = na_child_iterator (na_elem_child (instances, "counters"));
1501         for (counter = na_iterator_next (&counter_iter);
1502                         counter != NULL;
1503                         counter = na_iterator_next (&counter_iter))
1504         {
1505                 const char *name;
1506                 uint64_t value;
1507
1508                 name = na_child_get_string(counter, "name");
1509                 if (name == NULL)
1510                         continue;
1511
1512                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
1513                 if (value == UINT64_MAX)
1514                         continue;
1515
1516                 if (!strcmp(name, "disk_data_read")) {
1517                         disk_read = (counter_t) (value * 1024);
1518                         counter_flags |= 0x01;
1519                 } else if (!strcmp(name, "disk_data_written")) {
1520                         disk_written = (counter_t) (value * 1024);
1521                         counter_flags |= 0x02;
1522                 } else if (!strcmp(name, "net_data_recv")) {
1523                         net_recv = (counter_t) (value * 1024);
1524                         counter_flags |= 0x04;
1525                 } else if (!strcmp(name, "net_data_sent")) {
1526                         net_sent = (counter_t) (value * 1024);
1527                         counter_flags |= 0x08;
1528                 } else if (!strcmp(name, "cpu_busy")) {
1529                         cpu_busy = (counter_t) value;
1530                         counter_flags |= 0x10;
1531                 } else if (!strcmp(name, "cpu_elapsed_time")) {
1532                         cpu_total = (counter_t) value;
1533                         counter_flags |= 0x20;
1534                 } else if ((cfg_system->flags & CFG_SYSTEM_OPS)
1535                                 && (value > 0) && (strlen(name) > 4)
1536                                 && (!strcmp(name + strlen(name) - 4, "_ops"))) {
1537                         submit_counter (hostname, instance, "disk_ops_complex", name,
1538                                         (counter_t) value, timestamp);
1539                 }
1540         } /* for (counter) */
1541
1542         if ((cfg_system->flags & CFG_SYSTEM_DISK)
1543                         && (HAS_ALL_FLAGS (counter_flags, 0x01 | 0x02)))
1544                 submit_two_counters (hostname, instance, "disk_octets", NULL,
1545                                 disk_read, disk_written, timestamp);
1546                                 
1547         if ((cfg_system->flags & CFG_SYSTEM_NET)
1548                         && (HAS_ALL_FLAGS (counter_flags, 0x04 | 0x08)))
1549                 submit_two_counters (hostname, instance, "if_octets", NULL,
1550                                 net_recv, net_sent, timestamp);
1551
1552         if ((cfg_system->flags & CFG_SYSTEM_CPU)
1553                         && (HAS_ALL_FLAGS (counter_flags, 0x10 | 0x20)))
1554         {
1555                 submit_counter (hostname, instance, "cpu", "system",
1556                                 cpu_busy, timestamp);
1557                 submit_counter (hostname, instance, "cpu", "idle",
1558                                 cpu_total - cpu_busy, timestamp);
1559         }
1560
1561         return (0);
1562 } /* }}} int cna_handle_system_data */
1563
1564 static int cna_setup_system (cfg_system_t *cs) /* {{{ */
1565 {
1566         if (cs == NULL)
1567                 return (EINVAL);
1568
1569         if (cs->query != NULL)
1570                 return (0);
1571
1572         cs->query = na_elem_new ("perf-object-get-instances");
1573         if (cs->query == NULL)
1574         {
1575                 ERROR ("netapp plugin: na_elem_new failed.");
1576                 return (-1);
1577         }
1578         na_child_add_string (cs->query, "objectname", "system");
1579
1580         return (0);
1581 } /* }}} int cna_setup_system */
1582
1583 static int cna_query_system (host_config_t *host) /* {{{ */
1584 {
1585         na_elem_t *data;
1586         int status;
1587         time_t now;
1588
1589         if (host == NULL)
1590                 return (EINVAL);
1591
1592         /* If system statistics were not configured, return without doing anything. */
1593         if (host->cfg_system == NULL)
1594                 return (0);
1595
1596         now = time (NULL);
1597         if ((host->cfg_system->interval.interval + host->cfg_system->interval.last_read) > now)
1598                 return (0);
1599
1600         status = cna_setup_system (host->cfg_system);
1601         if (status != 0)
1602                 return (status);
1603         assert (host->cfg_system->query != NULL);
1604
1605         data = na_server_invoke_elem(host->srv, host->cfg_system->query);
1606         if (na_results_status (data) != NA_OK)
1607         {
1608                 ERROR ("netapp plugin: cna_query_system: na_server_invoke_elem failed: %s",
1609                                 na_results_reason (data));
1610                 na_elem_free (data);
1611                 return (-1);
1612         }
1613
1614         status = cna_handle_system_data (host->name, host->cfg_system, data);
1615
1616         if (status == 0)
1617                 host->cfg_system->interval.last_read = now;
1618
1619         na_elem_free (data);
1620         return (status);
1621 } /* }}} int cna_query_system */
1622
1623 /*
1624  * Configuration handling
1625  */
1626 /* Sets a given flag if the boolean argument is true and unsets the flag if it
1627  * is false. On error, the flag-field is not changed. */
1628 static int cna_config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */
1629                 uint32_t *flags, uint32_t flag)
1630 {
1631         if ((ci == NULL) || (flags == NULL))
1632                 return (EINVAL);
1633
1634         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1635         {
1636                 WARNING ("netapp plugin: The %s option needs exactly one boolean argument.",
1637                                 ci->key);
1638                 return (-1);
1639         }
1640
1641         if (ci->values[0].value.boolean)
1642                 *flags |= flag;
1643         else
1644                 *flags &= ~flag;
1645
1646         return (0);
1647 } /* }}} int cna_config_bool_to_flag */
1648
1649 /* Handling of the "Multiplier" option which is allowed in every block. */
1650 static int cna_config_get_multiplier (const oconfig_item_t *ci, /* {{{ */
1651                 cfg_service_t *service)
1652 {
1653         int tmp;
1654
1655         if ((ci == NULL) || (service == NULL))
1656                 return (EINVAL);
1657
1658         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1659         {
1660                 WARNING ("netapp plugin: The `Multiplier' option needs exactly one numeric argument.");
1661                 return (-1);
1662         }
1663
1664         tmp = (int) (ci->values[0].value.number + .5);
1665         if (tmp < 1)
1666         {
1667                 WARNING ("netapp plugin: The `Multiplier' option needs a positive integer argument.");
1668                 return (-1);
1669         }
1670
1671         service->multiplier = tmp;
1672         service->skip_countdown = tmp;
1673
1674         return (0);
1675 } /* }}} int cna_config_get_multiplier */
1676
1677 /* Handling of the "Interval" option which is allowed in every block. */
1678 static int cna_config_get_interval (const oconfig_item_t *ci, /* {{{ */
1679                 cna_interval_t *out_interval)
1680 {
1681         time_t tmp;
1682
1683         if ((ci == NULL) || (out_interval == NULL))
1684                 return (EINVAL);
1685
1686         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1687         {
1688                 WARNING ("netapp plugin: The `Multiplier' option needs exactly one numeric argument.");
1689                 return (-1);
1690         }
1691
1692         tmp = (time_t) (ci->values[0].value.number + .5);
1693         if (tmp < 1)
1694         {
1695                 WARNING ("netapp plugin: The `Multiplier' option needs a positive integer argument.");
1696                 return (-1);
1697         }
1698
1699         out_interval->interval = tmp;
1700         out_interval->last_read = 0;
1701
1702         return (0);
1703 } /* }}} int cna_config_get_interval */
1704
1705 /* Handling of the "GetIO", "GetOps" and "GetLatency" options within a
1706  * <GetVolumePerfData /> block. */
1707 static void cna_config_volume_performance_option (host_config_t *host, /* {{{ */
1708                 cfg_volume_perf_t *perf_volume, const oconfig_item_t *item,
1709                 uint32_t flag)
1710 {
1711         int i;
1712         
1713         for (i = 0; i < item->values_num; ++i) {
1714                 const char *name;
1715                 volume_t *v;
1716                 _Bool set = true;
1717
1718                 if (item->values[i].type != OCONFIG_TYPE_STRING) {
1719                         WARNING("netapp plugin: Ignoring non-string argument in "
1720                                         "\"GetVolumePerfData\" block for host %s", host->name);
1721                         continue;
1722                 }
1723
1724                 name = item->values[i].value.string;
1725                 if (name[0] == '+') {
1726                         set = true;
1727                         ++name;
1728                 } else if (name[0] == '-') {
1729                         set = false;
1730                         ++name;
1731                 }
1732
1733                 if (!name[0]) {
1734                         if (set)
1735                                 perf_volume->flags |= flag;
1736                         else /* if (!set) */
1737                                 perf_volume->flags &= ~flag;
1738
1739                         host_set_all_perf_data_flags(host, flag, set);
1740                         continue;
1741                 }
1742
1743                 v = get_volume (host, name, perf_volume->flags);
1744                 if (v == NULL)
1745                         continue;
1746
1747                 if (set)
1748                         v->perf_data.flags |= flag;
1749                 else /* if (!set) */
1750                         v->perf_data.flags &= ~flag;
1751         } /* for (i = 0 .. item->values_num) */
1752 } /* }}} void cna_config_volume_performance_option */
1753
1754 /* Corresponds to a <GetVolumePerfData /> block */
1755 static void cna_config_volume_performance(host_config_t *host, const oconfig_item_t *ci) { /* {{{ */
1756         int i, had_io = 0, had_ops = 0, had_latency = 0;
1757         cfg_service_t *service;
1758         cfg_volume_perf_t *perf_volume;
1759         
1760         service = malloc(sizeof(*service));
1761         service->query = 0;
1762         service->handler = query_volume_perf_data;
1763         perf_volume = service->data = malloc(sizeof(*perf_volume));
1764         perf_volume->flags = CFG_VOLUME_PERF_INIT;
1765         service->next = host->services;
1766         host->services = service;
1767         for (i = 0; i < ci->children_num; ++i) {
1768                 oconfig_item_t *item = ci->children + i;
1769                 
1770                 /* if (!item || !item->key || !*item->key) continue; */
1771                 if (!strcasecmp(item->key, "Multiplier")) {
1772                         cna_config_get_multiplier (item, service);
1773                 } else if (!strcasecmp(item->key, "GetIO")) {
1774                         had_io = 1;
1775                         cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_IO);
1776                 } else if (!strcasecmp(item->key, "GetOps")) {
1777                         had_ops = 1;
1778                         cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_OPS);
1779                 } else if (!strcasecmp(item->key, "GetLatency")) {
1780                         had_latency = 1;
1781                         cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_LATENCY);
1782                 }
1783         }
1784         if (!had_io) {
1785                 perf_volume->flags |= CFG_VOLUME_PERF_IO;
1786                 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_IO, /* set = */ true);
1787         }
1788         if (!had_ops) {
1789                 perf_volume->flags |= CFG_VOLUME_PERF_OPS;
1790                 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_OPS, /* set = */ true);
1791         }
1792         if (!had_latency) {
1793                 perf_volume->flags |= CFG_VOLUME_PERF_LATENCY;
1794                 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_LATENCY, /* set = */ true);
1795         }
1796 } /* }}} void cna_config_volume_performance */
1797
1798 /* Handling of the "Capacity" and "Snapshot" options within a <VolumeUsage />
1799  * block. */
1800 static void cna_config_volume_usage_option (cfg_volume_usage_t *cvu, /* {{{ */
1801                 const oconfig_item_t *ci)
1802 {
1803         char *name;
1804         ignorelist_t * il;
1805
1806         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1807         {
1808                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
1809                                 ci->key);
1810                 return;
1811         }
1812
1813         name = ci->values[0].value.string;
1814
1815         if (strcasecmp ("Capacity", ci->key) == 0)
1816                 il = cvu->il_capacity;
1817         else if (strcasecmp ("Snapshot", ci->key) == 0)
1818                 il = cvu->il_snapshot;
1819         else
1820                 return;
1821
1822         ignorelist_add (il, name);
1823 } /* }}} void cna_config_volume_usage_option */
1824
1825 /* Handling of the "IgnoreSelectedCapacity" and "IgnoreSelectedSnapshot"
1826  * options within a <VolumeUsage /> block. */
1827 static void cna_config_volume_usage_default (cfg_volume_usage_t *cvu, /* {{{ */
1828                 const oconfig_item_t *ci)
1829 {
1830         ignorelist_t *il;
1831
1832         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1833         {
1834                 WARNING ("netapp plugin: The %s option requires exactly one string argument.",
1835                                 ci->key);
1836                 return;
1837         }
1838
1839         if (strcasecmp ("IgnoreSelectedCapacity", ci->key) == 0)
1840                 il = cvu->il_capacity;
1841         else if (strcasecmp ("IgnoreSelectedSnapshot", ci->key) == 0)
1842                 il = cvu->il_snapshot;
1843         else
1844                 return;
1845
1846         if (ci->values[0].value.boolean)
1847                 ignorelist_set_invert (il, /* invert = */ 0);
1848         else
1849                 ignorelist_set_invert (il, /* invert = */ 1);
1850 } /* }}} void cna_config_volume_usage_default */
1851
1852 /* Corresponds to a <Disks /> block */
1853 static int cna_config_disk(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
1854         cfg_disk_t *cfg_disk;
1855         int i;
1856
1857         if ((host == NULL) || (ci == NULL))
1858                 return (EINVAL);
1859
1860         if (host->cfg_disk == NULL)
1861         {
1862                 cfg_disk = malloc (sizeof (*cfg_disk));
1863                 if (cfg_disk == NULL)
1864                         return (ENOMEM);
1865                 memset (cfg_disk, 0, sizeof (*cfg_disk));
1866
1867                 /* Set default flags */
1868                 cfg_disk->flags = CFG_DISK_ALL;
1869                 cfg_disk->query = NULL;
1870                 cfg_disk->disks = NULL;
1871
1872                 host->cfg_disk = cfg_disk;
1873         }
1874         cfg_disk = host->cfg_disk;
1875         
1876         for (i = 0; i < ci->children_num; ++i) {
1877                 oconfig_item_t *item = ci->children + i;
1878                 
1879                 /* if (!item || !item->key || !*item->key) continue; */
1880                 if (strcasecmp(item->key, "Interval") == 0)
1881                         cna_config_get_interval (item, &cfg_disk->interval);
1882                 else if (strcasecmp(item->key, "GetBusy") == 0)
1883                         cna_config_bool_to_flag (item, &cfg_disk->flags, CFG_DISK_BUSIEST);
1884         }
1885
1886         if ((cfg_disk->flags & CFG_DISK_ALL) == 0)
1887         {
1888                 NOTICE ("netapp plugin: All disk related values have been disabled. "
1889                                 "Collection of per-disk data will be disabled entirely.");
1890                 free_cfg_disk (host->cfg_disk);
1891                 host->cfg_disk = NULL;
1892         }
1893
1894         return (0);
1895 } /* }}} int cna_config_disk */
1896
1897 /* Corresponds to a <WAFL /> block */
1898 static int cna_config_wafl(host_config_t *host, oconfig_item_t *ci) /* {{{ */
1899 {
1900         cfg_wafl_t *cfg_wafl;
1901         int i;
1902
1903         if ((host == NULL) || (ci == NULL))
1904                 return (EINVAL);
1905
1906         if (host->cfg_wafl == NULL)
1907         {
1908                 cfg_wafl = malloc (sizeof (*cfg_wafl));
1909                 if (cfg_wafl == NULL)
1910                         return (ENOMEM);
1911                 memset (cfg_wafl, 0, sizeof (*cfg_wafl));
1912
1913                 /* Set default flags */
1914                 cfg_wafl->flags = CFG_WAFL_ALL;
1915
1916                 host->cfg_wafl = cfg_wafl;
1917         }
1918         cfg_wafl = host->cfg_wafl;
1919
1920         for (i = 0; i < ci->children_num; ++i) {
1921                 oconfig_item_t *item = ci->children + i;
1922                 
1923                 if (strcasecmp(item->key, "Interval") == 0)
1924                         cna_config_get_interval (item, &cfg_wafl->interval);
1925                 else if (!strcasecmp(item->key, "GetNameCache"))
1926                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_NAME_CACHE);
1927                 else if (!strcasecmp(item->key, "GetDirCache"))
1928                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_DIR_CACHE);
1929                 else if (!strcasecmp(item->key, "GetBufferCache"))
1930                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_BUF_CACHE);
1931                 else if (!strcasecmp(item->key, "GetInodeCache"))
1932                         cna_config_bool_to_flag (item, &cfg_wafl->flags, CFG_WAFL_INODE_CACHE);
1933                 else
1934                         WARNING ("netapp plugin: The %s config option is not allowed within "
1935                                         "`WAFL' blocks.", item->key);
1936         }
1937
1938         if ((cfg_wafl->flags & CFG_WAFL_ALL) == 0)
1939         {
1940                 NOTICE ("netapp plugin: All WAFL related values have been disabled. "
1941                                 "Collection of WAFL data will be disabled entirely.");
1942                 free_cfg_wafl (host->cfg_wafl);
1943                 host->cfg_wafl = NULL;
1944         }
1945
1946         return (0);
1947 } /* }}} int cna_config_wafl */
1948
1949 /*
1950  * <VolumeUsage>
1951  *   Capacity "vol0"
1952  *   Capacity "vol1"
1953  *   Capacity "vol2"
1954  *   Capacity "vol3"
1955  *   Capacity "vol4"
1956  *   IgnoreSelectedCapacity false
1957  *
1958  *   Snapshot "vol0"
1959  *   Snapshot "vol3"
1960  *   Snapshot "vol4"
1961  *   Snapshot "vol7"
1962  *   IgnoreSelectedSnapshot false
1963  * </VolumeUsage>
1964  */
1965 /* Corresponds to a <VolumeUsage /> block */
1966 static int cna_config_volume_usage(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
1967         cfg_volume_usage_t *cfg_volume_usage;
1968         int i;
1969
1970         if ((host == NULL) || (ci == NULL))
1971                 return (EINVAL);
1972
1973         if (host->cfg_volume_usage == NULL)
1974         {
1975                 cfg_volume_usage = malloc (sizeof (*cfg_volume_usage));
1976                 if (cfg_volume_usage == NULL)
1977                         return (ENOMEM);
1978                 memset (cfg_volume_usage, 0, sizeof (*cfg_volume_usage));
1979
1980                 /* Set default flags */
1981                 cfg_volume_usage->query = NULL;
1982                 cfg_volume_usage->volumes = NULL;
1983
1984                 cfg_volume_usage->il_capacity = ignorelist_create (/* invert = */ 1);
1985                 if (cfg_volume_usage->il_capacity == NULL)
1986                 {
1987                         sfree (cfg_volume_usage);
1988                         return (ENOMEM);
1989                 }
1990
1991                 cfg_volume_usage->il_snapshot = ignorelist_create (/* invert = */ 1);
1992                 if (cfg_volume_usage->il_snapshot == NULL)
1993                 {
1994                         ignorelist_free (cfg_volume_usage->il_capacity);
1995                         sfree (cfg_volume_usage);
1996                         return (ENOMEM);
1997                 }
1998
1999                 host->cfg_volume_usage = cfg_volume_usage;
2000         }
2001         cfg_volume_usage = host->cfg_volume_usage;
2002         
2003         for (i = 0; i < ci->children_num; ++i) {
2004                 oconfig_item_t *item = ci->children + i;
2005                 
2006                 /* if (!item || !item->key || !*item->key) continue; */
2007                 if (strcasecmp(item->key, "Interval") == 0)
2008                         cna_config_get_interval (item, &cfg_volume_usage->interval);
2009                 else if (!strcasecmp(item->key, "Capacity"))
2010                         cna_config_volume_usage_option (cfg_volume_usage, item);
2011                 else if (!strcasecmp(item->key, "Snapshot"))
2012                         cna_config_volume_usage_option (cfg_volume_usage, item);
2013                 else if (!strcasecmp(item->key, "IgnoreSelectedCapacity"))
2014                         cna_config_volume_usage_default (cfg_volume_usage, item);
2015                 else if (!strcasecmp(item->key, "IgnoreSelectedSnapshot"))
2016                         cna_config_volume_usage_default (cfg_volume_usage, item);
2017         }
2018
2019         return (0);
2020 } /* }}} int cna_config_volume_usage */
2021
2022 /* Corresponds to a <System /> block */
2023 static int cna_config_system (host_config_t *host, /* {{{ */
2024                 oconfig_item_t *ci, const cfg_service_t *default_service)
2025 {
2026         cfg_system_t *cfg_system;
2027         int i;
2028         
2029         if ((host == NULL) || (ci == NULL))
2030                 return (EINVAL);
2031
2032         if (host->cfg_system == NULL)
2033         {
2034                 cfg_system = malloc (sizeof (*cfg_system));
2035                 if (cfg_system == NULL)
2036                         return (ENOMEM);
2037                 memset (cfg_system, 0, sizeof (*cfg_system));
2038
2039                 /* Set default flags */
2040                 cfg_system->flags = CFG_SYSTEM_ALL;
2041                 cfg_system->query = NULL;
2042
2043                 host->cfg_system = cfg_system;
2044         }
2045         cfg_system = host->cfg_system;
2046
2047         for (i = 0; i < ci->children_num; ++i) {
2048                 oconfig_item_t *item = ci->children + i;
2049
2050                 if (strcasecmp(item->key, "Interval") == 0) {
2051                         cna_config_get_interval (item, &cfg_system->interval);
2052                 } else if (!strcasecmp(item->key, "GetCPULoad")) {
2053                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_CPU);
2054                 } else if (!strcasecmp(item->key, "GetInterfaces")) {
2055                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_NET);
2056                 } else if (!strcasecmp(item->key, "GetDiskOps")) {
2057                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_OPS);
2058                 } else if (!strcasecmp(item->key, "GetDiskIO")) {
2059                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_DISK);
2060                 } else {
2061                         WARNING ("netapp plugin: The %s config option is not allowed within "
2062                                         "`System' blocks.", item->key);
2063                 }
2064         }
2065
2066         if ((cfg_system->flags & CFG_SYSTEM_ALL) == 0)
2067         {
2068                 NOTICE ("netapp plugin: All system related values have been disabled. "
2069                                 "Collection of system data will be disabled entirely.");
2070                 free_cfg_system (host->cfg_system);
2071                 host->cfg_system = NULL;
2072         }
2073
2074         return (0);
2075 } /* }}} int cna_config_system */
2076
2077 /* Corresponds to a <Host /> block. */
2078 static host_config_t *cna_config_host (const oconfig_item_t *ci, /* {{{ */
2079                 const host_config_t *default_host, const cfg_service_t *def_def_service)
2080 {
2081         oconfig_item_t *item;
2082         host_config_t *host;
2083         cfg_service_t default_service = *def_def_service;
2084         int status;
2085         int i;
2086         
2087         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2088                 WARNING("netapp plugin: \"Host\" needs exactly one string argument. Ignoring host block.");
2089                 return 0;
2090         }
2091
2092         host = malloc(sizeof(*host));
2093         memcpy (host, default_host, sizeof (*host));
2094
2095         status = cf_util_get_string (ci, &host->name);
2096         if (status != 0)
2097         {
2098                 sfree (host);
2099                 return (NULL);
2100         }
2101
2102         for (i = 0; i < ci->children_num; ++i) {
2103                 item = ci->children + i;
2104
2105                 status = 0;
2106
2107                 if (!strcasecmp(item->key, "Address")) {
2108                         status = cf_util_get_string (item, &host->host);
2109                 } else if (!strcasecmp(item->key, "Port")) {
2110                         int tmp;
2111
2112                         tmp = cf_util_get_port_number (item);
2113                         if (tmp > 0)
2114                                 host->port = tmp;
2115                 } else if (!strcasecmp(item->key, "Protocol")) {
2116                         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"))) {
2117                                 WARNING("netapp plugin: \"Protocol\" needs to be either \"http\" or \"https\". Ignoring host block \"%s\".", ci->values[0].value.string);
2118                                 return 0;
2119                         }
2120                         if (!strcasecmp(item->values[0].value.string, "http")) host->protocol = NA_SERVER_TRANSPORT_HTTP;
2121                         else host->protocol = NA_SERVER_TRANSPORT_HTTPS;
2122                 } else if (!strcasecmp(item->key, "User")) {
2123                         status = cf_util_get_string (item, &host->username);
2124                 } else if (!strcasecmp(item->key, "Password")) {
2125                         status = cf_util_get_string (item, &host->password);
2126                 } else if (!strcasecmp(item->key, "Interval")) {
2127                         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) {
2128                                 WARNING("netapp plugin: \"Interval\" of host %s needs exactly one integer argument.", ci->values[0].value.string);
2129                                 continue;
2130                         }
2131                         host->interval = item->values[0].value.number;
2132                 } else if (!strcasecmp(item->key, "WAFL")) {
2133                         cna_config_wafl(host, item);
2134                 } else if (!strcasecmp(item->key, "Disks")) {
2135                         cna_config_disk(host, item);
2136                 } else if (!strcasecmp(item->key, "GetVolumePerfData")) {
2137                         cna_config_volume_performance(host, item);
2138                 } else if (!strcasecmp(item->key, "VolumeUsage")) {
2139                         cna_config_volume_usage(host, item);
2140                 } else if (!strcasecmp(item->key, "System")) {
2141                         cna_config_system(host, item, &default_service);
2142                 } else {
2143                         WARNING("netapp plugin: Ignoring unknown config option \"%s\" in host block \"%s\".",
2144                                         item->key, ci->values[0].value.string);
2145                 }
2146
2147                 if (status != 0)
2148                         break;
2149         }
2150
2151         if (host->host == NULL)
2152                 host->host = strdup (host->name);
2153
2154         if (host->host == NULL)
2155                 status = -1;
2156
2157         if (host->port <= 0)
2158                 host->port = (host->protocol == NA_SERVER_TRANSPORT_HTTP) ? 80 : 443;
2159
2160         if ((host->username == NULL) || (host->password == NULL)) {
2161                 WARNING("netapp plugin: Please supply login information for host \"%s\". "
2162                                 "Ignoring host block.", host->name);
2163                 status = -1;
2164         }
2165
2166         if (status != 0)
2167         {
2168                 free_host_config (host);
2169                 return (NULL);
2170         }
2171
2172         return host;
2173 } /* }}} host_config_t *cna_config_host */
2174
2175 /*
2176  * Callbacks registered with the daemon
2177  *
2178  * Pretty standard stuff here.
2179  */
2180 static int cna_init(void) { /* {{{ */
2181         char err[256];
2182         na_elem_t *e;
2183         host_config_t *host;
2184         cfg_service_t *service;
2185         
2186         if (!global_host_config) {
2187                 WARNING("netapp plugin: Plugin loaded but no hosts defined.");
2188                 return 1;
2189         }
2190
2191         memset (err, 0, sizeof (err));
2192         if (!na_startup(err, sizeof(err))) {
2193                 err[sizeof (err) - 1] = 0;
2194                 ERROR("netapp plugin: Error initializing netapp API: %s", err);
2195                 return 1;
2196         }
2197
2198         for (host = global_host_config; host; host = host->next) {
2199                 /* Request version 1.1 of the ONTAP API */
2200                 host->srv = na_server_open(host->host,
2201                                 /* major version = */ 1, /* minor version = */ 1); 
2202                 if (host->srv == NULL) {
2203                         ERROR ("netapp plugin: na_server_open (%s) failed.", host->host);
2204                         continue;
2205                 }
2206
2207                 if (host->interval < interval_g)
2208                         host->interval = interval_g;
2209
2210                 na_server_set_transport_type(host->srv, host->protocol,
2211                                 /* transportarg = */ NULL);
2212                 na_server_set_port(host->srv, host->port);
2213                 na_server_style(host->srv, NA_STYLE_LOGIN_PASSWORD);
2214                 na_server_adminuser(host->srv, host->username, host->password);
2215                 na_server_set_timeout(host->srv, 5 /* seconds */);
2216
2217                 for (service = host->services; service; service = service->next) {
2218                         service->interval = host->interval * service->multiplier;
2219
2220                         if (service->handler == query_volume_perf_data) {
2221                                 service->query = na_elem_new("perf-object-get-instances");
2222                                 na_child_add_string(service->query, "objectname", "volume");
2223                                 e = na_elem_new("counters");
2224                                 /* "foo" means: This string has to be here but
2225                                    the content doesn't matter. */
2226                                 na_child_add_string(e, "foo", "read_ops");
2227                                 na_child_add_string(e, "foo", "write_ops");
2228                                 na_child_add_string(e, "foo", "read_data");
2229                                 na_child_add_string(e, "foo", "write_data");
2230                                 na_child_add_string(e, "foo", "read_latency");
2231                                 na_child_add_string(e, "foo", "write_latency");
2232                                 na_child_add(service->query, e);
2233                         }
2234                 } /* for (host->services) */
2235         }
2236         return 0;
2237 } /* }}} int cna_init */
2238
2239 static int cna_config (oconfig_item_t *ci) { /* {{{ */
2240         int i;
2241         oconfig_item_t *item;
2242         host_config_t default_host = HOST_INIT;
2243         cfg_service_t default_service = SERVICE_INIT;
2244         
2245         for (i = 0; i < ci->children_num; ++i) {
2246                 item = ci->children + i;
2247
2248                 if (!strcasecmp(item->key, "Host")) {
2249                         host_config_t *host;
2250                         host_config_t *tmp;
2251
2252                         host = cna_config_host(item, &default_host, &default_service);
2253                         if (host == NULL)
2254                                 continue;
2255
2256                         for (tmp = global_host_config; tmp != NULL; tmp = tmp->next)
2257                         {
2258                                 if (strcasecmp (host->name, tmp->name) == 0)
2259                                         WARNING ("netapp plugin: Duplicate definition of host `%s'. "
2260                                                         "This is probably a bad idea.",
2261                                                         host->name);
2262
2263                                 if (tmp->next == NULL)
2264                                         break;
2265                         }
2266
2267                         host->next = NULL;
2268                         if (tmp == NULL)
2269                                 global_host_config = host;
2270                         else
2271                                 tmp->next = host;
2272                 } else {
2273                         WARNING("netapp plugin: Ignoring unknown config option \"%s\".", item->key);
2274                 }
2275         }
2276         return 0;
2277 } /* }}} int cna_config */
2278
2279 static int cna_read(void) { /* {{{ */
2280         na_elem_t *out;
2281         host_config_t *host;
2282         cfg_service_t *service;
2283         
2284         for (host = global_host_config; host; host = host->next) {
2285                 for (service = host->services; service; service = service->next) {
2286                         if (--service->skip_countdown > 0) continue;
2287                         service->skip_countdown = service->multiplier;
2288                         out = na_server_invoke_elem(host->srv, service->query);
2289                         if (na_results_status(out) != NA_OK) {
2290                                 int netapp_errno = na_results_errno(out);
2291                                 ERROR("netapp plugin: Error %d from host %s: %s", netapp_errno, host->name, na_results_reason(out));
2292                                 na_elem_free(out);
2293                                 if (netapp_errno == EIO || netapp_errno == ETIMEDOUT) {
2294                                         /* Network problems. Just give up on all other services on this host. */
2295                                         break;
2296                                 }
2297                                 continue;
2298                         }
2299                         service->handler(host, out, service->data);
2300                         na_elem_free(out);
2301                 } /* for (host->services) */
2302
2303                 cna_query_wafl (host);
2304                 cna_query_disk (host);
2305                 cna_query_volume_usage (host);
2306                 cna_query_system (host);
2307         }
2308         return 0;
2309 } /* }}} int cna_read */
2310
2311 void module_register(void) {
2312         plugin_register_complex_config("netapp", cna_config);
2313         plugin_register_init("netapp", cna_init);
2314         plugin_register_read("netapp", cna_read);
2315 }
2316
2317 /* vim: set sw=2 ts=2 noet fdm=marker : */