1 /**
2 * collectd - src/netapp.c
3 * Copyright (C) 2009,2010 Sven Trenkel
4 * Copyright (C) 2012-2013 teamix GmbH
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Sven Trenkel <collectd at semidefinite.de>
26 * Sebastian 'tokkee' Harl <sh@teamix.net>
27 **/
29 #include "collectd.h"
31 #include "common.h"
32 #include "utils_ignorelist.h"
34 #include <netapp_api.h>
35 #include <netapp_errno.h>
37 #define HAS_ALL_FLAGS(has, needs) (((has) & (needs)) == (needs))
39 typedef struct host_config_s host_config_t;
40 typedef void service_handler_t(host_config_t *host, na_elem_t *result,
41 void *data);
43 struct cna_interval_s {
44 cdtime_t interval;
45 cdtime_t last_read;
46 };
47 typedef struct cna_interval_s cna_interval_t;
49 /*! Data types for WAFL statistics {{{
50 *
51 * \brief Persistent data for WAFL performance counters. (a.k.a. cache
52 * performance)
53 *
54 * The cache counters use old counter values to calculate a hit ratio for each
55 * counter. The "cfg_wafl_t" struct therefore contains old counter values along
56 * with flags, which are set if the counter is valid.
57 *
58 * The function "cna_handle_wafl_data" will fill a new structure of this kind
59 * with new values, then pass both, new and old data, to "submit_wafl_data".
60 * That function calculates the hit ratios, submits the calculated values and
61 * updates the old counter values for the next iteration.
62 */
63 #define CFG_WAFL_NAME_CACHE 0x0001
64 #define CFG_WAFL_DIR_CACHE 0x0002
65 #define CFG_WAFL_BUF_CACHE 0x0004
66 #define CFG_WAFL_INODE_CACHE 0x0008
67 #define CFG_WAFL_ALL 0x000F
68 #define HAVE_WAFL_NAME_CACHE_HIT 0x0100
69 #define HAVE_WAFL_NAME_CACHE_MISS 0x0200
70 #define HAVE_WAFL_NAME_CACHE \
71 (HAVE_WAFL_NAME_CACHE_HIT | HAVE_WAFL_NAME_CACHE_MISS)
72 #define HAVE_WAFL_FIND_DIR_HIT 0x0400
73 #define HAVE_WAFL_FIND_DIR_MISS 0x0800
74 #define HAVE_WAFL_FIND_DIR (HAVE_WAFL_FIND_DIR_HIT | HAVE_WAFL_FIND_DIR_MISS)
75 #define HAVE_WAFL_BUF_HASH_HIT 0x1000
76 #define HAVE_WAFL_BUF_HASH_MISS 0x2000
77 #define HAVE_WAFL_BUF_HASH (HAVE_WAFL_BUF_HASH_HIT | HAVE_WAFL_BUF_HASH_MISS)
78 #define HAVE_WAFL_INODE_CACHE_HIT 0x4000
79 #define HAVE_WAFL_INODE_CACHE_MISS 0x8000
80 #define HAVE_WAFL_INODE_CACHE \
81 (HAVE_WAFL_INODE_CACHE_HIT | HAVE_WAFL_INODE_CACHE_MISS)
82 #define HAVE_WAFL_ALL 0xff00
83 typedef struct {
84 uint32_t flags;
85 cna_interval_t interval;
86 na_elem_t *query;
88 cdtime_t timestamp;
89 uint64_t name_cache_hit;
90 uint64_t name_cache_miss;
91 uint64_t find_dir_hit;
92 uint64_t find_dir_miss;
93 uint64_t buf_hash_hit;
94 uint64_t buf_hash_miss;
95 uint64_t inode_cache_hit;
96 uint64_t inode_cache_miss;
97 } cfg_wafl_t;
98 /* }}} cfg_wafl_t */
100 /*! Data types for disk statistics {{{
101 *
102 * \brief A disk in the NetApp.
103 *
104 * A disk doesn't have any more information than its name at the moment.
105 * The name includes the "disk_" prefix.
106 */
107 #define HAVE_DISK_BUSY 0x10
108 #define HAVE_DISK_BASE 0x20
109 #define HAVE_DISK_ALL 0x30
110 typedef struct disk_s {
111 char *name;
112 uint32_t flags;
113 cdtime_t timestamp;
114 uint64_t disk_busy;
115 uint64_t base_for_disk_busy;
116 double disk_busy_percent;
117 struct disk_s *next;
118 } disk_t;
120 #define CFG_DISK_BUSIEST 0x01
121 #define CFG_DISK_ALL 0x01
122 typedef struct {
123 uint32_t flags;
124 cna_interval_t interval;
125 na_elem_t *query;
126 disk_t *disks;
127 } cfg_disk_t;
128 /* }}} cfg_disk_t */
130 /*! Data types for volume performance statistics {{{
131 *
132 * \brief Persistent data for volume performance data.
133 *
134 * The code below uses the difference of the operations and latency counters to
135 * calculate an average per-operation latency. For this, old counters need to
136 * be stored in the "data_volume_perf_t" structure. The byte-counters are just
137 * kept for completeness sake. The "flags" member indicates if each counter is
138 * valid or not.
139 *
140 * The "cna_handle_volume_perf_data" function will fill a new struct of this
141 * type and pass both, old and new data, to "submit_volume_perf_data". In that
142 * function, the per-operation latency is calculated and dispatched, then the
143 * old counters are updated.
144 */
145 #define CFG_VOLUME_PERF_INIT 0x0001
146 #define CFG_VOLUME_PERF_IO 0x0002
147 #define CFG_VOLUME_PERF_OPS 0x0003
148 #define CFG_VOLUME_PERF_LATENCY 0x0008
149 #define CFG_VOLUME_PERF_ALL 0x000F
150 #define HAVE_VOLUME_PERF_BYTES_READ 0x0010
151 #define HAVE_VOLUME_PERF_BYTES_WRITE 0x0020
152 #define HAVE_VOLUME_PERF_OPS_READ 0x0040
153 #define HAVE_VOLUME_PERF_OPS_WRITE 0x0080
154 #define HAVE_VOLUME_PERF_LATENCY_READ 0x0100
155 #define HAVE_VOLUME_PERF_LATENCY_WRITE 0x0200
156 #define HAVE_VOLUME_PERF_ALL 0x03F0
157 struct data_volume_perf_s;
158 typedef struct data_volume_perf_s data_volume_perf_t;
159 struct data_volume_perf_s {
160 char *name;
161 uint32_t flags;
162 cdtime_t timestamp;
164 uint64_t read_bytes;
165 uint64_t write_bytes;
166 uint64_t read_ops;
167 uint64_t write_ops;
168 uint64_t read_latency;
169 uint64_t write_latency;
171 data_volume_perf_t *next;
172 };
174 typedef struct {
175 cna_interval_t interval;
176 na_elem_t *query;
178 ignorelist_t *il_octets;
179 ignorelist_t *il_operations;
180 ignorelist_t *il_latency;
182 data_volume_perf_t *volumes;
183 } cfg_volume_perf_t;
184 /* }}} data_volume_perf_t */
186 /*! Data types for volume usage statistics {{{
187 *
188 * \brief Configuration struct for volume usage data (free / used).
189 */
190 #define CFG_VOLUME_USAGE_DF 0x0002
191 #define CFG_VOLUME_USAGE_SNAP 0x0004
192 #define CFG_VOLUME_USAGE_ALL 0x0006
193 #define HAVE_VOLUME_USAGE_NORM_FREE 0x0010
194 #define HAVE_VOLUME_USAGE_NORM_USED 0x0020
195 #define HAVE_VOLUME_USAGE_SNAP_RSVD 0x0040
196 #define HAVE_VOLUME_USAGE_SNAP_USED 0x0080
197 #define HAVE_VOLUME_USAGE_SIS_SAVED 0x0100
198 #define HAVE_VOLUME_USAGE_COMPRESS_SAVED 0x0200
199 #define HAVE_VOLUME_USAGE_DEDUP_SAVED 0x0400
200 #define HAVE_VOLUME_USAGE_ALL 0x07f0
201 #define IS_VOLUME_USAGE_OFFLINE 0x0800
202 struct data_volume_usage_s;
203 typedef struct data_volume_usage_s data_volume_usage_t;
204 struct data_volume_usage_s {
205 char *name;
206 uint32_t flags;
208 na_elem_t *snap_query;
210 uint64_t norm_free;
211 uint64_t norm_used;
212 uint64_t snap_reserved;
213 uint64_t snap_used;
214 uint64_t sis_saved;
215 uint64_t compress_saved;
216 uint64_t dedup_saved;
218 data_volume_usage_t *next;
219 };
221 typedef struct {
222 cna_interval_t interval;
223 na_elem_t *query;
225 ignorelist_t *il_capacity;
226 ignorelist_t *il_snapshot;
228 data_volume_usage_t *volumes;
229 } cfg_volume_usage_t;
230 /* }}} cfg_volume_usage_t */
232 /*! Data types for quota statistics {{{
233 *
234 * \brief Persistent data for quota statistics
235 */
236 typedef struct {
237 cna_interval_t interval;
238 na_elem_t *query;
239 } cfg_quota_t;
240 /* }}} cfg_quota_t */
242 /*! Data types for SnapVault statistics {{{
243 *
244 * \brief Persistent data for SnapVault(R) statistics
245 */
246 typedef struct {
247 cna_interval_t interval;
248 na_elem_t *query;
249 } cfg_snapvault_t;
250 /* }}} cfg_snapvault_t */
252 /*! Data types for system statistics {{{
253 *
254 * \brief Persistent data for system performance counters
255 */
256 #define CFG_SYSTEM_CPU 0x01
257 #define CFG_SYSTEM_NET 0x02
258 #define CFG_SYSTEM_OPS 0x04
259 #define CFG_SYSTEM_DISK 0x08
260 #define CFG_SYSTEM_ALL 0x0F
261 typedef struct {
262 uint32_t flags;
263 cna_interval_t interval;
264 na_elem_t *query;
265 } cfg_system_t;
266 /* }}} cfg_system_t */
268 struct host_config_s {
269 char *name;
270 na_server_transport_t protocol;
271 char *host;
272 int port;
273 char *username;
274 char *password;
275 char *vfiler;
276 cdtime_t interval;
278 na_server_t *srv;
279 cfg_wafl_t *cfg_wafl;
280 cfg_disk_t *cfg_disk;
281 cfg_volume_perf_t *cfg_volume_perf;
282 cfg_volume_usage_t *cfg_volume_usage;
283 cfg_quota_t *cfg_quota;
284 cfg_snapvault_t *cfg_snapvault;
285 cfg_system_t *cfg_system;
287 struct host_config_s *next;
288 };
290 /*
291 * Free functions
292 *
293 * Used to free the various structures above.
294 */
295 static void free_disk(disk_t *disk) /* {{{ */
296 {
297 disk_t *next;
299 if (disk == NULL)
300 return;
302 next = disk->next;
304 sfree(disk->name);
305 sfree(disk);
307 free_disk(next);
308 } /* }}} void free_disk */
310 static void free_cfg_wafl(cfg_wafl_t *cw) /* {{{ */
311 {
312 if (cw == NULL)
313 return;
315 if (cw->query != NULL)
316 na_elem_free(cw->query);
318 sfree(cw);
319 } /* }}} void free_cfg_wafl */
321 static void free_cfg_disk(cfg_disk_t *cfg_disk) /* {{{ */
322 {
323 if (cfg_disk == NULL)
324 return;
326 if (cfg_disk->query != NULL)
327 na_elem_free(cfg_disk->query);
329 free_disk(cfg_disk->disks);
330 sfree(cfg_disk);
331 } /* }}} void free_cfg_disk */
333 static void free_cfg_volume_perf(cfg_volume_perf_t *cvp) /* {{{ */
334 {
335 data_volume_perf_t *data;
337 if (cvp == NULL)
338 return;
340 /* Free the ignorelists */
341 ignorelist_free(cvp->il_octets);
342 ignorelist_free(cvp->il_operations);
343 ignorelist_free(cvp->il_latency);
345 /* Free the linked list of volumes */
346 data = cvp->volumes;
347 while (data != NULL) {
348 data_volume_perf_t *next = data->next;
349 sfree(data->name);
350 sfree(data);
351 data = next;
352 }
354 if (cvp->query != NULL)
355 na_elem_free(cvp->query);
357 sfree(cvp);
358 } /* }}} void free_cfg_volume_perf */
360 static void free_cfg_volume_usage(cfg_volume_usage_t *cvu) /* {{{ */
361 {
362 data_volume_usage_t *data;
364 if (cvu == NULL)
365 return;
367 /* Free the ignorelists */
368 ignorelist_free(cvu->il_capacity);
369 ignorelist_free(cvu->il_snapshot);
371 /* Free the linked list of volumes */
372 data = cvu->volumes;
373 while (data != NULL) {
374 data_volume_usage_t *next = data->next;
375 sfree(data->name);
376 if (data->snap_query != NULL)
377 na_elem_free(data->snap_query);
378 sfree(data);
379 data = next;
380 }
382 if (cvu->query != NULL)
383 na_elem_free(cvu->query);
385 sfree(cvu);
386 } /* }}} void free_cfg_volume_usage */
388 static void free_cfg_quota(cfg_quota_t *q) /* {{{ */
389 {
390 if (q == NULL)
391 return;
393 if (q->query != NULL)
394 na_elem_free(q->query);
396 sfree(q);
397 } /* }}} void free_cfg_quota */
399 static void free_cfg_snapvault(cfg_snapvault_t *sv) /* {{{ */
400 {
401 if (sv == NULL)
402 return;
404 if (sv->query != NULL)
405 na_elem_free(sv->query);
407 sfree(sv);
408 } /* }}} void free_cfg_snapvault */
410 static void free_cfg_system(cfg_system_t *cs) /* {{{ */
411 {
412 if (cs == NULL)
413 return;
415 if (cs->query != NULL)
416 na_elem_free(cs->query);
418 sfree(cs);
419 } /* }}} void free_cfg_system */
421 static void free_host_config(host_config_t *hc) /* {{{ */
422 {
423 host_config_t *next;
425 if (hc == NULL)
426 return;
428 next = hc->next;
430 sfree(hc->name);
431 sfree(hc->host);
432 sfree(hc->username);
433 sfree(hc->password);
434 sfree(hc->vfiler);
436 free_cfg_disk(hc->cfg_disk);
437 free_cfg_wafl(hc->cfg_wafl);
438 free_cfg_volume_perf(hc->cfg_volume_perf);
439 free_cfg_volume_usage(hc->cfg_volume_usage);
440 free_cfg_quota(hc->cfg_quota);
441 free_cfg_snapvault(hc->cfg_snapvault);
442 free_cfg_system(hc->cfg_system);
444 if (hc->srv != NULL)
445 na_server_close(hc->srv);
447 sfree(hc);
449 free_host_config(next);
450 } /* }}} void free_host_config */
452 /*
453 * Auxiliary functions
454 *
455 * Used to look up volumes and disks or to handle flags.
456 */
457 static disk_t *get_disk(cfg_disk_t *cd, const char *name) /* {{{ */
458 {
459 disk_t *d;
461 if ((cd == NULL) || (name == NULL))
462 return (NULL);
464 for (d = cd->disks; d != NULL; d = d->next) {
465 if (strcmp(d->name, name) == 0)
466 return d;
467 }
469 d = calloc(1, sizeof(*d));
470 if (d == NULL)
471 return (NULL);
472 d->next = NULL;
474 d->name = strdup(name);
475 if (d->name == NULL) {
476 sfree(d);
477 return (NULL);
478 }
480 d->next = cd->disks;
481 cd->disks = d;
483 return d;
484 } /* }}} disk_t *get_disk */
486 static data_volume_usage_t *get_volume_usage(cfg_volume_usage_t *cvu, /* {{{ */
487 const char *name) {
488 data_volume_usage_t *last;
489 data_volume_usage_t *new;
491 int ignore_capacity = 0;
492 int ignore_snapshot = 0;
494 if ((cvu == NULL) || (name == NULL))
495 return (NULL);
497 last = cvu->volumes;
498 while (last != NULL) {
499 if (strcmp(last->name, name) == 0)
500 return (last);
502 if (last->next == NULL)
503 break;
505 last = last->next;
506 }
508 /* Check the ignorelists. If *both* tell us to ignore a volume, return NULL.
509 */
510 ignore_capacity = ignorelist_match(cvu->il_capacity, name);
511 ignore_snapshot = ignorelist_match(cvu->il_snapshot, name);
512 if ((ignore_capacity != 0) && (ignore_snapshot != 0))
513 return (NULL);
515 /* Not found: allocate. */
516 new = calloc(1, sizeof(*new));
517 if (new == NULL)
518 return (NULL);
519 new->next = NULL;
521 new->name = strdup(name);
522 if (new->name == NULL) {
523 sfree(new);
524 return (NULL);
525 }
527 if (ignore_capacity == 0)
528 new->flags |= CFG_VOLUME_USAGE_DF;
529 if (ignore_snapshot == 0) {
530 new->flags |= CFG_VOLUME_USAGE_SNAP;
531 new->snap_query = na_elem_new("snapshot-list-info");
532 na_child_add_string(new->snap_query, "target-type", "volume");
533 na_child_add_string(new->snap_query, "target-name", name);
534 } else {
535 new->snap_query = NULL;
536 }
538 /* Add to end of list. */
539 if (last == NULL)
540 cvu->volumes = new;
541 else
542 last->next = new;
544 return (new);
545 } /* }}} data_volume_usage_t *get_volume_usage */
547 static data_volume_perf_t *get_volume_perf(cfg_volume_perf_t *cvp, /* {{{ */
548 const char *name) {
549 data_volume_perf_t *last;
550 data_volume_perf_t *new;
552 int ignore_octets = 0;
553 int ignore_operations = 0;
554 int ignore_latency = 0;
556 if ((cvp == NULL) || (name == NULL))
557 return (NULL);
559 last = cvp->volumes;
560 while (last != NULL) {
561 if (strcmp(last->name, name) == 0)
562 return (last);
564 if (last->next == NULL)
565 break;
567 last = last->next;
568 }
570 /* Check the ignorelists. If *all three* tell us to ignore a volume, return
571 * NULL. */
572 ignore_octets = ignorelist_match(cvp->il_octets, name);
573 ignore_operations = ignorelist_match(cvp->il_operations, name);
574 ignore_latency = ignorelist_match(cvp->il_latency, name);
575 if ((ignore_octets != 0) || (ignore_operations != 0) || (ignore_latency != 0))
576 return (NULL);
578 /* Not found: allocate. */
579 new = calloc(1, sizeof(*new));
580 if (new == NULL)
581 return (NULL);
582 new->next = NULL;
584 new->name = strdup(name);
585 if (new->name == NULL) {
586 sfree(new);
587 return (NULL);
588 }
590 if (ignore_octets == 0)
591 new->flags |= CFG_VOLUME_PERF_IO;
592 if (ignore_operations == 0)
593 new->flags |= CFG_VOLUME_PERF_OPS;
594 if (ignore_latency == 0)
595 new->flags |= CFG_VOLUME_PERF_LATENCY;
597 /* Add to end of list. */
598 if (last == NULL)
599 cvp->volumes = new;
600 else
601 last->next = new;
603 return (new);
604 } /* }}} data_volume_perf_t *get_volume_perf */
606 /*
607 * Various submit functions.
608 *
609 * They all eventually call "submit_values" which creates a value_list_t and
610 * dispatches it to the daemon.
611 */
612 static int submit_values(const char *host, /* {{{ */
613 const char *plugin_inst, const char *type,
614 const char *type_inst, value_t *values,
615 size_t values_len, cdtime_t timestamp,
616 cdtime_t interval) {
617 value_list_t vl = VALUE_LIST_INIT;
619 vl.values = values;
620 vl.values_len = values_len;
622 if (timestamp > 0)
623 vl.time = timestamp;
625 if (interval > 0)
626 vl.interval = interval;
628 if (host != NULL)
629 sstrncpy(vl.host, host, sizeof(vl.host));
630 sstrncpy(vl.plugin, "netapp", sizeof(vl.plugin));
631 if (plugin_inst != NULL)
632 sstrncpy(vl.plugin_instance, plugin_inst, sizeof(vl.plugin_instance));
633 sstrncpy(vl.type, type, sizeof(vl.type));
634 if (type_inst != NULL)
635 sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
637 return (plugin_dispatch_values(&vl));
638 } /* }}} int submit_uint64 */
640 static int submit_two_derive(const char *host,
641 const char *plugin_inst, /* {{{ */
642 const char *type, const char *type_inst,
643 derive_t val0, derive_t val1, cdtime_t timestamp,
644 cdtime_t interval) {
645 value_t values[] = {
646 {.derive = val0}, {.derive = val1},
647 };
649 return (submit_values(host, plugin_inst, type, type_inst, values,
650 STATIC_ARRAY_SIZE(values), timestamp, interval));
651 } /* }}} int submit_two_derive */
653 static int submit_derive(const char *host, const char *plugin_inst, /* {{{ */
654 const char *type, const char *type_inst,
655 derive_t counter, cdtime_t timestamp,
656 cdtime_t interval) {
657 return (submit_values(host, plugin_inst, type, type_inst,
658 &(value_t){.derive = counter}, 1, timestamp, interval));
659 } /* }}} int submit_derive */
661 static int submit_two_gauge(const char *host, const char *plugin_inst, /* {{{ */
662 const char *type, const char *type_inst,
663 gauge_t val0, gauge_t val1, cdtime_t timestamp,
664 cdtime_t interval) {
665 value_t values[] = {
666 {.gauge = val0}, {.gauge = val1},
667 };
669 return (submit_values(host, plugin_inst, type, type_inst, values,
670 STATIC_ARRAY_SIZE(values), timestamp, interval));
671 } /* }}} int submit_two_gauge */
673 static int submit_double(const char *host, const char *plugin_inst, /* {{{ */
674 const char *type, const char *type_inst, double d,
675 cdtime_t timestamp, cdtime_t interval) {
676 return (submit_values(host, plugin_inst, type, type_inst,
677 &(value_t){.gauge = counter}, 1, timestamp, interval));
678 } /* }}} int submit_uint64 */
680 /* Calculate hit ratio from old and new counters and submit the resulting
681 * percentage. Used by "submit_wafl_data". */
682 static int submit_cache_ratio(const char *host, /* {{{ */
683 const char *plugin_inst, const char *type_inst,
684 uint64_t new_hits, uint64_t new_misses,
685 uint64_t old_hits, uint64_t old_misses,
686 cdtime_t timestamp, cdtime_t interval) {
687 value_t v = {.gauge = NAN};
689 if ((new_hits >= old_hits) && (new_misses >= old_misses)) {
690 uint64_t hits;
691 uint64_t misses;
693 hits = new_hits - old_hits;
694 misses = new_misses - old_misses;
696 v.gauge = 100.0 * ((gauge_t)hits) / ((gauge_t)(hits + misses));
697 }
699 return (submit_values(host, plugin_inst, "cache_ratio", type_inst, &v, 1,
700 timestamp, interval));
701 } /* }}} int submit_cache_ratio */
703 /* Submits all the caches used by WAFL. Uses "submit_cache_ratio". */
704 static int submit_wafl_data(const char *hostname,
705 const char *instance, /* {{{ */
706 cfg_wafl_t *old_data, const cfg_wafl_t *new_data,
707 cdtime_t interval) {
708 /* Submit requested counters */
709 if (HAS_ALL_FLAGS(old_data->flags,
710 CFG_WAFL_NAME_CACHE | HAVE_WAFL_NAME_CACHE) &&
711 HAS_ALL_FLAGS(new_data->flags, HAVE_WAFL_NAME_CACHE))
712 submit_cache_ratio(hostname, instance, "name_cache_hit",
713 new_data->name_cache_hit, new_data->name_cache_miss,
714 old_data->name_cache_hit, old_data->name_cache_miss,
715 new_data->timestamp, interval);
717 if (HAS_ALL_FLAGS(old_data->flags, CFG_WAFL_DIR_CACHE | HAVE_WAFL_FIND_DIR) &&
718 HAS_ALL_FLAGS(new_data->flags, HAVE_WAFL_FIND_DIR))
719 submit_cache_ratio(hostname, instance, "find_dir_hit",
720 new_data->find_dir_hit, new_data->find_dir_miss,
721 old_data->find_dir_hit, old_data->find_dir_miss,
722 new_data->timestamp, interval);
724 if (HAS_ALL_FLAGS(old_data->flags, CFG_WAFL_BUF_CACHE | HAVE_WAFL_BUF_HASH) &&
725 HAS_ALL_FLAGS(new_data->flags, HAVE_WAFL_BUF_HASH))
726 submit_cache_ratio(hostname, instance, "buf_hash_hit",
727 new_data->buf_hash_hit, new_data->buf_hash_miss,
728 old_data->buf_hash_hit, old_data->buf_hash_miss,
729 new_data->timestamp, interval);
731 if (HAS_ALL_FLAGS(old_data->flags,
732 CFG_WAFL_INODE_CACHE | HAVE_WAFL_INODE_CACHE) &&
733 HAS_ALL_FLAGS(new_data->flags, HAVE_WAFL_INODE_CACHE))
734 submit_cache_ratio(hostname, instance, "inode_cache_hit",
735 new_data->inode_cache_hit, new_data->inode_cache_miss,
736 old_data->inode_cache_hit, old_data->inode_cache_miss,
737 new_data->timestamp, interval);
739 /* Clear old HAVE_* flags */
740 old_data->flags &= ~HAVE_WAFL_ALL;
742 /* Copy all counters */
743 old_data->timestamp = new_data->timestamp;
744 old_data->name_cache_hit = new_data->name_cache_hit;
745 old_data->name_cache_miss = new_data->name_cache_miss;
746 old_data->find_dir_hit = new_data->find_dir_hit;
747 old_data->find_dir_miss = new_data->find_dir_miss;
748 old_data->buf_hash_hit = new_data->buf_hash_hit;
749 old_data->buf_hash_miss = new_data->buf_hash_miss;
750 old_data->inode_cache_hit = new_data->inode_cache_hit;
751 old_data->inode_cache_miss = new_data->inode_cache_miss;
753 /* Copy HAVE_* flags */
754 old_data->flags |= (new_data->flags & HAVE_WAFL_ALL);
756 return (0);
757 } /* }}} int submit_wafl_data */
759 /* Submits volume performance data to the daemon, taking care to honor and
760 * update flags appropriately. */
761 static int submit_volume_perf_data(const char *hostname, /* {{{ */
762 data_volume_perf_t *old_data,
763 const data_volume_perf_t *new_data,
764 int interval) {
765 char plugin_instance[DATA_MAX_NAME_LEN];
767 if ((hostname == NULL) || (old_data == NULL) || (new_data == NULL))
768 return (-1);
770 ssnprintf(plugin_instance, sizeof(plugin_instance), "volume-%s",
771 old_data->name);
773 /* Check for and submit disk-octet values */
774 if (HAS_ALL_FLAGS(old_data->flags, CFG_VOLUME_PERF_IO) &&
775 HAS_ALL_FLAGS(new_data->flags, HAVE_VOLUME_PERF_BYTES_READ |
776 HAVE_VOLUME_PERF_BYTES_WRITE)) {
777 submit_two_derive(
778 hostname, plugin_instance, "disk_octets", /* type instance = */ NULL,
779 (derive_t)new_data->read_bytes, (derive_t)new_data->write_bytes,
780 new_data->timestamp, interval);
781 }
783 /* Check for and submit disk-operations values */
784 if (HAS_ALL_FLAGS(old_data->flags, CFG_VOLUME_PERF_OPS) &&
785 HAS_ALL_FLAGS(new_data->flags,
786 HAVE_VOLUME_PERF_OPS_READ | HAVE_VOLUME_PERF_OPS_WRITE)) {
787 submit_two_derive(hostname, plugin_instance, "disk_ops",
788 /* type instance = */ NULL, (derive_t)new_data->read_ops,
789 (derive_t)new_data->write_ops, new_data->timestamp,
790 interval);
791 }
793 /* Check for, calculate and submit disk-latency values */
794 if (HAS_ALL_FLAGS(old_data->flags, CFG_VOLUME_PERF_LATENCY |
795 HAVE_VOLUME_PERF_OPS_READ |
796 HAVE_VOLUME_PERF_OPS_WRITE |
797 HAVE_VOLUME_PERF_LATENCY_READ |
798 HAVE_VOLUME_PERF_LATENCY_WRITE) &&
799 HAS_ALL_FLAGS(new_data->flags, HAVE_VOLUME_PERF_OPS_READ |
800 HAVE_VOLUME_PERF_OPS_WRITE |
801 HAVE_VOLUME_PERF_LATENCY_READ |
802 HAVE_VOLUME_PERF_LATENCY_WRITE)) {
803 gauge_t latency_per_op_read;
804 gauge_t latency_per_op_write;
806 latency_per_op_read = NAN;
807 latency_per_op_write = NAN;
809 /* Check if a counter wrapped around. */
810 if ((new_data->read_ops > old_data->read_ops) &&
811 (new_data->read_latency > old_data->read_latency)) {
812 uint64_t diff_ops_read;
813 uint64_t diff_latency_read;
815 diff_ops_read = new_data->read_ops - old_data->read_ops;
816 diff_latency_read = new_data->read_latency - old_data->read_latency;
818 if (diff_ops_read > 0)
819 latency_per_op_read =
820 ((gauge_t)diff_latency_read) / ((gauge_t)diff_ops_read);
821 }
823 if ((new_data->write_ops > old_data->write_ops) &&
824 (new_data->write_latency > old_data->write_latency)) {
825 uint64_t diff_ops_write;
826 uint64_t diff_latency_write;
828 diff_ops_write = new_data->write_ops - old_data->write_ops;
829 diff_latency_write = new_data->write_latency - old_data->write_latency;
831 if (diff_ops_write > 0)
832 latency_per_op_write =
833 ((gauge_t)diff_latency_write) / ((gauge_t)diff_ops_write);
834 }
836 submit_two_gauge(hostname, plugin_instance, "disk_latency",
837 /* type instance = */ NULL, latency_per_op_read,
838 latency_per_op_write, new_data->timestamp, interval);
839 }
841 /* Clear all HAVE_* flags. */
842 old_data->flags &= ~HAVE_VOLUME_PERF_ALL;
844 /* Copy all counters */
845 old_data->timestamp = new_data->timestamp;
846 old_data->read_bytes = new_data->read_bytes;
847 old_data->write_bytes = new_data->write_bytes;
848 old_data->read_ops = new_data->read_ops;
849 old_data->write_ops = new_data->write_ops;
850 old_data->read_latency = new_data->read_latency;
851 old_data->write_latency = new_data->write_latency;
853 /* Copy the HAVE_* flags */
854 old_data->flags |= (new_data->flags & HAVE_VOLUME_PERF_ALL);
856 return (0);
857 } /* }}} int submit_volume_perf_data */
859 static cdtime_t cna_child_get_cdtime(na_elem_t *data) /* {{{ */
860 {
861 time_t t;
863 t = (time_t)na_child_get_uint64(data, "timestamp", /* default = */ 0);
865 return (TIME_T_TO_CDTIME_T(t));
866 } /* }}} cdtime_t cna_child_get_cdtime */
868 /*
869 * Query functions
870 *
871 * These functions are called with appropriate data returned by the libnetapp
872 * interface which is parsed and submitted with the above functions.
873 */
874 /* Data corresponding to <WAFL /> */
875 static int cna_handle_wafl_data(const char *hostname,
876 cfg_wafl_t *cfg_wafl, /* {{{ */
877 na_elem_t *data, cdtime_t interval) {
878 cfg_wafl_t perf_data = {0};
879 const char *plugin_inst;
881 na_elem_t *instances;
882 na_elem_iter_t counter_iter;
884 perf_data.timestamp = cna_child_get_cdtime(data);
886 instances = na_elem_child(na_elem_child(data, "instances"), "instance-data");
887 if (instances == NULL) {
888 ERROR("netapp plugin: cna_handle_wafl_data: "
889 "na_elem_child (\"instances\") failed "
890 "for host %s.",
891 hostname);
892 return (-1);
893 }
895 plugin_inst = na_child_get_string(instances, "name");
896 if (plugin_inst == NULL) {
897 ERROR("netapp plugin: cna_handle_wafl_data: "
898 "na_child_get_string (\"name\") failed "
899 "for host %s.",
900 hostname);
901 return (-1);
902 }
904 /* Iterate over all counters */
905 counter_iter = na_child_iterator(na_elem_child(instances, "counters"));
906 for (na_elem_t *counter = na_iterator_next(&counter_iter); counter != NULL;
907 counter = na_iterator_next(&counter_iter)) {
908 const char *name;
909 uint64_t value;
911 name = na_child_get_string(counter, "name");
912 if (name == NULL)
913 continue;
915 value = na_child_get_uint64(counter, "value", UINT64_MAX);
916 if (value == UINT64_MAX)
917 continue;
919 if (!strcmp(name, "name_cache_hit")) {
920 perf_data.name_cache_hit = value;
921 perf_data.flags |= HAVE_WAFL_NAME_CACHE_HIT;
922 } else if (!strcmp(name, "name_cache_miss")) {
923 perf_data.name_cache_miss = value;
924 perf_data.flags |= HAVE_WAFL_NAME_CACHE_MISS;
925 } else if (!strcmp(name, "find_dir_hit")) {
926 perf_data.find_dir_hit = value;
927 perf_data.flags |= HAVE_WAFL_FIND_DIR_HIT;
928 } else if (!strcmp(name, "find_dir_miss")) {
929 perf_data.find_dir_miss = value;
930 perf_data.flags |= HAVE_WAFL_FIND_DIR_MISS;
931 } else if (!strcmp(name, "buf_hash_hit")) {
932 perf_data.buf_hash_hit = value;
933 perf_data.flags |= HAVE_WAFL_BUF_HASH_HIT;
934 } else if (!strcmp(name, "buf_hash_miss")) {
935 perf_data.buf_hash_miss = value;
936 perf_data.flags |= HAVE_WAFL_BUF_HASH_MISS;
937 } else if (!strcmp(name, "inode_cache_hit")) {
938 perf_data.inode_cache_hit = value;
939 perf_data.flags |= HAVE_WAFL_INODE_CACHE_HIT;
940 } else if (!strcmp(name, "inode_cache_miss")) {
941 perf_data.inode_cache_miss = value;
942 perf_data.flags |= HAVE_WAFL_INODE_CACHE_MISS;
943 } else {
944 DEBUG("netapp plugin: cna_handle_wafl_data: "
945 "Found unexpected child: %s "
946 "for host %s.",
947 name, hostname);
948 }
949 }
951 return (
952 submit_wafl_data(hostname, plugin_inst, cfg_wafl, &perf_data, interval));
953 } /* }}} void cna_handle_wafl_data */
955 static int cna_setup_wafl(cfg_wafl_t *cw) /* {{{ */
956 {
957 na_elem_t *e;
959 if (cw == NULL)
960 return (EINVAL);
962 if (cw->query != NULL)
963 return (0);
965 cw->query = na_elem_new("perf-object-get-instances");
966 if (cw->query == NULL) {
967 ERROR("netapp plugin: na_elem_new failed.");
968 return (-1);
969 }
970 na_child_add_string(cw->query, "objectname", "wafl");
972 e = na_elem_new("counters");
973 if (e == NULL) {
974 na_elem_free(cw->query);
975 cw->query = NULL;
976 ERROR("netapp plugin: na_elem_new failed.");
977 return (-1);
978 }
979 na_child_add_string(e, "counter", "name_cache_hit");
980 na_child_add_string(e, "counter", "name_cache_miss");
981 na_child_add_string(e, "counter", "find_dir_hit");
982 na_child_add_string(e, "counter", "find_dir_miss");
983 na_child_add_string(e, "counter", "buf_hash_hit");
984 na_child_add_string(e, "counter", "buf_hash_miss");
985 na_child_add_string(e, "counter", "inode_cache_hit");
986 na_child_add_string(e, "counter", "inode_cache_miss");
988 na_child_add(cw->query, e);
990 return (0);
991 } /* }}} int cna_setup_wafl */
993 static int cna_query_wafl(host_config_t *host) /* {{{ */
994 {
995 na_elem_t *data;
996 int status;
997 cdtime_t now;
999 if (host == NULL)
1000 return (EINVAL);
1002 /* If WAFL was not configured, return without doing anything. */
1003 if (host->cfg_wafl == NULL)
1004 return (0);
1006 now = cdtime();
1007 if ((host->cfg_wafl->interval.interval + host->cfg_wafl->interval.last_read) >
1008 now)
1009 return (0);
1011 status = cna_setup_wafl(host->cfg_wafl);
1012 if (status != 0)
1013 return (status);
1014 assert(host->cfg_wafl->query != NULL);
1016 data = na_server_invoke_elem(host->srv, host->cfg_wafl->query);
1017 if (na_results_status(data) != NA_OK) {
1018 ERROR("netapp plugin: cna_query_wafl: na_server_invoke_elem failed for "
1019 "host %s: %s",
1020 host->name, na_results_reason(data));
1021 na_elem_free(data);
1022 return (-1);
1023 }
1025 status = cna_handle_wafl_data(host->name, host->cfg_wafl, data,
1026 host->cfg_wafl->interval.interval);
1028 if (status == 0)
1029 host->cfg_wafl->interval.last_read = now;
1031 na_elem_free(data);
1032 return (status);
1033 } /* }}} int cna_query_wafl */
1035 /* Data corresponding to <Disks /> */
1036 static int cna_handle_disk_data(const char *hostname, /* {{{ */
1037 cfg_disk_t *cfg_disk, na_elem_t *data,
1038 cdtime_t interval) {
1039 cdtime_t timestamp;
1040 na_elem_t *instances;
1041 na_elem_iter_t instance_iter;
1042 disk_t *worst_disk = NULL;
1044 if ((cfg_disk == NULL) || (data == NULL))
1045 return (EINVAL);
1047 timestamp = cna_child_get_cdtime(data);
1049 instances = na_elem_child(data, "instances");
1050 if (instances == NULL) {
1051 ERROR("netapp plugin: cna_handle_disk_data: "
1052 "na_elem_child (\"instances\") failed "
1053 "for host %s.",
1054 hostname);
1055 return (-1);
1056 }
1058 /* Iterate over all children */
1059 instance_iter = na_child_iterator(instances);
1060 for (na_elem_t *instance = na_iterator_next(&instance_iter); instance != NULL;
1061 instance = na_iterator_next(&instance_iter)) {
1062 disk_t *old_data;
1063 disk_t new_data = {0};
1065 na_elem_iter_t counter_iterator;
1067 new_data.timestamp = timestamp;
1068 new_data.disk_busy_percent = NAN;
1070 old_data = get_disk(cfg_disk, na_child_get_string(instance, "name"));
1071 if (old_data == NULL)
1072 continue;
1074 /* Look for the "disk_busy" and "base_for_disk_busy" counters */
1075 counter_iterator = na_child_iterator(na_elem_child(instance, "counters"));
1076 for (na_elem_t *counter = na_iterator_next(&counter_iterator);
1077 counter != NULL; counter = na_iterator_next(&counter_iterator)) {
1078 const char *name;
1079 uint64_t value;
1081 name = na_child_get_string(counter, "name");
1082 if (name == NULL)
1083 continue;
1085 value = na_child_get_uint64(counter, "value", UINT64_MAX);
1086 if (value == UINT64_MAX)
1087 continue;
1089 if (strcmp(name, "disk_busy") == 0) {
1090 new_data.disk_busy = value;
1091 new_data.flags |= HAVE_DISK_BUSY;
1092 } else if (strcmp(name, "base_for_disk_busy") == 0) {
1093 new_data.base_for_disk_busy = value;
1094 new_data.flags |= HAVE_DISK_BASE;
1095 } else {
1096 DEBUG("netapp plugin: cna_handle_disk_data: "
1097 "Counter not handled: %s = %" PRIu64,
1098 name, value);
1099 }
1100 }
1102 /* If all required counters are available and did not just wrap around,
1103 * calculate the busy percentage. Otherwise, the value is initialized to
1104 * NAN at the top of the for-loop. */
1105 if (HAS_ALL_FLAGS(old_data->flags, HAVE_DISK_BUSY | HAVE_DISK_BASE) &&
1106 HAS_ALL_FLAGS(new_data.flags, HAVE_DISK_BUSY | HAVE_DISK_BASE) &&
1107 (new_data.disk_busy >= old_data->disk_busy) &&
1108 (new_data.base_for_disk_busy > old_data->base_for_disk_busy)) {
1109 uint64_t busy_diff;
1110 uint64_t base_diff;
1112 busy_diff = new_data.disk_busy - old_data->disk_busy;
1113 base_diff = new_data.base_for_disk_busy - old_data->base_for_disk_busy;
1115 new_data.disk_busy_percent =
1116 100.0 * ((gauge_t)busy_diff) / ((gauge_t)base_diff);
1117 }
1119 /* Clear HAVE_* flags */
1120 old_data->flags &= ~HAVE_DISK_ALL;
1122 /* Copy data */
1123 old_data->timestamp = new_data.timestamp;
1124 old_data->disk_busy = new_data.disk_busy;
1125 old_data->base_for_disk_busy = new_data.base_for_disk_busy;
1126 old_data->disk_busy_percent = new_data.disk_busy_percent;
1128 /* Copy flags */
1129 old_data->flags |= (new_data.flags & HAVE_DISK_ALL);
1131 if ((worst_disk == NULL) ||
1132 (worst_disk->disk_busy_percent < old_data->disk_busy_percent))
1133 worst_disk = old_data;
1134 } /* for (all disks) */
1136 if ((cfg_disk->flags & CFG_DISK_BUSIEST) && (worst_disk != NULL))
1137 submit_double(hostname, "system", "percent", "disk_busy",
1138 worst_disk->disk_busy_percent, timestamp, interval);
1140 return (0);
1141 } /* }}} int cna_handle_disk_data */
1143 static int cna_setup_disk(cfg_disk_t *cd) /* {{{ */
1144 {
1145 na_elem_t *e;
1147 if (cd == NULL)
1148 return (EINVAL);
1150 if (cd->query != NULL)
1151 return (0);
1153 cd->query = na_elem_new("perf-object-get-instances");
1154 if (cd->query == NULL) {
1155 ERROR("netapp plugin: na_elem_new failed.");
1156 return (-1);
1157 }
1158 na_child_add_string(cd->query, "objectname", "disk");
1160 e = na_elem_new("counters");
1161 if (e == NULL) {
1162 na_elem_free(cd->query);
1163 cd->query = NULL;
1164 ERROR("netapp plugin: na_elem_new failed.");
1165 return (-1);
1166 }
1167 na_child_add_string(e, "counter", "disk_busy");
1168 na_child_add_string(e, "counter", "base_for_disk_busy");
1169 na_child_add(cd->query, e);
1171 return (0);
1172 } /* }}} int cna_setup_disk */
1174 static int cna_query_disk(host_config_t *host) /* {{{ */
1175 {
1176 na_elem_t *data;
1177 int status;
1178 cdtime_t now;
1180 if (host == NULL)
1181 return (EINVAL);
1183 /* If the user did not configure disk statistics, return without doing
1184 * anything. */
1185 if (host->cfg_disk == NULL)
1186 return (0);
1188 now = cdtime();
1189 if ((host->cfg_disk->interval.interval + host->cfg_disk->interval.last_read) >
1190 now)
1191 return (0);
1193 status = cna_setup_disk(host->cfg_disk);
1194 if (status != 0)
1195 return (status);
1196 assert(host->cfg_disk->query != NULL);
1198 data = na_server_invoke_elem(host->srv, host->cfg_disk->query);
1199 if (na_results_status(data) != NA_OK) {
1200 ERROR("netapp plugin: cna_query_disk: na_server_invoke_elem failed for "
1201 "host %s: %s",
1202 host->name, na_results_reason(data));
1203 na_elem_free(data);
1204 return (-1);
1205 }
1207 status = cna_handle_disk_data(host->name, host->cfg_disk, data,
1208 host->cfg_disk->interval.interval);
1210 if (status == 0)
1211 host->cfg_disk->interval.last_read = now;
1213 na_elem_free(data);
1214 return (status);
1215 } /* }}} int cna_query_disk */
1217 /* Data corresponding to <VolumePerf /> */
1218 static int cna_handle_volume_perf_data(const char *hostname, /* {{{ */
1219 cfg_volume_perf_t *cvp, na_elem_t *data,
1220 cdtime_t interval) {
1221 cdtime_t timestamp;
1222 na_elem_t *elem_instances;
1223 na_elem_iter_t iter_instances;
1225 timestamp = cna_child_get_cdtime(data);
1227 elem_instances = na_elem_child(data, "instances");
1228 if (elem_instances == NULL) {
1229 ERROR("netapp plugin: handle_volume_perf_data: "
1230 "na_elem_child (\"instances\") failed "
1231 "for host %s.",
1232 hostname);
1233 return (-1);
1234 }
1236 iter_instances = na_child_iterator(elem_instances);
1237 for (na_elem_t *elem_instance = na_iterator_next(&iter_instances);
1238 elem_instance != NULL;
1239 elem_instance = na_iterator_next(&iter_instances)) {
1240 const char *name;
1242 data_volume_perf_t perf_data = {0};
1243 data_volume_perf_t *v;
1245 na_elem_t *elem_counters;
1246 na_elem_iter_t iter_counters;
1248 perf_data.timestamp = timestamp;
1250 name = na_child_get_string(elem_instance, "name");
1251 if (name == NULL)
1252 continue;
1254 /* get_volume_perf may return NULL if this volume is to be ignored. */
1255 v = get_volume_perf(cvp, name);
1256 if (v == NULL)
1257 continue;
1259 elem_counters = na_elem_child(elem_instance, "counters");
1260 if (elem_counters == NULL)
1261 continue;
1263 iter_counters = na_child_iterator(elem_counters);
1264 for (na_elem_t *elem_counter = na_iterator_next(&iter_counters);
1265 elem_counter != NULL;
1266 elem_counter = na_iterator_next(&iter_counters)) {
1267 const char *name;
1268 uint64_t value;
1270 name = na_child_get_string(elem_counter, "name");
1271 if (name == NULL)
1272 continue;
1274 value = na_child_get_uint64(elem_counter, "value", UINT64_MAX);
1275 if (value == UINT64_MAX)
1276 continue;
1278 if (!strcmp(name, "read_data")) {
1279 perf_data.read_bytes = value;
1280 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_READ;
1281 } else if (!strcmp(name, "write_data")) {
1282 perf_data.write_bytes = value;
1283 perf_data.flags |= HAVE_VOLUME_PERF_BYTES_WRITE;
1284 } else if (!strcmp(name, "read_ops")) {
1285 perf_data.read_ops = value;
1286 perf_data.flags |= HAVE_VOLUME_PERF_OPS_READ;
1287 } else if (!strcmp(name, "write_ops")) {
1288 perf_data.write_ops = value;
1289 perf_data.flags |= HAVE_VOLUME_PERF_OPS_WRITE;
1290 } else if (!strcmp(name, "read_latency")) {
1291 perf_data.read_latency = value;
1292 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_READ;
1293 } else if (!strcmp(name, "write_latency")) {
1294 perf_data.write_latency = value;
1295 perf_data.flags |= HAVE_VOLUME_PERF_LATENCY_WRITE;
1296 }
1297 } /* for (elem_counter) */
1299 submit_volume_perf_data(hostname, v, &perf_data, interval);
1300 } /* for (volume) */
1302 return (0);
1303 } /* }}} int cna_handle_volume_perf_data */
1305 static int cna_setup_volume_perf(cfg_volume_perf_t *cd) /* {{{ */
1306 {
1307 na_elem_t *e;
1309 if (cd == NULL)
1310 return (EINVAL);
1312 if (cd->query != NULL)
1313 return (0);
1315 cd->query = na_elem_new("perf-object-get-instances");
1316 if (cd->query == NULL) {
1317 ERROR("netapp plugin: na_elem_new failed.");
1318 return (-1);
1319 }
1320 na_child_add_string(cd->query, "objectname", "volume");
1322 e = na_elem_new("counters");
1323 if (e == NULL) {
1324 na_elem_free(cd->query);
1325 cd->query = NULL;
1326 ERROR("netapp plugin: na_elem_new failed.");
1327 return (-1);
1328 }
1329 na_child_add_string(e, "counter", "read_ops");
1330 na_child_add_string(e, "counter", "write_ops");
1331 na_child_add_string(e, "counter", "read_data");
1332 na_child_add_string(e, "counter", "write_data");
1333 na_child_add_string(e, "counter", "read_latency");
1334 na_child_add_string(e, "counter", "write_latency");
1335 na_child_add(cd->query, e);
1337 return (0);
1338 } /* }}} int cna_setup_volume_perf */
1340 static int cna_query_volume_perf(host_config_t *host) /* {{{ */
1341 {
1342 na_elem_t *data;
1343 int status;
1344 cdtime_t now;
1346 if (host == NULL)
1347 return (EINVAL);
1349 /* If the user did not configure volume performance statistics, return
1350 * without doing anything. */
1351 if (host->cfg_volume_perf == NULL)
1352 return (0);
1354 now = cdtime();
1355 if ((host->cfg_volume_perf->interval.interval +
1356 host->cfg_volume_perf->interval.last_read) > now)
1357 return (0);
1359 status = cna_setup_volume_perf(host->cfg_volume_perf);
1360 if (status != 0)
1361 return (status);
1362 assert(host->cfg_volume_perf->query != NULL);
1364 data = na_server_invoke_elem(host->srv, host->cfg_volume_perf->query);
1365 if (na_results_status(data) != NA_OK) {
1366 ERROR("netapp plugin: cna_query_volume_perf: na_server_invoke_elem failed "
1367 "for host %s: %s",
1368 host->name, na_results_reason(data));
1369 na_elem_free(data);
1370 return (-1);
1371 }
1373 status =
1374 cna_handle_volume_perf_data(host->name, host->cfg_volume_perf, data,
1375 host->cfg_volume_perf->interval.interval);
1377 if (status == 0)
1378 host->cfg_volume_perf->interval.last_read = now;
1380 na_elem_free(data);
1381 return (status);
1382 } /* }}} int cna_query_volume_perf */
1384 /* Data corresponding to <VolumeUsage /> */
1385 static int cna_submit_volume_usage_data(const char *hostname, /* {{{ */
1386 cfg_volume_usage_t *cfg_volume,
1387 int interval) {
1388 for (data_volume_usage_t *v = cfg_volume->volumes; v != NULL; v = v->next) {
1389 char plugin_instance[DATA_MAX_NAME_LEN];
1391 uint64_t norm_used = v->norm_used;
1392 uint64_t norm_free = v->norm_free;
1393 uint64_t sis_saved = v->sis_saved;
1394 uint64_t compress_saved = v->compress_saved;
1395 uint64_t dedup_saved = v->dedup_saved;
1396 uint64_t snap_reserve_used = 0;
1397 uint64_t snap_reserve_free = v->snap_reserved;
1398 uint64_t snap_norm_used = v->snap_used;
1400 ssnprintf(plugin_instance, sizeof(plugin_instance), "volume-%s", v->name);
1402 if (HAS_ALL_FLAGS(v->flags, HAVE_VOLUME_USAGE_SNAP_USED |
1403 HAVE_VOLUME_USAGE_SNAP_RSVD)) {
1404 if (v->snap_reserved > v->snap_used) {
1405 snap_reserve_free = v->snap_reserved - v->snap_used;
1406 snap_reserve_used = v->snap_used;
1407 snap_norm_used = 0;
1408 } else {
1409 snap_reserve_free = 0;
1410 snap_reserve_used = v->snap_reserved;
1411 snap_norm_used = v->snap_used - v->snap_reserved;
1412 }
1413 }
1415 /* The space used by snapshots but not reserved for them is included in
1416 * both, norm_used and snap_norm_used. If possible, subtract this here. */
1417 if (HAS_ALL_FLAGS(v->flags, HAVE_VOLUME_USAGE_NORM_USED |
1418 HAVE_VOLUME_USAGE_SNAP_USED)) {
1419 if (norm_used >= snap_norm_used)
1420 norm_used -= snap_norm_used;
1421 else {
1422 ERROR("netapp plugin: (norm_used = %" PRIu64 ") < (snap_norm_used = "
1423 "%" PRIu64 ") for host %s. Invalidating both.",
1424 norm_used, snap_norm_used, hostname);
1425 v->flags &=
1426 ~(HAVE_VOLUME_USAGE_NORM_USED | HAVE_VOLUME_USAGE_SNAP_USED);
1427 }
1428 }
1430 if (HAS_ALL_FLAGS(v->flags, HAVE_VOLUME_USAGE_NORM_FREE))
1431 submit_double(hostname, /* plugin instance = */ plugin_instance,
1432 "df_complex", "free", (double)norm_free,
1433 /* timestamp = */ 0, interval);
1435 if (HAS_ALL_FLAGS(v->flags, HAVE_VOLUME_USAGE_SIS_SAVED))
1436 submit_double(hostname, /* plugin instance = */ plugin_instance,
1437 "df_complex", "sis_saved", (double)sis_saved,
1438 /* timestamp = */ 0, interval);
1440 if (HAS_ALL_FLAGS(v->flags, HAVE_VOLUME_USAGE_COMPRESS_SAVED))
1441 submit_double(hostname, /* plugin instance = */ plugin_instance,
1442 "df_complex", "compression_saved", (double)compress_saved,
1443 /* timestamp = */ 0, interval);
1445 if (HAS_ALL_FLAGS(v->flags, HAVE_VOLUME_USAGE_DEDUP_SAVED))
1446 submit_double(hostname, /* plugin instance = */ plugin_instance,
1447 "df_complex", "dedup_saved", (double)dedup_saved,
1448 /* timestamp = */ 0, interval);
1450 if (HAS_ALL_FLAGS(v->flags, HAVE_VOLUME_USAGE_NORM_USED))
1451 submit_double(hostname, /* plugin instance = */ plugin_instance,
1452 "df_complex", "used", (double)norm_used,
1453 /* timestamp = */ 0, interval);
1455 if (HAS_ALL_FLAGS(v->flags, HAVE_VOLUME_USAGE_SNAP_RSVD))
1456 submit_double(hostname, /* plugin instance = */ plugin_instance,
1457 "df_complex", "snap_reserved", (double)snap_reserve_free,
1458 /* timestamp = */ 0, interval);
1460 if (HAS_ALL_FLAGS(v->flags, HAVE_VOLUME_USAGE_SNAP_USED |
1461 HAVE_VOLUME_USAGE_SNAP_RSVD))
1462 submit_double(hostname, /* plugin instance = */ plugin_instance,
1463 "df_complex", "snap_reserve_used",
1464 (double)snap_reserve_used, /* timestamp = */ 0, interval);
1466 if (HAS_ALL_FLAGS(v->flags, HAVE_VOLUME_USAGE_SNAP_USED))
1467 submit_double(hostname, /* plugin instance = */ plugin_instance,
1468 "df_complex", "snap_normal_used", (double)snap_norm_used,
1469 /* timestamp = */ 0, interval);
1471 /* Clear all the HAVE_* flags */
1472 v->flags &= ~HAVE_VOLUME_USAGE_ALL;
1473 } /* for (v = cfg_volume->volumes) */
1475 return (0);
1476 } /* }}} int cna_submit_volume_usage_data */
1478 /* Switch the state of a volume between online and offline and send out a
1479 * notification. */
1480 static int cna_change_volume_status(const char *hostname, /* {{{ */
1481 data_volume_usage_t *v) {
1482 notification_t n = {0};
1484 n.time = cdtime();
1485 sstrncpy(n.host, hostname, sizeof(n.host));
1486 sstrncpy(n.plugin, "netapp", sizeof(n.plugin));
1487 sstrncpy(n.plugin_instance, v->name, sizeof(n.plugin_instance));
1489 if ((v->flags & IS_VOLUME_USAGE_OFFLINE) != 0) {
1490 n.severity = NOTIF_OKAY;
1491 ssnprintf(n.message, sizeof(n.message), "Volume %s is now online.",
1492 v->name);
1493 v->flags &= ~IS_VOLUME_USAGE_OFFLINE;
1494 } else {
1495 n.severity = NOTIF_WARNING;
1496 ssnprintf(n.message, sizeof(n.message), "Volume %s is now offline.",
1497 v->name);
1498 v->flags |= IS_VOLUME_USAGE_OFFLINE;
1499 }
1501 return (plugin_dispatch_notification(&n));
1502 } /* }}} int cna_change_volume_status */
1504 static void cna_handle_volume_snap_usage(const host_config_t *host, /* {{{ */
1505 data_volume_usage_t *v) {
1506 uint64_t snap_used = 0, value;
1507 na_elem_t *data, *elem_snapshots;
1508 na_elem_iter_t iter_snap;
1510 data = na_server_invoke_elem(host->srv, v->snap_query);
1511 if (na_results_status(data) != NA_OK) {
1512 if (na_results_errno(data) == EVOLUMEOFFLINE) {
1513 if ((v->flags & IS_VOLUME_USAGE_OFFLINE) == 0)
1514 cna_change_volume_status(host->name, v);
1515 } else {
1516 ERROR("netapp plugin: cna_handle_volume_snap_usage: "
1517 "na_server_invoke_elem for "
1518 "volume \"%s\" on host %s failed with error %d: %s",
1519 v->name, host->name, na_results_errno(data),
1520 na_results_reason(data));
1521 }
1522 na_elem_free(data);
1523 return;
1524 }
1526 if ((v->flags & IS_VOLUME_USAGE_OFFLINE) != 0)
1527 cna_change_volume_status(host->name, v);
1529 elem_snapshots = na_elem_child(data, "snapshots");
1530 if (elem_snapshots == NULL) {
1531 ERROR("netapp plugin: cna_handle_volume_snap_usage: "
1532 "na_elem_child (\"snapshots\") failed "
1533 "for host %s.",
1534 host->name);
1535 na_elem_free(data);
1536 return;
1537 }
1539 iter_snap = na_child_iterator(elem_snapshots);
1540 for (na_elem_t *elem_snap = na_iterator_next(&iter_snap); elem_snap != NULL;
1541 elem_snap = na_iterator_next(&iter_snap)) {
1542 value = na_child_get_uint64(elem_snap, "cumulative-total", 0);
1543 /* "cumulative-total" is the total size of the oldest snapshot plus all
1544 * newer ones in blocks (1KB). We therefore are looking for the highest
1545 * number of all snapshots - that's the size required for the snapshots. */
1546 if (value > snap_used)
1547 snap_used = value;
1548 }
1549 na_elem_free(data);
1550 /* snap_used is in 1024 byte blocks */
1551 v->snap_used = snap_used * 1024;
1552 v->flags |= HAVE_VOLUME_USAGE_SNAP_USED;
1553 } /* }}} void cna_handle_volume_snap_usage */
1555 static void cna_handle_volume_sis_data(const host_config_t *host, /* {{{ */
1556 data_volume_usage_t *v, na_elem_t *sis) {
1557 const char *sis_state;
1558 uint64_t sis_saved_reported;
1560 if (na_elem_child(sis, "sis-info"))
1561 sis = na_elem_child(sis, "sis-info");
1563 sis_state = na_child_get_string(sis, "state");
1564 if (sis_state == NULL)
1565 return;
1567 /* If SIS is not enabled, there's nothing left to do for this volume. */
1568 if (strcmp("enabled", sis_state) != 0)
1569 return;
1571 sis_saved_reported = na_child_get_uint64(sis, "size-saved", UINT64_MAX);
1572 if (sis_saved_reported == UINT64_MAX)
1573 return;
1575 /* size-saved is actually a 32 bit number, so ... time for some guesswork. */
1576 if ((sis_saved_reported >> 32) != 0) {
1577 /* In case they ever fix this bug. */
1578 v->sis_saved = sis_saved_reported;
1579 v->flags |= HAVE_VOLUME_USAGE_SIS_SAVED;
1580 } else { /* really hacky work-around code. {{{ */
1581 uint64_t sis_saved_percent;
1582 uint64_t sis_saved_guess;
1583 uint64_t overflow_guess;
1584 uint64_t guess1, guess2, guess3;
1586 /* Check if we have v->norm_used. Without it, we cannot calculate
1587 * sis_saved_guess. */
1588 if ((v->flags & HAVE_VOLUME_USAGE_NORM_USED) == 0)
1589 return;
1591 sis_saved_percent =
1592 na_child_get_uint64(sis, "percentage-saved", UINT64_MAX);
1593 if (sis_saved_percent > 100)
1594 return;
1596 /* The "size-saved" value is a 32bit unsigned integer. This is a bug and
1597 * will hopefully be fixed in later versions. To work around the bug, try
1598 * to figure out how often the 32bit integer wrapped around by using the
1599 * "percentage-saved" value. Because the percentage is in the range
1600 * [0-100], this should work as long as the saved space does not exceed
1601 * 400 GBytes. */
1602 /* percentage-saved = size-saved / (size-saved + size-used) */
1603 if (sis_saved_percent < 100)
1604 sis_saved_guess =
1605 v->norm_used * sis_saved_percent / (100 - sis_saved_percent);
1606 else
1607 sis_saved_guess = v->norm_used;
1609 overflow_guess = sis_saved_guess >> 32;
1610 guess1 = overflow_guess ? ((overflow_guess - 1) << 32) + sis_saved_reported
1611 : sis_saved_reported;
1612 guess2 = (overflow_guess << 32) + sis_saved_reported;
1613 guess3 = ((overflow_guess + 1) << 32) + sis_saved_reported;
1615 if (sis_saved_guess < guess2) {
1616 if ((sis_saved_guess - guess1) < (guess2 - sis_saved_guess))
1617 v->sis_saved = guess1;
1618 else
1619 v->sis_saved = guess2;
1620 } else {
1621 if ((sis_saved_guess - guess2) < (guess3 - sis_saved_guess))
1622 v->sis_saved = guess2;
1623 else
1624 v->sis_saved = guess3;
1625 }
1626 v->flags |= HAVE_VOLUME_USAGE_SIS_SAVED;
1627 } /* }}} end of 32-bit workaround */
1628 } /* }}} void cna_handle_volume_sis_data */
1630 /* ONTAP >= 8.1 uses SIS for managing dedup and compression */
1631 static void cna_handle_volume_sis_saved(const host_config_t *host, /* {{{ */
1632 data_volume_usage_t *v,
1633 na_elem_t *sis) {
1634 uint64_t saved;
1636 if (na_elem_child(sis, "sis-info"))
1637 sis = na_elem_child(sis, "sis-info");
1639 saved = na_child_get_uint64(sis, "compress-saved", UINT64_MAX);
1640 if (saved != UINT64_MAX) {
1641 v->compress_saved = saved;
1642 v->flags |= HAVE_VOLUME_USAGE_COMPRESS_SAVED;
1643 }
1645 saved = na_child_get_uint64(sis, "dedup-saved", UINT64_MAX);
1646 if (saved != UINT64_MAX) {
1647 v->dedup_saved = saved;
1648 v->flags |= HAVE_VOLUME_USAGE_DEDUP_SAVED;
1649 }
1650 } /* }}} void cna_handle_volume_sis_saved */
1652 static int cna_handle_volume_usage_data(const host_config_t *host, /* {{{ */
1653 cfg_volume_usage_t *cfg_volume,
1654 na_elem_t *data) {
1655 na_elem_t *elem_volumes;
1656 na_elem_iter_t iter_volume;
1658 elem_volumes = na_elem_child(data, "volumes");
1659 if (elem_volumes == NULL) {
1660 ERROR("netapp plugin: cna_handle_volume_usage_data: "
1661 "na_elem_child (\"volumes\") failed "
1662 "for host %s.",
1663 host->name);
1664 return (-1);
1665 }
1667 iter_volume = na_child_iterator(elem_volumes);
1668 for (na_elem_t *elem_volume = na_iterator_next(&iter_volume);
1669 elem_volume != NULL; elem_volume = na_iterator_next(&iter_volume)) {
1670 const char *volume_name, *state;
1672 data_volume_usage_t *v;
1673 uint64_t value;
1675 na_elem_t *sis;
1677 volume_name = na_child_get_string(elem_volume, "name");
1678 if (volume_name == NULL)
1679 continue;
1681 state = na_child_get_string(elem_volume, "state");
1682 if ((state == NULL) || (strcmp(state, "online") != 0))
1683 continue;
1685 /* get_volume_usage may return NULL if the volume is to be ignored. */
1686 v = get_volume_usage(cfg_volume, volume_name);
1687 if (v == NULL)
1688 continue;
1690 if ((v->flags & CFG_VOLUME_USAGE_SNAP) != 0)
1691 cna_handle_volume_snap_usage(host, v);
1693 if ((v->flags & CFG_VOLUME_USAGE_DF) == 0)
1694 continue;
1696 /* 2^4 exa-bytes? This will take a while ;) */
1697 value = na_child_get_uint64(elem_volume, "size-available", UINT64_MAX);
1698 if (value != UINT64_MAX) {
1699 v->norm_free = value;
1700 v->flags |= HAVE_VOLUME_USAGE_NORM_FREE;
1701 }
1703 value = na_child_get_uint64(elem_volume, "size-used", UINT64_MAX);
1704 if (value != UINT64_MAX) {
1705 v->norm_used = value;
1706 v->flags |= HAVE_VOLUME_USAGE_NORM_USED;
1707 }
1709 value = na_child_get_uint64(elem_volume, "snapshot-blocks-reserved",
1710 UINT64_MAX);
1711 if (value != UINT64_MAX) {
1712 /* 1 block == 1024 bytes as per API docs */
1713 v->snap_reserved = 1024 * value;
1714 v->flags |= HAVE_VOLUME_USAGE_SNAP_RSVD;
1715 }
1717 sis = na_elem_child(elem_volume, "sis");
1718 if (sis != NULL) {
1719 cna_handle_volume_sis_data(host, v, sis);
1720 cna_handle_volume_sis_saved(host, v, sis);
1721 }
1722 } /* for (elem_volume) */
1724 return (cna_submit_volume_usage_data(
1725 host->name, cfg_volume, host->cfg_volume_usage->interval.interval));
1726 } /* }}} int cna_handle_volume_usage_data */
1728 static int cna_setup_volume_usage(cfg_volume_usage_t *cvu) /* {{{ */
1729 {
1730 if (cvu == NULL)
1731 return (EINVAL);
1733 if (cvu->query != NULL)
1734 return (0);
1736 cvu->query = na_elem_new("volume-list-info");
1737 if (cvu->query == NULL) {
1738 ERROR("netapp plugin: na_elem_new failed.");
1739 return (-1);
1740 }
1742 return (0);
1743 } /* }}} int cna_setup_volume_usage */
1745 static int cna_query_volume_usage(host_config_t *host) /* {{{ */
1746 {
1747 na_elem_t *data;
1748 int status;
1749 cdtime_t now;
1751 if (host == NULL)
1752 return (EINVAL);
1754 /* If the user did not configure volume_usage statistics, return without
1755 * doing anything. */
1756 if (host->cfg_volume_usage == NULL)
1757 return (0);
1759 now = cdtime();
1760 if ((host->cfg_volume_usage->interval.interval +
1761 host->cfg_volume_usage->interval.last_read) > now)
1762 return (0);
1764 status = cna_setup_volume_usage(host->cfg_volume_usage);
1765 if (status != 0)
1766 return (status);
1767 assert(host->cfg_volume_usage->query != NULL);
1769 data = na_server_invoke_elem(host->srv, host->cfg_volume_usage->query);
1770 if (na_results_status(data) != NA_OK) {
1771 ERROR("netapp plugin: cna_query_volume_usage: na_server_invoke_elem failed "
1772 "for host %s: %s",
1773 host->name, na_results_reason(data));
1774 na_elem_free(data);
1775 return (-1);
1776 }
1778 status = cna_handle_volume_usage_data(host, host->cfg_volume_usage, data);
1780 if (status == 0)
1781 host->cfg_volume_usage->interval.last_read = now;
1783 na_elem_free(data);
1784 return (status);
1785 } /* }}} int cna_query_volume_usage */
1787 /* Data corresponding to <Quota /> */
1788 static int cna_handle_quota_data(const host_config_t *host, /* {{{ */
1789 cfg_quota_t *cfg_quota, na_elem_t *data) {
1790 na_elem_t *elem_quotas;
1791 na_elem_iter_t iter_quota;
1793 elem_quotas = na_elem_child(data, "quotas");
1794 if (elem_quotas == NULL) {
1795 ERROR("netapp plugin: cna_handle_quota_data: "
1796 "na_elem_child (\"quotas\") failed "
1797 "for host %s.",
1798 host->name);
1799 return (-1);
1800 }
1802 iter_quota = na_child_iterator(elem_quotas);
1803 for (na_elem_t *elem_quota = na_iterator_next(&iter_quota);
1804 elem_quota != NULL; elem_quota = na_iterator_next(&iter_quota)) {
1805 const char *quota_type, *volume_name, *tree_name;
1806 uint64_t value;
1808 char plugin_instance[DATA_MAX_NAME_LEN];
1810 quota_type = na_child_get_string(elem_quota, "quota-type");
1811 if (quota_type == NULL)
1812 continue;
1814 /* possible TODO: support other types as well */
1815 if (strcmp(quota_type, "tree") != 0)
1816 continue;
1818 tree_name = na_child_get_string(elem_quota, "tree");
1819 if ((tree_name == NULL) || (*tree_name == '\0'))
1820 continue;
1822 volume_name = na_child_get_string(elem_quota, "volume");
1823 if (volume_name == NULL)
1824 continue;
1826 ssnprintf(plugin_instance, sizeof(plugin_instance), "quota-%s-%s",
1827 volume_name, tree_name);
1829 value = na_child_get_uint64(elem_quota, "disk-used", UINT64_MAX);
1830 if (value != UINT64_MAX) {
1831 value *= 1024; /* disk-used reports kilobytes */
1832 submit_double(host->name, plugin_instance,
1833 /* type = */ "df_complex", /* type instance = */ NULL,
1834 (double)value, /* timestamp = */ 0,
1835 host->cfg_quota->interval.interval);
1836 }
1838 value = na_child_get_uint64(elem_quota, "files-used", UINT64_MAX);
1839 if (value != UINT64_MAX) {
1840 submit_double(host->name, plugin_instance,
1841 /* type = */ "files", /* type instance = */ NULL,
1842 (double)value, /* timestamp = */ 0,
1843 host->cfg_quota->interval.interval);
1844 }
1845 } /* for (elem_quota) */
1847 return (0);
1848 } /* }}} int cna_handle_volume_usage_data */
1850 static int cna_setup_quota(cfg_quota_t *cq) /* {{{ */
1851 {
1852 if (cq == NULL)
1853 return (EINVAL);
1855 if (cq->query != NULL)
1856 return (0);
1858 cq->query = na_elem_new("quota-report");
1859 if (cq->query == NULL) {
1860 ERROR("netapp plugin: na_elem_new failed.");
1861 return (-1);
1862 }
1864 return (0);
1865 } /* }}} int cna_setup_quota */
1867 static int cna_query_quota(host_config_t *host) /* {{{ */
1868 {
1869 na_elem_t *data;
1870 int status;
1871 cdtime_t now;
1873 if (host == NULL)
1874 return (EINVAL);
1876 /* If the user did not configure quota statistics, return without
1877 * doing anything. */
1878 if (host->cfg_quota == NULL)
1879 return (0);
1881 now = cdtime();
1882 if ((host->cfg_quota->interval.interval +
1883 host->cfg_quota->interval.last_read) > now)
1884 return (0);
1886 status = cna_setup_quota(host->cfg_quota);
1887 if (status != 0)
1888 return (status);
1889 assert(host->cfg_quota->query != NULL);
1891 data = na_server_invoke_elem(host->srv, host->cfg_quota->query);
1892 if (na_results_status(data) != NA_OK) {
1893 ERROR("netapp plugin: cna_query_quota: na_server_invoke_elem failed for "
1894 "host %s: %s",
1895 host->name, na_results_reason(data));
1896 na_elem_free(data);
1897 return (-1);
1898 }
1900 status = cna_handle_quota_data(host, host->cfg_quota, data);
1902 if (status == 0)
1903 host->cfg_quota->interval.last_read = now;
1905 na_elem_free(data);
1906 return (status);
1907 } /* }}} int cna_query_quota */
1909 /* Data corresponding to <SnapVault /> */
1910 static int cna_handle_snapvault_data(const char *hostname, /* {{{ */
1911 cfg_snapvault_t *cfg_snapvault,
1912 na_elem_t *data, cdtime_t interval) {
1913 na_elem_iter_t status_iter;
1915 status = na_elem_child(data, "status-list");
1916 if (!status) {
1917 ERROR("netapp plugin: SnapVault status record missing status-list");
1918 return (0);
1919 }
1921 status_iter = na_child_iterator(status);
1922 for (na_elem_t *status = na_iterator_next(&status_iter); status != NULL;
1923 status = na_iterator_next(&status_iter)) {
1924 const char *dest_sys, *dest_path, *src_sys, *src_path;
1925 char plugin_instance[DATA_MAX_NAME_LEN];
1926 uint64_t value;
1928 dest_sys = na_child_get_string(status, "destination-system");
1929 dest_path = na_child_get_string(status, "destination-path");
1930 src_sys = na_child_get_string(status, "source-system");
1931 src_path = na_child_get_string(status, "source-path");
1933 if ((!dest_sys) || (!dest_path) || (!src_sys) || (!src_path))
1934 continue;
1936 value = na_child_get_uint64(status, "lag-time", UINT64_MAX);
1937 if (value == UINT64_MAX) /* no successful baseline transfer yet */
1938 continue;
1940 /* possible TODO: make plugin instance configurable */
1941 ssnprintf(plugin_instance, sizeof(plugin_instance), "snapvault-%s",
1942 dest_path);
1943 submit_double(hostname, plugin_instance, /* type = */ "delay", NULL,
1944 (double)value, /* timestamp = */ 0, interval);
1946 value = na_child_get_uint64(status, "last-transfer-duration", UINT64_MAX);
1947 if (value != UINT64_MAX)
1948 submit_double(hostname, plugin_instance, /* type = */ "duration",
1949 "last_transfer", (double)value, /* timestamp = */ 0,
1950 interval);
1952 value = na_child_get_uint64(status, "transfer-progress", UINT64_MAX);
1953 if (value == UINT64_MAX)
1954 value = na_child_get_uint64(status, "last-transfer-size", UINT64_MAX);
1955 if (value != UINT64_MAX) {
1956 value *= 1024; /* this is kilobytes */
1957 submit_derive(hostname, plugin_instance, /* type = */ "if_rx_octets",
1958 "transferred", value, /* timestamp = */ 0, interval);
1959 }
1960 } /* for (status) */
1962 return (0);
1963 } /* }}} int cna_handle_snapvault_data */
1965 static int cna_handle_snapvault_iter(host_config_t *host, /* {{{ */
1966 na_elem_t *data) {
1967 const char *tag;
1969 uint32_t records_count;
1971 records_count = na_child_get_uint32(data, "records", UINT32_MAX);
1972 if (records_count == UINT32_MAX)
1973 return 0;
1975 tag = na_child_get_string(data, "tag");
1976 if (!tag)
1977 return 0;
1979 DEBUG("netapp plugin: Iterating %u SV records (tag = %s)", records_count,
1980 tag);
1982 for (uint32_t i = 0; i < records_count; ++i) {
1983 na_elem_t *elem;
1985 elem = na_server_invoke(
1986 host->srv, "snapvault-secondary-relationship-status-list-iter-next",
1987 "maximum", "1", "tag", tag, NULL);
1989 if (na_results_status(elem) != NA_OK) {
1990 ERROR("netapp plugin: cna_handle_snapvault_iter: "
1991 "na_server_invoke failed for host %s: %s",
1992 host->name, na_results_reason(data));
1993 na_elem_free(elem);
1994 return (-1);
1995 }
1997 cna_handle_snapvault_data(host->name, host->cfg_snapvault, elem,
1998 host->cfg_snapvault->interval.interval);
1999 na_elem_free(elem);
2000 }
2002 na_elem_free(na_server_invoke(
2003 host->srv, "snapvault-secondary-relationship-status-list-iter-end", "tag",
2004 tag, NULL));
2005 return (0);
2006 } /* }}} int cna_handle_snapvault_iter */
2008 static int cna_setup_snapvault(cfg_snapvault_t *sv) /* {{{ */
2009 {
2010 if (sv == NULL)
2011 return (EINVAL);
2013 if (sv->query != NULL)
2014 return (0);
2016 sv->query =
2017 na_elem_new("snapvault-secondary-relationship-status-list-iter-start");
2018 if (sv->query == NULL) {
2019 ERROR("netapp plugin: na_elem_new failed.");
2020 return (-1);
2021 }
2023 return (0);
2024 } /* }}} int cna_setup_snapvault */
2026 static int cna_query_snapvault(host_config_t *host) /* {{{ */
2027 {
2028 na_elem_t *data;
2029 int status;
2030 cdtime_t now;
2032 if (host == NULL)
2033 return EINVAL;
2035 if (host->cfg_snapvault == NULL)
2036 return 0;
2038 now = cdtime();
2039 if ((host->cfg_snapvault->interval.interval +
2040 host->cfg_snapvault->interval.last_read) > now)
2041 return (0);
2043 status = cna_setup_snapvault(host->cfg_snapvault);
2044 if (status != 0)
2045 return (status);
2046 assert(host->cfg_snapvault->query != NULL);
2048 data = na_server_invoke_elem(host->srv, host->cfg_snapvault->query);
2049 if (na_results_status(data) != NA_OK) {
2050 ERROR("netapp plugin: cna_query_snapvault: na_server_invoke_elem failed "
2051 "for host %s: %s",
2052 host->name, na_results_reason(data));
2053 na_elem_free(data);
2054 return (-1);
2055 }
2057 status = cna_handle_snapvault_iter(host, data);
2059 if (status == 0)
2060 host->cfg_snapvault->interval.last_read = now;
2062 na_elem_free(data);
2063 return (status);
2064 } /* }}} int cna_query_snapvault */
2066 /* Data corresponding to <System /> */
2067 static int cna_handle_system_data(const char *hostname, /* {{{ */
2068 cfg_system_t *cfg_system, na_elem_t *data,
2069 int interval) {
2070 na_elem_t *instances;
2071 na_elem_iter_t counter_iter;
2073 derive_t disk_read = 0, disk_written = 0;
2074 derive_t net_recv = 0, net_sent = 0;
2075 derive_t cpu_busy = 0, cpu_total = 0;
2076 uint32_t counter_flags = 0;
2078 const char *instance;
2079 cdtime_t timestamp;
2081 timestamp = cna_child_get_cdtime(data);
2083 instances = na_elem_child(na_elem_child(data, "instances"), "instance-data");
2084 if (instances == NULL) {
2085 ERROR("netapp plugin: cna_handle_system_data: "
2086 "na_elem_child (\"instances\") failed "
2087 "for host %s.",
2088 hostname);
2089 return (-1);
2090 }
2092 instance = na_child_get_string(instances, "name");
2093 if (instance == NULL) {
2094 ERROR("netapp plugin: cna_handle_system_data: "
2095 "na_child_get_string (\"name\") failed "
2096 "for host %s.",
2097 hostname);
2098 return (-1);
2099 }
2101 counter_iter = na_child_iterator(na_elem_child(instances, "counters"));
2102 for (na_elem_t *counter = na_iterator_next(&counter_iter); counter != NULL;
2103 counter = na_iterator_next(&counter_iter)) {
2104 const char *name;
2105 uint64_t value;
2107 name = na_child_get_string(counter, "name");
2108 if (name == NULL)
2109 continue;
2111 value = na_child_get_uint64(counter, "value", UINT64_MAX);
2112 if (value == UINT64_MAX)
2113 continue;
2115 if (!strcmp(name, "disk_data_read")) {
2116 disk_read = (derive_t)(value * 1024);
2117 counter_flags |= 0x01;
2118 } else if (!strcmp(name, "disk_data_written")) {
2119 disk_written = (derive_t)(value * 1024);
2120 counter_flags |= 0x02;
2121 } else if (!strcmp(name, "net_data_recv")) {
2122 net_recv = (derive_t)(value * 1024);
2123 counter_flags |= 0x04;
2124 } else if (!strcmp(name, "net_data_sent")) {
2125 net_sent = (derive_t)(value * 1024);
2126 counter_flags |= 0x08;
2127 } else if (!strcmp(name, "cpu_busy")) {
2128 cpu_busy = (derive_t)value;
2129 counter_flags |= 0x10;
2130 } else if (!strcmp(name, "cpu_elapsed_time")) {
2131 cpu_total = (derive_t)value;
2132 counter_flags |= 0x20;
2133 } else if ((cfg_system->flags & CFG_SYSTEM_OPS) && (value > 0) &&
2134 (strlen(name) > 4) &&
2135 (!strcmp(name + strlen(name) - 4, "_ops"))) {
2136 submit_derive(hostname, instance, "disk_ops_complex", name,
2137 (derive_t)value, timestamp, interval);
2138 }
2139 } /* for (counter) */
2141 if ((cfg_system->flags & CFG_SYSTEM_DISK) &&
2142 (HAS_ALL_FLAGS(counter_flags, 0x01 | 0x02)))
2143 submit_two_derive(hostname, instance, "disk_octets", NULL, disk_read,
2144 disk_written, timestamp, interval);
2146 if ((cfg_system->flags & CFG_SYSTEM_NET) &&
2147 (HAS_ALL_FLAGS(counter_flags, 0x04 | 0x08)))
2148 submit_two_derive(hostname, instance, "if_octets", NULL, net_recv, net_sent,
2149 timestamp, interval);
2151 if ((cfg_system->flags & CFG_SYSTEM_CPU) &&
2152 (HAS_ALL_FLAGS(counter_flags, 0x10 | 0x20))) {
2153 submit_derive(hostname, instance, "cpu", "system", cpu_busy, timestamp,
2154 interval);
2155 submit_derive(hostname, instance, "cpu", "idle", cpu_total - cpu_busy,
2156 timestamp, interval);
2157 }
2159 return (0);
2160 } /* }}} int cna_handle_system_data */
2162 static int cna_setup_system(cfg_system_t *cs) /* {{{ */
2163 {
2164 if (cs == NULL)
2165 return (EINVAL);
2167 if (cs->query != NULL)
2168 return (0);
2170 cs->query = na_elem_new("perf-object-get-instances");
2171 if (cs->query == NULL) {
2172 ERROR("netapp plugin: na_elem_new failed.");
2173 return (-1);
2174 }
2175 na_child_add_string(cs->query, "objectname", "system");
2177 return (0);
2178 } /* }}} int cna_setup_system */
2180 static int cna_query_system(host_config_t *host) /* {{{ */
2181 {
2182 na_elem_t *data;
2183 int status;
2184 cdtime_t now;
2186 if (host == NULL)
2187 return (EINVAL);
2189 /* If system statistics were not configured, return without doing anything. */
2190 if (host->cfg_system == NULL)
2191 return (0);
2193 now = cdtime();
2194 if ((host->cfg_system->interval.interval +
2195 host->cfg_system->interval.last_read) > now)
2196 return (0);
2198 status = cna_setup_system(host->cfg_system);
2199 if (status != 0)
2200 return (status);
2201 assert(host->cfg_system->query != NULL);
2203 data = na_server_invoke_elem(host->srv, host->cfg_system->query);
2204 if (na_results_status(data) != NA_OK) {
2205 ERROR("netapp plugin: cna_query_system: na_server_invoke_elem failed for "
2206 "host %s: %s",
2207 host->name, na_results_reason(data));
2208 na_elem_free(data);
2209 return (-1);
2210 }
2212 status = cna_handle_system_data(host->name, host->cfg_system, data,
2213 host->cfg_system->interval.interval);
2215 if (status == 0)
2216 host->cfg_system->interval.last_read = now;
2218 na_elem_free(data);
2219 return (status);
2220 } /* }}} int cna_query_system */
2222 /*
2223 * Configuration handling
2224 */
2225 /* Sets a given flag if the boolean argument is true and unsets the flag if it
2226 * is false. On error, the flag-field is not changed. */
2227 static int cna_config_bool_to_flag(const oconfig_item_t *ci, /* {{{ */
2228 uint32_t *flags, uint32_t flag) {
2229 if ((ci == NULL) || (flags == NULL))
2230 return (EINVAL);
2232 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) {
2233 WARNING("netapp plugin: The %s option needs exactly one boolean argument.",
2234 ci->key);
2235 return (-1);
2236 }
2238 if (ci->values[0].value.boolean)
2239 *flags |= flag;
2240 else
2241 *flags &= ~flag;
2243 return (0);
2244 } /* }}} int cna_config_bool_to_flag */
2246 /* Handling of the "Interval" option which is allowed in every block. */
2247 static int cna_config_get_interval(const oconfig_item_t *ci, /* {{{ */
2248 cna_interval_t *out_interval) {
2249 cdtime_t tmp = 0;
2250 int status;
2252 status = cf_util_get_cdtime(ci, &tmp);
2253 if (status != 0)
2254 return (status);
2256 out_interval->interval = tmp;
2257 out_interval->last_read = 0;
2259 return (0);
2260 } /* }}} int cna_config_get_interval */
2262 /* Handling of the "GetIO", "GetOps" and "GetLatency" options within a
2263 * <VolumePerf /> block. */
2264 static void cna_config_volume_perf_option(cfg_volume_perf_t *cvp, /* {{{ */
2265 const oconfig_item_t *ci) {
2266 char *name;
2267 ignorelist_t *il;
2269 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2270 WARNING(
2271 "netapp plugin: The %s option requires exactly one string argument.",
2272 ci->key);
2273 return;
2274 }
2276 name = ci->values[0].value.string;
2278 if (strcasecmp("GetIO", ci->key) == 0)
2279 il = cvp->il_octets;
2280 else if (strcasecmp("GetOps", ci->key) == 0)
2281 il = cvp->il_operations;
2282 else if (strcasecmp("GetLatency", ci->key) == 0)
2283 il = cvp->il_latency;
2284 else
2285 return;
2287 ignorelist_add(il, name);
2288 } /* }}} void cna_config_volume_perf_option */
2290 /* Handling of the "IgnoreSelectedIO", "IgnoreSelectedOps" and
2291 * "IgnoreSelectedLatency" options within a <VolumePerf /> block. */
2292 static void cna_config_volume_perf_default(cfg_volume_perf_t *cvp, /* {{{ */
2293 const oconfig_item_t *ci) {
2294 ignorelist_t *il;
2296 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) {
2297 WARNING(
2298 "netapp plugin: The %s option requires exactly one string argument.",
2299 ci->key);
2300 return;
2301 }
2303 if (strcasecmp("IgnoreSelectedIO", ci->key) == 0)
2304 il = cvp->il_octets;
2305 else if (strcasecmp("IgnoreSelectedOps", ci->key) == 0)
2306 il = cvp->il_operations;
2307 else if (strcasecmp("IgnoreSelectedLatency", ci->key) == 0)
2308 il = cvp->il_latency;
2309 else
2310 return;
2312 if (ci->values[0].value.boolean)
2313 ignorelist_set_invert(il, /* invert = */ 0);
2314 else
2315 ignorelist_set_invert(il, /* invert = */ 1);
2316 } /* }}} void cna_config_volume_perf_default */
2318 /* Corresponds to a <Disks /> block */
2319 /*
2320 * <VolumePerf>
2321 * GetIO "vol0"
2322 * GetIO "vol1"
2323 * IgnoreSelectedIO false
2324 *
2325 * GetOps "vol0"
2326 * GetOps "vol2"
2327 * IgnoreSelectedOps false
2328 *
2329 * GetLatency "vol2"
2330 * GetLatency "vol3"
2331 * IgnoreSelectedLatency false
2332 * </VolumePerf>
2333 */
2334 /* Corresponds to a <VolumePerf /> block */
2335 static int cna_config_volume_performance(host_config_t *host, /* {{{ */
2336 const oconfig_item_t *ci) {
2337 cfg_volume_perf_t *cfg_volume_perf;
2339 if ((host == NULL) || (ci == NULL))
2340 return (EINVAL);
2342 if (host->cfg_volume_perf == NULL) {
2343 cfg_volume_perf = calloc(1, sizeof(*cfg_volume_perf));
2344 if (cfg_volume_perf == NULL)
2345 return (ENOMEM);
2347 /* Set default flags */
2348 cfg_volume_perf->query = NULL;
2349 cfg_volume_perf->volumes = NULL;
2351 cfg_volume_perf->il_octets = ignorelist_create(/* invert = */ 1);
2352 if (cfg_volume_perf->il_octets == NULL) {
2353 sfree(cfg_volume_perf);
2354 return (ENOMEM);
2355 }
2357 cfg_volume_perf->il_operations = ignorelist_create(/* invert = */ 1);
2358 if (cfg_volume_perf->il_operations == NULL) {
2359 ignorelist_free(cfg_volume_perf->il_octets);
2360 sfree(cfg_volume_perf);
2361 return (ENOMEM);
2362 }
2364 cfg_volume_perf->il_latency = ignorelist_create(/* invert = */ 1);
2365 if (cfg_volume_perf->il_latency == NULL) {
2366 ignorelist_free(cfg_volume_perf->il_octets);
2367 ignorelist_free(cfg_volume_perf->il_operations);
2368 sfree(cfg_volume_perf);
2369 return (ENOMEM);
2370 }
2372 host->cfg_volume_perf = cfg_volume_perf;
2373 }
2374 cfg_volume_perf = host->cfg_volume_perf;
2376 for (int i = 0; i < ci->children_num; ++i) {
2377 oconfig_item_t *item = ci->children + i;
2379 /* if (!item || !item->key || !*item->key) continue; */
2380 if (strcasecmp(item->key, "Interval") == 0)
2381 cna_config_get_interval(item, &cfg_volume_perf->interval);
2382 else if (!strcasecmp(item->key, "GetIO"))
2383 cna_config_volume_perf_option(cfg_volume_perf, item);
2384 else if (!strcasecmp(item->key, "GetOps"))
2385 cna_config_volume_perf_option(cfg_volume_perf, item);
2386 else if (!strcasecmp(item->key, "GetLatency"))
2387 cna_config_volume_perf_option(cfg_volume_perf, item);
2388 else if (!strcasecmp(item->key, "IgnoreSelectedIO"))
2389 cna_config_volume_perf_default(cfg_volume_perf, item);
2390 else if (!strcasecmp(item->key, "IgnoreSelectedOps"))
2391 cna_config_volume_perf_default(cfg_volume_perf, item);
2392 else if (!strcasecmp(item->key, "IgnoreSelectedLatency"))
2393 cna_config_volume_perf_default(cfg_volume_perf, item);
2394 else
2395 WARNING("netapp plugin: The option %s is not allowed within "
2396 "`VolumePerf' blocks.",
2397 item->key);
2398 }
2400 return (0);
2401 } /* }}} int cna_config_volume_performance */
2403 /* Handling of the "GetCapacity" and "GetSnapshot" options within a
2404 * <VolumeUsage /> block. */
2405 static void cna_config_volume_usage_option(cfg_volume_usage_t *cvu, /* {{{ */
2406 const oconfig_item_t *ci) {
2407 char *name;
2408 ignorelist_t *il;
2410 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2411 WARNING(
2412 "netapp plugin: The %s option requires exactly one string argument.",
2413 ci->key);
2414 return;
2415 }
2417 name = ci->values[0].value.string;
2419 if (strcasecmp("GetCapacity", ci->key) == 0)
2420 il = cvu->il_capacity;
2421 else if (strcasecmp("GetSnapshot", ci->key) == 0)
2422 il = cvu->il_snapshot;
2423 else
2424 return;
2426 ignorelist_add(il, name);
2427 } /* }}} void cna_config_volume_usage_option */
2429 /* Handling of the "IgnoreSelectedCapacity" and "IgnoreSelectedSnapshot"
2430 * options within a <VolumeUsage /> block. */
2431 static void cna_config_volume_usage_default(cfg_volume_usage_t *cvu, /* {{{ */
2432 const oconfig_item_t *ci) {
2433 ignorelist_t *il;
2435 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) {
2436 WARNING(
2437 "netapp plugin: The %s option requires exactly one string argument.",
2438 ci->key);
2439 return;
2440 }
2442 if (strcasecmp("IgnoreSelectedCapacity", ci->key) == 0)
2443 il = cvu->il_capacity;
2444 else if (strcasecmp("IgnoreSelectedSnapshot", ci->key) == 0)
2445 il = cvu->il_snapshot;
2446 else
2447 return;
2449 if (ci->values[0].value.boolean)
2450 ignorelist_set_invert(il, /* invert = */ 0);
2451 else
2452 ignorelist_set_invert(il, /* invert = */ 1);
2453 } /* }}} void cna_config_volume_usage_default */
2455 /* Corresponds to a <Quota /> block */
2456 static int cna_config_quota(host_config_t *host, oconfig_item_t *ci) /* {{{ */
2457 {
2458 cfg_quota_t *cfg_quota;
2460 if ((host == NULL) || (ci == NULL))
2461 return (EINVAL);
2463 if (host->cfg_quota == NULL) {
2464 cfg_quota = calloc(1, sizeof(*cfg_quota));
2465 if (cfg_quota == NULL)
2466 return (ENOMEM);
2467 cfg_quota->query = NULL;
2469 host->cfg_quota = cfg_quota;
2470 }
2471 cfg_quota = host->cfg_quota;
2473 for (int i = 0; i < ci->children_num; ++i) {
2474 oconfig_item_t *item = ci->children + i;
2476 if (strcasecmp(item->key, "Interval") == 0)
2477 cna_config_get_interval(item, &cfg_quota->interval);
2478 else
2479 WARNING("netapp plugin: The option %s is not allowed within "
2480 "`Quota' blocks.",
2481 item->key);
2482 }
2484 return (0);
2485 } /* }}} int cna_config_quota */
2487 /* Corresponds to a <Disks /> block */
2488 static int cna_config_disk(host_config_t *host, oconfig_item_t *ci) { /* {{{ */
2489 cfg_disk_t *cfg_disk;
2491 if ((host == NULL) || (ci == NULL))
2492 return (EINVAL);
2494 if (host->cfg_disk == NULL) {
2495 cfg_disk = calloc(1, sizeof(*cfg_disk));
2496 if (cfg_disk == NULL)
2497 return (ENOMEM);
2499 /* Set default flags */
2500 cfg_disk->flags = CFG_DISK_ALL;
2501 cfg_disk->query = NULL;
2502 cfg_disk->disks = NULL;
2504 host->cfg_disk = cfg_disk;
2505 }
2506 cfg_disk = host->cfg_disk;
2508 for (int i = 0; i < ci->children_num; ++i) {
2509 oconfig_item_t *item = ci->children + i;
2511 /* if (!item || !item->key || !*item->key) continue; */
2512 if (strcasecmp(item->key, "Interval") == 0)
2513 cna_config_get_interval(item, &cfg_disk->interval);
2514 else if (strcasecmp(item->key, "GetBusy") == 0)
2515 cna_config_bool_to_flag(item, &cfg_disk->flags, CFG_DISK_BUSIEST);
2516 }
2518 if ((cfg_disk->flags & CFG_DISK_ALL) == 0) {
2519 NOTICE("netapp plugin: All disk related values have been disabled. "
2520 "Collection of per-disk data will be disabled entirely.");
2521 free_cfg_disk(host->cfg_disk);
2522 host->cfg_disk = NULL;
2523 }
2525 return (0);
2526 } /* }}} int cna_config_disk */
2528 /* Corresponds to a <WAFL /> block */
2529 static int cna_config_wafl(host_config_t *host, oconfig_item_t *ci) /* {{{ */
2530 {
2531 cfg_wafl_t *cfg_wafl;
2533 if ((host == NULL) || (ci == NULL))
2534 return (EINVAL);
2536 if (host->cfg_wafl == NULL) {
2537 cfg_wafl = calloc(1, sizeof(*cfg_wafl));
2538 if (cfg_wafl == NULL)
2539 return (ENOMEM);
2541 /* Set default flags */
2542 cfg_wafl->flags = CFG_WAFL_ALL;
2544 host->cfg_wafl = cfg_wafl;
2545 }
2546 cfg_wafl = host->cfg_wafl;
2548 for (int i = 0; i < ci->children_num; ++i) {
2549 oconfig_item_t *item = ci->children + i;
2551 if (strcasecmp(item->key, "Interval") == 0)
2552 cna_config_get_interval(item, &cfg_wafl->interval);
2553 else if (!strcasecmp(item->key, "GetNameCache"))
2554 cna_config_bool_to_flag(item, &cfg_wafl->flags, CFG_WAFL_NAME_CACHE);
2555 else if (!strcasecmp(item->key, "GetDirCache"))
2556 cna_config_bool_to_flag(item, &cfg_wafl->flags, CFG_WAFL_DIR_CACHE);
2557 else if (!strcasecmp(item->key, "GetBufferCache"))
2558 cna_config_bool_to_flag(item, &cfg_wafl->flags, CFG_WAFL_BUF_CACHE);
2559 else if (!strcasecmp(item->key, "GetInodeCache"))
2560 cna_config_bool_to_flag(item, &cfg_wafl->flags, CFG_WAFL_INODE_CACHE);
2561 else
2562 WARNING("netapp plugin: The %s config option is not allowed within "
2563 "`WAFL' blocks.",
2564 item->key);
2565 }
2567 if ((cfg_wafl->flags & CFG_WAFL_ALL) == 0) {
2568 NOTICE("netapp plugin: All WAFL related values have been disabled. "
2569 "Collection of WAFL data will be disabled entirely.");
2570 free_cfg_wafl(host->cfg_wafl);
2571 host->cfg_wafl = NULL;
2572 }
2574 return (0);
2575 } /* }}} int cna_config_wafl */
2577 /*
2578 * <VolumeUsage>
2579 * GetCapacity "vol0"
2580 * GetCapacity "vol1"
2581 * GetCapacity "vol2"
2582 * GetCapacity "vol3"
2583 * GetCapacity "vol4"
2584 * IgnoreSelectedCapacity false
2585 *
2586 * GetSnapshot "vol0"
2587 * GetSnapshot "vol3"
2588 * GetSnapshot "vol4"
2589 * GetSnapshot "vol7"
2590 * IgnoreSelectedSnapshot false
2591 * </VolumeUsage>
2592 */
2593 /* Corresponds to a <VolumeUsage /> block */
2594 static int cna_config_volume_usage(host_config_t *host, /* {{{ */
2595 const oconfig_item_t *ci) {
2596 cfg_volume_usage_t *cfg_volume_usage;
2598 if ((host == NULL) || (ci == NULL))
2599 return (EINVAL);
2601 if (host->cfg_volume_usage == NULL) {
2602 cfg_volume_usage = calloc(1, sizeof(*cfg_volume_usage));
2603 if (cfg_volume_usage == NULL)
2604 return (ENOMEM);
2606 /* Set default flags */
2607 cfg_volume_usage->query = NULL;
2608 cfg_volume_usage->volumes = NULL;
2610 cfg_volume_usage->il_capacity = ignorelist_create(/* invert = */ 1);
2611 if (cfg_volume_usage->il_capacity == NULL) {
2612 sfree(cfg_volume_usage);
2613 return (ENOMEM);
2614 }
2616 cfg_volume_usage->il_snapshot = ignorelist_create(/* invert = */ 1);
2617 if (cfg_volume_usage->il_snapshot == NULL) {
2618 ignorelist_free(cfg_volume_usage->il_capacity);
2619 sfree(cfg_volume_usage);
2620 return (ENOMEM);
2621 }
2623 host->cfg_volume_usage = cfg_volume_usage;
2624 }
2625 cfg_volume_usage = host->cfg_volume_usage;
2627 for (int i = 0; i < ci->children_num; ++i) {
2628 oconfig_item_t *item = ci->children + i;
2630 /* if (!item || !item->key || !*item->key) continue; */
2631 if (strcasecmp(item->key, "Interval") == 0)
2632 cna_config_get_interval(item, &cfg_volume_usage->interval);
2633 else if (!strcasecmp(item->key, "GetCapacity"))
2634 cna_config_volume_usage_option(cfg_volume_usage, item);
2635 else if (!strcasecmp(item->key, "GetSnapshot"))
2636 cna_config_volume_usage_option(cfg_volume_usage, item);
2637 else if (!strcasecmp(item->key, "IgnoreSelectedCapacity"))
2638 cna_config_volume_usage_default(cfg_volume_usage, item);
2639 else if (!strcasecmp(item->key, "IgnoreSelectedSnapshot"))
2640 cna_config_volume_usage_default(cfg_volume_usage, item);
2641 else
2642 WARNING("netapp plugin: The option %s is not allowed within "
2643 "`VolumeUsage' blocks.",
2644 item->key);
2645 }
2647 return (0);
2648 } /* }}} int cna_config_volume_usage */
2650 /* Corresponds to a <SnapVault /> block */
2651 static int cna_config_snapvault(host_config_t *host, /* {{{ */
2652 const oconfig_item_t *ci) {
2653 cfg_snapvault_t *cfg_snapvault;
2655 if ((host == NULL) || (ci == NULL))
2656 return EINVAL;
2658 if (host->cfg_snapvault == NULL) {
2659 cfg_snapvault = calloc(1, sizeof(*cfg_snapvault));
2660 if (cfg_snapvault == NULL)
2661 return ENOMEM;
2662 cfg_snapvault->query = NULL;
2664 host->cfg_snapvault = cfg_snapvault;
2665 }
2667 cfg_snapvault = host->cfg_snapvault;
2669 for (int i = 0; i < ci->children_num; ++i) {
2670 oconfig_item_t *item = ci->children + i;
2672 if (strcasecmp(item->key, "Interval") == 0)
2673 cna_config_get_interval(item, &cfg_snapvault->interval);
2674 else
2675 WARNING("netapp plugin: The option %s is not allowed within "
2676 "`SnapVault' blocks.",
2677 item->key);
2678 }
2680 return 0;
2681 } /* }}} int cna_config_snapvault */
2683 /* Corresponds to a <System /> block */
2684 static int cna_config_system(host_config_t *host, /* {{{ */
2685 oconfig_item_t *ci) {
2686 cfg_system_t *cfg_system;
2688 if ((host == NULL) || (ci == NULL))
2689 return (EINVAL);
2691 if (host->cfg_system == NULL) {
2692 cfg_system = calloc(1, sizeof(*cfg_system));
2693 if (cfg_system == NULL)
2694 return (ENOMEM);
2696 /* Set default flags */
2697 cfg_system->flags = CFG_SYSTEM_ALL;
2698 cfg_system->query = NULL;
2700 host->cfg_system = cfg_system;
2701 }
2702 cfg_system = host->cfg_system;
2704 for (int i = 0; i < ci->children_num; ++i) {
2705 oconfig_item_t *item = ci->children + i;
2707 if (strcasecmp(item->key, "Interval") == 0) {
2708 cna_config_get_interval(item, &cfg_system->interval);
2709 } else if (!strcasecmp(item->key, "GetCPULoad")) {
2710 cna_config_bool_to_flag(item, &cfg_system->flags, CFG_SYSTEM_CPU);
2711 } else if (!strcasecmp(item->key, "GetInterfaces")) {
2712 cna_config_bool_to_flag(item, &cfg_system->flags, CFG_SYSTEM_NET);
2713 } else if (!strcasecmp(item->key, "GetDiskOps")) {
2714 cna_config_bool_to_flag(item, &cfg_system->flags, CFG_SYSTEM_OPS);
2715 } else if (!strcasecmp(item->key, "GetDiskIO")) {
2716 cna_config_bool_to_flag(item, &cfg_system->flags, CFG_SYSTEM_DISK);
2717 } else {
2718 WARNING("netapp plugin: The %s config option is not allowed within "
2719 "`System' blocks.",
2720 item->key);
2721 }
2722 }
2724 if ((cfg_system->flags & CFG_SYSTEM_ALL) == 0) {
2725 NOTICE("netapp plugin: All system related values have been disabled. "
2726 "Collection of system data will be disabled entirely.");
2727 free_cfg_system(host->cfg_system);
2728 host->cfg_system = NULL;
2729 }
2731 return (0);
2732 } /* }}} int cna_config_system */
2734 /* Corresponds to a <Host /> block. */
2735 static host_config_t *cna_alloc_host(void) /* {{{ */
2736 {
2737 host_config_t *host;
2739 host = calloc(1, sizeof(*host));
2740 if (host == NULL)
2741 return (NULL);
2743 host->name = NULL;
2744 host->protocol = NA_SERVER_TRANSPORT_HTTPS;
2745 host->host = NULL;
2746 host->username = NULL;
2747 host->password = NULL;
2748 host->vfiler = NULL;
2749 host->srv = NULL;
2750 host->cfg_wafl = NULL;
2751 host->cfg_disk = NULL;
2752 host->cfg_volume_perf = NULL;
2753 host->cfg_volume_usage = NULL;
2754 host->cfg_quota = NULL;
2755 host->cfg_snapvault = NULL;
2756 host->cfg_system = NULL;
2758 return (host);
2759 } /* }}} host_config_t *cna_alloc_host */
2761 static host_config_t *cna_shallow_clone_host(host_config_t *host) /* {{{ */
2762 {
2763 host_config_t *clone;
2765 if (host == NULL)
2766 return (NULL);
2768 clone = cna_alloc_host();
2769 if (clone == NULL)
2770 return (NULL);
2772 if (host->name != NULL) {
2773 clone->name = strdup(host->name);
2774 if (clone->name == NULL) {
2775 free_host_config(clone);
2776 return NULL;
2777 }
2778 }
2780 clone->protocol = host->protocol;
2782 if (host->host != NULL) {
2783 clone->host = strdup(host->host);
2784 if (clone->host == NULL) {
2785 free_host_config(clone);
2786 return NULL;
2787 }
2788 }
2790 clone->port = host->port;
2792 if (host->username != NULL) {
2793 clone->username = strdup(host->username);
2794 if (clone->username == NULL) {
2795 free_host_config(clone);
2796 return NULL;
2797 }
2798 }
2799 if (host->password != NULL) {
2800 clone->password = strdup(host->password);
2801 if (clone->password == NULL) {
2802 free_host_config(clone);
2803 return NULL;
2804 }
2805 }
2807 clone->interval = host->interval;
2809 return (clone);
2810 } /* }}} host_config_t *cna_shallow_clone_host */
2812 static int cna_read(user_data_t *ud);
2814 static int cna_register_host(host_config_t *host) /* {{{ */
2815 {
2816 char cb_name[256];
2818 if (host->vfiler)
2819 ssnprintf(cb_name, sizeof(cb_name), "netapp-%s-%s", host->name,
2820 host->vfiler);
2821 else
2822 ssnprintf(cb_name, sizeof(cb_name), "netapp-%s", host->name);
2824 plugin_register_complex_read(
2825 /* group = */ NULL, cb_name,
2826 /* callback = */ cna_read,
2827 /* interval = */ host->interval,
2828 &(user_data_t){
2829 .data = host, .free_func = (void *)free_host_config,
2830 });
2832 return (0);
2833 } /* }}} int cna_register_host */
2835 static int cna_config_host(host_config_t *host, /* {{{ */
2836 const oconfig_item_t *ci) {
2837 oconfig_item_t *item;
2838 _Bool is_vfiler = 0;
2839 int status;
2841 if (!strcasecmp(ci->key, "VFiler"))
2842 is_vfiler = 1;
2844 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2845 WARNING("netapp plugin: \"%s\" needs exactly one string argument. Ignoring "
2846 "host block.",
2847 ci->key);
2848 return (1);
2849 }
2851 status = cf_util_get_string(ci, &host->name);
2852 if (status != 0)
2853 return (1);
2855 for (int i = 0; i < ci->children_num; ++i) {
2856 item = ci->children + i;
2858 status = 0;
2860 if (!strcasecmp(item->key, "Address")) {
2861 status = cf_util_get_string(item, &host->host);
2862 } else if (!strcasecmp(item->key, "Port")) {
2863 int tmp;
2865 tmp = cf_util_get_port_number(item);
2866 if (tmp > 0)
2867 host->port = tmp;
2868 } else if (!strcasecmp(item->key, "Protocol")) {
2869 if ((item->values_num != 1) ||
2870 (item->values[0].type != OCONFIG_TYPE_STRING) ||
2871 (strcasecmp(item->values[0].value.string, "http") &&
2872 strcasecmp(item->values[0].value.string, "https"))) {
2873 WARNING("netapp plugin: \"Protocol\" needs to be either \"http\" or "
2874 "\"https\". Ignoring host block \"%s\".",
2875 ci->values[0].value.string);
2876 return (1);
2877 }
2878 if (!strcasecmp(item->values[0].value.string, "http"))
2879 host->protocol = NA_SERVER_TRANSPORT_HTTP;
2880 else
2881 host->protocol = NA_SERVER_TRANSPORT_HTTPS;
2882 } else if (!strcasecmp(item->key, "User")) {
2883 status = cf_util_get_string(item, &host->username);
2884 } else if (!strcasecmp(item->key, "Password")) {
2885 status = cf_util_get_string(item, &host->password);
2886 } else if (!strcasecmp(item->key, "Interval")) {
2887 status = cf_util_get_cdtime(item, &host->interval);
2888 } else if (!strcasecmp(item->key, "WAFL")) {
2889 cna_config_wafl(host, item);
2890 } else if (!strcasecmp(item->key, "Disks")) {
2891 cna_config_disk(host, item);
2892 } else if (!strcasecmp(item->key, "VolumePerf")) {
2893 cna_config_volume_performance(host, item);
2894 } else if (!strcasecmp(item->key, "VolumeUsage")) {
2895 cna_config_volume_usage(host, item);
2896 } else if (!strcasecmp(item->key, "Quota")) {
2897 cna_config_quota(host, item);
2898 } else if (!strcasecmp(item->key, "SnapVault")) {
2899 cna_config_snapvault(host, item);
2900 } else if (!strcasecmp(item->key, "System")) {
2901 cna_config_system(host, item);
2902 } else if ((!strcasecmp(item->key, "VFiler")) && (!is_vfiler)) {
2903 host_config_t *vfiler;
2905 vfiler = cna_shallow_clone_host(host);
2906 if (!vfiler) {
2907 ERROR("netapp plugin: Failed to allocate host object for vfiler.");
2908 continue;
2909 }
2911 if (cna_config_host(vfiler, item)) {
2912 free_host_config(vfiler);
2913 continue;
2914 }
2916 cna_register_host(vfiler);
2917 } else if ((!strcasecmp(item->key, "VFilerName")) && is_vfiler) {
2918 status = cf_util_get_string(item, &host->vfiler);
2919 } else {
2920 WARNING("netapp plugin: Ignoring unknown config option \"%s\" in %s "
2921 "block \"%s\".",
2922 item->key, is_vfiler ? "vfiler" : "host",
2923 ci->values[0].value.string);
2924 }
2926 if (status != 0)
2927 break;
2928 }
2930 if (host->host == NULL)
2931 host->host = strdup(host->name);
2933 if (is_vfiler && (!host->vfiler))
2934 host->vfiler = strdup(host->name);
2936 if (host->host == NULL)
2937 status = -1;
2939 if (host->port <= 0)
2940 host->port = (host->protocol == NA_SERVER_TRANSPORT_HTTP) ? 80 : 443;
2942 if ((host->username == NULL) || (host->password == NULL)) {
2943 WARNING("netapp plugin: Please supply login information for host \"%s\". "
2944 "Ignoring host block.",
2945 host->name);
2946 status = -1;
2947 }
2949 if (status != 0)
2950 return status;
2952 return (0);
2953 } /* }}} host_config_t *cna_config_host */
2955 /*
2956 * Callbacks registered with the daemon
2957 *
2958 * Pretty standard stuff here.
2959 */
2960 static int cna_init_host(host_config_t *host) /* {{{ */
2961 {
2962 /* Request version 1.1 of the ONTAP API */
2963 int major_version = 1, minor_version = 1;
2965 if (host == NULL)
2966 return (EINVAL);
2968 if (host->srv != NULL)
2969 return (0);
2971 if (host->vfiler != NULL) /* Request version 1.7 of the ONTAP API */
2972 minor_version = 7;
2974 host->srv = na_server_open(host->host, major_version, minor_version);
2975 if (host->srv == NULL) {
2976 ERROR("netapp plugin: na_server_open (%s) failed.", host->host);
2977 return (-1);
2978 }
2980 na_server_set_transport_type(host->srv, host->protocol,
2981 /* transportarg = */ NULL);
2982 na_server_set_port(host->srv, host->port);
2983 na_server_style(host->srv, NA_STYLE_LOGIN_PASSWORD);
2984 na_server_adminuser(host->srv, host->username, host->password);
2985 na_server_set_timeout(host->srv, 5 /* seconds */);
2987 if (host->vfiler != NULL) {
2988 if (!na_server_set_vfiler(host->srv, host->vfiler)) {
2989 ERROR("netapp plugin: Failed to connect to VFiler '%s' on host '%s'.",
2990 host->vfiler, host->host);
2991 return (-1);
2992 } else {
2993 INFO("netapp plugin: Connected to VFiler '%s' on host '%s'.",
2994 host->vfiler, host->host);
2995 }
2996 }
2998 return (0);
2999 } /* }}} int cna_init_host */
3001 static int cna_init(void) /* {{{ */
3002 {
3003 char err[256] = {0};
3005 if (!na_startup(err, sizeof(err))) {
3006 err[sizeof(err) - 1] = 0;
3007 ERROR("netapp plugin: Error initializing netapp API: %s", err);
3008 return 1;
3009 }
3011 return (0);
3012 } /* }}} cna_init */
3014 static int cna_read_internal(host_config_t *host) { /* {{{ */
3015 int status;
3017 status = cna_query_wafl(host);
3018 if (status != 0)
3019 return (status);
3021 status = cna_query_disk(host);
3022 if (status != 0)
3023 return (status);
3025 status = cna_query_volume_perf(host);
3026 if (status != 0)
3027 return (status);
3029 status = cna_query_volume_usage(host);
3030 if (status != 0)
3031 return (status);
3033 status = cna_query_quota(host);
3034 if (status != 0)
3035 return (status);
3037 status = cna_query_snapvault(host);
3038 if (status != 0)
3039 return (status);
3041 status = cna_query_system(host);
3042 if (status != 0)
3043 return (status);
3045 return 0;
3046 } /* }}} int cna_read_internal */
3048 static int cna_read(user_data_t *ud) { /* {{{ */
3049 host_config_t *host;
3050 int status;
3052 if ((ud == NULL) || (ud->data == NULL))
3053 return (-1);
3055 host = ud->data;
3057 status = cna_init_host(host);
3058 if (status != 0)
3059 return (status);
3061 status = cna_read_internal(host);
3062 if (status != 0) {
3063 if (host->srv != NULL)
3064 na_server_close(host->srv);
3065 host->srv = NULL;
3066 }
3068 return 0;
3069 } /* }}} int cna_read */
3071 static int cna_config(oconfig_item_t *ci) { /* {{{ */
3072 oconfig_item_t *item;
3074 for (int i = 0; i < ci->children_num; ++i) {
3075 item = ci->children + i;
3077 if (strcasecmp(item->key, "Host") == 0) {
3078 host_config_t *host;
3080 host = cna_alloc_host();
3081 if (host == NULL) {
3082 ERROR("netapp plugin: Failed to allocate host object.");
3083 continue;
3084 }
3086 if (cna_config_host(host, item) != 0) {
3087 free_host_config(host);
3088 continue;
3089 }
3091 cna_register_host(host);
3092 } else /* if (item->key != "Host") */
3093 {
3094 WARNING("netapp plugin: Ignoring unknown config option \"%s\".",
3095 item->key);
3096 }
3097 }
3098 return 0;
3099 } /* }}} int cna_config */
3101 static int cna_shutdown(void) /* {{{ */
3102 {
3103 /* Clean up system resources and stuff. */
3104 na_shutdown();
3106 return (0);
3107 } /* }}} int cna_shutdown */
3109 void module_register(void) {
3110 plugin_register_complex_config("netapp", cna_config);
3111 plugin_register_init("netapp", cna_init);
3112 plugin_register_shutdown("netapp", cna_shutdown);
3113 }