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