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