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