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