Code

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