Copypasta error in volume usage.
[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", "free",
832                                         (double) size_free, /* time = */ 0);
833
834                 size_used = na_child_get_uint64(inst, "size-used", UINT64_MAX);
835                 if (size_used != UINT64_MAX) {
836                         if ((volume->cfg_volume_usage.flags & HAVE_VOLUME_USAGE_SNAP)
837                                         && (size_used >= volume->cfg_volume_usage.snap_used))
838                                 size_used -= volume->cfg_volume_usage.snap_used;
839                         submit_double (host->name, volume->name, "df_complex", "used",
840                                         (double) size_used, /* time = */ 0);
841                 }
842
843                 snap_reserved = na_child_get_uint64(inst, "snapshot-blocks-reserved", UINT64_MAX);
844                 if (!(volume->cfg_volume_usage.flags & HAVE_VOLUME_USAGE_SNAP) && (snap_reserved != UINT64_MAX))
845                         /* If we have snap usage data this value has already been submitted. */
846                         /* 1 block == 1024 bytes  as per API docs */
847                         submit_double (host->name, volume->name, "df_complex", "snap_reserved",
848                                         (double) (1024 * snap_reserved), /* time = */ 0);
849
850                 sis = na_elem_child(inst, "sis");
851                 if (sis == NULL)
852                         continue;
853
854                 sis_state = na_child_get_string(sis, "state");
855                 if ((sis_state == NULL)
856                                 || (strcmp ("enabled", sis_state) != 0))
857                         continue;
858
859                 sis_saved_reported = na_child_get_uint64(sis, "size-saved", UINT64_MAX);
860                 if (sis_saved_reported == UINT64_MAX)
861                         continue;
862
863                 /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
864                 if ((sis_saved_reported >> 32) != 0) {
865                         /* In case they ever fix this bug. */
866                         sis_saved = sis_saved_reported;
867                 } else {
868                         uint64_t sis_saved_percent;
869                         uint64_t sis_saved_guess;
870                         uint64_t overflow_guess;
871                         uint64_t guess1, guess2, guess3;
872
873                         sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
874                         if (sis_saved_percent > 100)
875                                 continue;
876
877                         /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
878                          * will hopefully be fixed in later versions. To work around the bug, try
879                          * to figure out how often the 32bit integer wrapped around by using the
880                          * "percentage-saved" value. Because the percentage is in the range
881                          * [0-100], this should work as long as the saved space does not exceed
882                          * 400 GBytes. */
883                         /* percentage-saved = size-saved / (size-saved + size-used) */
884                         if (sis_saved_percent < 100)
885                                 sis_saved_guess = size_used * sis_saved_percent / (100 - sis_saved_percent);
886                         else
887                                 sis_saved_guess = size_used;
888
889                         overflow_guess = sis_saved_guess >> 32;
890                         guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
891                         guess2 = (overflow_guess << 32) + sis_saved_reported;
892                         guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
893
894                         if (sis_saved_guess < guess2) {
895                                 if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
896                                         sis_saved = guess1;
897                                 else
898                                         sis_saved = guess2;
899                         } else {
900                                 if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
901                                         sis_saved = guess2;
902                                 else
903                                         sis_saved = guess3;
904                         }
905                 } /* end of 32-bit workaround */
906
907                 submit_double (host->name, volume->name, "df_complex", "sis_saved",
908                                 (double) sis_saved, /* time = */ 0);
909         }
910 } /* }}} void collect_volume_data */
911
912 /* Data corresponding to <GetVolumePerfData /> */
913 static void query_volume_perf_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
914         cfg_volume_perf_t *cfg_volume_perf = data;
915         time_t timestamp;
916         na_elem_t *counter, *inst;
917         
918         timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
919
920         out = na_elem_child(out, "instances");
921         na_elem_iter_t inst_iter = na_child_iterator(out);
922         for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
923                 data_volume_perf_t perf_data;
924                 volume_t *volume;
925
926                 memset (&perf_data, 0, sizeof (perf_data));
927                 perf_data.timestamp = timestamp;
928
929                 volume = get_volume(host, na_child_get_string(inst, "name"),
930                                 /* data_flags = */ 0, cfg_volume_perf->flags);
931                 if (volume == NULL)
932                         continue;
933
934                 na_elem_iter_t count_iter = na_child_iterator(na_elem_child(inst, "counters"));
935                 for (counter = na_iterator_next(&count_iter); counter; counter = na_iterator_next(&count_iter)) {
936                         const char *name;
937                         uint64_t value;
938
939                         name = na_child_get_string(counter, "name");
940                         if (name == NULL)
941                                 continue;
942
943                         value = na_child_get_uint64(counter, "value", UINT64_MAX);
944                         if (value == UINT64_MAX)
945                                 continue;
946
947                         if (!strcmp(name, "read_data")) {
948                                 perf_data.read_bytes = value;
949                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_READ;
950                         } else if (!strcmp(name, "write_data")) {
951                                 perf_data.write_bytes = value;
952                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_WRITE;
953                         } else if (!strcmp(name, "read_ops")) {
954                                 perf_data.read_ops = value;
955                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_READ;
956                         } else if (!strcmp(name, "write_ops")) {
957                                 perf_data.write_ops = value;
958                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_WRITE;
959                         } else if (!strcmp(name, "read_latency")) {
960                                 perf_data.read_latency = value;
961                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_READ;
962                         } else if (!strcmp(name, "write_latency")) {
963                                 perf_data.write_latency = value;
964                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_WRITE;
965                         }
966                 }
967
968                 submit_volume_perf_data (host, volume, &perf_data);
969         } /* for (volume) */
970 } /* }}} void query_volume_perf_data */
971
972 /* Data corresponding to <GetSystemPerfData /> */
973 static void collect_perf_system_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
974         counter_t disk_read = 0, disk_written = 0;
975         counter_t net_recv = 0, net_sent = 0;
976         counter_t cpu_busy = 0, cpu_total = 0;
977         unsigned int counter_flags = 0;
978
979         cfg_system_t *cfg_system = data;
980         const char *instance;
981         time_t timestamp;
982         na_elem_t *counter;
983         
984         timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
985         out = na_elem_child(na_elem_child(out, "instances"), "instance-data");
986         instance = na_child_get_string(out, "name");
987
988         na_elem_iter_t iter = na_child_iterator(na_elem_child(out, "counters"));
989         for (counter = na_iterator_next(&iter); counter; counter = na_iterator_next(&iter)) {
990                 const char *name;
991                 uint64_t value;
992
993                 name = na_child_get_string(counter, "name");
994                 if (name == NULL)
995                         continue;
996
997                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
998                 if (value == UINT64_MAX)
999                         continue;
1000
1001                 if (!strcmp(name, "disk_data_read")) {
1002                         disk_read = (counter_t) (value * 1024);
1003                         counter_flags |= 0x01;
1004                 } else if (!strcmp(name, "disk_data_written")) {
1005                         disk_written = (counter_t) (value * 1024);
1006                         counter_flags |= 0x02;
1007                 } else if (!strcmp(name, "net_data_recv")) {
1008                         net_recv = (counter_t) (value * 1024);
1009                         counter_flags |= 0x04;
1010                 } else if (!strcmp(name, "net_data_sent")) {
1011                         net_sent = (counter_t) (value * 1024);
1012                         counter_flags |= 0x08;
1013                 } else if (!strcmp(name, "cpu_busy")) {
1014                         cpu_busy = (counter_t) value;
1015                         counter_flags |= 0x10;
1016                 } else if (!strcmp(name, "cpu_elapsed_time")) {
1017                         cpu_total = (counter_t) value;
1018                         counter_flags |= 0x20;
1019                 } else if ((cfg_system->flags & CFG_SYSTEM_OPS)
1020                                 && (value > 0) && (strlen(name) > 4)
1021                                 && (!strcmp(name + strlen(name) - 4, "_ops"))) {
1022                         submit_counter (host->name, instance, "disk_ops_complex", name,
1023                                         (counter_t) value, timestamp);
1024                 }
1025         } /* for (counter) */
1026
1027         if ((cfg_system->flags & CFG_SYSTEM_DISK)
1028                         && ((counter_flags & 0x03) == 0x03))
1029                 submit_two_counters (host->name, instance, "disk_octets", NULL,
1030                                 disk_read, disk_written, timestamp);
1031                                 
1032         if ((cfg_system->flags & CFG_SYSTEM_NET)
1033                         && ((counter_flags & 0x0c) == 0x0c))
1034                 submit_two_counters (host->name, instance, "if_octets", NULL,
1035                                 net_recv, net_sent, timestamp);
1036
1037         if ((cfg_system->flags & CFG_SYSTEM_CPU)
1038                         && ((counter_flags & 0x30) == 0x30)) {
1039                 submit_counter (host->name, instance, "cpu", "system",
1040                                 cpu_busy, timestamp);
1041                 submit_counter (host->name, instance, "cpu", "idle",
1042                                 cpu_total - cpu_busy, timestamp);
1043         }
1044 } /* }}} void collect_perf_system_data */
1045
1046 /*
1047  * Configuration handling
1048  */
1049 /* Sets a given flag if the boolean argument is true and unsets the flag if it
1050  * is false. On error, the flag-field is not changed. */
1051 static int cna_config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */
1052                 uint32_t *flags, uint32_t flag)
1053 {
1054         if ((ci == NULL) || (flags == NULL))
1055                 return (EINVAL);
1056
1057         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1058         {
1059                 WARNING ("netapp plugin: The %s option needs exactly one boolean argument.",
1060                                 ci->key);
1061                 return (-1);
1062         }
1063
1064         if (ci->values[0].value.boolean)
1065                 *flags |= flag;
1066         else
1067                 *flags &= ~flag;
1068
1069         return (0);
1070 } /* }}} int cna_config_bool_to_flag */
1071
1072 /* Handling of the "Multiplier" option which is allowed in every block. */
1073 static int cna_config_get_multiplier (const oconfig_item_t *ci, /* {{{ */
1074                 cfg_service_t *service)
1075 {
1076         int tmp;
1077
1078         if ((ci == NULL) || (service == NULL))
1079                 return (EINVAL);
1080
1081         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1082         {
1083                 WARNING ("netapp plugin: The `Multiplier' option needs exactly one numeric argument.");
1084                 return (-1);
1085         }
1086
1087         tmp = (int) (ci->values[0].value.number + .5);
1088         if (tmp < 1)
1089         {
1090                 WARNING ("netapp plugin: The `Multiplier' option needs a positive integer argument.");
1091                 return (-1);
1092         }
1093
1094         service->multiplier = tmp;
1095         service->skip_countdown = tmp;
1096
1097         return (0);
1098 } /* }}} int cna_config_get_multiplier */
1099
1100 /* Handling of the "GetIO", "GetOps" and "GetLatency" options within a
1101  * <GetVolumePerfData /> block. */
1102 static void cna_config_volume_performance_option (host_config_t *host, /* {{{ */
1103                 cfg_volume_perf_t *perf_volume, const oconfig_item_t *item,
1104                 uint32_t flag)
1105 {
1106         int i;
1107         
1108         for (i = 0; i < item->values_num; ++i) {
1109                 const char *name;
1110                 volume_t *v;
1111                 _Bool set = true;
1112
1113                 if (item->values[i].type != OCONFIG_TYPE_STRING) {
1114                         WARNING("netapp plugin: Ignoring non-string argument in "
1115                                         "\"GetVolumePerfData\" block for host %s", host->name);
1116                         continue;
1117                 }
1118
1119                 name = item->values[i].value.string;
1120                 if (name[0] == '+') {
1121                         set = true;
1122                         ++name;
1123                 } else if (name[0] == '-') {
1124                         set = false;
1125                         ++name;
1126                 }
1127
1128                 if (!name[0]) {
1129                         if (set)
1130                                 perf_volume->flags |= flag;
1131                         else /* if (!set) */
1132                                 perf_volume->flags &= ~flag;
1133
1134                         host_set_all_perf_data_flags(host, flag, set);
1135                         continue;
1136                 }
1137
1138                 v = get_volume (host, name, /* data_flags = */ 0, perf_volume->flags);
1139                 if (v == NULL)
1140                         continue;
1141
1142                 if (set)
1143                         v->perf_data.flags |= flag;
1144                 else /* if (!set) */
1145                         v->perf_data.flags &= ~flag;
1146         } /* for (i = 0 .. item->values_num) */
1147 } /* }}} void cna_config_volume_performance_option */
1148
1149 /* Corresponds to a <GetDiskPerfData /> block */
1150 static void cna_config_volume_performance(host_config_t *host, const oconfig_item_t *ci) { /* {{{ */
1151         int i, had_io = 0, had_ops = 0, had_latency = 0;
1152         cfg_service_t *service;
1153         cfg_volume_perf_t *perf_volume;
1154         
1155         service = malloc(sizeof(*service));
1156         service->query = 0;
1157         service->handler = query_volume_perf_data;
1158         perf_volume = service->data = malloc(sizeof(*perf_volume));
1159         perf_volume->flags = CFG_VOLUME_PERF_INIT;
1160         service->next = host->services;
1161         host->services = service;
1162         for (i = 0; i < ci->children_num; ++i) {
1163                 oconfig_item_t *item = ci->children + i;
1164                 
1165                 /* if (!item || !item->key || !*item->key) continue; */
1166                 if (!strcasecmp(item->key, "Multiplier")) {
1167                         cna_config_get_multiplier (item, service);
1168                 } else if (!strcasecmp(item->key, "GetIO")) {
1169                         had_io = 1;
1170                         cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_IO);
1171                 } else if (!strcasecmp(item->key, "GetOps")) {
1172                         had_ops = 1;
1173                         cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_OPS);
1174                 } else if (!strcasecmp(item->key, "GetLatency")) {
1175                         had_latency = 1;
1176                         cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_LATENCY);
1177                 }
1178         }
1179         if (!had_io) {
1180                 perf_volume->flags |= CFG_VOLUME_PERF_IO;
1181                 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_IO, /* set = */ true);
1182         }
1183         if (!had_ops) {
1184                 perf_volume->flags |= CFG_VOLUME_PERF_OPS;
1185                 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_OPS, /* set = */ true);
1186         }
1187         if (!had_latency) {
1188                 perf_volume->flags |= CFG_VOLUME_PERF_LATENCY;
1189                 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_LATENCY, /* set = */ true);
1190         }
1191 } /* }}} void cna_config_volume_performance */
1192
1193 /* Handling of the "GetDiskUtil" option within a <GetVolumeData /> block. */
1194 static void cna_config_volume_usage_option (host_config_t *host, /* {{{ */
1195                 cfg_volume_usage_t *cfg_volume_data, const oconfig_item_t *item, uint32_t flag)
1196 {
1197         int i;
1198         
1199         for (i = 0; i < item->values_num; ++i) {
1200                 const char *name;
1201                 volume_t *v;
1202                 _Bool set = true;
1203
1204                 if (item->values[i].type != OCONFIG_TYPE_STRING) {
1205                         WARNING("netapp plugin: Ignoring non-string argument in \"GetVolData\""
1206                                         "block for host %s", host->name);
1207                         continue;
1208                 }
1209
1210                 name = item->values[i].value.string;
1211                 if (name[0] == '+') {
1212                         set = true;
1213                         ++name;
1214                 } else if (name[0] == '-') {
1215                         set = false;
1216                         ++name;
1217                 }
1218
1219                 if (!name[0]) {
1220                         if (set)
1221                                 cfg_volume_data->flags |= flag;
1222                         else /* if (!set) */
1223                                 cfg_volume_data->flags &= ~flag;
1224
1225                         host_set_all_cfg_volume_usage_flags(host, flag, set);
1226                         continue;
1227                 }
1228
1229                 v = get_volume(host, name, cfg_volume_data->flags, /* perf_flags = */ 0);
1230                 if (v == NULL)
1231                         continue;
1232
1233                 if (!v->cfg_volume_usage.flags)
1234                         v->cfg_volume_usage.flags = cfg_volume_data->flags;
1235
1236                 if (set)
1237                         v->cfg_volume_usage.flags |= flag;
1238                 else /* if (!set) */
1239                         v->cfg_volume_usage.flags &= ~flag;
1240         }
1241 } /* }}} void cna_config_volume_usage_option */
1242
1243 /* Corresponds to a <GetVolumeData /> block */
1244 static void cna_config_volume_usage(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
1245         int i, had_df = 0;
1246         cfg_service_t *service;
1247         cfg_volume_usage_t *cfg_volume_data;
1248         
1249         service = malloc(sizeof(*service));
1250         service->query = 0;
1251         service->handler = collect_volume_data;
1252         cfg_volume_data = service->data = malloc(sizeof(*cfg_volume_data));
1253         cfg_volume_data->flags = CFG_VOLUME_USAGE_INIT;
1254         service->next = host->services;
1255         host->services = service;
1256         for (i = 0; i < ci->children_num; ++i) {
1257                 oconfig_item_t *item = ci->children + i;
1258                 
1259                 /* if (!item || !item->key || !*item->key) continue; */
1260                 if (!strcasecmp(item->key, "Multiplier")) {
1261                         cna_config_get_multiplier (item, service);
1262                 } else if (!strcasecmp(item->key, "GetDiskUtil")) {
1263                         had_df = 1;
1264                         cna_config_volume_usage_option(host, cfg_volume_data, item, CFG_VOLUME_USAGE_DF);
1265                 } else if (!strcasecmp(item->key, "GetSnapUtil")) {
1266                         had_df = 1;
1267                         cna_config_volume_usage_option(host, cfg_volume_data, item, CFG_VOLUME_USAGE_SNAP);
1268                 }
1269         }
1270         if (!had_df) {
1271                 cfg_volume_data->flags |= CFG_VOLUME_USAGE_DF;
1272                 host_set_all_cfg_volume_usage_flags(host, CFG_VOLUME_USAGE_DF, /* set = */ true);
1273         }
1274         if (cfg_volume_data->flags & CFG_VOLUME_USAGE_SNAP) {
1275                 WARNING("netapp plugin: The \"GetSnapUtil\" option does not support the \"+\" wildcard.");
1276         }
1277 } /* }}} void cna_config_volume_usage */
1278
1279 /* Corresponds to a <GetDiskPerfData /> block */
1280 static void cna_config_disk(host_config_t *temp, oconfig_item_t *ci) { /* {{{ */
1281         int i;
1282         cfg_service_t *service;
1283         cfg_disk_t *cfg_disk;
1284         
1285         service = malloc(sizeof(*service));
1286         service->query = 0;
1287         service->handler = query_submit_disk_data;
1288         cfg_disk = service->data = malloc(sizeof(*cfg_disk));
1289         cfg_disk->flags = CFG_DISK_ALL;
1290         service->next = temp->services;
1291         temp->services = service;
1292         for (i = 0; i < ci->children_num; ++i) {
1293                 oconfig_item_t *item = ci->children + i;
1294                 
1295                 /* if (!item || !item->key || !*item->key) continue; */
1296                 if (!strcasecmp(item->key, "Multiplier")) {
1297                         cna_config_get_multiplier (item, service);
1298                 } else if (!strcasecmp(item->key, "GetBusy")) {
1299                         cna_config_bool_to_flag (item, &cfg_disk->flags, CFG_SYSTEM_CPU);
1300                 }
1301         }
1302 } /* }}} void cna_config_disk */
1303
1304 /* Corresponds to a <GetWaflPerfData /> block */
1305 static void cna_config_wafl(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
1306         int i;
1307         cfg_service_t *service;
1308         data_wafl_t *perf_wafl;
1309         
1310         service = malloc(sizeof(*service));
1311         if (service == NULL)
1312                 return;
1313         memset (service, 0, sizeof (*service));
1314
1315         service->query = 0;
1316         service->handler = query_wafl_data;
1317         perf_wafl = service->data = malloc(sizeof(*perf_wafl));
1318         perf_wafl->flags = CFG_WAFL_ALL;
1319
1320         for (i = 0; i < ci->children_num; ++i) {
1321                 oconfig_item_t *item = ci->children + i;
1322                 
1323                 if (!strcasecmp(item->key, "Multiplier")) {
1324                         cna_config_get_multiplier (item, service);
1325                 } else if (!strcasecmp(item->key, "GetNameCache")) {
1326                         cna_config_bool_to_flag (item, &perf_wafl->flags, CFG_WAFL_NAME_CACHE);
1327                 } else if (!strcasecmp(item->key, "GetDirCache")) {
1328                         cna_config_bool_to_flag (item, &perf_wafl->flags, CFG_WAFL_DIR_CACHE);
1329                 } else if (!strcasecmp(item->key, "GetBufCache")) {
1330                         cna_config_bool_to_flag (item, &perf_wafl->flags, CFG_WAFL_BUF_CACHE);
1331                 } else if (!strcasecmp(item->key, "GetInodeCache")) {
1332                         cna_config_bool_to_flag (item, &perf_wafl->flags, CFG_WAFL_INODE_CACHE);
1333                 } else {
1334                         WARNING ("netapp plugin: The %s config option is not allowed within "
1335                                         "`GetWaflPerfData' blocks.", item->key);
1336                 }
1337         }
1338
1339         service->next = host->services;
1340         host->services = service;
1341 } /* }}} void cna_config_wafl */
1342
1343 /* Corresponds to a <GetSystemPerfData /> block */
1344 static int cna_config_system (host_config_t *host, /* {{{ */
1345                 oconfig_item_t *ci, const cfg_service_t *default_service)
1346 {
1347         int i;
1348         cfg_service_t *service;
1349         cfg_system_t *cfg_system;
1350         
1351         service = malloc(sizeof(*service));
1352         if (service == NULL)
1353                 return (-1);
1354         memset (service, 0, sizeof (*service));
1355         *service = *default_service;
1356         service->handler = collect_perf_system_data;
1357
1358         cfg_system = malloc(sizeof(*cfg_system));
1359         if (cfg_system == NULL) {
1360                 sfree (service);
1361                 return (-1);
1362         }
1363         memset (cfg_system, 0, sizeof (*cfg_system));
1364         cfg_system->flags = CFG_SYSTEM_ALL;
1365         service->data = cfg_system;
1366
1367         for (i = 0; i < ci->children_num; ++i) {
1368                 oconfig_item_t *item = ci->children + i;
1369
1370                 if (!strcasecmp(item->key, "Multiplier")) {
1371                         cna_config_get_multiplier (item, service);
1372                 } else if (!strcasecmp(item->key, "GetCPULoad")) {
1373                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_CPU);
1374                 } else if (!strcasecmp(item->key, "GetInterfaces")) {
1375                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_NET);
1376                 } else if (!strcasecmp(item->key, "GetDiskOps")) {
1377                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_OPS);
1378                 } else if (!strcasecmp(item->key, "GetDiskIO")) {
1379                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_DISK);
1380                 } else {
1381                         WARNING ("netapp plugin: The %s config option is not allowed within "
1382                                         "`GetSystemPerfData' blocks.", item->key);
1383                 }
1384         }
1385
1386         service->next = host->services;
1387         host->services = service;
1388
1389         return (0);
1390 } /* }}} int cna_config_system */
1391
1392 /* Corresponds to a <Host /> block. */
1393 static host_config_t *cna_config_host (const oconfig_item_t *ci, /* {{{ */
1394                 const host_config_t *default_host, const cfg_service_t *def_def_service)
1395 {
1396         oconfig_item_t *item;
1397         host_config_t *host;
1398         cfg_service_t default_service = *def_def_service;
1399         int status;
1400         int i;
1401         
1402         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1403                 WARNING("netapp plugin: \"Host\" needs exactly one string argument. Ignoring host block.");
1404                 return 0;
1405         }
1406
1407         host = malloc(sizeof(*host));
1408         memcpy (host, default_host, sizeof (*host));
1409
1410         status = cf_util_get_string (ci, &host->name);
1411         if (status != 0)
1412         {
1413                 sfree (host);
1414                 return (NULL);
1415         }
1416
1417         for (i = 0; i < ci->children_num; ++i) {
1418                 item = ci->children + i;
1419
1420                 status = 0;
1421
1422                 if (!strcasecmp(item->key, "Address")) {
1423                         status = cf_util_get_string (item, &host->host);
1424                 } else if (!strcasecmp(item->key, "Port")) {
1425                         int tmp;
1426
1427                         tmp = cf_util_get_port_number (item);
1428                         if (tmp > 0)
1429                                 host->port = tmp;
1430                 } else if (!strcasecmp(item->key, "Protocol")) {
1431                         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"))) {
1432                                 WARNING("netapp plugin: \"Protocol\" needs to be either \"http\" or \"https\". Ignoring host block \"%s\".", ci->values[0].value.string);
1433                                 return 0;
1434                         }
1435                         if (!strcasecmp(item->values[0].value.string, "http")) host->protocol = NA_SERVER_TRANSPORT_HTTP;
1436                         else host->protocol = NA_SERVER_TRANSPORT_HTTPS;
1437                 } else if (!strcasecmp(item->key, "User")) {
1438                         status = cf_util_get_string (item, &host->username);
1439                 } else if (!strcasecmp(item->key, "Password")) {
1440                         status = cf_util_get_string (item, &host->password);
1441                 } else if (!strcasecmp(item->key, "Interval")) {
1442                         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) {
1443                                 WARNING("netapp plugin: \"Interval\" of host %s needs exactly one integer argument.", ci->values[0].value.string);
1444                                 continue;
1445                         }
1446                         host->interval = item->values[0].value.number;
1447                 } else if (!strcasecmp(item->key, "GetVolumePerfData")) {
1448                         cna_config_volume_performance(host, item);
1449                 } else if (!strcasecmp(item->key, "GetSystemPerfData")) {
1450                         cna_config_system(host, item, &default_service);
1451                 } else if (!strcasecmp(item->key, "GetWaflPerfData")) {
1452                         cna_config_wafl(host, item);
1453                 } else if (!strcasecmp(item->key, "GetDiskPerfData")) {
1454                         cna_config_disk(host, item);
1455                 } else if (!strcasecmp(item->key, "GetVolumeData")) {
1456                         cna_config_volume_usage(host, item);
1457                 } else {
1458                         WARNING("netapp plugin: Ignoring unknown config option \"%s\" in host block \"%s\".",
1459                                         item->key, ci->values[0].value.string);
1460                 }
1461
1462                 if (status != 0)
1463                         break;
1464         }
1465
1466         if (host->host == NULL)
1467                 host->host = strdup (host->name);
1468
1469         if (host->host == NULL)
1470                 status = -1;
1471
1472         if (host->port <= 0)
1473                 host->port = (host->protocol == NA_SERVER_TRANSPORT_HTTP) ? 80 : 443;
1474
1475         if ((host->username == NULL) || (host->password == NULL)) {
1476                 WARNING("netapp plugin: Please supply login information for host \"%s\". "
1477                                 "Ignoring host block.", host->name);
1478                 status = -1;
1479         }
1480
1481         if (status != 0)
1482         {
1483                 free_host_config (host);
1484                 return (NULL);
1485         }
1486
1487         return host;
1488 } /* }}} host_config_t *cna_config_host */
1489
1490 /*
1491  * Callbacks registered with the daemon
1492  *
1493  * Pretty standard stuff here.
1494  */
1495 static int cna_init(void) { /* {{{ */
1496         char err[256];
1497         na_elem_t *e;
1498         host_config_t *host;
1499         cfg_service_t *service;
1500         
1501         if (!global_host_config) {
1502                 WARNING("netapp plugin: Plugin loaded but no hosts defined.");
1503                 return 1;
1504         }
1505
1506         if (!na_startup(err, sizeof(err))) {
1507                 ERROR("netapp plugin: Error initializing netapp API: %s", err);
1508                 return 1;
1509         }
1510
1511         for (host = global_host_config; host; host = host->next) {
1512                 host->srv = na_server_open(host->host, 1, 1); 
1513                 na_server_set_transport_type(host->srv, host->protocol, 0);
1514                 na_server_set_port(host->srv, host->port);
1515                 na_server_style(host->srv, NA_STYLE_LOGIN_PASSWORD);
1516                 na_server_adminuser(host->srv, host->username, host->password);
1517                 na_server_set_timeout(host->srv, 5);
1518                 for (service = host->services; service; service = service->next) {
1519                         service->interval = host->interval * service->multiplier;
1520                         if (service->handler == collect_perf_system_data) {
1521                                 service->query = na_elem_new("perf-object-get-instances");
1522                                 na_child_add_string(service->query, "objectname", "system");
1523                         } else if (service->handler == query_volume_perf_data) {
1524                                 service->query = na_elem_new("perf-object-get-instances");
1525                                 na_child_add_string(service->query, "objectname", "volume");
1526 /*                              e = na_elem_new("instances");
1527                                 na_child_add_string(e, "foo", "system");
1528                                 na_child_add(root, e);*/
1529                                 e = na_elem_new("counters");
1530                                 /* "foo" means: This string has to be here but
1531                                    the content doesn't matter. */
1532                                 na_child_add_string(e, "foo", "read_ops");
1533                                 na_child_add_string(e, "foo", "write_ops");
1534                                 na_child_add_string(e, "foo", "read_data");
1535                                 na_child_add_string(e, "foo", "write_data");
1536                                 na_child_add_string(e, "foo", "read_latency");
1537                                 na_child_add_string(e, "foo", "write_latency");
1538                                 na_child_add(service->query, e);
1539                         } else if (service->handler == query_wafl_data) {
1540                                 service->query = na_elem_new("perf-object-get-instances");
1541                                 na_child_add_string(service->query, "objectname", "wafl");
1542 /*                              e = na_elem_new("instances");
1543                                 na_child_add_string(e, "foo", "system");
1544                                 na_child_add(root, e);*/
1545                                 e = na_elem_new("counters");
1546                                 na_child_add_string(e, "foo", "name_cache_hit");
1547                                 na_child_add_string(e, "foo", "name_cache_miss");
1548                                 na_child_add_string(e, "foo", "find_dir_hit");
1549                                 na_child_add_string(e, "foo", "find_dir_miss");
1550                                 na_child_add_string(e, "foo", "buf_hash_hit");
1551                                 na_child_add_string(e, "foo", "buf_hash_miss");
1552                                 na_child_add_string(e, "foo", "inode_cache_hit");
1553                                 na_child_add_string(e, "foo", "inode_cache_miss");
1554                                 /* na_child_add_string(e, "foo", "inode_eject_time"); */
1555                                 /* na_child_add_string(e, "foo", "buf_eject_time"); */
1556                                 na_child_add(service->query, e);
1557                         } else if (service->handler == query_submit_disk_data) {
1558                                 service->query = na_elem_new("perf-object-get-instances");
1559                                 na_child_add_string(service->query, "objectname", "disk");
1560                                 e = na_elem_new("counters");
1561                                 na_child_add_string(e, "foo", "disk_busy");
1562                                 na_child_add_string(e, "foo", "base_for_disk_busy");
1563                                 na_child_add(service->query, e);
1564                         } else if (service->handler == collect_volume_data) {
1565                                 service->query = na_elem_new("volume-list-info");
1566                                 /* na_child_add_string(service->query, "objectname", "volume"); */
1567                                 /* } else if (service->handler == collect_snapshot_data) { */
1568                                 /* service->query = na_elem_new("snapshot-list-info"); */
1569                         }
1570                 }
1571         }
1572         return 0;
1573 } /* }}} int cna_init */
1574
1575 static int cna_config (oconfig_item_t *ci) { /* {{{ */
1576         int i;
1577         oconfig_item_t *item;
1578         host_config_t default_host = HOST_INIT;
1579         cfg_service_t default_service = SERVICE_INIT;
1580         
1581         for (i = 0; i < ci->children_num; ++i) {
1582                 item = ci->children + i;
1583
1584                 if (!strcasecmp(item->key, "Host")) {
1585                         host_config_t *host;
1586                         host_config_t *tmp;
1587
1588                         host = cna_config_host(item, &default_host, &default_service);
1589                         if (host == NULL)
1590                                 continue;
1591
1592                         for (tmp = global_host_config; tmp != NULL; tmp = tmp->next)
1593                         {
1594                                 if (strcasecmp (host->name, tmp->name) == 0)
1595                                         WARNING ("netapp plugin: Duplicate definition of host `%s'. "
1596                                                         "This is probably a bad idea.",
1597                                                         host->name);
1598
1599                                 if (tmp->next == NULL)
1600                                         break;
1601                         }
1602
1603                         host->next = NULL;
1604                         if (tmp == NULL)
1605                                 global_host_config = host;
1606                         else
1607                                 tmp->next = host;
1608                 } else {
1609                         WARNING("netapp plugin: Ignoring unknown config option \"%s\".", item->key);
1610                 }
1611         }
1612         return 0;
1613 } /* }}} int cna_config */
1614
1615 static int cna_read(void) { /* {{{ */
1616         na_elem_t *out;
1617         host_config_t *host;
1618         cfg_service_t *service;
1619         
1620         for (host = global_host_config; host; host = host->next) {
1621                 for (service = host->services; service; service = service->next) {
1622                         if (--service->skip_countdown > 0) continue;
1623                         service->skip_countdown = service->multiplier;
1624                         out = na_server_invoke_elem(host->srv, service->query);
1625                         if (na_results_status(out) != NA_OK) {
1626                                 int netapp_errno = na_results_errno(out);
1627                                 ERROR("netapp plugin: Error %d from host %s: %s", netapp_errno, host->name, na_results_reason(out));
1628                                 na_elem_free(out);
1629                                 if (netapp_errno == EIO || netapp_errno == ETIMEDOUT) {
1630                                         /* Network problems. Just give up on all other services on this host. */
1631                                         break;
1632                                 }
1633                                 continue;
1634                         }
1635                         service->handler(host, out, service->data);
1636                         na_elem_free(out);
1637                 }
1638         }
1639         return 0;
1640 } /* }}} int cna_read */
1641
1642 void module_register(void) {
1643         plugin_register_complex_config("netapp", cna_config);
1644         plugin_register_init("netapp", cna_init);
1645         plugin_register_read("netapp", cna_read);
1646 }
1647
1648 /* vim: set sw=2 ts=2 noet fdm=marker : */