netapp plugin: Split “collect_perf_volume_data” …
[collectd.git] / src / netapp.c
1 /**
2  * collectd - src/netapp.c
3  * Copyright (C) 2009  Sven Trenkel
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Sven Trenkel <sven.trenkel at noris.net>
25  **/
26
27 #include "collectd.h"
28 #include "common.h"
29
30 #include <netapp_api.h>
31
32 #define HAS_ALL_FLAGS(has,needs) (((has) & (needs)) == (needs))
33
34 typedef struct host_config_s host_config_t;
35 typedef void service_handler_t(host_config_t *host, na_elem_t *result, void *data);
36
37 #define PERF_SYSTEM_CPU            0x01
38 #define PERF_SYSTEM_NET            0x02
39 #define PERF_SYSTEM_OPS            0x04
40 #define PERF_SYSTEM_DISK           0x08
41 #define PERF_SYSTEM_ALL            0x0F
42
43 /*!
44  * \brief Persistent data for system performence counters
45  */
46
47 typedef struct {
48         uint32_t flags;
49 } perf_system_data_t;
50
51 /*!
52  * \brief Persistent data for WAFL performence counters. (a.k.a. cache performence)
53  */
54
55 #define PERF_WAFL_NAME_CACHE       0x01
56 #define PERF_WAFL_DIR_CACHE        0x02
57 #define PERF_WAFL_BUF_CACHE        0x04
58 #define PERF_WAFL_INODE_CACHE      0x08
59 #define PERF_WAFL_ALL              0x0F
60
61 typedef struct {
62         uint32_t flags;
63         uint64_t last_name_cache_hit;
64         uint64_t last_name_cache_miss;
65         uint64_t last_find_dir_hit;
66         uint64_t last_find_dir_miss;
67         uint64_t last_buf_hash_hit;
68         uint64_t last_buf_hash_miss;
69         uint64_t last_inode_cache_hit;
70         uint64_t last_inode_cache_miss;
71 } perf_wafl_data_t;
72
73 #define CFG_VOLUME_PERF_INIT           0x0001
74 #define CFG_VOLUME_PERF_IO             0x0002
75 #define CFG_VOLUME_PERF_OPS            0x0003
76 #define CFG_VOLUME_PERF_LATENCY        0x0008
77 #define CFG_VOLUME_PERF_ALL            0x000F
78
79 typedef struct {
80         uint32_t flags;
81 } cfg_volume_perf_t;
82
83 typedef struct {
84         uint32_t flags;
85 } cfg_volume_data_t;
86
87 #define PERF_DISK_BUSIEST          0x01
88 #define PERF_DISK_ALL              0x01
89
90 typedef struct {
91         uint32_t flags;
92 } perf_disk_data_t;
93
94 #define HAVE_VOLUME_PERF_BYTES_READ    0x0010
95 #define HAVE_VOLUME_PERF_BYTES_WRITE   0x0020
96 #define HAVE_VOLUME_PERF_OPS_READ      0x0040
97 #define HAVE_VOLUME_PERF_OPS_WRITE     0x0080
98 #define HAVE_VOLUME_PERF_LATENCY_READ  0x0100
99 #define HAVE_VOLUME_PERF_LATENCY_WRITE 0x0200
100 #define HAVE_VOLUME_PERF_ALL           0x03F0
101 typedef struct {
102         uint32_t flags;
103         time_t timestamp;
104         uint64_t read_bytes;
105         uint64_t write_bytes;
106         uint64_t read_ops;
107         uint64_t write_ops;
108         uint64_t read_latency;
109         uint64_t write_latency;
110 } per_volume_perf_data_t;
111
112 #define VOLUME_INIT           0x01
113 #define VOLUME_DF             0x02
114 #define VOLUME_SNAP           0x04
115
116 typedef struct {
117         uint32_t flags;
118 } per_volume_data_t;
119
120 typedef struct {
121         time_t last_update;
122         double last_disk_busy_percent;
123         uint64_t last_disk_busy;
124         uint64_t last_base_for_disk_busy;
125 } per_disk_perf_data_t;
126
127 typedef struct service_config_s {
128         na_elem_t *query;
129         service_handler_t *handler;
130         int multiplier;
131         int skip_countdown;
132         int interval;
133         void *data;
134         struct service_config_s *next;
135 } service_config_t;
136
137 #define SERVICE_INIT {0, 0, 1, 1, 0, 0, 0}
138
139 typedef struct volume_s {
140         char *name;
141         per_volume_perf_data_t perf_data;
142         per_volume_data_t volume_data;
143         struct volume_s *next;
144 } volume_t;
145
146 /*!
147  * \brief A disk in the netapp.
148  *
149  * A disk doesn't have any more information than its name atm.
150  * The name includes the "disk_" prefix.
151  */
152
153 typedef struct disk_s {
154         char *name;
155         per_disk_perf_data_t perf_data;
156         struct disk_s *next;
157 } disk_t;
158
159 #define DISK_INIT {0, {0, 0, 0, 0}, 0}
160
161 struct host_config_s {
162         na_server_t *srv;
163         char *name;
164         na_server_transport_t protocol;
165         char *host;
166         int port;
167         char *username;
168         char *password;
169         int interval;
170         service_config_t *services;
171         disk_t *disks;
172         volume_t *volumes;
173         struct host_config_s *next;
174 };
175
176 #define HOST_INIT {0, 0, NA_SERVER_TRANSPORT_HTTPS, 0, 0, 0, 0, 10, 0, 0, 0}
177
178 static host_config_t *host_config;
179
180 static volume_t *get_volume (host_config_t *host, const char *name, /* {{{ */
181                 uint32_t vol_data_flags, uint32_t vol_perf_flags)
182 {
183         volume_t *v;
184
185         if (name == NULL)
186                 return (NULL);
187         
188         /* Make sure the default flags include the init-bit. */
189         if (vol_data_flags != 0)
190                 vol_data_flags |= VOLUME_INIT;
191         if (vol_perf_flags != 0)
192                 vol_perf_flags |= CFG_VOLUME_PERF_INIT;
193
194         for (v = host->volumes; v; v = v->next) {
195                 if (strcmp(v->name, name) != 0)
196                         continue;
197
198                 /* Check if the flags have been initialized. */
199                 if (((v->volume_data.flags & VOLUME_INIT) == 0)
200                                 && (vol_data_flags != 0))
201                         v->volume_data.flags = vol_data_flags;
202                 if (((v->perf_data.flags & CFG_VOLUME_PERF_INIT) == 0)
203                                 && (vol_perf_flags != 0))
204                         v->perf_data.flags = vol_perf_flags;
205
206                 return v;
207         }
208
209         DEBUG ("netapp plugin: Allocating new entry for volume %s.", name);
210         v = malloc(sizeof(*v));
211         if (v == NULL)
212                 return (NULL);
213         memset (v, 0, sizeof (*v));
214
215         v->volume_data.flags = vol_data_flags;
216         v->perf_data.flags = vol_perf_flags;
217
218         v->name = strdup(name);
219         if (v->name == NULL) {
220                 sfree (v);
221                 return (NULL);
222         }
223
224         v->next = host->volumes;
225         host->volumes = v;
226
227         return v;
228 } /* }}} volume_t *get_volume */
229
230 static disk_t *get_disk(host_config_t *host, const char *name) /* {{{ */
231 {
232         disk_t *v, init = DISK_INIT;
233
234         if (name == NULL)
235                 return (NULL);
236         
237         for (v = host->disks; v; v = v->next) {
238                 if (strcmp(v->name, name) == 0)
239                         return v;
240         }
241         v = malloc(sizeof(*v));
242         if (v == NULL)
243                 return (NULL);
244
245         *v = init;
246         v->name = strdup(name);
247         if (v->name == NULL) {
248                 sfree (v);
249                 return (NULL);
250         }
251
252         v->next = host->disks;
253         host->disks = v;
254
255         return v;
256 } /* }}} disk_t *get_disk */
257
258 static int submit_values (const char *host, /* {{{ */
259                 const char *plugin_inst,
260                 const char *type, const char *type_inst,
261                 value_t *values, int values_len,
262                 time_t timestamp)
263 {
264         value_list_t vl = VALUE_LIST_INIT;
265
266         vl.values = values;
267         vl.values_len = values_len;
268
269         if (timestamp > 0)
270                 vl.time = timestamp;
271
272         if (host != NULL)
273                 sstrncpy (vl.host, host, sizeof (vl.host));
274         else
275                 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
276         sstrncpy (vl.plugin, "netapp", sizeof (vl.plugin));
277         if (plugin_inst != NULL)
278                 sstrncpy (vl.plugin_instance, plugin_inst, sizeof (vl.plugin_instance));
279         sstrncpy (vl.type, type, sizeof (vl.type));
280         if (type_inst != NULL)
281                 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
282
283         return (plugin_dispatch_values (&vl));
284 } /* }}} int submit_uint64 */
285
286 static int submit_two_counters (const char *host, const char *plugin_inst, /* {{{ */
287                 const char *type, const char *type_inst, counter_t val0, counter_t val1,
288                 time_t timestamp)
289 {
290         value_t values[2];
291
292         values[0].counter = val0;
293         values[1].counter = val1;
294
295         return (submit_values (host, plugin_inst, type, type_inst,
296                                 values, 2, timestamp));
297 } /* }}} int submit_two_counters */
298
299 static int submit_counter (const char *host, const char *plugin_inst, /* {{{ */
300                 const char *type, const char *type_inst, counter_t counter, time_t timestamp)
301 {
302         value_t v;
303
304         v.counter = counter;
305
306         return (submit_values (host, plugin_inst, type, type_inst,
307                                 &v, 1, timestamp));
308 } /* }}} int submit_counter */
309
310 static int submit_two_gauge (const char *host, const char *plugin_inst, /* {{{ */
311                 const char *type, const char *type_inst, gauge_t val0, gauge_t val1,
312                 time_t timestamp)
313 {
314         value_t values[2];
315
316         values[0].gauge = val0;
317         values[1].gauge = val1;
318
319         return (submit_values (host, plugin_inst, type, type_inst,
320                                 values, 2, timestamp));
321 } /* }}} int submit_two_gauge */
322
323 static int submit_double (const char *host, const char *plugin_inst, /* {{{ */
324                 const char *type, const char *type_inst, double d, time_t timestamp)
325 {
326         value_t v;
327
328         v.gauge = (gauge_t) d;
329
330         return (submit_values (host, plugin_inst, type, type_inst,
331                                 &v, 1, timestamp));
332 } /* }}} int submit_uint64 */
333
334 static int submit_cache_ratio (const char *host, /* {{{ */
335                 const char *plugin_inst,
336                 const char *type_inst,
337                 uint64_t new_hits,
338                 uint64_t new_misses,
339                 uint64_t *old_hits,
340                 uint64_t *old_misses,
341                 time_t timestamp)
342 {
343         value_t v;
344
345         if ((new_hits >= (*old_hits)) && (new_misses >= (*old_misses))) {
346                 uint64_t hits;
347                 uint64_t misses;
348
349                 hits = new_hits - (*old_hits);
350                 misses = new_misses - (*old_misses);
351
352                 v.gauge = 100.0 * ((gauge_t) hits) / ((gauge_t) (hits + misses));
353         } else {
354                 v.gauge = NAN;
355         }
356
357         *old_hits = new_hits;
358         *old_misses = new_misses;
359
360         return (submit_values (host, plugin_inst, "cache_ratio", type_inst,
361                                 &v, 1, timestamp));
362 } /* }}} int submit_cache_ratio */
363
364 static int submit_volume_perf_data (const host_config_t *host, /* {{{ */
365                 volume_t *volume,
366                 const per_volume_perf_data_t *new_data)
367 {
368         /* Check for and submit disk-octet values */
369         if (HAS_ALL_FLAGS (volume->perf_data.flags, CFG_VOLUME_PERF_IO)
370                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_BYTES_READ | HAVE_VOLUME_PERF_BYTES_WRITE))
371         {
372                 submit_two_counters (host->name, volume->name, "disk_octets", /* type instance = */ NULL,
373                                 (counter_t) new_data->read_bytes, (counter_t) new_data->write_bytes, new_data->timestamp);
374         }
375
376         /* Check for and submit disk-operations values */
377         if (HAS_ALL_FLAGS (volume->perf_data.flags, CFG_VOLUME_PERF_OPS)
378                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE))
379         {
380                 submit_two_counters (host->name, volume->name, "disk_ops", /* type instance = */ NULL,
381                                 (counter_t) new_data->read_ops, (counter_t) new_data->write_ops, new_data->timestamp);
382         }
383
384         /* Check for, calculate and submit disk-latency values */
385         if (HAS_ALL_FLAGS (volume->perf_data.flags, CFG_VOLUME_PERF_LATENCY
386                                 | HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
387                                 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE)
388                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
389                                 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE))
390         {
391                 gauge_t latency_per_op_read;
392                 gauge_t latency_per_op_write;
393
394                 latency_per_op_read = NAN;
395                 latency_per_op_write = NAN;
396
397                 /* Check if a counter wrapped around. */
398                 if ((new_data->read_ops > volume->perf_data.read_ops)
399                                 && (new_data->read_latency > volume->perf_data.read_latency))
400                 {
401                         uint64_t diff_ops_read;
402                         uint64_t diff_latency_read;
403
404                         diff_ops_read = new_data->read_ops - volume->perf_data.read_ops;
405                         diff_latency_read = new_data->read_latency - volume->perf_data.read_latency;
406
407                         if (diff_ops_read > 0)
408                                 latency_per_op_read = ((gauge_t) diff_latency_read) / ((gauge_t) diff_ops_read);
409                 }
410
411                 if ((new_data->write_ops > volume->perf_data.write_ops)
412                                 && (new_data->write_latency > volume->perf_data.write_latency))
413                 {
414                         uint64_t diff_ops_write;
415                         uint64_t diff_latency_write;
416
417                         diff_ops_write = new_data->write_ops - volume->perf_data.write_ops;
418                         diff_latency_write = new_data->write_latency - volume->perf_data.write_latency;
419
420                         if (diff_ops_write > 0)
421                                 latency_per_op_write = ((gauge_t) diff_latency_write) / ((gauge_t) diff_ops_write);
422                 }
423
424                 submit_two_gauge (host->name, volume->name, "disk_latency", /* type instance = */ NULL,
425                                 latency_per_op_read, latency_per_op_write, new_data->timestamp);
426         }
427
428         /* Clear all HAVE_* flags. */
429         volume->perf_data.flags &= ~HAVE_VOLUME_PERF_ALL;
430
431         /* Copy all counters */
432         volume->perf_data.timestamp = new_data->timestamp;
433         volume->perf_data.read_bytes = new_data->read_bytes;
434         volume->perf_data.write_bytes = new_data->write_bytes;
435         volume->perf_data.read_ops = new_data->read_ops;
436         volume->perf_data.write_ops = new_data->write_ops;
437         volume->perf_data.read_latency = new_data->read_latency;
438         volume->perf_data.write_latency = new_data->write_latency;
439
440         /* Copy the HAVE_* flags */
441         volume->perf_data.flags |= (new_data->flags & HAVE_VOLUME_PERF_ALL);
442
443         return (0);
444 } /* }}} int submit_volume_perf_data */
445
446 static void collect_perf_wafl_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
447         perf_wafl_data_t *wafl = data;
448         uint64_t name_cache_hit  = 0,  name_cache_miss  = 0;
449         uint64_t find_dir_hit    = 0,  find_dir_miss    = 0;
450         uint64_t buf_hash_hit    = 0,  buf_hash_miss    = 0;
451         uint64_t inode_cache_hit = 0,  inode_cache_miss = 0;
452         const char *plugin_inst;
453         time_t timestamp;
454         na_elem_t *counter;
455         
456         timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
457         out = na_elem_child(na_elem_child(out, "instances"), "instance-data");
458         plugin_inst = na_child_get_string(out, "name");
459
460         /* Iterate over all counters */
461         na_elem_iter_t iter = na_child_iterator(na_elem_child(out, "counters"));
462         for (counter = na_iterator_next(&iter); counter; counter = na_iterator_next(&iter)) {
463                 const char *name;
464
465                 name = na_child_get_string(counter, "name");
466                 if (!strcmp(name, "name_cache_hit"))
467                         name_cache_hit = na_child_get_uint64(counter, "value", UINT64_MAX);
468                 else if (!strcmp(name, "name_cache_miss"))
469                         name_cache_miss = na_child_get_uint64(counter, "value", UINT64_MAX);
470                 else if (!strcmp(name, "find_dir_hit"))
471                         find_dir_hit = na_child_get_uint64(counter, "value", UINT64_MAX);
472                 else if (!strcmp(name, "find_dir_miss"))
473                         find_dir_miss = na_child_get_uint64(counter, "value", UINT64_MAX);
474                 else if (!strcmp(name, "buf_hash_hit"))
475                         buf_hash_hit = na_child_get_uint64(counter, "value", UINT64_MAX);
476                 else if (!strcmp(name, "buf_hash_miss"))
477                         buf_hash_miss = na_child_get_uint64(counter, "value", UINT64_MAX);
478                 else if (!strcmp(name, "inode_cache_hit"))
479                         inode_cache_hit = na_child_get_uint64(counter, "value", UINT64_MAX);
480                 else if (!strcmp(name, "inode_cache_miss"))
481                         inode_cache_miss = na_child_get_uint64(counter, "value", UINT64_MAX);
482                 else
483                         DEBUG("netapp plugin: Found unexpected child: %s", name);
484         }
485
486         /* Submit requested counters */
487         if ((wafl->flags & PERF_WAFL_NAME_CACHE)
488                         && (name_cache_hit != UINT64_MAX) && (name_cache_miss != UINT64_MAX))
489                 submit_cache_ratio (host->name, plugin_inst, "name_cache_hit",
490                                 name_cache_hit, name_cache_miss,
491                                 &wafl->last_name_cache_hit, &wafl->last_name_cache_miss,
492                                 timestamp);
493
494         if ((wafl->flags & PERF_WAFL_DIR_CACHE)
495                         && (find_dir_hit != UINT64_MAX) && (find_dir_miss != UINT64_MAX))
496                 submit_cache_ratio (host->name, plugin_inst, "find_dir_hit",
497                                 find_dir_hit, find_dir_miss,
498                                 &wafl->last_find_dir_hit, &wafl->last_find_dir_miss,
499                                 timestamp);
500
501         if ((wafl->flags & PERF_WAFL_BUF_CACHE)
502                         && (buf_hash_hit != UINT64_MAX) && (buf_hash_miss != UINT64_MAX))
503                 submit_cache_ratio (host->name, plugin_inst, "buf_hash_hit",
504                                 buf_hash_hit, buf_hash_miss,
505                                 &wafl->last_buf_hash_hit, &wafl->last_buf_hash_miss,
506                                 timestamp);
507
508         if ((wafl->flags & PERF_WAFL_INODE_CACHE)
509                         && (inode_cache_hit != UINT64_MAX) && (inode_cache_miss != UINT64_MAX))
510                 submit_cache_ratio (host->name, plugin_inst, "inode_cache_hit",
511                                 inode_cache_hit, inode_cache_miss,
512                                 &wafl->last_inode_cache_hit, &wafl->last_inode_cache_miss,
513                                 timestamp);
514 } /* }}} void collect_perf_wafl_data */
515
516 static void collect_perf_disk_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
517         perf_disk_data_t *perf = data;
518         const char *name;
519         time_t timestamp;
520         na_elem_t *counter, *inst;
521         disk_t *disk, *worst_disk = 0;
522         
523         timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
524         out = na_elem_child(out, "instances");
525
526         /* Iterate over all children */
527         na_elem_iter_t inst_iter = na_child_iterator(out);
528         for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
529                 uint64_t disk_busy = 0;
530                 uint64_t base_for_disk_busy = 0;
531
532                 disk = get_disk(host, na_child_get_string(inst, "name"));
533                 if (disk == NULL)
534                         continue;
535
536                 /* Look for the "disk_busy" and "base_for_disk_busy" counters */
537                 na_elem_iter_t count_iter = na_child_iterator(na_elem_child(inst, "counters"));
538                 for (counter = na_iterator_next(&count_iter); counter; counter = na_iterator_next(&count_iter)) {
539                         name = na_child_get_string(counter, "name");
540                         if (name == NULL)
541                                 continue;
542
543                         if (strcmp(name, "disk_busy") == 0)
544                                 disk_busy = na_child_get_uint64(counter, "value", UINT64_MAX);
545                         else if (strcmp(name, "base_for_disk_busy") == 0)
546                                 base_for_disk_busy = na_child_get_uint64(counter, "value", UINT64_MAX);
547                 }
548
549                 if ((disk_busy == UINT64_MAX) || (base_for_disk_busy == UINT64_MAX))
550                 {
551                         disk->perf_data.last_disk_busy = 0;
552                         disk->perf_data.last_base_for_disk_busy = 0;
553                         continue;
554                 }
555
556                 disk->perf_data.last_update = timestamp;
557                 if ((disk_busy >= disk->perf_data.last_disk_busy)
558                                 && (base_for_disk_busy >= disk->perf_data.last_base_for_disk_busy))
559                 {
560                         uint64_t disk_busy_diff;
561                         uint64_t base_diff;
562
563                         disk_busy_diff = disk_busy - disk->perf_data.last_disk_busy;
564                         base_diff = base_for_disk_busy - disk->perf_data.last_base_for_disk_busy;
565
566                         if (base_diff == 0)
567                                 disk->perf_data.last_disk_busy_percent = NAN;
568                         else
569                                 disk->perf_data.last_disk_busy_percent = 100.0
570                                         * ((gauge_t) disk_busy_diff) / ((gauge_t) base_diff);
571                 }
572                 else
573                 {
574                         disk->perf_data.last_disk_busy_percent = NAN;
575                 }
576
577                 disk->perf_data.last_disk_busy = disk_busy;
578                 disk->perf_data.last_base_for_disk_busy = base_for_disk_busy;
579
580                 if ((worst_disk == NULL)
581                                 || (worst_disk->perf_data.last_disk_busy_percent < disk->perf_data.last_disk_busy_percent))
582                         worst_disk = disk;
583         }
584
585         if ((perf->flags & PERF_DISK_BUSIEST) && (worst_disk != NULL))
586                 submit_double (host->name, "system", "percent", "disk_busy",
587                                 worst_disk->perf_data.last_disk_busy_percent, timestamp);
588 } /* }}} void collect_perf_disk_data */
589
590 static void collect_volume_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
591         na_elem_t *inst;
592         volume_t *volume;
593         cfg_volume_data_t *cfg_volume_data = data;
594
595         out = na_elem_child(out, "volumes");
596         na_elem_iter_t inst_iter = na_child_iterator(out);
597         for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
598                 uint64_t size_free = 0, size_used = 0, snap_reserved = 0;
599
600                 na_elem_t *sis;
601                 const char *sis_state;
602                 uint64_t sis_saved_reported;
603                 uint64_t sis_saved;
604
605                 volume = get_volume(host, na_child_get_string(inst, "name"),
606                                 cfg_volume_data->flags, /* perf_flags = */ 0);
607                 if (volume == NULL)
608                         continue;
609
610                 if (!(volume->volume_data.flags & VOLUME_DF))
611                         continue;
612
613                 /* 2^4 exa-bytes? This will take a while ;) */
614                 size_free = na_child_get_uint64(inst, "size-available", UINT64_MAX);
615                 if (size_free != UINT64_MAX)
616                         submit_double (host->name, volume->name, "df_complex", "used",
617                                         (double) size_used, /* time = */ 0);
618
619                 size_used = na_child_get_uint64(inst, "size-used", UINT64_MAX);
620                 if (size_free != UINT64_MAX)
621                         submit_double (host->name, volume->name, "df_complex", "free",
622                                         (double) size_free, /* time = */ 0);
623
624                 snap_reserved = na_child_get_uint64(inst, "snapshot-blocks-reserved", UINT64_MAX);
625                 if (snap_reserved != UINT64_MAX)
626                         /* 1 block == 1024 bytes  as per API docs */
627                         submit_double (host->name, volume->name, "df_complex", "snap_reserved",
628                                         (double) (1024 * snap_reserved), /* time = */ 0);
629
630                 sis = na_elem_child(inst, "sis");
631                 if (sis == NULL)
632                         continue;
633
634                 sis_state = na_child_get_string(sis, "state");
635                 if ((sis_state == NULL)
636                                 || (strcmp ("enabled", sis_state) != 0))
637                         continue;
638
639                 sis_saved_reported = na_child_get_uint64(sis, "size-saved", UINT64_MAX);
640                 if (sis_saved_reported == UINT64_MAX)
641                         continue;
642
643                 /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
644                 if ((sis_saved_reported >> 32) != 0) {
645                         /* In case they ever fix this bug. */
646                         sis_saved = sis_saved_reported;
647                 } else {
648                         uint64_t sis_saved_percent;
649                         uint64_t sis_saved_guess;
650                         uint64_t overflow_guess;
651                         uint64_t guess1, guess2, guess3;
652
653                         sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
654                         if (sis_saved_percent > 100)
655                                 continue;
656
657                         /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
658                          * will hopefully be fixed in later versions. To work around the bug, try
659                          * to figure out how often the 32bit integer wrapped around by using the
660                          * "percentage-saved" value. Because the percentage is in the range
661                          * [0-100], this should work as long as the saved space does not exceed
662                          * 400 GBytes. */
663                         /* percentage-saved = size-saved / (size-saved + size-used) */
664                         if (sis_saved_percent < 100)
665                                 sis_saved_guess = size_used * sis_saved_percent / (100 - sis_saved_percent);
666                         else
667                                 sis_saved_guess = size_used;
668
669                         overflow_guess = sis_saved_guess >> 32;
670                         guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
671                         guess2 = (overflow_guess << 32) + sis_saved_reported;
672                         guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
673
674                         if (sis_saved_guess < guess2) {
675                                 if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
676                                         sis_saved = guess1;
677                                 else
678                                         sis_saved = guess2;
679                         } else {
680                                 if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
681                                         sis_saved = guess2;
682                                 else
683                                         sis_saved = guess3;
684                         }
685                 } /* end of 32-bit workaround */
686
687                 submit_double (host->name, volume->name, "df_complex", "sis_saved",
688                                 (double) sis_saved, /* time = */ 0);
689         }
690 } /* }}} void collect_volume_data */
691
692 static void query_volume_perf_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
693         cfg_volume_perf_t *perf = data;
694         time_t timestamp;
695         na_elem_t *counter, *inst;
696         
697         timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
698
699         out = na_elem_child(out, "instances");
700         na_elem_iter_t inst_iter = na_child_iterator(out);
701         for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
702                 per_volume_perf_data_t perf_data;
703                 volume_t *volume;
704
705                 memset (&perf_data, 0, sizeof (perf_data));
706                 perf_data.timestamp = timestamp;
707
708                 volume = get_volume(host, na_child_get_string(inst, "name"),
709                                 /* data_flags = */ 0, perf->flags);
710                 if (volume == NULL)
711                         continue;
712
713                 na_elem_iter_t count_iter = na_child_iterator(na_elem_child(inst, "counters"));
714                 for (counter = na_iterator_next(&count_iter); counter; counter = na_iterator_next(&count_iter)) {
715                         const char *name;
716                         uint64_t value;
717
718                         name = na_child_get_string(counter, "name");
719                         if (name == NULL)
720                                 continue;
721
722                         value = na_child_get_uint64(counter, "value", UINT64_MAX);
723                         if (value == UINT64_MAX)
724                                 continue;
725
726                         if (!strcmp(name, "read_data")) {
727                                 perf_data.read_bytes = value;
728                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_READ;
729                         } else if (!strcmp(name, "write_data")) {
730                                 perf_data.write_bytes = value;
731                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_WRITE;
732                         } else if (!strcmp(name, "read_ops")) {
733                                 perf_data.read_ops = value;
734                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_READ;
735                         } else if (!strcmp(name, "write_ops")) {
736                                 perf_data.write_ops = value;
737                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_WRITE;
738                         } else if (!strcmp(name, "read_latency")) {
739                                 perf_data.read_latency = value;
740                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_READ;
741                         } else if (!strcmp(name, "write_latency")) {
742                                 perf_data.write_latency = value;
743                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_WRITE;
744                         }
745                 }
746
747                 submit_volume_perf_data (host, volume, &perf_data);
748         } /* for (volume) */
749 } /* }}} void query_volume_perf_data */
750
751 static void collect_perf_system_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
752         counter_t disk_read = 0, disk_written = 0;
753         counter_t net_recv = 0, net_sent = 0;
754         counter_t cpu_busy = 0, cpu_total = 0;
755         unsigned int counter_flags = 0;
756
757         perf_system_data_t *perf = data;
758         const char *instance;
759         time_t timestamp;
760         na_elem_t *counter;
761         
762         timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
763         out = na_elem_child(na_elem_child(out, "instances"), "instance-data");
764         instance = na_child_get_string(out, "name");
765
766         na_elem_iter_t iter = na_child_iterator(na_elem_child(out, "counters"));
767         for (counter = na_iterator_next(&iter); counter; counter = na_iterator_next(&iter)) {
768                 const char *name;
769                 uint64_t value;
770
771                 name = na_child_get_string(counter, "name");
772                 if (name == NULL)
773                         continue;
774
775                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
776                 if (value == UINT64_MAX)
777                         continue;
778
779                 if (!strcmp(name, "disk_data_read")) {
780                         disk_read = (counter_t) (value * 1024);
781                         counter_flags |= 0x01;
782                 } else if (!strcmp(name, "disk_data_written")) {
783                         disk_written = (counter_t) (value * 1024);
784                         counter_flags |= 0x02;
785                 } else if (!strcmp(name, "net_data_recv")) {
786                         net_recv = (counter_t) (value * 1024);
787                         counter_flags |= 0x04;
788                 } else if (!strcmp(name, "net_data_sent")) {
789                         net_sent = (counter_t) (value * 1024);
790                         counter_flags |= 0x08;
791                 } else if (!strcmp(name, "cpu_busy")) {
792                         cpu_busy = (counter_t) value;
793                         counter_flags |= 0x10;
794                 } else if (!strcmp(name, "cpu_elapsed_time")) {
795                         cpu_total = (counter_t) value;
796                         counter_flags |= 0x20;
797                 } else if ((perf->flags & PERF_SYSTEM_OPS)
798                                 && (strlen(name) > 4)
799                                 && (!strcmp(name + strlen(name) - 4, "_ops"))) {
800                         submit_counter (host->name, instance, "disk_ops_complex", name,
801                                         (counter_t) value, timestamp);
802                 }
803         } /* for (counter) */
804
805         if ((perf->flags & PERF_SYSTEM_DISK)
806                         && ((counter_flags & 0x03) == 0x03))
807                 submit_two_counters (host->name, instance, "disk_octets", NULL,
808                                 disk_read, disk_written, timestamp);
809                                 
810         if ((perf->flags & PERF_SYSTEM_NET)
811                         && ((counter_flags & 0x0c) == 0x0c))
812                 submit_two_counters (host->name, instance, "if_octets", NULL,
813                                 net_recv, net_sent, timestamp);
814
815         if ((perf->flags & PERF_SYSTEM_CPU)
816                         && ((counter_flags & 0x30) == 0x30)) {
817                 submit_counter (host->name, instance, "cpu", "system",
818                                 cpu_busy, timestamp);
819                 submit_counter (host->name, instance, "cpu", "idle",
820                                 cpu_total - cpu_busy, timestamp);
821         }
822 } /* }}} void collect_perf_system_data */
823
824 static int config_init(void) { /* {{{ */
825         char err[256];
826         na_elem_t *e;
827         host_config_t *host;
828         service_config_t *service;
829         
830         if (!host_config) {
831                 WARNING("netapp plugin: Plugin loaded but no hosts defined.");
832                 return 1;
833         }
834
835         if (!na_startup(err, sizeof(err))) {
836                 ERROR("netapp plugin: Error initializing netapp API: %s", err);
837                 return 1;
838         }
839
840         for (host = host_config; host; host = host->next) {
841                 host->srv = na_server_open(host->host, 1, 1); 
842                 na_server_set_transport_type(host->srv, host->protocol, 0);
843                 na_server_set_port(host->srv, host->port);
844                 na_server_style(host->srv, NA_STYLE_LOGIN_PASSWORD);
845                 na_server_adminuser(host->srv, host->username, host->password);
846                 na_server_set_timeout(host->srv, 5);
847                 for (service = host->services; service; service = service->next) {
848                         service->interval = host->interval * service->multiplier;
849                         if (service->handler == collect_perf_system_data) {
850                                 service->query = na_elem_new("perf-object-get-instances");
851                                 na_child_add_string(service->query, "objectname", "system");
852                         } else if (service->handler == query_volume_perf_data) {
853                                 service->query = na_elem_new("perf-object-get-instances");
854                                 na_child_add_string(service->query, "objectname", "volume");
855 /*                              e = na_elem_new("instances");
856                                 na_child_add_string(e, "foo", "system");
857                                 na_child_add(root, e);*/
858                                 e = na_elem_new("counters");
859                                 na_child_add_string(e, "foo", "read_ops");
860                                 na_child_add_string(e, "foo", "write_ops");
861                                 na_child_add_string(e, "foo", "read_data");
862                                 na_child_add_string(e, "foo", "write_data");
863                                 na_child_add_string(e, "foo", "read_latency");
864                                 na_child_add_string(e, "foo", "write_latency");
865                                 na_child_add(service->query, e);
866                         } else if (service->handler == collect_perf_wafl_data) {
867                                 service->query = na_elem_new("perf-object-get-instances");
868                                 na_child_add_string(service->query, "objectname", "wafl");
869 /*                              e = na_elem_new("instances");
870                                 na_child_add_string(e, "foo", "system");
871                                 na_child_add(root, e);*/
872                                 e = na_elem_new("counters");
873                                 na_child_add_string(e, "foo", "name_cache_hit");
874                                 na_child_add_string(e, "foo", "name_cache_miss");
875                                 na_child_add_string(e, "foo", "find_dir_hit");
876                                 na_child_add_string(e, "foo", "find_dir_miss");
877                                 na_child_add_string(e, "foo", "buf_hash_hit");
878                                 na_child_add_string(e, "foo", "buf_hash_miss");
879                                 na_child_add_string(e, "foo", "inode_cache_hit");
880                                 na_child_add_string(e, "foo", "inode_cache_miss");
881                                 /* na_child_add_string(e, "foo", "inode_eject_time"); */
882                                 /* na_child_add_string(e, "foo", "buf_eject_time"); */
883                                 na_child_add(service->query, e);
884                         } else if (service->handler == collect_perf_disk_data) {
885                                 service->query = na_elem_new("perf-object-get-instances");
886                                 na_child_add_string(service->query, "objectname", "disk");
887                                 e = na_elem_new("counters");
888                                 na_child_add_string(e, "foo", "disk_busy");
889                                 na_child_add_string(e, "foo", "base_for_disk_busy");
890                                 na_child_add(service->query, e);
891                         } else if (service->handler == collect_volume_data) {
892                                 service->query = na_elem_new("volume-list-info");
893                                 /* na_child_add_string(service->query, "objectname", "volume"); */
894                                 /* } else if (service->handler == collect_snapshot_data) { */
895                                 /* service->query = na_elem_new("snapshot-list-info"); */
896                         }
897                 }
898         }
899         return 0;
900 } /* }}} int config_init */
901
902 static int config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */
903                 uint32_t *flags, uint32_t flag)
904 {
905         if ((ci == NULL) || (flags == NULL))
906                 return (EINVAL);
907
908         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
909         {
910                 WARNING ("netapp plugin: The %s option needs exactly one boolean argument.",
911                                 ci->key);
912                 return (-1);
913         }
914
915         if (ci->values[0].value.boolean)
916                 *flags |= flag;
917         else
918                 *flags &= ~flag;
919
920         return (0);
921 } /* }}} int config_bool_to_flag */
922
923 static int config_get_multiplier (const oconfig_item_t *ci, /* {{{ */
924                 service_config_t *service)
925 {
926         int tmp;
927
928         if ((ci == NULL) || (service == NULL))
929                 return (EINVAL);
930
931         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
932         {
933                 WARNING ("netapp plugin: The `Multiplier' option needs exactly one numeric argument.");
934                 return (-1);
935         }
936
937         tmp = (int) (ci->values[0].value.number + .5);
938         if (tmp < 1)
939         {
940                 WARNING ("netapp plugin: The `Multiplier' option needs a positive integer argument.");
941                 return (-1);
942         }
943
944         service->multiplier = tmp;
945         service->skip_countdown = tmp;
946
947         return (0);
948 } /* }}} int config_get_multiplier */
949
950 static void set_global_perf_vol_flag(const host_config_t *host, /* {{{ */
951                 uint32_t flag, _Bool set)
952 {
953         volume_t *v;
954         
955         for (v = host->volumes; v; v = v->next) {
956                 if (set)
957                         v->perf_data.flags |= flag;
958                 else /* if (!set) */
959                         v->perf_data.flags &= ~flag;
960         }
961 } /* }}} void set_global_perf_vol_flag */
962
963 static void set_global_vol_flag(const host_config_t *host, /* {{{ */
964                 uint32_t flag, _Bool set) {
965         volume_t *v;
966         
967         for (v = host->volumes; v; v = v->next) {
968                 if (set)
969                         v->volume_data.flags |= flag;
970                 else /* if (!set) */
971                         v->volume_data.flags &= ~flag;
972         }
973 } /* }}} void set_global_vol_flag */
974
975 static void process_perf_volume_flag (host_config_t *host, /* {{{ */
976                 cfg_volume_perf_t *perf_volume, const oconfig_item_t *item,
977                 uint32_t flag)
978 {
979         int i;
980         
981         for (i = 0; i < item->values_num; ++i) {
982                 const char *name;
983                 volume_t *v;
984                 _Bool set = true;
985
986                 if (item->values[i].type != OCONFIG_TYPE_STRING) {
987                         WARNING("netapp plugin: Ignoring non-string argument in "
988                                         "\"GetVolumePerfData\" block for host %s", host->name);
989                         continue;
990                 }
991
992                 name = item->values[i].value.string;
993                 if (name[0] == '+') {
994                         set = true;
995                         ++name;
996                 } else if (name[0] == '-') {
997                         set = false;
998                         ++name;
999                 }
1000
1001                 if (!name[0]) {
1002                         if (set)
1003                                 perf_volume->flags |= flag;
1004                         else /* if (!set) */
1005                                 perf_volume->flags &= ~flag;
1006
1007                         set_global_perf_vol_flag(host, flag, set);
1008                         continue;
1009                 }
1010
1011                 v = get_volume (host, name, /* data_flags = */ 0, perf_volume->flags);
1012                 if (v == NULL)
1013                         continue;
1014
1015                 if (set)
1016                         v->perf_data.flags |= flag;
1017                 else /* if (!set) */
1018                         v->perf_data.flags &= ~flag;
1019         } /* for (i = 0 .. item->values_num) */
1020 } /* }}} void process_perf_volume_flag */
1021
1022 static void process_volume_flag (host_config_t *host, /* {{{ */
1023                 cfg_volume_data_t *cfg_volume_data, const oconfig_item_t *item, uint32_t flag)
1024 {
1025         int i;
1026         
1027         for (i = 0; i < item->values_num; ++i) {
1028                 const char *name;
1029                 volume_t *v;
1030                 _Bool set = true;
1031
1032                 if (item->values[i].type != OCONFIG_TYPE_STRING) {
1033                         WARNING("netapp plugin: Ignoring non-string argument in \"GetVolData\""
1034                                         "block for host %s", host->name);
1035                         continue;
1036                 }
1037
1038                 name = item->values[i].value.string;
1039                 if (name[0] == '+') {
1040                         set = true;
1041                         ++name;
1042                 } else if (name[0] == '-') {
1043                         set = false;
1044                         ++name;
1045                 }
1046
1047                 if (!name[0]) {
1048                         if (set)
1049                                 cfg_volume_data->flags |= flag;
1050                         else /* if (!set) */
1051                                 cfg_volume_data->flags &= ~flag;
1052
1053                         set_global_vol_flag(host, flag, set);
1054                         continue;
1055                 }
1056
1057                 v = get_volume(host, name, cfg_volume_data->flags, /* perf_flags = */ 0);
1058                 if (v == NULL)
1059                         continue;
1060
1061                 if (!v->volume_data.flags)
1062                         v->volume_data.flags = cfg_volume_data->flags;
1063
1064                 if (set)
1065                         v->volume_data.flags |= flag;
1066                 else /* if (!set) */
1067                         v->volume_data.flags &= ~flag;
1068         }
1069 } /* }}} void process_volume_flag */
1070
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         service_config_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 }
1113
1114 static void build_volume_config(host_config_t *host, oconfig_item_t *ci) {
1115         int i, had_df = 0;
1116         service_config_t *service;
1117         cfg_volume_data_t *cfg_volume_data;
1118         
1119         service = malloc(sizeof(*service));
1120         service->query = 0;
1121         service->handler = collect_volume_data;
1122         cfg_volume_data = service->data = malloc(sizeof(*cfg_volume_data));
1123         cfg_volume_data->flags = VOLUME_INIT;
1124         service->next = host->services;
1125         host->services = service;
1126         for (i = 0; i < ci->children_num; ++i) {
1127                 oconfig_item_t *item = ci->children + i;
1128                 
1129                 /* if (!item || !item->key || !*item->key) continue; */
1130                 if (!strcasecmp(item->key, "Multiplier")) {
1131                         config_get_multiplier (item, service);
1132                 } else if (!strcasecmp(item->key, "GetDiskUtil")) {
1133                         had_df = 1;
1134                         process_volume_flag(host, cfg_volume_data, item, VOLUME_DF);
1135                 }
1136         }
1137         if (!had_df) {
1138                 cfg_volume_data->flags |= VOLUME_DF;
1139                 set_global_vol_flag(host, VOLUME_DF, /* set = */ true);
1140         }
1141 }
1142
1143 static void build_perf_disk_config(host_config_t *temp, oconfig_item_t *ci) {
1144         int i;
1145         service_config_t *service;
1146         perf_disk_data_t *perf_disk;
1147         
1148         service = malloc(sizeof(*service));
1149         service->query = 0;
1150         service->handler = collect_perf_disk_data;
1151         perf_disk = service->data = malloc(sizeof(*perf_disk));
1152         perf_disk->flags = PERF_DISK_ALL;
1153         service->next = temp->services;
1154         temp->services = service;
1155         for (i = 0; i < ci->children_num; ++i) {
1156                 oconfig_item_t *item = ci->children + i;
1157                 
1158                 /* if (!item || !item->key || !*item->key) continue; */
1159                 if (!strcasecmp(item->key, "Multiplier")) {
1160                         config_get_multiplier (item, service);
1161                 } else if (!strcasecmp(item->key, "GetBusy")) {
1162                         config_bool_to_flag (item, &perf_disk->flags, PERF_SYSTEM_CPU);
1163                 }
1164         }
1165 }
1166
1167 static void build_perf_wafl_config(host_config_t *temp, oconfig_item_t *ci) {
1168         int i;
1169         service_config_t *service;
1170         perf_wafl_data_t *perf_wafl;
1171         
1172         service = malloc(sizeof(*service));
1173         service->query = 0;
1174         service->handler = collect_perf_wafl_data;
1175         perf_wafl = service->data = malloc(sizeof(*perf_wafl));
1176         perf_wafl->flags = PERF_WAFL_ALL;
1177         perf_wafl->last_name_cache_hit = 0;
1178         perf_wafl->last_name_cache_miss = 0;
1179         perf_wafl->last_find_dir_hit = 0;
1180         perf_wafl->last_find_dir_miss = 0;
1181         perf_wafl->last_buf_hash_hit = 0;
1182         perf_wafl->last_buf_hash_miss = 0;
1183         perf_wafl->last_inode_cache_hit = 0;
1184         perf_wafl->last_inode_cache_miss = 0;
1185         service->next = temp->services;
1186         temp->services = service;
1187         for (i = 0; i < ci->children_num; ++i) {
1188                 oconfig_item_t *item = ci->children + i;
1189                 
1190                 /* if (!item || !item->key || !*item->key) continue; */
1191                 if (!strcasecmp(item->key, "Multiplier")) {
1192                         config_get_multiplier (item, service);
1193                 } else if (!strcasecmp(item->key, "GetNameCache")) {
1194                         config_bool_to_flag (item, &perf_wafl->flags, PERF_WAFL_NAME_CACHE);
1195                 } else if (!strcasecmp(item->key, "GetDirCache")) {
1196                         config_bool_to_flag (item, &perf_wafl->flags, PERF_WAFL_DIR_CACHE);
1197                 } else if (!strcasecmp(item->key, "GetBufCache")) {
1198                         config_bool_to_flag (item, &perf_wafl->flags, PERF_WAFL_BUF_CACHE);
1199                 } else if (!strcasecmp(item->key, "GetInodeCache")) {
1200                         config_bool_to_flag (item, &perf_wafl->flags, PERF_WAFL_INODE_CACHE);
1201                 } else {
1202                         WARNING ("netapp plugin: The %s config option is not allowed within "
1203                                         "`GetWaflPerfData' blocks.", item->key);
1204                 }
1205         }
1206 }
1207
1208 static int build_perf_sys_config (host_config_t *host, /* {{{ */
1209                 oconfig_item_t *ci, const service_config_t *default_service)
1210 {
1211         int i;
1212         service_config_t *service;
1213         perf_system_data_t *perf_system;
1214         
1215         service = malloc(sizeof(*service));
1216         if (service == NULL)
1217                 return (-1);
1218         memset (service, 0, sizeof (*service));
1219         *service = *default_service;
1220         service->handler = collect_perf_system_data;
1221
1222         perf_system = malloc(sizeof(*perf_system));
1223         if (perf_system == NULL) {
1224                 sfree (service);
1225                 return (-1);
1226         }
1227         memset (perf_system, 0, sizeof (*perf_system));
1228         perf_system->flags = PERF_SYSTEM_ALL;
1229         service->data = perf_system;
1230
1231         for (i = 0; i < ci->children_num; ++i) {
1232                 oconfig_item_t *item = ci->children + i;
1233
1234                 if (!strcasecmp(item->key, "Multiplier")) {
1235                         config_get_multiplier (item, service);
1236                 } else if (!strcasecmp(item->key, "GetCPULoad")) {
1237                         config_bool_to_flag (item, &perf_system->flags, PERF_SYSTEM_CPU);
1238                 } else if (!strcasecmp(item->key, "GetInterfaces")) {
1239                         config_bool_to_flag (item, &perf_system->flags, PERF_SYSTEM_NET);
1240                 } else if (!strcasecmp(item->key, "GetDiskOps")) {
1241                         config_bool_to_flag (item, &perf_system->flags, PERF_SYSTEM_OPS);
1242                 } else if (!strcasecmp(item->key, "GetDiskIO")) {
1243                         config_bool_to_flag (item, &perf_system->flags, PERF_SYSTEM_DISK);
1244                 } else {
1245                         WARNING ("netapp plugin: The %s config option is not allowed within "
1246                                         "`GetSystemPerfData' blocks.", item->key);
1247                 }
1248         }
1249
1250         service->next = host->services;
1251         host->services = service;
1252
1253         return (0);
1254 } /* }}} int build_perf_sys_config */
1255
1256 static host_config_t *build_host_config(const oconfig_item_t *ci, const host_config_t *default_host, const service_config_t *def_def_service) {
1257         int i;
1258         oconfig_item_t *item;
1259         host_config_t *host, *hc, temp = *default_host;
1260         service_config_t default_service = *def_def_service;
1261         
1262         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1263                 WARNING("netapp plugin: \"Host\" needs exactly one string argument. Ignoring host block.");
1264                 return 0;
1265         }
1266
1267         temp.name = ci->values[0].value.string;
1268         for (i = 0; i < ci->children_num; ++i) {
1269                 item = ci->children + i;
1270
1271                 /* if (!item || !item->key || !*item->key) continue; */
1272                 if (!strcasecmp(item->key, "Address")) {
1273                         if ((item->values_num != 1) || (item->values[0].type != OCONFIG_TYPE_STRING)) {
1274                                 WARNING("netapp plugin: \"Name\" needs exactly one string argument. Ignoring host block \"%s\".", ci->values[0].value.string);
1275                                 return 0;
1276                         }
1277                         temp.host = item->values[0].value.string;
1278                 } else if (!strcasecmp(item->key, "Port")) {
1279                         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)) {
1280                                 WARNING("netapp plugin: \"Port\" needs exactly one integer argument in the range of 1-65535. Ignoring host block \"%s\".", ci->values[0].value.string);
1281                                 return 0;
1282                         }
1283                         temp.port = item->values[0].value.number;
1284                 } else if (!strcasecmp(item->key, "Protocol")) {
1285                         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"))) {
1286                                 WARNING("netapp plugin: \"Protocol\" needs to be either \"http\" or \"https\". Ignoring host block \"%s\".", ci->values[0].value.string);
1287                                 return 0;
1288                         }
1289                         if (!strcasecmp(item->values[0].value.string, "http")) temp.protocol = NA_SERVER_TRANSPORT_HTTP;
1290                         else temp.protocol = NA_SERVER_TRANSPORT_HTTPS;
1291                 } else if (!strcasecmp(item->key, "Login")) {
1292                         if ((item->values_num != 2) || (item->values[0].type != OCONFIG_TYPE_STRING) || (item->values[1].type != OCONFIG_TYPE_STRING)) {
1293                                 WARNING("netapp plugin: \"Login\" needs exactly two string arguments, username and password. Ignoring host block \"%s\".", ci->values[0].value.string);
1294                                 return 0;
1295                         }
1296                         temp.username = item->values[0].value.string;
1297                         temp.password = item->values[1].value.string;
1298                 } else if (!strcasecmp(item->key, "Interval")) {
1299                         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) {
1300                                 WARNING("netapp plugin: \"Interval\" of host %s needs exactly one integer argument.", ci->values[0].value.string);
1301                                 continue;
1302                         }
1303                         temp.interval = item->values[0].value.number;
1304                 } else if (!strcasecmp(item->key, "GetVolumePerfData")) {
1305                         build_perf_vol_config(&temp, item);
1306                 } else if (!strcasecmp(item->key, "GetSystemPerfData")) {
1307                         build_perf_sys_config(&temp, item, &default_service);
1308                 } else if (!strcasecmp(item->key, "GetWaflPerfData")) {
1309                         build_perf_wafl_config(&temp, item);
1310                 } else if (!strcasecmp(item->key, "GetDiskPerfData")) {
1311                         build_perf_disk_config(&temp, item);
1312                 } else if (!strcasecmp(item->key, "GetVolumeData")) {
1313                         build_volume_config(&temp, item);
1314                 } else {
1315                         WARNING("netapp plugin: Ignoring unknown config option \"%s\" in host block \"%s\".",
1316                                         item->key, ci->values[0].value.string);
1317                 }
1318         }
1319         
1320         if (!temp.host) temp.host = temp.name;
1321         if (!temp.port) temp.port = temp.protocol == NA_SERVER_TRANSPORT_HTTP ? 80 : 443;
1322         if (!temp.username) {
1323                 WARNING("netapp plugin: Please supply login information for host \"%s\". Ignoring host block.", temp.name);
1324                 return 0;
1325         }
1326         for (hc = host_config; hc; hc = hc->next) {
1327                 if (!strcasecmp(hc->name, temp.name)) WARNING("netapp plugin: Duplicate definition of host \"%s\". This is probably a bad idea.", hc->name);
1328         }
1329         host = malloc(sizeof(*host));
1330         *host = temp;
1331         host->name = strdup(temp.name);
1332         host->protocol = temp.protocol;
1333         host->host = strdup(temp.host);
1334         host->username = strdup(temp.username);
1335         host->password = strdup(temp.password);
1336         host->next = host_config;
1337         host_config = host;
1338         return host;
1339 }
1340
1341 static int build_config (oconfig_item_t *ci) {
1342         int i;
1343         oconfig_item_t *item;
1344         host_config_t default_host = HOST_INIT;
1345         service_config_t default_service = SERVICE_INIT;
1346         
1347         for (i = 0; i < ci->children_num; ++i) {
1348                 item = ci->children + i;
1349
1350                 /* if (!item || !item->key || !*item->key) continue; */
1351                 if (!strcasecmp(item->key, "Host")) {
1352                         build_host_config(item, &default_host, &default_service);
1353                 } else {
1354                         WARNING("netapp plugin: Ignoring unknown config option \"%s\".", item->key);
1355                 }
1356         }
1357         return 0;
1358 }
1359
1360 static int netapp_read() {
1361         na_elem_t *out;
1362         host_config_t *host;
1363         service_config_t *service;
1364         
1365         for (host = host_config; host; host = host->next) {
1366                 for (service = host->services; service; service = service->next) {
1367                         if (--service->skip_countdown > 0) continue;
1368                         service->skip_countdown = service->multiplier;
1369                         out = na_server_invoke_elem(host->srv, service->query);
1370                         if (na_results_status(out) != NA_OK) {
1371                                 int netapp_errno = na_results_errno(out);
1372                                 ERROR("netapp plugin: Error %d from host %s: %s", netapp_errno, host->name, na_results_reason(out));
1373                                 na_elem_free(out);
1374                                 if (netapp_errno == EIO || netapp_errno == ETIMEDOUT) {
1375                                         /* Network problems. Just give up on all other services on this host. */
1376                                         break;
1377                                 }
1378                                 continue;
1379                         }
1380                         service->handler(host, out, service->data);
1381                         na_elem_free(out);
1382                 }
1383         }
1384         return 0;
1385 }
1386
1387 void module_register() {
1388         plugin_register_complex_config("netapp", build_config);
1389         plugin_register_init("netapp", config_init);
1390         plugin_register_read("netapp", netapp_read);
1391 }
1392
1393 /* vim: set sw=2 ts=2 noet fdm=marker : */