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