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