ad50b689fb48da4d4d5431752dbc78cbaab259ea
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 #define HAS_ALL_FLAGS(has,needs) (((has) & (needs)) == (needs))
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);
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;
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;
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;
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;
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;
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}
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;
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;
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;
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}
212 static host_config_t *host_config;
214 /*
215 * Auxiliary functions
216 *
217 * Used to look up volumes and disks or to handle flags.
218 */
219 static volume_t *get_volume (host_config_t *host, const char *name, /* {{{ */
220 uint32_t vol_usage_flags, uint32_t vol_perf_flags)
221 {
222 volume_t *v;
224 if (name == NULL)
225 return (NULL);
227 /* Make sure the default flags include the init-bit. */
228 if (vol_usage_flags != 0)
229 vol_usage_flags |= VOLUME_INIT;
230 if (vol_perf_flags != 0)
231 vol_perf_flags |= CFG_VOLUME_PERF_INIT;
233 for (v = host->volumes; v; v = v->next) {
234 if (strcmp(v->name, name) != 0)
235 continue;
237 /* Check if the flags have been initialized. */
238 if (((v->cfg_volume_usage.flags & VOLUME_INIT) == 0)
239 && (vol_usage_flags != 0))
240 v->cfg_volume_usage.flags = vol_usage_flags;
241 if (((v->perf_data.flags & CFG_VOLUME_PERF_INIT) == 0)
242 && (vol_perf_flags != 0))
243 v->perf_data.flags = vol_perf_flags;
245 return v;
246 }
248 DEBUG ("netapp plugin: Allocating new entry for volume %s.", name);
249 v = malloc(sizeof(*v));
250 if (v == NULL)
251 return (NULL);
252 memset (v, 0, sizeof (*v));
254 v->cfg_volume_usage.flags = vol_usage_flags;
255 v->perf_data.flags = vol_perf_flags;
257 v->name = strdup(name);
258 if (v->name == NULL) {
259 sfree (v);
260 return (NULL);
261 }
263 v->next = host->volumes;
264 host->volumes = v;
266 return v;
267 } /* }}} volume_t *get_volume */
269 static disk_t *get_disk(host_config_t *host, const char *name) /* {{{ */
270 {
271 disk_t *v;
273 if (name == NULL)
274 return (NULL);
276 for (v = host->disks; v; v = v->next) {
277 if (strcmp(v->name, name) == 0)
278 return v;
279 }
280 v = malloc(sizeof(*v));
281 if (v == NULL)
282 return (NULL);
283 memset (v, 0, sizeof (*v));
284 v->next = NULL;
286 v->name = strdup(name);
287 if (v->name == NULL) {
288 sfree (v);
289 return (NULL);
290 }
292 v->next = host->disks;
293 host->disks = v;
295 return v;
296 } /* }}} disk_t *get_disk */
298 static void host_set_all_perf_data_flags(const host_config_t *host, /* {{{ */
299 uint32_t flag, _Bool set)
300 {
301 volume_t *v;
303 for (v = host->volumes; v; v = v->next) {
304 if (set)
305 v->perf_data.flags |= flag;
306 else /* if (!set) */
307 v->perf_data.flags &= ~flag;
308 }
309 } /* }}} void host_set_all_perf_data_flags */
311 static void host_set_all_cfg_volume_usage_flags(const host_config_t *host, /* {{{ */
312 uint32_t flag, _Bool set) {
313 volume_t *v;
315 for (v = host->volumes; v; v = v->next) {
316 if (set)
317 v->cfg_volume_usage.flags |= flag;
318 else /* if (!set) */
319 v->cfg_volume_usage.flags &= ~flag;
320 }
321 } /* }}} void host_set_all_cfg_volume_usage_flags */
323 /*
324 * Various submit functions.
325 *
326 * They all eventually call "submit_values" which creates a value_list_t and
327 * dispatches it to the daemon.
328 */
329 static int submit_values (const char *host, /* {{{ */
330 const char *plugin_inst,
331 const char *type, const char *type_inst,
332 value_t *values, int values_len,
333 time_t timestamp)
334 {
335 value_list_t vl = VALUE_LIST_INIT;
337 vl.values = values;
338 vl.values_len = values_len;
340 if (timestamp > 0)
341 vl.time = timestamp;
343 if (host != NULL)
344 sstrncpy (vl.host, host, sizeof (vl.host));
345 else
346 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
347 sstrncpy (vl.plugin, "netapp", sizeof (vl.plugin));
348 if (plugin_inst != NULL)
349 sstrncpy (vl.plugin_instance, plugin_inst, sizeof (vl.plugin_instance));
350 sstrncpy (vl.type, type, sizeof (vl.type));
351 if (type_inst != NULL)
352 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
354 return (plugin_dispatch_values (&vl));
355 } /* }}} int submit_uint64 */
357 static int submit_two_counters (const char *host, const char *plugin_inst, /* {{{ */
358 const char *type, const char *type_inst, counter_t val0, counter_t val1,
359 time_t timestamp)
360 {
361 value_t values[2];
363 values[0].counter = val0;
364 values[1].counter = val1;
366 return (submit_values (host, plugin_inst, type, type_inst,
367 values, 2, timestamp));
368 } /* }}} int submit_two_counters */
370 static int submit_counter (const char *host, const char *plugin_inst, /* {{{ */
371 const char *type, const char *type_inst, counter_t counter, time_t timestamp)
372 {
373 value_t v;
375 v.counter = counter;
377 return (submit_values (host, plugin_inst, type, type_inst,
378 &v, 1, timestamp));
379 } /* }}} int submit_counter */
381 static int submit_two_gauge (const char *host, const char *plugin_inst, /* {{{ */
382 const char *type, const char *type_inst, gauge_t val0, gauge_t val1,
383 time_t timestamp)
384 {
385 value_t values[2];
387 values[0].gauge = val0;
388 values[1].gauge = val1;
390 return (submit_values (host, plugin_inst, type, type_inst,
391 values, 2, timestamp));
392 } /* }}} int submit_two_gauge */
394 static int submit_double (const char *host, const char *plugin_inst, /* {{{ */
395 const char *type, const char *type_inst, double d, time_t timestamp)
396 {
397 value_t v;
399 v.gauge = (gauge_t) d;
401 return (submit_values (host, plugin_inst, type, type_inst,
402 &v, 1, timestamp));
403 } /* }}} int submit_uint64 */
405 /* Calculate hit ratio from old and new counters and submit the resulting
406 * percentage. Used by "submit_wafl_data". */
407 static int submit_cache_ratio (const char *host, /* {{{ */
408 const char *plugin_inst,
409 const char *type_inst,
410 uint64_t new_hits,
411 uint64_t new_misses,
412 uint64_t old_hits,
413 uint64_t old_misses,
414 time_t timestamp)
415 {
416 value_t v;
418 if ((new_hits >= old_hits) && (new_misses >= old_misses)) {
419 uint64_t hits;
420 uint64_t misses;
422 hits = new_hits - old_hits;
423 misses = new_misses - old_misses;
425 v.gauge = 100.0 * ((gauge_t) hits) / ((gauge_t) (hits + misses));
426 } else {
427 v.gauge = NAN;
428 }
430 return (submit_values (host, plugin_inst, "cache_ratio", type_inst,
431 &v, 1, timestamp));
432 } /* }}} int submit_cache_ratio */
434 /* Submits all the caches used by WAFL. Uses "submit_cache_ratio". */
435 static int submit_wafl_data (const host_config_t *host, const char *instance, /* {{{ */
436 data_wafl_t *old_data, const data_wafl_t *new_data)
437 {
438 /* Submit requested counters */
439 if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_NAME_CACHE | HAVE_WAFL_NAME_CACHE)
440 && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_NAME_CACHE))
441 submit_cache_ratio (host->name, instance, "name_cache_hit",
442 new_data->name_cache_hit, new_data->name_cache_miss,
443 old_data->name_cache_hit, old_data->name_cache_miss,
444 new_data->timestamp);
446 if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_DIR_CACHE | HAVE_WAFL_FIND_DIR)
447 && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_FIND_DIR))
448 submit_cache_ratio (host->name, instance, "find_dir_hit",
449 new_data->find_dir_hit, new_data->find_dir_miss,
450 old_data->find_dir_hit, old_data->find_dir_miss,
451 new_data->timestamp);
453 if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_BUF_CACHE | HAVE_WAFL_BUF_HASH)
454 && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_BUF_HASH))
455 submit_cache_ratio (host->name, instance, "buf_hash_hit",
456 new_data->buf_hash_hit, new_data->buf_hash_miss,
457 old_data->buf_hash_hit, old_data->buf_hash_miss,
458 new_data->timestamp);
460 if (HAS_ALL_FLAGS (old_data->flags, CFG_WAFL_INODE_CACHE | HAVE_WAFL_INODE_CACHE)
461 && HAS_ALL_FLAGS (new_data->flags, HAVE_WAFL_INODE_CACHE))
462 submit_cache_ratio (host->name, instance, "inode_cache_hit",
463 new_data->inode_cache_hit, new_data->inode_cache_miss,
464 old_data->inode_cache_hit, old_data->inode_cache_miss,
465 new_data->timestamp);
467 /* Clear old HAVE_* flags */
468 old_data->flags &= ~HAVE_WAFL_ALL;
470 /* Copy all counters */
471 old_data->timestamp = new_data->timestamp;
472 old_data->name_cache_hit = new_data->name_cache_hit;
473 old_data->name_cache_miss = new_data->name_cache_miss;
474 old_data->find_dir_hit = new_data->find_dir_hit;
475 old_data->find_dir_miss = new_data->find_dir_miss;
476 old_data->buf_hash_hit = new_data->buf_hash_hit;
477 old_data->buf_hash_miss = new_data->buf_hash_miss;
478 old_data->inode_cache_hit = new_data->inode_cache_hit;
479 old_data->inode_cache_miss = new_data->inode_cache_miss;
481 /* Copy HAVE_* flags */
482 old_data->flags |= (new_data->flags & HAVE_WAFL_ALL);
484 return (0);
485 } /* }}} int submit_wafl_data */
487 /* Submits volume performance data to the daemon, taking care to honor and
488 * update flags appropriately. */
489 static int submit_volume_perf_data (const host_config_t *host, /* {{{ */
490 volume_t *volume,
491 const data_volume_perf_t *new_data)
492 {
493 /* Check for and submit disk-octet values */
494 if (HAS_ALL_FLAGS (volume->perf_data.flags, CFG_VOLUME_PERF_IO)
495 && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_BYTES_READ | HAVE_VOLUME_PERF_BYTES_WRITE))
496 {
497 submit_two_counters (host->name, volume->name, "disk_octets", /* type instance = */ NULL,
498 (counter_t) new_data->read_bytes, (counter_t) new_data->write_bytes, new_data->timestamp);
499 }
501 /* Check for and submit disk-operations values */
502 if (HAS_ALL_FLAGS (volume->perf_data.flags, CFG_VOLUME_PERF_OPS)
503 && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE))
504 {
505 submit_two_counters (host->name, volume->name, "disk_ops", /* type instance = */ NULL,
506 (counter_t) new_data->read_ops, (counter_t) new_data->write_ops, new_data->timestamp);
507 }
509 /* Check for, calculate and submit disk-latency values */
510 if (HAS_ALL_FLAGS (volume->perf_data.flags, CFG_VOLUME_PERF_LATENCY
511 | HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
512 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE)
513 && HAS_ALL_FLAGS (new_data->flags, HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE
514 | HAVE_VOLUME_PERF_LATENCY_READ | HAVE_VOLUME_PERF_LATENCY_WRITE))
515 {
516 gauge_t latency_per_op_read;
517 gauge_t latency_per_op_write;
519 latency_per_op_read = NAN;
520 latency_per_op_write = NAN;
522 /* Check if a counter wrapped around. */
523 if ((new_data->read_ops > volume->perf_data.read_ops)
524 && (new_data->read_latency > volume->perf_data.read_latency))
525 {
526 uint64_t diff_ops_read;
527 uint64_t diff_latency_read;
529 diff_ops_read = new_data->read_ops - volume->perf_data.read_ops;
530 diff_latency_read = new_data->read_latency - volume->perf_data.read_latency;
532 if (diff_ops_read > 0)
533 latency_per_op_read = ((gauge_t) diff_latency_read) / ((gauge_t) diff_ops_read);
534 }
536 if ((new_data->write_ops > volume->perf_data.write_ops)
537 && (new_data->write_latency > volume->perf_data.write_latency))
538 {
539 uint64_t diff_ops_write;
540 uint64_t diff_latency_write;
542 diff_ops_write = new_data->write_ops - volume->perf_data.write_ops;
543 diff_latency_write = new_data->write_latency - volume->perf_data.write_latency;
545 if (diff_ops_write > 0)
546 latency_per_op_write = ((gauge_t) diff_latency_write) / ((gauge_t) diff_ops_write);
547 }
549 submit_two_gauge (host->name, volume->name, "disk_latency", /* type instance = */ NULL,
550 latency_per_op_read, latency_per_op_write, new_data->timestamp);
551 }
553 /* Clear all HAVE_* flags. */
554 volume->perf_data.flags &= ~HAVE_VOLUME_PERF_ALL;
556 /* Copy all counters */
557 volume->perf_data.timestamp = new_data->timestamp;
558 volume->perf_data.read_bytes = new_data->read_bytes;
559 volume->perf_data.write_bytes = new_data->write_bytes;
560 volume->perf_data.read_ops = new_data->read_ops;
561 volume->perf_data.write_ops = new_data->write_ops;
562 volume->perf_data.read_latency = new_data->read_latency;
563 volume->perf_data.write_latency = new_data->write_latency;
565 /* Copy the HAVE_* flags */
566 volume->perf_data.flags |= (new_data->flags & HAVE_VOLUME_PERF_ALL);
568 return (0);
569 } /* }}} int submit_volume_perf_data */
571 /*
572 * Query functions
573 *
574 * These functions are called with appropriate data returned by the libnetapp
575 * interface which is parsed and submitted with the above functions.
576 */
577 /* Data corresponding to <GetWaflPerfData /> */
578 static void query_wafl_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
579 data_wafl_t *wafl = data;
580 data_wafl_t perf_data;
581 const char *plugin_inst;
582 na_elem_t *counter;
584 memset (&perf_data, 0, sizeof (perf_data));
586 perf_data.timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
588 out = na_elem_child(na_elem_child(out, "instances"), "instance-data");
589 if (out == NULL)
590 return;
592 plugin_inst = na_child_get_string(out, "name");
593 if (plugin_inst == NULL)
594 return;
596 /* Iterate over all counters */
597 na_elem_iter_t iter = na_child_iterator(na_elem_child(out, "counters"));
598 for (counter = na_iterator_next(&iter); counter; counter = na_iterator_next(&iter)) {
599 const char *name;
600 uint64_t value;
602 name = na_child_get_string(counter, "name");
603 if (name == NULL)
604 continue;
606 value = na_child_get_uint64(counter, "value", UINT64_MAX);
607 if (value == UINT64_MAX)
608 continue;
610 if (!strcmp(name, "name_cache_hit")) {
611 perf_data.name_cache_hit = value;
612 perf_data.flags |= HAVE_WAFL_NAME_CACHE_HIT;
613 } else if (!strcmp(name, "name_cache_miss")) {
614 perf_data.name_cache_miss = value;
615 perf_data.flags |= HAVE_WAFL_NAME_CACHE_MISS;
616 } else if (!strcmp(name, "find_dir_hit")) {
617 perf_data.find_dir_hit = value;
618 perf_data.flags |= HAVE_WAFL_FIND_DIR_HIT;
619 } else if (!strcmp(name, "find_dir_miss")) {
620 perf_data.find_dir_miss = value;
621 perf_data.flags |= HAVE_WAFL_FIND_DIR_MISS;
622 } else if (!strcmp(name, "buf_hash_hit")) {
623 perf_data.buf_hash_hit = value;
624 perf_data.flags |= HAVE_WAFL_BUF_HASH_HIT;
625 } else if (!strcmp(name, "buf_hash_miss")) {
626 perf_data.buf_hash_miss = value;
627 perf_data.flags |= HAVE_WAFL_BUF_HASH_MISS;
628 } else if (!strcmp(name, "inode_cache_hit")) {
629 perf_data.inode_cache_hit = value;
630 perf_data.flags |= HAVE_WAFL_INODE_CACHE_HIT;
631 } else if (!strcmp(name, "inode_cache_miss")) {
632 perf_data.inode_cache_miss = value;
633 perf_data.flags |= HAVE_WAFL_INODE_CACHE_MISS;
634 } else {
635 DEBUG("netapp plugin: query_wafl_data: Found unexpected child: %s",
636 name);
637 }
638 }
640 submit_wafl_data (host, plugin_inst, wafl, &perf_data);
641 } /* }}} void query_wafl_data */
643 /* Data corresponding to <GetDiskPerfData /> */
644 static void query_submit_disk_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
645 cfg_disk_t *cfg_disk = data;
646 time_t timestamp;
647 na_elem_t *counter, *inst;
648 disk_t *worst_disk = 0;
650 timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
651 out = na_elem_child(out, "instances");
653 /* Iterate over all children */
654 na_elem_iter_t inst_iter = na_child_iterator(out);
655 for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
656 disk_t *old_data;
657 disk_t new_data;
659 memset (&new_data, 0, sizeof (new_data));
660 new_data.timestamp = timestamp;
661 new_data.disk_busy_percent = NAN;
663 old_data = get_disk(host, na_child_get_string(inst, "name"));
664 if (old_data == NULL)
665 continue;
667 /* Look for the "disk_busy" and "base_for_disk_busy" counters */
668 na_elem_iter_t count_iter = na_child_iterator(na_elem_child(inst, "counters"));
669 for (counter = na_iterator_next(&count_iter); counter; counter = na_iterator_next(&count_iter)) {
670 const char *name;
671 uint64_t value;
673 name = na_child_get_string(counter, "name");
674 if (name == NULL)
675 continue;
677 value = na_child_get_uint64(counter, "value", UINT64_MAX);
678 if (value == UINT64_MAX)
679 continue;
681 if (strcmp(name, "disk_busy") == 0)
682 {
683 new_data.disk_busy = value;
684 new_data.flags |= HAVE_DISK_BUSY;
685 }
686 else if (strcmp(name, "base_for_disk_busy") == 0)
687 {
688 new_data.base_for_disk_busy = value;
689 new_data.flags |= HAVE_DISK_BASE;
690 }
691 }
693 /* If all required counters are available and did not just wrap around,
694 * calculate the busy percentage. Otherwise, the value is initialized to
695 * NAN at the top of the for-loop. */
696 if (HAS_ALL_FLAGS (old_data->flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
697 && HAS_ALL_FLAGS (new_data.flags, HAVE_DISK_BUSY | HAVE_DISK_BASE)
698 && (new_data.disk_busy >= old_data->disk_busy)
699 && (new_data.base_for_disk_busy > old_data->base_for_disk_busy))
700 {
701 uint64_t busy_diff;
702 uint64_t base_diff;
704 busy_diff = new_data.disk_busy - old_data->disk_busy;
705 base_diff = new_data.base_for_disk_busy - old_data->base_for_disk_busy;
707 new_data.disk_busy_percent = 100.0
708 * ((gauge_t) busy_diff) / ((gauge_t) base_diff);
709 }
711 /* Clear HAVE_* flags */
712 old_data->flags &= ~HAVE_DISK_ALL;
714 /* Copy data */
715 old_data->timestamp = new_data.timestamp;
716 old_data->disk_busy = new_data.disk_busy;
717 old_data->base_for_disk_busy = new_data.base_for_disk_busy;
718 old_data->disk_busy_percent = new_data.disk_busy_percent;
720 /* Copy flags */
721 old_data->flags |= (new_data.flags & HAVE_DISK_ALL);
723 if ((worst_disk == NULL)
724 || (worst_disk->disk_busy_percent < old_data->disk_busy_percent))
725 worst_disk = old_data;
726 } /* for (all disks) */
728 if ((cfg_disk->flags & CFG_DISK_BUSIEST) && (worst_disk != NULL))
729 submit_double (host->name, "system", "percent", "disk_busy",
730 worst_disk->disk_busy_percent, timestamp);
731 } /* }}} void query_submit_disk_data */
733 /* Data corresponding to <GetVolumeData /> */
734 static void collect_volume_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
735 na_elem_t *inst;
736 volume_t *volume;
737 cfg_volume_usage_t *cfg_volume_data = data;
739 out = na_elem_child(out, "volumes");
740 na_elem_iter_t inst_iter = na_child_iterator(out);
741 for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
742 uint64_t size_free = 0, size_used = 0, snap_reserved = 0;
744 na_elem_t *sis;
745 const char *sis_state;
746 uint64_t sis_saved_reported;
747 uint64_t sis_saved;
749 volume = get_volume(host, na_child_get_string(inst, "name"),
750 cfg_volume_data->flags, /* perf_flags = */ 0);
751 if (volume == NULL)
752 continue;
754 if (!(volume->cfg_volume_usage.flags & VOLUME_DF))
755 continue;
757 /* 2^4 exa-bytes? This will take a while ;) */
758 size_free = na_child_get_uint64(inst, "size-available", UINT64_MAX);
759 if (size_free != UINT64_MAX)
760 submit_double (host->name, volume->name, "df_complex", "used",
761 (double) size_used, /* time = */ 0);
763 size_used = na_child_get_uint64(inst, "size-used", UINT64_MAX);
764 if (size_free != UINT64_MAX)
765 submit_double (host->name, volume->name, "df_complex", "free",
766 (double) size_free, /* time = */ 0);
768 snap_reserved = na_child_get_uint64(inst, "snapshot-blocks-reserved", UINT64_MAX);
769 if (snap_reserved != UINT64_MAX)
770 /* 1 block == 1024 bytes as per API docs */
771 submit_double (host->name, volume->name, "df_complex", "snap_reserved",
772 (double) (1024 * snap_reserved), /* time = */ 0);
774 sis = na_elem_child(inst, "sis");
775 if (sis == NULL)
776 continue;
778 sis_state = na_child_get_string(sis, "state");
779 if ((sis_state == NULL)
780 || (strcmp ("enabled", sis_state) != 0))
781 continue;
783 sis_saved_reported = na_child_get_uint64(sis, "size-saved", UINT64_MAX);
784 if (sis_saved_reported == UINT64_MAX)
785 continue;
787 /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
788 if ((sis_saved_reported >> 32) != 0) {
789 /* In case they ever fix this bug. */
790 sis_saved = sis_saved_reported;
791 } else {
792 uint64_t sis_saved_percent;
793 uint64_t sis_saved_guess;
794 uint64_t overflow_guess;
795 uint64_t guess1, guess2, guess3;
797 sis_saved_percent = na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
798 if (sis_saved_percent > 100)
799 continue;
801 /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
802 * will hopefully be fixed in later versions. To work around the bug, try
803 * to figure out how often the 32bit integer wrapped around by using the
804 * "percentage-saved" value. Because the percentage is in the range
805 * [0-100], this should work as long as the saved space does not exceed
806 * 400 GBytes. */
807 /* percentage-saved = size-saved / (size-saved + size-used) */
808 if (sis_saved_percent < 100)
809 sis_saved_guess = size_used * sis_saved_percent / (100 - sis_saved_percent);
810 else
811 sis_saved_guess = size_used;
813 overflow_guess = sis_saved_guess >> 32;
814 guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported : sis_saved_reported;
815 guess2 = (overflow_guess << 32) + sis_saved_reported;
816 guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
818 if (sis_saved_guess < guess2) {
819 if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
820 sis_saved = guess1;
821 else
822 sis_saved = guess2;
823 } else {
824 if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
825 sis_saved = guess2;
826 else
827 sis_saved = guess3;
828 }
829 } /* end of 32-bit workaround */
831 submit_double (host->name, volume->name, "df_complex", "sis_saved",
832 (double) sis_saved, /* time = */ 0);
833 }
834 } /* }}} void collect_volume_data */
836 /* Data corresponding to <GetVolumePerfData /> */
837 static void query_volume_perf_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
838 cfg_volume_perf_t *cfg_volume_perf = data;
839 time_t timestamp;
840 na_elem_t *counter, *inst;
842 timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
844 out = na_elem_child(out, "instances");
845 na_elem_iter_t inst_iter = na_child_iterator(out);
846 for (inst = na_iterator_next(&inst_iter); inst; inst = na_iterator_next(&inst_iter)) {
847 data_volume_perf_t perf_data;
848 volume_t *volume;
850 memset (&perf_data, 0, sizeof (perf_data));
851 perf_data.timestamp = timestamp;
853 volume = get_volume(host, na_child_get_string(inst, "name"),
854 /* data_flags = */ 0, cfg_volume_perf->flags);
855 if (volume == NULL)
856 continue;
858 na_elem_iter_t count_iter = na_child_iterator(na_elem_child(inst, "counters"));
859 for (counter = na_iterator_next(&count_iter); counter; counter = na_iterator_next(&count_iter)) {
860 const char *name;
861 uint64_t value;
863 name = na_child_get_string(counter, "name");
864 if (name == NULL)
865 continue;
867 value = na_child_get_uint64(counter, "value", UINT64_MAX);
868 if (value == UINT64_MAX)
869 continue;
871 if (!strcmp(name, "read_data")) {
872 perf_data.read_bytes = value;
873 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_READ;
874 } else if (!strcmp(name, "write_data")) {
875 perf_data.write_bytes = value;
876 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_WRITE;
877 } else if (!strcmp(name, "read_ops")) {
878 perf_data.read_ops = value;
879 perf_data.flags |= HAVE_VOLUME_PERF_OPS_READ;
880 } else if (!strcmp(name, "write_ops")) {
881 perf_data.write_ops = value;
882 perf_data.flags |= HAVE_VOLUME_PERF_OPS_WRITE;
883 } else if (!strcmp(name, "read_latency")) {
884 perf_data.read_latency = value;
885 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_READ;
886 } else if (!strcmp(name, "write_latency")) {
887 perf_data.write_latency = value;
888 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_WRITE;
889 }
890 }
892 submit_volume_perf_data (host, volume, &perf_data);
893 } /* for (volume) */
894 } /* }}} void query_volume_perf_data */
896 /* Data corresponding to <GetSystemPerfData /> */
897 static void collect_perf_system_data(host_config_t *host, na_elem_t *out, void *data) { /* {{{ */
898 counter_t disk_read = 0, disk_written = 0;
899 counter_t net_recv = 0, net_sent = 0;
900 counter_t cpu_busy = 0, cpu_total = 0;
901 unsigned int counter_flags = 0;
903 cfg_system_t *cfg_system = data;
904 const char *instance;
905 time_t timestamp;
906 na_elem_t *counter;
908 timestamp = (time_t) na_child_get_uint64(out, "timestamp", 0);
909 out = na_elem_child(na_elem_child(out, "instances"), "instance-data");
910 instance = na_child_get_string(out, "name");
912 na_elem_iter_t iter = na_child_iterator(na_elem_child(out, "counters"));
913 for (counter = na_iterator_next(&iter); counter; counter = na_iterator_next(&iter)) {
914 const char *name;
915 uint64_t value;
917 name = na_child_get_string(counter, "name");
918 if (name == NULL)
919 continue;
921 value = na_child_get_uint64(counter, "value", UINT64_MAX);
922 if (value == UINT64_MAX)
923 continue;
925 if (!strcmp(name, "disk_data_read")) {
926 disk_read = (counter_t) (value * 1024);
927 counter_flags |= 0x01;
928 } else if (!strcmp(name, "disk_data_written")) {
929 disk_written = (counter_t) (value * 1024);
930 counter_flags |= 0x02;
931 } else if (!strcmp(name, "net_data_recv")) {
932 net_recv = (counter_t) (value * 1024);
933 counter_flags |= 0x04;
934 } else if (!strcmp(name, "net_data_sent")) {
935 net_sent = (counter_t) (value * 1024);
936 counter_flags |= 0x08;
937 } else if (!strcmp(name, "cpu_busy")) {
938 cpu_busy = (counter_t) value;
939 counter_flags |= 0x10;
940 } else if (!strcmp(name, "cpu_elapsed_time")) {
941 cpu_total = (counter_t) value;
942 counter_flags |= 0x20;
943 } else if ((cfg_system->flags & CFG_SYSTEM_OPS)
944 && (strlen(name) > 4)
945 && (!strcmp(name + strlen(name) - 4, "_ops"))) {
946 submit_counter (host->name, instance, "disk_ops_complex", name,
947 (counter_t) value, timestamp);
948 }
949 } /* for (counter) */
951 if ((cfg_system->flags & CFG_SYSTEM_DISK)
952 && ((counter_flags & 0x03) == 0x03))
953 submit_two_counters (host->name, instance, "disk_octets", NULL,
954 disk_read, disk_written, timestamp);
956 if ((cfg_system->flags & CFG_SYSTEM_NET)
957 && ((counter_flags & 0x0c) == 0x0c))
958 submit_two_counters (host->name, instance, "if_octets", NULL,
959 net_recv, net_sent, timestamp);
961 if ((cfg_system->flags & CFG_SYSTEM_CPU)
962 && ((counter_flags & 0x30) == 0x30)) {
963 submit_counter (host->name, instance, "cpu", "system",
964 cpu_busy, timestamp);
965 submit_counter (host->name, instance, "cpu", "idle",
966 cpu_total - cpu_busy, timestamp);
967 }
968 } /* }}} void collect_perf_system_data */
970 /*
971 * Configuration handling
972 */
973 /* Sets a given flag if the boolean argument is true and unsets the flag if it
974 * is false. On error, the flag-field is not changed. */
975 static int cna_config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */
976 uint32_t *flags, uint32_t flag)
977 {
978 if ((ci == NULL) || (flags == NULL))
979 return (EINVAL);
981 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
982 {
983 WARNING ("netapp plugin: The %s option needs exactly one boolean argument.",
984 ci->key);
985 return (-1);
986 }
988 if (ci->values[0].value.boolean)
989 *flags |= flag;
990 else
991 *flags &= ~flag;
993 return (0);
994 } /* }}} int cna_config_bool_to_flag */
996 /* Handling of the "Multiplier" option which is allowed in every block. */
997 static int cna_config_get_multiplier (const oconfig_item_t *ci, /* {{{ */
998 cfg_service_t *service)
999 {
1000 int tmp;
1002 if ((ci == NULL) || (service == NULL))
1003 return (EINVAL);
1005 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1006 {
1007 WARNING ("netapp plugin: The `Multiplier' option needs exactly one numeric argument.");
1008 return (-1);
1009 }
1011 tmp = (int) (ci->values[0].value.number + .5);
1012 if (tmp < 1)
1013 {
1014 WARNING ("netapp plugin: The `Multiplier' option needs a positive integer argument.");
1015 return (-1);
1016 }
1018 service->multiplier = tmp;
1019 service->skip_countdown = tmp;
1021 return (0);
1022 } /* }}} int cna_config_get_multiplier */
1024 /* Handling of the "GetIO", "GetOps" and "GetLatency" options within a
1025 * <GetVolumePerfData /> block. */
1026 static void cna_config_volume_performance_option (host_config_t *host, /* {{{ */
1027 cfg_volume_perf_t *perf_volume, const oconfig_item_t *item,
1028 uint32_t flag)
1029 {
1030 int i;
1032 for (i = 0; i < item->values_num; ++i) {
1033 const char *name;
1034 volume_t *v;
1035 _Bool set = true;
1037 if (item->values[i].type != OCONFIG_TYPE_STRING) {
1038 WARNING("netapp plugin: Ignoring non-string argument in "
1039 "\"GetVolumePerfData\" block for host %s", host->name);
1040 continue;
1041 }
1043 name = item->values[i].value.string;
1044 if (name[0] == '+') {
1045 set = true;
1046 ++name;
1047 } else if (name[0] == '-') {
1048 set = false;
1049 ++name;
1050 }
1052 if (!name[0]) {
1053 if (set)
1054 perf_volume->flags |= flag;
1055 else /* if (!set) */
1056 perf_volume->flags &= ~flag;
1058 host_set_all_perf_data_flags(host, flag, set);
1059 continue;
1060 }
1062 v = get_volume (host, name, /* data_flags = */ 0, perf_volume->flags);
1063 if (v == NULL)
1064 continue;
1066 if (set)
1067 v->perf_data.flags |= flag;
1068 else /* if (!set) */
1069 v->perf_data.flags &= ~flag;
1070 } /* for (i = 0 .. item->values_num) */
1071 } /* }}} void cna_config_volume_performance_option */
1073 /* Corresponds to a <GetDiskPerfData /> block */
1074 static void cna_config_volume_performance(host_config_t *host, const oconfig_item_t *ci) { /* {{{ */
1075 int i, had_io = 0, had_ops = 0, had_latency = 0;
1076 cfg_service_t *service;
1077 cfg_volume_perf_t *perf_volume;
1079 service = malloc(sizeof(*service));
1080 service->query = 0;
1081 service->handler = query_volume_perf_data;
1082 perf_volume = service->data = malloc(sizeof(*perf_volume));
1083 perf_volume->flags = CFG_VOLUME_PERF_INIT;
1084 service->next = host->services;
1085 host->services = service;
1086 for (i = 0; i < ci->children_num; ++i) {
1087 oconfig_item_t *item = ci->children + i;
1089 /* if (!item || !item->key || !*item->key) continue; */
1090 if (!strcasecmp(item->key, "Multiplier")) {
1091 cna_config_get_multiplier (item, service);
1092 } else if (!strcasecmp(item->key, "GetIO")) {
1093 had_io = 1;
1094 cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_IO);
1095 } else if (!strcasecmp(item->key, "GetOps")) {
1096 had_ops = 1;
1097 cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_OPS);
1098 } else if (!strcasecmp(item->key, "GetLatency")) {
1099 had_latency = 1;
1100 cna_config_volume_performance_option(host, perf_volume, item, CFG_VOLUME_PERF_LATENCY);
1101 }
1102 }
1103 if (!had_io) {
1104 perf_volume->flags |= CFG_VOLUME_PERF_IO;
1105 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_IO, /* set = */ true);
1106 }
1107 if (!had_ops) {
1108 perf_volume->flags |= CFG_VOLUME_PERF_OPS;
1109 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_OPS, /* set = */ true);
1110 }
1111 if (!had_latency) {
1112 perf_volume->flags |= CFG_VOLUME_PERF_LATENCY;
1113 host_set_all_perf_data_flags(host, CFG_VOLUME_PERF_LATENCY, /* set = */ true);
1114 }
1115 } /* }}} void cna_config_volume_performance */
1117 /* Handling of the "GetDiskUtil" option within a <GetVolumeData /> block. */
1118 static void cna_config_volume_usage_option (host_config_t *host, /* {{{ */
1119 cfg_volume_usage_t *cfg_volume_data, const oconfig_item_t *item, uint32_t flag)
1120 {
1121 int i;
1123 for (i = 0; i < item->values_num; ++i) {
1124 const char *name;
1125 volume_t *v;
1126 _Bool set = true;
1128 if (item->values[i].type != OCONFIG_TYPE_STRING) {
1129 WARNING("netapp plugin: Ignoring non-string argument in \"GetVolData\""
1130 "block for host %s", host->name);
1131 continue;
1132 }
1134 name = item->values[i].value.string;
1135 if (name[0] == '+') {
1136 set = true;
1137 ++name;
1138 } else if (name[0] == '-') {
1139 set = false;
1140 ++name;
1141 }
1143 if (!name[0]) {
1144 if (set)
1145 cfg_volume_data->flags |= flag;
1146 else /* if (!set) */
1147 cfg_volume_data->flags &= ~flag;
1149 host_set_all_cfg_volume_usage_flags(host, flag, set);
1150 continue;
1151 }
1153 v = get_volume(host, name, cfg_volume_data->flags, /* perf_flags = */ 0);
1154 if (v == NULL)
1155 continue;
1157 if (!v->cfg_volume_usage.flags)
1158 v->cfg_volume_usage.flags = cfg_volume_data->flags;
1160 if (set)
1161 v->cfg_volume_usage.flags |= flag;
1162 else /* if (!set) */
1163 v->cfg_volume_usage.flags &= ~flag;
1164 }
1165 } /* }}} void cna_config_volume_usage_option */
1167 /* Corresponds to a <GetVolumeData /> block */
1168 static void cna_config_volume_usage(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
1169 int i, had_df = 0;
1170 cfg_service_t *service;
1171 cfg_volume_usage_t *cfg_volume_data;
1173 service = malloc(sizeof(*service));
1174 service->query = 0;
1175 service->handler = collect_volume_data;
1176 cfg_volume_data = service->data = malloc(sizeof(*cfg_volume_data));
1177 cfg_volume_data->flags = VOLUME_INIT;
1178 service->next = host->services;
1179 host->services = service;
1180 for (i = 0; i < ci->children_num; ++i) {
1181 oconfig_item_t *item = ci->children + i;
1183 /* if (!item || !item->key || !*item->key) continue; */
1184 if (!strcasecmp(item->key, "Multiplier")) {
1185 cna_config_get_multiplier (item, service);
1186 } else if (!strcasecmp(item->key, "GetDiskUtil")) {
1187 had_df = 1;
1188 cna_config_volume_usage_option(host, cfg_volume_data, item, VOLUME_DF);
1189 }
1190 }
1191 if (!had_df) {
1192 cfg_volume_data->flags |= VOLUME_DF;
1193 host_set_all_cfg_volume_usage_flags(host, VOLUME_DF, /* set = */ true);
1194 }
1195 } /* }}} void cna_config_volume_usage */
1197 /* Corresponds to a <GetDiskPerfData /> block */
1198 static void cna_config_disk(host_config_t *temp, oconfig_item_t *ci) { /* {{{ */
1199 int i;
1200 cfg_service_t *service;
1201 cfg_disk_t *cfg_disk;
1203 service = malloc(sizeof(*service));
1204 service->query = 0;
1205 service->handler = query_submit_disk_data;
1206 cfg_disk = service->data = malloc(sizeof(*cfg_disk));
1207 cfg_disk->flags = CFG_DISK_ALL;
1208 service->next = temp->services;
1209 temp->services = service;
1210 for (i = 0; i < ci->children_num; ++i) {
1211 oconfig_item_t *item = ci->children + i;
1213 /* if (!item || !item->key || !*item->key) continue; */
1214 if (!strcasecmp(item->key, "Multiplier")) {
1215 cna_config_get_multiplier (item, service);
1216 } else if (!strcasecmp(item->key, "GetBusy")) {
1217 cna_config_bool_to_flag (item, &cfg_disk->flags, CFG_SYSTEM_CPU);
1218 }
1219 }
1220 } /* }}} void cna_config_disk */
1222 /* Corresponds to a <GetWaflPerfData /> block */
1223 static void cna_config_wafl(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
1224 int i;
1225 cfg_service_t *service;
1226 data_wafl_t *perf_wafl;
1228 service = malloc(sizeof(*service));
1229 if (service == NULL)
1230 return;
1231 memset (service, 0, sizeof (*service));
1233 service->query = 0;
1234 service->handler = query_wafl_data;
1235 perf_wafl = service->data = malloc(sizeof(*perf_wafl));
1236 perf_wafl->flags = CFG_WAFL_ALL;
1238 for (i = 0; i < ci->children_num; ++i) {
1239 oconfig_item_t *item = ci->children + i;
1241 if (!strcasecmp(item->key, "Multiplier")) {
1242 cna_config_get_multiplier (item, service);
1243 } else if (!strcasecmp(item->key, "GetNameCache")) {
1244 cna_config_bool_to_flag (item, &perf_wafl->flags, CFG_WAFL_NAME_CACHE);
1245 } else if (!strcasecmp(item->key, "GetDirCache")) {
1246 cna_config_bool_to_flag (item, &perf_wafl->flags, CFG_WAFL_DIR_CACHE);
1247 } else if (!strcasecmp(item->key, "GetBufCache")) {
1248 cna_config_bool_to_flag (item, &perf_wafl->flags, CFG_WAFL_BUF_CACHE);
1249 } else if (!strcasecmp(item->key, "GetInodeCache")) {
1250 cna_config_bool_to_flag (item, &perf_wafl->flags, CFG_WAFL_INODE_CACHE);
1251 } else {
1252 WARNING ("netapp plugin: The %s config option is not allowed within "
1253 "`GetWaflPerfData' blocks.", item->key);
1254 }
1255 }
1257 service->next = host->services;
1258 host->services = service;
1259 } /* }}} void cna_config_wafl */
1261 /* Corresponds to a <GetSystemPerfData /> block */
1262 static int cna_config_system (host_config_t *host, /* {{{ */
1263 oconfig_item_t *ci, const cfg_service_t *default_service)
1264 {
1265 int i;
1266 cfg_service_t *service;
1267 cfg_system_t *cfg_system;
1269 service = malloc(sizeof(*service));
1270 if (service == NULL)
1271 return (-1);
1272 memset (service, 0, sizeof (*service));
1273 *service = *default_service;
1274 service->handler = collect_perf_system_data;
1276 cfg_system = malloc(sizeof(*cfg_system));
1277 if (cfg_system == NULL) {
1278 sfree (service);
1279 return (-1);
1280 }
1281 memset (cfg_system, 0, sizeof (*cfg_system));
1282 cfg_system->flags = CFG_SYSTEM_ALL;
1283 service->data = cfg_system;
1285 for (i = 0; i < ci->children_num; ++i) {
1286 oconfig_item_t *item = ci->children + i;
1288 if (!strcasecmp(item->key, "Multiplier")) {
1289 cna_config_get_multiplier (item, service);
1290 } else if (!strcasecmp(item->key, "GetCPULoad")) {
1291 cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_CPU);
1292 } else if (!strcasecmp(item->key, "GetInterfaces")) {
1293 cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_NET);
1294 } else if (!strcasecmp(item->key, "GetDiskOps")) {
1295 cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_OPS);
1296 } else if (!strcasecmp(item->key, "GetDiskIO")) {
1297 cna_config_bool_to_flag (item, &cfg_system->flags, CFG_SYSTEM_DISK);
1298 } else {
1299 WARNING ("netapp plugin: The %s config option is not allowed within "
1300 "`GetSystemPerfData' blocks.", item->key);
1301 }
1302 }
1304 service->next = host->services;
1305 host->services = service;
1307 return (0);
1308 } /* }}} int cna_config_system */
1310 /* Corresponds to a <Host /> block. */
1311 static host_config_t *cna_config_host (const oconfig_item_t *ci, /* {{{ */
1312 const host_config_t *default_host, const cfg_service_t *def_def_service)
1313 {
1314 int i;
1315 oconfig_item_t *item;
1316 host_config_t *host, *hc, temp = *default_host;
1317 cfg_service_t default_service = *def_def_service;
1319 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1320 WARNING("netapp plugin: \"Host\" needs exactly one string argument. Ignoring host block.");
1321 return 0;
1322 }
1324 temp.name = ci->values[0].value.string;
1325 for (i = 0; i < ci->children_num; ++i) {
1326 item = ci->children + i;
1328 /* if (!item || !item->key || !*item->key) continue; */
1329 if (!strcasecmp(item->key, "Address")) {
1330 if ((item->values_num != 1) || (item->values[0].type != OCONFIG_TYPE_STRING)) {
1331 WARNING("netapp plugin: \"Name\" needs exactly one string argument. Ignoring host block \"%s\".", ci->values[0].value.string);
1332 return 0;
1333 }
1334 temp.host = item->values[0].value.string;
1335 } else if (!strcasecmp(item->key, "Port")) {
1336 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)) {
1337 WARNING("netapp plugin: \"Port\" needs exactly one integer argument in the range of 1-65535. Ignoring host block \"%s\".", ci->values[0].value.string);
1338 return 0;
1339 }
1340 temp.port = item->values[0].value.number;
1341 } else if (!strcasecmp(item->key, "Protocol")) {
1342 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"))) {
1343 WARNING("netapp plugin: \"Protocol\" needs to be either \"http\" or \"https\". Ignoring host block \"%s\".", ci->values[0].value.string);
1344 return 0;
1345 }
1346 if (!strcasecmp(item->values[0].value.string, "http")) temp.protocol = NA_SERVER_TRANSPORT_HTTP;
1347 else temp.protocol = NA_SERVER_TRANSPORT_HTTPS;
1348 } else if (!strcasecmp(item->key, "Login")) {
1349 if ((item->values_num != 2) || (item->values[0].type != OCONFIG_TYPE_STRING) || (item->values[1].type != OCONFIG_TYPE_STRING)) {
1350 WARNING("netapp plugin: \"Login\" needs exactly two string arguments, username and password. Ignoring host block \"%s\".", ci->values[0].value.string);
1351 return 0;
1352 }
1353 temp.username = item->values[0].value.string;
1354 temp.password = item->values[1].value.string;
1355 } else if (!strcasecmp(item->key, "Interval")) {
1356 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) {
1357 WARNING("netapp plugin: \"Interval\" of host %s needs exactly one integer argument.", ci->values[0].value.string);
1358 continue;
1359 }
1360 temp.interval = item->values[0].value.number;
1361 } else if (!strcasecmp(item->key, "GetVolumePerfData")) {
1362 cna_config_volume_performance(&temp, item);
1363 } else if (!strcasecmp(item->key, "GetSystemPerfData")) {
1364 cna_config_system(&temp, item, &default_service);
1365 } else if (!strcasecmp(item->key, "GetWaflPerfData")) {
1366 cna_config_wafl(&temp, item);
1367 } else if (!strcasecmp(item->key, "GetDiskPerfData")) {
1368 cna_config_disk(&temp, item);
1369 } else if (!strcasecmp(item->key, "GetVolumeData")) {
1370 cna_config_volume_usage(&temp, item);
1371 } else {
1372 WARNING("netapp plugin: Ignoring unknown config option \"%s\" in host block \"%s\".",
1373 item->key, ci->values[0].value.string);
1374 }
1375 }
1377 if (!temp.host) temp.host = temp.name;
1378 if (!temp.port) temp.port = temp.protocol == NA_SERVER_TRANSPORT_HTTP ? 80 : 443;
1379 if (!temp.username) {
1380 WARNING("netapp plugin: Please supply login information for host \"%s\". Ignoring host block.", temp.name);
1381 return 0;
1382 }
1383 for (hc = host_config; hc; hc = hc->next) {
1384 if (!strcasecmp(hc->name, temp.name)) WARNING("netapp plugin: Duplicate definition of host \"%s\". This is probably a bad idea.", hc->name);
1385 }
1386 host = malloc(sizeof(*host));
1387 *host = temp;
1388 host->name = strdup(temp.name);
1389 host->protocol = temp.protocol;
1390 host->host = strdup(temp.host);
1391 host->username = strdup(temp.username);
1392 host->password = strdup(temp.password);
1393 host->next = host_config;
1394 host_config = host;
1395 return host;
1396 } /* }}} host_config_t *cna_config_host */
1398 /*
1399 * Callbacks registered with the daemon
1400 *
1401 * Pretty standard stuff here.
1402 */
1403 static int cna_init(void) { /* {{{ */
1404 char err[256];
1405 na_elem_t *e;
1406 host_config_t *host;
1407 cfg_service_t *service;
1409 if (!host_config) {
1410 WARNING("netapp plugin: Plugin loaded but no hosts defined.");
1411 return 1;
1412 }
1414 if (!na_startup(err, sizeof(err))) {
1415 ERROR("netapp plugin: Error initializing netapp API: %s", err);
1416 return 1;
1417 }
1419 for (host = host_config; host; host = host->next) {
1420 host->srv = na_server_open(host->host, 1, 1);
1421 na_server_set_transport_type(host->srv, host->protocol, 0);
1422 na_server_set_port(host->srv, host->port);
1423 na_server_style(host->srv, NA_STYLE_LOGIN_PASSWORD);
1424 na_server_adminuser(host->srv, host->username, host->password);
1425 na_server_set_timeout(host->srv, 5);
1426 for (service = host->services; service; service = service->next) {
1427 service->interval = host->interval * service->multiplier;
1428 if (service->handler == collect_perf_system_data) {
1429 service->query = na_elem_new("perf-object-get-instances");
1430 na_child_add_string(service->query, "objectname", "system");
1431 } else if (service->handler == query_volume_perf_data) {
1432 service->query = na_elem_new("perf-object-get-instances");
1433 na_child_add_string(service->query, "objectname", "volume");
1434 /* e = na_elem_new("instances");
1435 na_child_add_string(e, "foo", "system");
1436 na_child_add(root, e);*/
1437 e = na_elem_new("counters");
1438 na_child_add_string(e, "foo", "read_ops");
1439 na_child_add_string(e, "foo", "write_ops");
1440 na_child_add_string(e, "foo", "read_data");
1441 na_child_add_string(e, "foo", "write_data");
1442 na_child_add_string(e, "foo", "read_latency");
1443 na_child_add_string(e, "foo", "write_latency");
1444 na_child_add(service->query, e);
1445 } else if (service->handler == query_wafl_data) {
1446 service->query = na_elem_new("perf-object-get-instances");
1447 na_child_add_string(service->query, "objectname", "wafl");
1448 /* e = na_elem_new("instances");
1449 na_child_add_string(e, "foo", "system");
1450 na_child_add(root, e);*/
1451 e = na_elem_new("counters");
1452 na_child_add_string(e, "foo", "name_cache_hit");
1453 na_child_add_string(e, "foo", "name_cache_miss");
1454 na_child_add_string(e, "foo", "find_dir_hit");
1455 na_child_add_string(e, "foo", "find_dir_miss");
1456 na_child_add_string(e, "foo", "buf_hash_hit");
1457 na_child_add_string(e, "foo", "buf_hash_miss");
1458 na_child_add_string(e, "foo", "inode_cache_hit");
1459 na_child_add_string(e, "foo", "inode_cache_miss");
1460 /* na_child_add_string(e, "foo", "inode_eject_time"); */
1461 /* na_child_add_string(e, "foo", "buf_eject_time"); */
1462 na_child_add(service->query, e);
1463 } else if (service->handler == query_submit_disk_data) {
1464 service->query = na_elem_new("perf-object-get-instances");
1465 na_child_add_string(service->query, "objectname", "disk");
1466 e = na_elem_new("counters");
1467 na_child_add_string(e, "foo", "disk_busy");
1468 na_child_add_string(e, "foo", "base_for_disk_busy");
1469 na_child_add(service->query, e);
1470 } else if (service->handler == collect_volume_data) {
1471 service->query = na_elem_new("volume-list-info");
1472 /* na_child_add_string(service->query, "objectname", "volume"); */
1473 /* } else if (service->handler == collect_snapshot_data) { */
1474 /* service->query = na_elem_new("snapshot-list-info"); */
1475 }
1476 }
1477 }
1478 return 0;
1479 } /* }}} int cna_init */
1481 static int cna_config (oconfig_item_t *ci) { /* {{{ */
1482 int i;
1483 oconfig_item_t *item;
1484 host_config_t default_host = HOST_INIT;
1485 cfg_service_t default_service = SERVICE_INIT;
1487 for (i = 0; i < ci->children_num; ++i) {
1488 item = ci->children + i;
1490 /* if (!item || !item->key || !*item->key) continue; */
1491 if (!strcasecmp(item->key, "Host")) {
1492 cna_config_host(item, &default_host, &default_service);
1493 } else {
1494 WARNING("netapp plugin: Ignoring unknown config option \"%s\".", item->key);
1495 }
1496 }
1497 return 0;
1498 } /* }}} int cna_config */
1500 static int cna_read(void) { /* {{{ */
1501 na_elem_t *out;
1502 host_config_t *host;
1503 cfg_service_t *service;
1505 for (host = host_config; host; host = host->next) {
1506 for (service = host->services; service; service = service->next) {
1507 if (--service->skip_countdown > 0) continue;
1508 service->skip_countdown = service->multiplier;
1509 out = na_server_invoke_elem(host->srv, service->query);
1510 if (na_results_status(out) != NA_OK) {
1511 int netapp_errno = na_results_errno(out);
1512 ERROR("netapp plugin: Error %d from host %s: %s", netapp_errno, host->name, na_results_reason(out));
1513 na_elem_free(out);
1514 if (netapp_errno == EIO || netapp_errno == ETIMEDOUT) {
1515 /* Network problems. Just give up on all other services on this host. */
1516 break;
1517 }
1518 continue;
1519 }
1520 service->handler(host, out, service->data);
1521 na_elem_free(out);
1522 }
1523 }
1524 return 0;
1525 } /* }}} int cna_read */
1527 void module_register(void) {
1528 plugin_register_complex_config("netapp", cna_config);
1529 plugin_register_init("netapp", cna_init);
1530 plugin_register_read("netapp", cna_read);
1531 }
1533 /* vim: set sw=2 ts=2 noet fdm=marker : */