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