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