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