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