bce92ea640c7429c02d2b60eceb978201a7675ec
[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 #define HAS_ALL_FLAGS(has,needs) (((has) & (needs)) == (needs))
33
34 typedef struct host_config_s host_config_t;
35 typedef void service_handler_t(host_config_t *host, na_elem_t *result, void *data);
36
37 /*!
38  * \brief Persistent data for system performance counters
39  */
40 #define CFG_SYSTEM_CPU  0x01
41 #define CFG_SYSTEM_NET  0x02
42 #define CFG_SYSTEM_OPS  0x04
43 #define CFG_SYSTEM_DISK 0x08
44 #define CFG_SYSTEM_ALL  0x0F
45 typedef struct {
46         uint32_t flags;
47 } cfg_system_t;
48
49 /*!
50  * \brief Persistent data for WAFL performance counters. (a.k.a. cache performance)
51  *
52  * The cache counters use old counter values to calculate a hit ratio for each
53  * counter. The "data_wafl_t" struct therefore contains old counter values
54  * along with flags, which are set if the counter is valid.
55  *
56  * The function "query_wafl_data" will fill a new structure of this kind with
57  * new values, then pass both, new and old data, to "submit_wafl_data". That
58  * function calculates the hit ratios, submits the calculated values and
59  * updates the old counter values for the next iteration.
60  */
61 #define CFG_WAFL_NAME_CACHE        0x0001
62 #define CFG_WAFL_DIR_CACHE         0x0002
63 #define CFG_WAFL_BUF_CACHE         0x0004
64 #define CFG_WAFL_INODE_CACHE       0x0008
65 #define CFG_WAFL_ALL               0x000F
66 #define HAVE_WAFL_NAME_CACHE_HIT   0x0100
67 #define HAVE_WAFL_NAME_CACHE_MISS  0x0200
68 #define HAVE_WAFL_NAME_CACHE       (HAVE_WAFL_NAME_CACHE_HIT | HAVE_WAFL_NAME_CACHE_MISS)
69 #define HAVE_WAFL_FIND_DIR_HIT     0x0400
70 #define HAVE_WAFL_FIND_DIR_MISS    0x0800
71 #define HAVE_WAFL_FIND_DIR         (HAVE_WAFL_FIND_DIR_HIT | HAVE_WAFL_FIND_DIR_MISS)
72 #define HAVE_WAFL_BUF_HASH_HIT     0x1000
73 #define HAVE_WAFL_BUF_HASH_MISS    0x2000
74 #define HAVE_WAFL_BUF_HASH         (HAVE_WAFL_BUF_HASH_HIT | HAVE_WAFL_BUF_HASH_MISS)
75 #define HAVE_WAFL_INODE_CACHE_HIT  0x4000
76 #define HAVE_WAFL_INODE_CACHE_MISS 0x8000
77 #define HAVE_WAFL_INODE_CACHE      (HAVE_WAFL_INODE_CACHE_HIT | HAVE_WAFL_INODE_CACHE_MISS)
78 #define HAVE_WAFL_ALL              0xff00
79 typedef struct {
80         uint32_t flags;
81         time_t timestamp;
82         uint64_t name_cache_hit;
83         uint64_t name_cache_miss;
84         uint64_t find_dir_hit;
85         uint64_t find_dir_miss;
86         uint64_t buf_hash_hit;
87         uint64_t buf_hash_miss;
88         uint64_t inode_cache_hit;
89         uint64_t inode_cache_miss;
90 } data_wafl_t;
91
92 /*!
93  * \brief Persistent data for volume performance data.
94  *
95  * The code below uses the difference of the operations and latency counters to
96  * calculate an average per-operation latency. For this, old counters need to
97  * be stored in the "data_volume_perf_t" structure. The byte-counters are just
98  * kept for completeness sake. The "flags" member indicates if each counter is
99  * valid or not.
100  *
101  * The "query_volume_perf_data" function will fill a new struct of this type
102  * and pass both, old and new data, to "submit_volume_perf_data". In that
103  * function, the per-operation latency is calculated and dispatched, then the
104  * old counters are updated.
105  */
106 #define CFG_VOLUME_PERF_INIT           0x0001
107 #define CFG_VOLUME_PERF_IO             0x0002
108 #define CFG_VOLUME_PERF_OPS            0x0003
109 #define CFG_VOLUME_PERF_LATENCY        0x0008
110 #define CFG_VOLUME_PERF_ALL            0x000F
111 #define HAVE_VOLUME_PERF_BYTES_READ    0x0010
112 #define HAVE_VOLUME_PERF_BYTES_WRITE   0x0020
113 #define HAVE_VOLUME_PERF_OPS_READ      0x0040
114 #define HAVE_VOLUME_PERF_OPS_WRITE     0x0080
115 #define HAVE_VOLUME_PERF_LATENCY_READ  0x0100
116 #define HAVE_VOLUME_PERF_LATENCY_WRITE 0x0200
117 #define HAVE_VOLUME_PERF_ALL           0x03F0
118 typedef struct {
119         uint32_t flags;
120 } cfg_volume_perf_t;
121
122 typedef struct {
123         uint32_t flags;
124         time_t timestamp;
125         uint64_t read_bytes;
126         uint64_t write_bytes;
127         uint64_t read_ops;
128         uint64_t write_ops;
129         uint64_t read_latency;
130         uint64_t write_latency;
131 } data_volume_perf_t;
132
133 /*!
134  * \brief Configuration struct for volume usage data (free / used).
135  */
136 #define VOLUME_INIT           0x01
137 #define VOLUME_DF             0x02
138 #define VOLUME_SNAP           0x04
139 typedef struct {
140         uint32_t flags;
141 } cfg_volume_usage_t;
142
143 typedef struct service_config_s {
144         na_elem_t *query;
145         service_handler_t *handler;
146         int multiplier;
147         int skip_countdown;
148         int interval;
149         void *data;
150         struct service_config_s *next;
151 } cfg_service_t;
152 #define SERVICE_INIT {0, 0, 1, 1, 0, 0, 0}
153
154 /*!
155  * \brief Struct representing a volume.
156  *
157  * A volume currently has a name and two sets of values:
158  *
159  *  - Performance data, such as bytes read/written, number of operations
160  *    performed and average time per operation.
161  *
162  *  - Usage data, i. e. amount of used and free space in the volume.
163  */
164 typedef struct volume_s {
165         char *name;
166         data_volume_perf_t perf_data;
167         cfg_volume_usage_t cfg_volume_usage;
168         struct volume_s *next;
169 } volume_t;
170
171 #define CFG_DISK_BUSIEST 0x01
172 #define CFG_DISK_ALL     0x01
173 #define HAVE_DISK_BUSY   0x10
174 #define HAVE_DISK_BASE   0x20
175 #define HAVE_DISK_ALL    0x30
176 typedef struct {
177         uint32_t flags;
178 } cfg_disk_t;
179
180 /*!
181  * \brief A disk in the NetApp.
182  *
183  * A disk doesn't have any more information than its name at the moment.
184  * The name includes the "disk_" prefix.
185  */
186 typedef struct disk_s {
187         char *name;
188         uint32_t flags;
189         time_t timestamp;
190         uint64_t disk_busy;
191         uint64_t base_for_disk_busy;
192         double disk_busy_percent;
193         struct disk_s *next;
194 } disk_t;
195
196 struct host_config_s {
197         na_server_t *srv;
198         char *name;
199         na_server_transport_t protocol;
200         char *host;
201         int port;
202         char *username;
203         char *password;
204         int interval;
205         cfg_service_t *services;
206         disk_t *disks;
207         volume_t *volumes;
208         struct host_config_s *next;
209 };
210 #define HOST_INIT {NULL, NULL, NA_SERVER_TRANSPORT_HTTPS, NULL, 0, NULL, NULL, 10, NULL, NULL, NULL, NULL}
211
212 static host_config_t *global_host_config;
213
214 /*
215  * Free functions
216  *
217  * Used to free the various structures above.
218  */
219 static void free_volume (volume_t *volume) /* {{{ */
220 {
221         volume_t *next;
222
223         next = volume->next;
224
225         sfree (volume->name);
226         sfree (volume);
227
228         free_volume (next);
229 } /* }}} void free_volume */
230
231 static void free_disk (disk_t *disk) /* {{{ */
232 {
233         disk_t *next;
234
235         next = disk->next;
236
237         sfree (disk->name);
238         sfree (disk);
239
240         free_disk (next);
241 } /* }}} void free_disk */
242
243 static void free_cfg_service (cfg_service_t *service) /* {{{ */
244 {
245         cfg_service_t *next;
246
247         next = service->next;
248
249         /* FIXME: Free service->data? */
250         sfree (service);
251
252         free_cfg_service (next);
253 } /* }}} void free_cfg_service */
254
255 static void free_host_config (host_config_t *hc) /* {{{ */
256 {
257         host_config_t *next;
258
259         if (hc == NULL)
260                 return;
261
262         next = hc->next;
263
264         sfree (hc->name);
265         sfree (hc->host);
266         sfree (hc->username);
267         sfree (hc->password);
268
269         free_cfg_service (hc->services);
270         free_disk (hc->disks);
271         free_volume (hc->volumes);
272
273         sfree (hc);
274
275         free_host_config (next);
276 } /* }}} void free_host_config */
277
278 /*
279  * Auxiliary functions
280  *
281  * Used to look up volumes and disks or to handle flags.
282  */
283 static volume_t *get_volume (host_config_t *host, const char *name, /* {{{ */
284                 uint32_t vol_usage_flags, uint32_t vol_perf_flags)
285 {
286         volume_t *v;
287
288         if (name == NULL)
289                 return (NULL);
290         
291         /* Make sure the default flags include the init-bit. */
292         if (vol_usage_flags != 0)
293                 vol_usage_flags |= VOLUME_INIT;
294         if (vol_perf_flags != 0)
295                 vol_perf_flags |= CFG_VOLUME_PERF_INIT;
296
297         for (v = host->volumes; v; v = v->next) {
298                 if (strcmp(v->name, name) != 0)
299                         continue;
300
301                 /* Check if the flags have been initialized. */
302                 if (((v->cfg_volume_usage.flags & VOLUME_INIT) == 0)
303                                 && (vol_usage_flags != 0))
304                         v->cfg_volume_usage.flags = vol_usage_flags;
305                 if (((v->perf_data.flags & CFG_VOLUME_PERF_INIT) == 0)
306                                 && (vol_perf_flags != 0))
307                         v->perf_data.flags = vol_perf_flags;
308
309                 return v;
310         }
311
312         DEBUG ("netapp plugin: Allocating new entry for volume %s.", name);
313         v = malloc(sizeof(*v));
314         if (v == NULL)
315                 return (NULL);
316         memset (v, 0, sizeof (*v));
317
318         v->cfg_volume_usage.flags = vol_usage_flags;
319         v->perf_data.flags = vol_perf_flags;
320
321         v->name = strdup(name);
322         if (v->name == NULL) {
323                 sfree (v);
324                 return (NULL);
325         }
326
327         v->next = host->volumes;
328         host->volumes = v;
329
330         return v;
331 } /* }}} volume_t *get_volume */
332
333 static disk_t *get_disk(host_config_t *host, const char *name) /* {{{ */
334 {
335         disk_t *v;
336
337         if (name == NULL)
338                 return (NULL);
339         
340         for (v = host->disks; v; v = v->next) {
341                 if (strcmp(v->name, name) == 0)
342                         return v;
343         }
344         v = malloc(sizeof(*v));
345         if (v == NULL)
346                 return (NULL);
347         memset (v, 0, sizeof (*v));
348         v->next = NULL;
349
350         v->name = strdup(name);
351         if (v->name == NULL) {
352                 sfree (v);
353                 return (NULL);
354         }
355
356         v->next = host->disks;
357         host->disks = v;
358
359         return v;
360 } /* }}} disk_t *get_disk */
361
362 static void host_set_all_perf_data_flags(const host_config_t *host, /* {{{ */
363                 uint32_t flag, _Bool set)
364 {
365         volume_t *v;
366         
367         for (v = host->volumes; v; v = v->next) {
368                 if (set)
369                         v->perf_data.flags |= flag;
370                 else /* if (!set) */
371                         v->perf_data.flags &= ~flag;
372         }
373 } /* }}} void host_set_all_perf_data_flags */
374
375 static void host_set_all_cfg_volume_usage_flags(const host_config_t *host, /* {{{ */
376                 uint32_t flag, _Bool set) {
377         volume_t *v;
378         
379         for (v = host->volumes; v; v = v->next) {
380                 if (set)
381                         v->cfg_volume_usage.flags |= flag;
382                 else /* if (!set) */
383                         v->cfg_volume_usage.flags &= ~flag;
384         }
385 } /* }}} void host_set_all_cfg_volume_usage_flags */
386
387 /*
388  * Various submit functions.
389  *
390  * They all eventually call "submit_values" which creates a value_list_t and
391  * dispatches it to the daemon.
392  */
393 static int submit_values (const char *host, /* {{{ */
394                 const char *plugin_inst,
395                 const char *type, const char *type_inst,
396                 value_t *values, int values_len,
397                 time_t timestamp)
398 {
399         value_list_t vl = VALUE_LIST_INIT;
400
401         vl.values = values;
402         vl.values_len = values_len;
403
404         if (timestamp > 0)
405                 vl.time = timestamp;
406
407         if (host != NULL)
408                 sstrncpy (vl.host, host, sizeof (vl.host));
409         else
410                 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
411         sstrncpy (vl.plugin, "netapp", sizeof (vl.plugin));
412         if (plugin_inst != NULL)
413                 sstrncpy (vl.plugin_instance, plugin_inst, sizeof (vl.plugin_instance));
414         sstrncpy (vl.type, type, sizeof (vl.type));
415         if (type_inst != NULL)
416                 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
417
418         return (plugin_dispatch_values (&vl));
419 } /* }}} int submit_uint64 */
420
421 static int submit_two_counters (const char *host, const char *plugin_inst, /* {{{ */
422                 const char *type, const char *type_inst, counter_t val0, counter_t val1,
423                 time_t timestamp)
424 {
425         value_t values[2];
426
427         values[0].counter = val0;
428         values[1].counter = val1;
429
430         return (submit_values (host, plugin_inst, type, type_inst,
431                                 values, 2, timestamp));
432 } /* }}} int submit_two_counters */
433
434 static int submit_counter (const char *host, const char *plugin_inst, /* {{{ */
435                 const char *type, const char *type_inst, counter_t counter, time_t timestamp)
436 {
437         value_t v;
438
439         v.counter = counter;
440
441         return (submit_values (host, plugin_inst, type, type_inst,
442                                 &v, 1, timestamp));
443 } /* }}} int submit_counter */
444
445 static int submit_two_gauge (const char *host, const char *plugin_inst, /* {{{ */
446                 const char *type, const char *type_inst, gauge_t val0, gauge_t val1,
447                 time_t timestamp)
448 {
449         value_t values[2];
450
451         values[0].gauge = val0;
452         values[1].gauge = val1;
453
454         return (submit_values (host, plugin_inst, type, type_inst,
455                                 values, 2, timestamp));
456 } /* }}} int submit_two_gauge */
457
458 static int submit_double (const char *host, const char *plugin_inst, /* {{{ */
459                 const char *type, const char *type_inst, double d, time_t timestamp)
460 {
461         value_t v;
462
463         v.gauge = (gauge_t) d;
464
465         return (submit_values (host, plugin_inst, type, type_inst,
466                                 &v, 1, timestamp));
467 } /* }}} int submit_uint64 */
468
469 /* Calculate hit ratio from old and new counters and submit the resulting
470  * percentage. Used by "submit_wafl_data". */
471 static int submit_cache_ratio (const char *host, /* {{{ */
472                 const char *plugin_inst,
473                 const char *type_inst,
474                 uint64_t new_hits,
475                 uint64_t new_misses,
476                 uint64_t old_hits,
477                 uint64_t old_misses,
478                 time_t timestamp)
479 {
480         value_t v;
481
482         if ((new_hits >= old_hits) && (new_misses >= old_misses)) {
483                 uint64_t hits;
484                 uint64_t misses;
485
486                 hits = new_hits - old_hits;
487                 misses = new_misses - old_misses;
488
489                 v.gauge = 100.0 * ((gauge_t) hits) / ((gauge_t) (hits + misses));
490         } else {
491                 v.gauge = NAN;
492         }
493
494         return (submit_values (host, plugin_inst, "cache_ratio", type_inst,
495                                 &v, 1, timestamp));
496 } /* }}} int submit_cache_ratio */
497
498 /* Submits all the caches used by WAFL. Uses "submit_cache_ratio". */
499 static int submit_wafl_data (const host_config_t *host, const char *instance, /* {{{ */
500                 data_wafl_t *old_data, const data_wafl_t *new_data)
501 {
502         /* Submit requested counters */
503         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_NAME_CACHE | HAVE_WAFL_NAME_CACHE)
504                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_NAME_CACHE))
505                 submit_cache_ratio (host->name, instance, "name_cache_hit",
506                                 new_data->name_cache_hit, new_data->name_cache_miss,
507                                 old_data->name_cache_hit, old_data->name_cache_miss,
508                                 new_data->timestamp);
509
510         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_DIR_CACHE | HAVE_WAFL_FIND_DIR)
511                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_FIND_DIR))
512                 submit_cache_ratio (host->name, instance, "find_dir_hit",
513                                 new_data->find_dir_hit, new_data->find_dir_miss,
514                                 old_data->find_dir_hit, old_data->find_dir_miss,
515                                 new_data->timestamp);
516
517         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_BUF_CACHE | HAVE_WAFL_BUF_HASH)
518                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_BUF_HASH))
519                 submit_cache_ratio (host->name, instance, "buf_hash_hit",
520                                 new_data->buf_hash_hit, new_data->buf_hash_miss,
521                                 old_data->buf_hash_hit, old_data->buf_hash_miss,
522                                 new_data->timestamp);
523
524         if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_INODE_CACHE | HAVE_WAFL_INODE_CACHE)
525                         && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_INODE_CACHE))
526                 submit_cache_ratio (host->name, instance, "inode_cache_hit",
527                                 new_data->inode_cache_hit, new_data->inode_cache_miss,
528                                 old_data->inode_cache_hit, old_data->inode_cache_miss,
529                                 new_data->timestamp);
530
531         /* Clear old HAVE_* flags */
532         old_data->flags &= ~HAVE_WAFL_ALL;
533
534         /* Copy all counters */
535         old_data->timestamp        = new_data->timestamp;
536         old_data->name_cache_hit   = new_data->name_cache_hit;
537         old_data->name_cache_miss  = new_data->name_cache_miss;
538         old_data->find_dir_hit     = new_data->find_dir_hit;
539         old_data->find_dir_miss    = new_data->find_dir_miss;
540         old_data->buf_hash_hit     = new_data->buf_hash_hit;
541         old_data->buf_hash_miss    = new_data->buf_hash_miss;
542         old_data->inode_cache_hit  = new_data->inode_cache_hit;
543         old_data->inode_cache_miss = new_data->inode_cache_miss;
544
545         /* Copy HAVE_* flags */
546         old_data->flags |= (new_data->flags & HAVE_WAFL_ALL);
547
548         return (0);
549 } /* }}} int submit_wafl_data */
550
551 /* Submits volume performance data to the daemon, taking care to honor and
552  * update flags appropriately. */
553 static int submit_volume_perf_data (const host_config_t *host, /* {{{ */
554                 volume_t *volume,
555                 const data_volume_perf_t *new_data)
556 {
557         /* Check for and submit disk-octet values */
558         if (HAS_ALL_FLAGS (volume->perf_data.flags, CFG_VOLUME_PERF_IO)
559                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_BYTES_READ | HAVE_VOLUME_PERF_BYTES_WRITE))
560         {
561                 submit_two_counters (host->name, volume->name, "disk_octets", /* type instance = */ NULL,
562                                 (counter_t) new_data->read_bytes, (counter_t) new_data->write_bytes, new_data->timestamp);
563         }
564
565         /* Check for and submit disk-operations values */
566         if (HAS_ALL_FLAGS (volume->perf_data.flags, CFG_VOLUME_PERF_OPS)
567                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE))
568         {
569                 submit_two_counters (host->name, volume->name, "disk_ops", /* type instance = */ NULL,
570                                 (counter_t) new_data->read_ops, (counter_t) new_data->write_ops, new_data->timestamp);
571         }
572
573         /* Check for, calculate and submit disk-latency values */
574         if (HAS_ALL_FLAGS (volume->perf_data.flags, CFG_VOLUME_PERF_LATENCY
575                                 | HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
576                                 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE)
577                         && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
578                                 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE))
579         {
580                 gauge_t latency_per_op_read;
581                 gauge_t latency_per_op_write;
582
583                 latency_per_op_read = NAN;
584                 latency_per_op_write = NAN;
585
586                 /* Check if a counter wrapped around. */
587                 if ((new_data->read_ops > volume->perf_data.read_ops)
588                                 && (new_data->read_latency > volume->perf_data.read_latency))
589                 {
590                         uint64_t diff_ops_read;
591                         uint64_t diff_latency_read;
592
593                         diff_ops_read = new_data->read_ops - volume->perf_data.read_ops;
594                         diff_latency_read = new_data->read_latency - volume->perf_data.read_latency;
595
596                         if (diff_ops_read > 0)
597                                 latency_per_op_read = ((gauge_t) diff_latency_read) / ((gauge_t) diff_ops_read);
598                 }
599
600                 if ((new_data->write_ops > volume->perf_data.write_ops)
601                                 && (new_data->write_latency > volume->perf_data.write_latency))
602                 {
603                         uint64_t diff_ops_write;
604                         uint64_t diff_latency_write;
605
606                         diff_ops_write = new_data->write_ops - volume->perf_data.write_ops;
607                         diff_latency_write = new_data->write_latency - volume->perf_data.write_latency;
608
609                         if (diff_ops_write > 0)
610                                 latency_per_op_write = ((gauge_t) diff_latency_write) / ((gauge_t) diff_ops_write);
611                 }
612
613                 submit_two_gauge (host->name, volume->name, "disk_latency", /* type instance = */ NULL,
614                                 latency_per_op_read, latency_per_op_write, new_data->timestamp);
615         }
616
617         /* Clear all HAVE_* flags. */
618         volume->perf_data.flags &= ~HAVE_VOLUME_PERF_ALL;
619
620         /* Copy all counters */
621         volume->perf_data.timestamp = new_data->timestamp;
622         volume->perf_data.read_bytes = new_data->read_bytes;
623         volume->perf_data.write_bytes = new_data->write_bytes;
624         volume->perf_data.read_ops = new_data->read_ops;
625         volume->perf_data.write_ops = new_data->write_ops;
626         volume->perf_data.read_latency = new_data->read_latency;
627         volume->perf_data.write_latency = new_data->write_latency;
628
629         /* Copy the HAVE_* flags */
630         volume->perf_data.flags |= (new_data->flags & HAVE_VOLUME_PERF_ALL);
631
632         return (0);
633 } /* }}} int submit_volume_perf_data */
634
635 /* 
636  * Query functions
637  *
638  * These functions are called with appropriate data returned by the libnetapp
639  * interface which is parsed and submitted with the above functions.
640  */
641 /* Data corresponding to <GetWaflPerfData /> */
642 static void query_wafl_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
643         data_wafl_t *wafl = data;
644         data_wafl_t perf_data;
645         const char *plugin_inst;
646         na_elem_t *counter;
647
648         memset (&perf_data, 0, sizeof (perf_data));
649         
650         perf_data.timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
651
652         out = na_elem_child(na_elem_child(out, "instances"), "instance-data");
653         if (out == NULL)
654                 return;
655
656         plugin_inst = na_child_get_string(out, "name");
657         if (plugin_inst == NULL)
658                 return;
659
660         /* Iterate over all counters */
661         na_elem_iter_t iter = na_child_iterator(na_elem_child(out, "counters"));
662         for (counter = na_iterator_next(&iter); counter; counter = na_iterator_next(&iter)) {
663                 const char *name;
664                 uint64_t value;
665
666                 name = na_child_get_string(counter, "name");
667                 if (name == NULL)
668                         continue;
669
670                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
671                 if (value == UINT64_MAX)
672                         continue;
673
674                 if (!strcmp(name, "name_cache_hit")) {
675                         perf_data.name_cache_hit = value;
676                         perf_data.flags |= HAVE_WAFL_NAME_CACHE_HIT;
677                 } else if (!strcmp(name, "name_cache_miss")) {
678                         perf_data.name_cache_miss = value;
679                         perf_data.flags |= HAVE_WAFL_NAME_CACHE_MISS;
680                 } else if (!strcmp(name, "find_dir_hit")) {
681                         perf_data.find_dir_hit = value;
682                         perf_data.flags |= HAVE_WAFL_FIND_DIR_HIT;
683                 } else if (!strcmp(name, "find_dir_miss")) {
684                         perf_data.find_dir_miss = value;
685                         perf_data.flags |= HAVE_WAFL_FIND_DIR_MISS;
686                 } else if (!strcmp(name, "buf_hash_hit")) {
687                         perf_data.buf_hash_hit = value;
688                         perf_data.flags |= HAVE_WAFL_BUF_HASH_HIT;
689                 } else if (!strcmp(name, "buf_hash_miss")) {
690                         perf_data.buf_hash_miss = value;
691                         perf_data.flags |= HAVE_WAFL_BUF_HASH_MISS;
692                 } else if (!strcmp(name, "inode_cache_hit")) {
693                         perf_data.inode_cache_hit = value;
694                         perf_data.flags |= HAVE_WAFL_INODE_CACHE_HIT;
695                 } else if (!strcmp(name, "inode_cache_miss")) {
696                         perf_data.inode_cache_miss = value;
697                         perf_data.flags |= HAVE_WAFL_INODE_CACHE_MISS;
698                 } else {
699                         DEBUG("netapp plugin: query_wafl_data: Found unexpected child: %s",
700                                         name);
701                 }
702         }
703
704         submit_wafl_data (host, plugin_inst, wafl, &perf_data);
705 } /* }}} void query_wafl_data */
706
707 /* Data corresponding to <GetDiskPerfData /> */
708 static void query_submit_disk_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
709         cfg_disk_t *cfg_disk = data;
710         time_t timestamp;
711         na_elem_t *counter, *inst;
712         disk_t *worst_disk = 0;
713         
714         timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
715         out = na_elem_child(out, "instances");
716
717         /* Iterate over all children */
718         na_elem_iter_t inst_iter = na_child_iterator(out);
719         for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
720                 disk_t *old_data;
721                 disk_t  new_data;
722
723                 memset (&new_data, 0, sizeof (new_data));
724                 new_data.timestamp = timestamp;
725                 new_data.disk_busy_percent = NAN;
726
727                 old_data = get_disk(host, na_child_get_string(inst, "name"));
728                 if (old_data == NULL)
729                         continue;
730
731                 /* Look for the "disk_busy" and "base_for_disk_busy" counters */
732                 na_elem_iter_t count_iter = na_child_iterator(na_elem_child(inst, "counters"));
733                 for (counter = na_iterator_next(&count_iter); counter; counter = na_iterator_next(&count_iter)) {
734                         const char *name;
735                         uint64_t value;
736
737                         name = na_child_get_string(counter, "name");
738                         if (name == NULL)
739                                 continue;
740
741                         value = na_child_get_uint64(counter, "value", UINT64_MAX);
742                         if (value == UINT64_MAX)
743                                 continue;
744
745                         if (strcmp(name, "disk_busy") == 0)
746                         {
747                                 new_data.disk_busy = value;
748                                 new_data.flags |= HAVE_DISK_BUSY;
749                         }
750                         else if (strcmp(name, "base_for_disk_busy") == 0)
751                         {
752                                 new_data.base_for_disk_busy = value;
753                                 new_data.flags |= HAVE_DISK_BASE;
754                         }
755                 }
756
757                 /* If all required counters are available and did not just wrap around,
758                  * calculate the busy percentage. Otherwise, the value is initialized to
759                  * NAN at the top of the for-loop. */
760                 if (HAS_ALL_FLAGS (old_data->flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
761                                 && HAS_ALL_FLAGS (new_data.flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
762                                 && (new_data.disk_busy >= old_data->disk_busy)
763                                 && (new_data.base_for_disk_busy > old_data->base_for_disk_busy))
764                 {
765                         uint64_t busy_diff;
766                         uint64_t base_diff;
767
768                         busy_diff = new_data.disk_busy - old_data->disk_busy;
769                         base_diff = new_data.base_for_disk_busy - old_data->base_for_disk_busy;
770
771                         new_data.disk_busy_percent = 100.0
772                                 * ((gauge_t) busy_diff) / ((gauge_t) base_diff);
773                 }
774
775                 /* Clear HAVE_* flags */
776                 old_data->flags &= ~HAVE_DISK_ALL;
777
778                 /* Copy data */
779                 old_data->timestamp = new_data.timestamp;
780                 old_data->disk_busy = new_data.disk_busy;
781                 old_data->base_for_disk_busy = new_data.base_for_disk_busy;
782                 old_data->disk_busy_percent = new_data.disk_busy_percent;
783
784                 /* Copy flags */
785                 old_data->flags |= (new_data.flags & HAVE_DISK_ALL);
786
787                 if ((worst_disk == NULL)
788                                 || (worst_disk->disk_busy_percent < old_data->disk_busy_percent))
789                         worst_disk = old_data;
790         } /* for (all disks) */
791
792         if ((cfg_disk->flags & CFG_DISK_BUSIEST) && (worst_disk != NULL))
793                 submit_double (host->name, "system", "percent", "disk_busy",
794                                 worst_disk->disk_busy_percent, timestamp);
795 } /* }}} void query_submit_disk_data */
796
797 /* Data corresponding to <GetVolumeData /> */
798 static void collect_volume_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
799         na_elem_t *inst;
800         volume_t *volume;
801         cfg_volume_usage_t *cfg_volume_data = data;
802
803         out = na_elem_child(out, "volumes");
804         na_elem_iter_t inst_iter = na_child_iterator(out);
805         for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
806                 uint64_t size_free = 0, size_used = 0, snap_reserved = 0;
807
808                 na_elem_t *sis;
809                 const char *sis_state;
810                 uint64_t sis_saved_reported;
811                 uint64_t sis_saved;
812
813                 volume = get_volume(host, na_child_get_string(inst, "name"),
814                                 cfg_volume_data->flags, /* perf_flags = */ 0);
815                 if (volume == NULL)
816                         continue;
817
818                 if (!(volume->cfg_volume_usage.flags & VOLUME_DF))
819                         continue;
820
821                 /* 2^4 exa-bytes? This will take a while ;) */
822                 size_free = na_child_get_uint64(inst, "size-available", UINT64_MAX);
823                 if (size_free != UINT64_MAX)
824                         submit_double (host->name, volume->name, "df_complex", "used",
825                                         (double) size_used, /* time = */ 0);
826
827                 size_used = na_child_get_uint64(inst, "size-used", UINT64_MAX);
828                 if (size_free != UINT64_MAX)
829                         submit_double (host->name, volume->name, "df_complex", "free",
830                                         (double) size_free, /* time = */ 0);
831
832                 snap_reserved = na_child_get_uint64(inst, "snapshot-blocks-reserved", UINT64_MAX);
833                 if (snap_reserved != UINT64_MAX)
834                         /* 1 block == 1024 bytes  as per API docs */
835                         submit_double (host->name, volume->name, "df_complex", "snap_reserved",
836                                         (double) (1024 * snap_reserved), /* time = */ 0);
837
838                 sis = na_elem_child(inst, "sis");
839                 if (sis == NULL)
840                         continue;
841
842                 sis_state = na_child_get_string(sis, "state");
843                 if ((sis_state == NULL)
844                                 || (strcmp ("enabled", sis_state) != 0))
845                         continue;
846
847                 sis_saved_reported = na_child_get_uint64(sis, "size-saved", UINT64_MAX);
848                 if (sis_saved_reported == UINT64_MAX)
849                         continue;
850
851                 /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
852                 if ((sis_saved_reported >> 32) != 0) {
853                         /* In case they ever fix this bug. */
854                         sis_saved = sis_saved_reported;
855                 } else {
856                         uint64_t sis_saved_percent;
857                         uint64_t sis_saved_guess;
858                         uint64_t overflow_guess;
859                         uint64_t guess1, guess2, guess3;
860
861                         sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
862                         if (sis_saved_percent > 100)
863                                 continue;
864
865                         /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
866                          * will hopefully be fixed in later versions. To work around the bug, try
867                          * to figure out how often the 32bit integer wrapped around by using the
868                          * "percentage-saved" value. Because the percentage is in the range
869                          * [0-100], this should work as long as the saved space does not exceed
870                          * 400 GBytes. */
871                         /* percentage-saved = size-saved / (size-saved + size-used) */
872                         if (sis_saved_percent < 100)
873                                 sis_saved_guess = size_used * sis_saved_percent / (100 - sis_saved_percent);
874                         else
875                                 sis_saved_guess = size_used;
876
877                         overflow_guess = sis_saved_guess >> 32;
878                         guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
879                         guess2 = (overflow_guess << 32) + sis_saved_reported;
880                         guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
881
882                         if (sis_saved_guess < guess2) {
883                                 if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
884                                         sis_saved = guess1;
885                                 else
886                                         sis_saved = guess2;
887                         } else {
888                                 if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
889                                         sis_saved = guess2;
890                                 else
891                                         sis_saved = guess3;
892                         }
893                 } /* end of 32-bit workaround */
894
895                 submit_double (host->name, volume->name, "df_complex", "sis_saved",
896                                 (double) sis_saved, /* time = */ 0);
897         }
898 } /* }}} void collect_volume_data */
899
900 /* Data corresponding to <GetVolumePerfData /> */
901 static void query_volume_perf_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
902         cfg_volume_perf_t *cfg_volume_perf = data;
903         time_t timestamp;
904         na_elem_t *counter, *inst;
905         
906         timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
907
908         out = na_elem_child(out, "instances");
909         na_elem_iter_t inst_iter = na_child_iterator(out);
910         for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
911                 data_volume_perf_t perf_data;
912                 volume_t *volume;
913
914                 memset (&perf_data, 0, sizeof (perf_data));
915                 perf_data.timestamp = timestamp;
916
917                 volume = get_volume(host, na_child_get_string(inst, "name"),
918                                 /* data_flags = */ 0, cfg_volume_perf->flags);
919                 if (volume == NULL)
920                         continue;
921
922                 na_elem_iter_t count_iter = na_child_iterator(na_elem_child(inst, "counters"));
923                 for (counter = na_iterator_next(&count_iter); counter; counter = na_iterator_next(&count_iter)) {
924                         const char *name;
925                         uint64_t value;
926
927                         name = na_child_get_string(counter, "name");
928                         if (name == NULL)
929                                 continue;
930
931                         value = na_child_get_uint64(counter, "value", UINT64_MAX);
932                         if (value == UINT64_MAX)
933                                 continue;
934
935                         if (!strcmp(name, "read_data")) {
936                                 perf_data.read_bytes = value;
937                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_READ;
938                         } else if (!strcmp(name, "write_data")) {
939                                 perf_data.write_bytes = value;
940                                 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_WRITE;
941                         } else if (!strcmp(name, "read_ops")) {
942                                 perf_data.read_ops = value;
943                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_READ;
944                         } else if (!strcmp(name, "write_ops")) {
945                                 perf_data.write_ops = value;
946                                 perf_data.flags |= HAVE_VOLUME_PERF_OPS_WRITE;
947                         } else if (!strcmp(name, "read_latency")) {
948                                 perf_data.read_latency = value;
949                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_READ;
950                         } else if (!strcmp(name, "write_latency")) {
951                                 perf_data.write_latency = value;
952                                 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_WRITE;
953                         }
954                 }
955
956                 submit_volume_perf_data (host, volume, &perf_data);
957         } /* for (volume) */
958 } /* }}} void query_volume_perf_data */
959
960 /* Data corresponding to <GetSystemPerfData /> */
961 static void collect_perf_system_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
962         counter_t disk_read = 0, disk_written = 0;
963         counter_t net_recv = 0, net_sent = 0;
964         counter_t cpu_busy = 0, cpu_total = 0;
965         unsigned int counter_flags = 0;
966
967         cfg_system_t *cfg_system = data;
968         const char *instance;
969         time_t timestamp;
970         na_elem_t *counter;
971         
972         timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
973         out = na_elem_child(na_elem_child(out, "instances"), "instance-data");
974         instance = na_child_get_string(out, "name");
975
976         na_elem_iter_t iter = na_child_iterator(na_elem_child(out, "counters"));
977         for (counter = na_iterator_next(&iter); counter; counter = na_iterator_next(&iter)) {
978                 const char *name;
979                 uint64_t value;
980
981                 name = na_child_get_string(counter, "name");
982                 if (name == NULL)
983                         continue;
984
985                 value = na_child_get_uint64(counter, "value", UINT64_MAX);
986                 if (value == UINT64_MAX)
987                         continue;
988
989                 if (!strcmp(name, "disk_data_read")) {
990                         disk_read = (counter_t) (value * 1024);
991                         counter_flags |= 0x01;
992                 } else if (!strcmp(name, "disk_data_written")) {
993                         disk_written = (counter_t) (value * 1024);
994                         counter_flags |= 0x02;
995                 } else if (!strcmp(name, "net_data_recv")) {
996                         net_recv = (counter_t) (value * 1024);
997                         counter_flags |= 0x04;
998                 } else if (!strcmp(name, "net_data_sent")) {
999                         net_sent = (counter_t) (value * 1024);
1000                         counter_flags |= 0x08;
1001                 } else if (!strcmp(name, "cpu_busy")) {
1002                         cpu_busy = (counter_t) value;
1003                         counter_flags |= 0x10;
1004                 } else if (!strcmp(name, "cpu_elapsed_time")) {
1005                         cpu_total = (counter_t) value;
1006                         counter_flags |= 0x20;
1007                 } else if ((cfg_system->flags & CFG_SYSTEM_OPS)
1008                                 && (value > 0) && (strlen(name) > 4)
1009                                 && (!strcmp(name + strlen(name) - 4, "_ops"))) {
1010                         submit_counter (host->name, instance, "disk_ops_complex", name,
1011                                         (counter_t) value, timestamp);
1012                 }
1013         } /* for (counter) */
1014
1015         if ((cfg_system->flags & CFG_SYSTEM_DISK)
1016                         && ((counter_flags & 0x03) == 0x03))
1017                 submit_two_counters (host->name, instance, "disk_octets", NULL,
1018                                 disk_read, disk_written, timestamp);
1019                                 
1020         if ((cfg_system->flags & CFG_SYSTEM_NET)
1021                         && ((counter_flags & 0x0c) == 0x0c))
1022                 submit_two_counters (host->name, instance, "if_octets", NULL,
1023                                 net_recv, net_sent, timestamp);
1024
1025         if ((cfg_system->flags & CFG_SYSTEM_CPU)
1026                         && ((counter_flags & 0x30) == 0x30)) {
1027                 submit_counter (host->name, instance, "cpu", "system",
1028                                 cpu_busy, timestamp);
1029                 submit_counter (host->name, instance, "cpu", "idle",
1030                                 cpu_total - cpu_busy, timestamp);
1031         }
1032 } /* }}} void collect_perf_system_data */
1033
1034 /*
1035  * Configuration handling
1036  */
1037 /* Sets a given flag if the boolean argument is true and unsets the flag if it
1038  * is false. On error, the flag-field is not changed. */
1039 static int cna_config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */
1040                 uint32_t *flags, uint32_t flag)
1041 {
1042         if ((ci == NULL) || (flags == NULL))
1043                 return (EINVAL);
1044
1045         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1046         {
1047                 WARNING ("netapp plugin: The %s option needs exactly one boolean argument.",
1048                                 ci->key);
1049                 return (-1);
1050         }
1051
1052         if (ci->values[0].value.boolean)
1053                 *flags |= flag;
1054         else
1055                 *flags &= ~flag;
1056
1057         return (0);
1058 } /* }}} int cna_config_bool_to_flag */
1059
1060 /* Handling of the "Multiplier" option which is allowed in every block. */
1061 static int cna_config_get_multiplier (const oconfig_item_t *ci, /* {{{ */
1062                 cfg_service_t *service)
1063 {
1064         int tmp;
1065
1066         if ((ci == NULL) || (service == NULL))
1067                 return (EINVAL);
1068
1069         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1070         {
1071                 WARNING ("netapp plugin: The `Multiplier' option needs exactly one numeric argument.");
1072                 return (-1);
1073         }
1074
1075         tmp = (int) (ci->values[0].value.number + .5);
1076         if (tmp < 1)
1077         {
1078                 WARNING ("netapp plugin: The `Multiplier' option needs a positive integer argument.");
1079                 return (-1);
1080         }
1081
1082         service->multiplier = tmp;
1083         service->skip_countdown = tmp;
1084
1085         return (0);
1086 } /* }}} int cna_config_get_multiplier */
1087
1088 /* Handling of the "GetIO", "GetOps" and "GetLatency" options within a
1089  * <GetVolumePerfData /> block. */
1090 static void cna_config_volume_performance_option (host_config_t *host, /* {{{ */
1091                 cfg_volume_perf_t *perf_volume, const oconfig_item_t *item,
1092                 uint32_t flag)
1093 {
1094         int i;
1095         
1096         for (i = 0; i < item->values_num; ++i) {
1097                 const char *name;
1098                 volume_t *v;
1099                 _Bool set = true;
1100
1101                 if (item->values[i].type != OCONFIG_TYPE_STRING) {
1102                         WARNING("netapp plugin: Ignoring non-string argument in "
1103                                         "\"GetVolumePerfData\" block for host %s", host->name);
1104                         continue;
1105                 }
1106
1107                 name = item->values[i].value.string;
1108                 if (name[0] == '+') {
1109                         set = true;
1110                         ++name;
1111                 } else if (name[0] == '-') {
1112                         set = false;
1113                         ++name;
1114                 }
1115
1116                 if (!name[0]) {
1117                         if (set)
1118                                 perf_volume->flags |= flag;
1119                         else /* if (!set) */
1120                                 perf_volume->flags &= ~flag;
1121
1122                         host_set_all_perf_data_flags(host, flag, set);
1123                         continue;
1124                 }
1125
1126                 v = get_volume (host, name, /* data_flags = */ 0, perf_volume->flags);
1127                 if (v == NULL)
1128                         continue;
1129
1130                 if (set)
1131                         v->perf_data.flags |= flag;
1132                 else /* if (!set) */
1133                         v->perf_data.flags &= ~flag;
1134         } /* for (i = 0 .. item->values_num) */
1135 } /* }}} void cna_config_volume_performance_option */
1136
1137 /* Corresponds to a <GetDiskPerfData /> block */
1138 static void cna_config_volume_performance(host_config_t *host, const oconfig_item_t *ci) { /* {{{ */
1139         int i, had_io = 0, had_ops = 0, had_latency = 0;
1140         cfg_service_t *service;
1141         cfg_volume_perf_t *perf_volume;
1142         
1143         service = malloc(sizeof(*service));
1144         service->query = 0;
1145         service->handler = query_volume_perf_data;
1146         perf_volume = service->data = malloc(sizeof(*perf_volume));
1147         perf_volume->flags = CFG_VOLUME_PERF_INIT;
1148         service->next = host->services;
1149         host->services = service;
1150         for (i = 0; i < ci->children_num; ++i) {
1151                 oconfig_item_t *item = ci->children + i;
1152                 
1153                 /* if (!item || !item->key || !*item->key) continue; */
1154                 if (!strcasecmp(item->key, "Multiplier")) {
1155                         cna_config_get_multiplier (item, service);
1156                 } else if (!strcasecmp(item->key, "GetIO")) {
1157                         had_io = 1;
1158                         cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_IO);
1159                 } else if (!strcasecmp(item->key, "GetOps")) {
1160                         had_ops = 1;
1161                         cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_OPS);
1162                 } else if (!strcasecmp(item->key, "GetLatency")) {
1163                         had_latency = 1;
1164                         cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_LATENCY);
1165                 }
1166         }
1167         if (!had_io) {
1168                 perf_volume->flags |= CFG_VOLUME_PERF_IO;
1169                 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_IO, /* set = */ true);
1170         }
1171         if (!had_ops) {
1172                 perf_volume->flags |= CFG_VOLUME_PERF_OPS;
1173                 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_OPS, /* set = */ true);
1174         }
1175         if (!had_latency) {
1176                 perf_volume->flags |= CFG_VOLUME_PERF_LATENCY;
1177                 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_LATENCY, /* set = */ true);
1178         }
1179 } /* }}} void cna_config_volume_performance */
1180
1181 /* Handling of the "GetDiskUtil" option within a <GetVolumeData /> block. */
1182 static void cna_config_volume_usage_option (host_config_t *host, /* {{{ */
1183                 cfg_volume_usage_t *cfg_volume_data, const oconfig_item_t *item, uint32_t flag)
1184 {
1185         int i;
1186         
1187         for (i = 0; i < item->values_num; ++i) {
1188                 const char *name;
1189                 volume_t *v;
1190                 _Bool set = true;
1191
1192                 if (item->values[i].type != OCONFIG_TYPE_STRING) {
1193                         WARNING("netapp plugin: Ignoring non-string argument in \"GetVolData\""
1194                                         "block for host %s", host->name);
1195                         continue;
1196                 }
1197
1198                 name = item->values[i].value.string;
1199                 if (name[0] == '+') {
1200                         set = true;
1201                         ++name;
1202                 } else if (name[0] == '-') {
1203                         set = false;
1204                         ++name;
1205                 }
1206
1207                 if (!name[0]) {
1208                         if (set)
1209                                 cfg_volume_data->flags |= flag;
1210                         else /* if (!set) */
1211                                 cfg_volume_data->flags &= ~flag;
1212
1213                         host_set_all_cfg_volume_usage_flags(host, flag, set);
1214                         continue;
1215                 }
1216
1217                 v = get_volume(host, name, cfg_volume_data->flags, /* perf_flags = */ 0);
1218                 if (v == NULL)
1219                         continue;
1220
1221                 if (!v->cfg_volume_usage.flags)
1222                         v->cfg_volume_usage.flags = cfg_volume_data->flags;
1223
1224                 if (set)
1225                         v->cfg_volume_usage.flags |= flag;
1226                 else /* if (!set) */
1227                         v->cfg_volume_usage.flags &= ~flag;
1228         }
1229 } /* }}} void cna_config_volume_usage_option */
1230
1231 /* Corresponds to a <GetVolumeData /> block */
1232 static void cna_config_volume_usage(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
1233         int i, had_df = 0;
1234         cfg_service_t *service;
1235         cfg_volume_usage_t *cfg_volume_data;
1236         
1237         service = malloc(sizeof(*service));
1238         service->query = 0;
1239         service->handler = collect_volume_data;
1240         cfg_volume_data = service->data = malloc(sizeof(*cfg_volume_data));
1241         cfg_volume_data->flags = VOLUME_INIT;
1242         service->next = host->services;
1243         host->services = service;
1244         for (i = 0; i < ci->children_num; ++i) {
1245                 oconfig_item_t *item = ci->children + i;
1246                 
1247                 /* if (!item || !item->key || !*item->key) continue; */
1248                 if (!strcasecmp(item->key, "Multiplier")) {
1249                         cna_config_get_multiplier (item, service);
1250                 } else if (!strcasecmp(item->key, "GetDiskUtil")) {
1251                         had_df = 1;
1252                         cna_config_volume_usage_option(host, cfg_volume_data, item, VOLUME_DF);
1253                 }
1254         }
1255         if (!had_df) {
1256                 cfg_volume_data->flags |= VOLUME_DF;
1257                 host_set_all_cfg_volume_usage_flags(host, VOLUME_DF, /* set = */ true);
1258         }
1259 } /* }}} void cna_config_volume_usage */
1260
1261 /* Corresponds to a <GetDiskPerfData /> block */
1262 static void cna_config_disk(host_config_t *temp, oconfig_item_t *ci) { /* {{{ */
1263         int i;
1264         cfg_service_t *service;
1265         cfg_disk_t *cfg_disk;
1266         
1267         service = malloc(sizeof(*service));
1268         service->query = 0;
1269         service->handler = query_submit_disk_data;
1270         cfg_disk = service->data = malloc(sizeof(*cfg_disk));
1271         cfg_disk->flags = CFG_DISK_ALL;
1272         service->next = temp->services;
1273         temp->services = service;
1274         for (i = 0; i < ci->children_num; ++i) {
1275                 oconfig_item_t *item = ci->children + i;
1276                 
1277                 /* if (!item || !item->key || !*item->key) continue; */
1278                 if (!strcasecmp(item->key, "Multiplier")) {
1279                         cna_config_get_multiplier (item, service);
1280                 } else if (!strcasecmp(item->key, "GetBusy")) {
1281                         cna_config_bool_to_flag (item, &cfg_disk->flags, CFG_SYSTEM_CPU);
1282                 }
1283         }
1284 } /* }}} void cna_config_disk */
1285
1286 /* Corresponds to a <GetWaflPerfData /> block */
1287 static void cna_config_wafl(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
1288         int i;
1289         cfg_service_t *service;
1290         data_wafl_t *perf_wafl;
1291         
1292         service = malloc(sizeof(*service));
1293         if (service == NULL)
1294                 return;
1295         memset (service, 0, sizeof (*service));
1296
1297         service->query = 0;
1298         service->handler = query_wafl_data;
1299         perf_wafl = service->data = malloc(sizeof(*perf_wafl));
1300         perf_wafl->flags = CFG_WAFL_ALL;
1301
1302         for (i = 0; i < ci->children_num; ++i) {
1303                 oconfig_item_t *item = ci->children + i;
1304                 
1305                 if (!strcasecmp(item->key, "Multiplier")) {
1306                         cna_config_get_multiplier (item, service);
1307                 } else if (!strcasecmp(item->key, "GetNameCache")) {
1308                         cna_config_bool_to_flag (item, &perf_wafl->flags, CFG_WAFL_NAME_CACHE);
1309                 } else if (!strcasecmp(item->key, "GetDirCache")) {
1310                         cna_config_bool_to_flag (item, &perf_wafl->flags, CFG_WAFL_DIR_CACHE);
1311                 } else if (!strcasecmp(item->key, "GetBufCache")) {
1312                         cna_config_bool_to_flag (item, &perf_wafl->flags, CFG_WAFL_BUF_CACHE);
1313                 } else if (!strcasecmp(item->key, "GetInodeCache")) {
1314                         cna_config_bool_to_flag (item, &perf_wafl->flags, CFG_WAFL_INODE_CACHE);
1315                 } else {
1316                         WARNING ("netapp plugin: The %s config option is not allowed within "
1317                                         "`GetWaflPerfData' blocks.", item->key);
1318                 }
1319         }
1320
1321         service->next = host->services;
1322         host->services = service;
1323 } /* }}} void cna_config_wafl */
1324
1325 /* Corresponds to a <GetSystemPerfData /> block */
1326 static int cna_config_system (host_config_t *host, /* {{{ */
1327                 oconfig_item_t *ci, const cfg_service_t *default_service)
1328 {
1329         int i;
1330         cfg_service_t *service;
1331         cfg_system_t *cfg_system;
1332         
1333         service = malloc(sizeof(*service));
1334         if (service == NULL)
1335                 return (-1);
1336         memset (service, 0, sizeof (*service));
1337         *service = *default_service;
1338         service->handler = collect_perf_system_data;
1339
1340         cfg_system = malloc(sizeof(*cfg_system));
1341         if (cfg_system == NULL) {
1342                 sfree (service);
1343                 return (-1);
1344         }
1345         memset (cfg_system, 0, sizeof (*cfg_system));
1346         cfg_system->flags = CFG_SYSTEM_ALL;
1347         service->data = cfg_system;
1348
1349         for (i = 0; i < ci->children_num; ++i) {
1350                 oconfig_item_t *item = ci->children + i;
1351
1352                 if (!strcasecmp(item->key, "Multiplier")) {
1353                         cna_config_get_multiplier (item, service);
1354                 } else if (!strcasecmp(item->key, "GetCPULoad")) {
1355                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_CPU);
1356                 } else if (!strcasecmp(item->key, "GetInterfaces")) {
1357                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_NET);
1358                 } else if (!strcasecmp(item->key, "GetDiskOps")) {
1359                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_OPS);
1360                 } else if (!strcasecmp(item->key, "GetDiskIO")) {
1361                         cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_DISK);
1362                 } else {
1363                         WARNING ("netapp plugin: The %s config option is not allowed within "
1364                                         "`GetSystemPerfData' blocks.", item->key);
1365                 }
1366         }
1367
1368         service->next = host->services;
1369         host->services = service;
1370
1371         return (0);
1372 } /* }}} int cna_config_system */
1373
1374 /* Corresponds to a <Host /> block. */
1375 static host_config_t *cna_config_host (const oconfig_item_t *ci, /* {{{ */
1376                 const host_config_t *default_host, const cfg_service_t *def_def_service)
1377 {
1378         oconfig_item_t *item;
1379         host_config_t *host, *hc;
1380         cfg_service_t default_service = *def_def_service;
1381         int status;
1382         int i;
1383         
1384         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1385                 WARNING("netapp plugin: \"Host\" needs exactly one string argument. Ignoring host block.");
1386                 return 0;
1387         }
1388
1389         host = malloc(sizeof(*host));
1390         memcpy (host, default_host, sizeof (*host));
1391
1392         status = cf_util_get_string (ci, &host->name);
1393         if (status != 0)
1394         {
1395                 sfree (host);
1396                 return (NULL);
1397         }
1398
1399         for (i = 0; i < ci->children_num; ++i) {
1400                 item = ci->children + i;
1401
1402                 status = 0;
1403
1404                 if (!strcasecmp(item->key, "Address")) {
1405                         status = cf_util_get_string (item, &host->host);
1406                 } else if (!strcasecmp(item->key, "Port")) {
1407                         int tmp;
1408
1409                         tmp = cf_util_get_port_number (item);
1410                         if (tmp > 0)
1411                                 host->port = tmp;
1412                 } else if (!strcasecmp(item->key, "Protocol")) {
1413                         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"))) {
1414                                 WARNING("netapp plugin: \"Protocol\" needs to be either \"http\" or \"https\". Ignoring host block \"%s\".", ci->values[0].value.string);
1415                                 return 0;
1416                         }
1417                         if (!strcasecmp(item->values[0].value.string, "http")) host->protocol = NA_SERVER_TRANSPORT_HTTP;
1418                         else host->protocol = NA_SERVER_TRANSPORT_HTTPS;
1419                 } else if (!strcasecmp(item->key, "User")) {
1420                         status = cf_util_get_string (item, &host->username);
1421                 } else if (!strcasecmp(item->key, "Password")) {
1422                         status = cf_util_get_string (item, &host->password);
1423                 } else if (!strcasecmp(item->key, "Interval")) {
1424                         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) {
1425                                 WARNING("netapp plugin: \"Interval\" of host %s needs exactly one integer argument.", ci->values[0].value.string);
1426                                 continue;
1427                         }
1428                         host->interval = item->values[0].value.number;
1429                 } else if (!strcasecmp(item->key, "GetVolumePerfData")) {
1430                         cna_config_volume_performance(host, item);
1431                 } else if (!strcasecmp(item->key, "GetSystemPerfData")) {
1432                         cna_config_system(host, item, &default_service);
1433                 } else if (!strcasecmp(item->key, "GetWaflPerfData")) {
1434                         cna_config_wafl(host, item);
1435                 } else if (!strcasecmp(item->key, "GetDiskPerfData")) {
1436                         cna_config_disk(host, item);
1437                 } else if (!strcasecmp(item->key, "GetVolumeData")) {
1438                         cna_config_volume_usage(host, item);
1439                 } else {
1440                         WARNING("netapp plugin: Ignoring unknown config option \"%s\" in host block \"%s\".",
1441                                         item->key, ci->values[0].value.string);
1442                 }
1443
1444                 if (status != 0)
1445                         break;
1446         }
1447
1448         if (host->host == NULL)
1449                 host->host = strdup (host->name);
1450
1451         if (host->host == NULL)
1452                 status = -1;
1453
1454         if (host->port <= 0)
1455                 host->port = (host->protocol == NA_SERVER_TRANSPORT_HTTP) ? 80 : 443;
1456
1457         if ((host->username == NULL) || (host->password == NULL)) {
1458                 WARNING("netapp plugin: Please supply login information for host \"%s\". "
1459                                 "Ignoring host block.", host->name);
1460                 status = -1;
1461         }
1462
1463         if (status != 0)
1464         {
1465                 free_host_config (host);
1466                 return (NULL);
1467         }
1468
1469         for (hc = global_host_config; hc; hc = hc->next) {
1470                 if (!strcasecmp(hc->name, host->name)) WARNING("netapp plugin: Duplicate definition of host \"%s\". This is probably a bad idea.", hc->name);
1471         }
1472         return host;
1473 } /* }}} host_config_t *cna_config_host */
1474
1475 /*
1476  * Callbacks registered with the daemon
1477  *
1478  * Pretty standard stuff here.
1479  */
1480 static int cna_init(void) { /* {{{ */
1481         char err[256];
1482         na_elem_t *e;
1483         host_config_t *host;
1484         cfg_service_t *service;
1485         
1486         if (!global_host_config) {
1487                 WARNING("netapp plugin: Plugin loaded but no hosts defined.");
1488                 return 1;
1489         }
1490
1491         if (!na_startup(err, sizeof(err))) {
1492                 ERROR("netapp plugin: Error initializing netapp API: %s", err);
1493                 return 1;
1494         }
1495
1496         for (host = global_host_config; host; host = host->next) {
1497                 host->srv = na_server_open(host->host, 1, 1); 
1498                 na_server_set_transport_type(host->srv, host->protocol, 0);
1499                 na_server_set_port(host->srv, host->port);
1500                 na_server_style(host->srv, NA_STYLE_LOGIN_PASSWORD);
1501                 na_server_adminuser(host->srv, host->username, host->password);
1502                 na_server_set_timeout(host->srv, 5);
1503                 for (service = host->services; service; service = service->next) {
1504                         service->interval = host->interval * service->multiplier;
1505                         if (service->handler == collect_perf_system_data) {
1506                                 service->query = na_elem_new("perf-object-get-instances");
1507                                 na_child_add_string(service->query, "objectname", "system");
1508                         } else if (service->handler == query_volume_perf_data) {
1509                                 service->query = na_elem_new("perf-object-get-instances");
1510                                 na_child_add_string(service->query, "objectname", "volume");
1511 /*                              e = na_elem_new("instances");
1512                                 na_child_add_string(e, "foo", "system");
1513                                 na_child_add(root, e);*/
1514                                 e = na_elem_new("counters");
1515                                 na_child_add_string(e, "foo", "read_ops");
1516                                 na_child_add_string(e, "foo", "write_ops");
1517                                 na_child_add_string(e, "foo", "read_data");
1518                                 na_child_add_string(e, "foo", "write_data");
1519                                 na_child_add_string(e, "foo", "read_latency");
1520                                 na_child_add_string(e, "foo", "write_latency");
1521                                 na_child_add(service->query, e);
1522                         } else if (service->handler == query_wafl_data) {
1523                                 service->query = na_elem_new("perf-object-get-instances");
1524                                 na_child_add_string(service->query, "objectname", "wafl");
1525 /*                              e = na_elem_new("instances");
1526                                 na_child_add_string(e, "foo", "system");
1527                                 na_child_add(root, e);*/
1528                                 e = na_elem_new("counters");
1529                                 na_child_add_string(e, "foo", "name_cache_hit");
1530                                 na_child_add_string(e, "foo", "name_cache_miss");
1531                                 na_child_add_string(e, "foo", "find_dir_hit");
1532                                 na_child_add_string(e, "foo", "find_dir_miss");
1533                                 na_child_add_string(e, "foo", "buf_hash_hit");
1534                                 na_child_add_string(e, "foo", "buf_hash_miss");
1535                                 na_child_add_string(e, "foo", "inode_cache_hit");
1536                                 na_child_add_string(e, "foo", "inode_cache_miss");
1537                                 /* na_child_add_string(e, "foo", "inode_eject_time"); */
1538                                 /* na_child_add_string(e, "foo", "buf_eject_time"); */
1539                                 na_child_add(service->query, e);
1540                         } else if (service->handler == query_submit_disk_data) {
1541                                 service->query = na_elem_new("perf-object-get-instances");
1542                                 na_child_add_string(service->query, "objectname", "disk");
1543                                 e = na_elem_new("counters");
1544                                 na_child_add_string(e, "foo", "disk_busy");
1545                                 na_child_add_string(e, "foo", "base_for_disk_busy");
1546                                 na_child_add(service->query, e);
1547                         } else if (service->handler == collect_volume_data) {
1548                                 service->query = na_elem_new("volume-list-info");
1549                                 /* na_child_add_string(service->query, "objectname", "volume"); */
1550                                 /* } else if (service->handler == collect_snapshot_data) { */
1551                                 /* service->query = na_elem_new("snapshot-list-info"); */
1552                         }
1553                 }
1554         }
1555         return 0;
1556 } /* }}} int cna_init */
1557
1558 static int cna_config (oconfig_item_t *ci) { /* {{{ */
1559         int i;
1560         oconfig_item_t *item;
1561         host_config_t default_host = HOST_INIT;
1562         cfg_service_t default_service = SERVICE_INIT;
1563         
1564         for (i = 0; i < ci->children_num; ++i) {
1565                 item = ci->children + i;
1566
1567                 /* if (!item || !item->key || !*item->key) continue; */
1568                 if (!strcasecmp(item->key, "Host")) {
1569                         host_config_t *host;
1570                         host_config_t *tmp;
1571
1572                         host = cna_config_host(item, &default_host, &default_service);
1573                         if (host == NULL)
1574                                 continue;
1575
1576                         for (tmp = global_host_config; tmp != NULL; tmp = tmp->next)
1577                         {
1578                                 if (strcasecmp (host->name, tmp->name) == 0)
1579                                         WARNING ("netapp plugin: Duplicate definition of host `%s'. "
1580                                                         "This is probably a bad idea.",
1581                                                         host->name);
1582
1583                                 if (tmp->next == NULL)
1584                                         break;
1585                         }
1586
1587                         host->next = NULL;
1588                         if (tmp == NULL)
1589                                 global_host_config = host;
1590                         else
1591                                 tmp->next = host;
1592                 } else {
1593                         WARNING("netapp plugin: Ignoring unknown config option \"%s\".", item->key);
1594                 }
1595         }
1596         return 0;
1597 } /* }}} int cna_config */
1598
1599 static int cna_read(void) { /* {{{ */
1600         na_elem_t *out;
1601         host_config_t *host;
1602         cfg_service_t *service;
1603         
1604         for (host = global_host_config; host; host = host->next) {
1605                 for (service = host->services; service; service = service->next) {
1606                         if (--service->skip_countdown > 0) continue;
1607                         service->skip_countdown = service->multiplier;
1608                         out = na_server_invoke_elem(host->srv, service->query);
1609                         if (na_results_status(out) != NA_OK) {
1610                                 int netapp_errno = na_results_errno(out);
1611                                 ERROR("netapp plugin: Error %d from host %s: %s", netapp_errno, host->name, na_results_reason(out));
1612                                 na_elem_free(out);
1613                                 if (netapp_errno == EIO || netapp_errno == ETIMEDOUT) {
1614                                         /* Network problems. Just give up on all other services on this host. */
1615                                         break;
1616                                 }
1617                                 continue;
1618                         }
1619                         service->handler(host, out, service->data);
1620                         na_elem_free(out);
1621                 }
1622         }
1623         return 0;
1624 } /* }}} int cna_read */
1625
1626 void module_register(void) {
1627         plugin_register_complex_config("netapp", cna_config);
1628         plugin_register_init("netapp", cna_init);
1629         plugin_register_read("netapp", cna_read);
1630 }
1631
1632 /* vim: set sw=2 ts=2 noet fdm=marker : */