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