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