1 /**
2 * collectd - src/virt.c
3 * Copyright (C) 2006-2008 Red Hat Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the license is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Richard W.M. Jones <rjones@redhat.com>
20 **/
22 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "utils_complain.h"
27 #include "utils_ignorelist.h"
29 #include <libgen.h> /* for basename(3) */
30 #include <libvirt/libvirt.h>
31 #include <libvirt/virterror.h>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
34 #include <libxml/xpath.h>
35 #include <libxml/xpathInternals.h>
37 /* Plugin name */
38 #define PLUGIN_NAME "virt"
40 #ifdef LIBVIR_CHECK_VERSION
42 #if LIBVIR_CHECK_VERSION(0, 9, 5)
43 #define HAVE_BLOCK_STATS_FLAGS 1
44 #endif
46 #if LIBVIR_CHECK_VERSION(0, 9, 11)
47 #define HAVE_CPU_STATS 1
48 #endif
50 #endif /* LIBVIR_CHECK_VERSION */
52 static const char *config_keys[] = {"Connection",
54 "RefreshInterval",
56 "Domain",
57 "BlockDevice",
58 "BlockDeviceFormat",
59 "BlockDeviceFormatBasename",
60 "InterfaceDevice",
61 "IgnoreSelected",
63 "HostnameFormat",
64 "InterfaceFormat",
66 "PluginInstanceFormat",
68 "Instances",
69 "ExtraStats",
71 NULL};
72 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
74 /* Connection. */
75 static virConnectPtr conn = 0;
76 static char *conn_string = NULL;
77 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
79 /* Seconds between list refreshes, 0 disables completely. */
80 static int interval = 60;
82 /* List of domains, if specified. */
83 static ignorelist_t *il_domains = NULL;
84 /* List of block devices, if specified. */
85 static ignorelist_t *il_block_devices = NULL;
86 /* List of network interface devices, if specified. */
87 static ignorelist_t *il_interface_devices = NULL;
89 static int ignore_device_match(ignorelist_t *, const char *domname,
90 const char *devpath);
92 /* Actual list of block devices found on last refresh. */
93 struct block_device {
94 virDomainPtr dom; /* domain */
95 char *path; /* name of block device */
96 };
98 /* Actual list of network interfaces found on last refresh. */
99 struct interface_device {
100 virDomainPtr dom; /* domain */
101 char *path; /* name of interface device */
102 char *address; /* mac address of interface device */
103 char *number; /* interface device number */
104 };
106 struct lv_read_state {
107 /* Actual list of domains found on last refresh. */
108 virDomainPtr *domains;
109 int nr_domains;
111 struct block_device *block_devices;
112 int nr_block_devices;
114 struct interface_device *interface_devices;
115 int nr_interface_devices;
116 };
118 static void free_domains(struct lv_read_state *state);
119 static int add_domain(struct lv_read_state *state, virDomainPtr dom);
121 static void free_block_devices(struct lv_read_state *state);
122 static int add_block_device(struct lv_read_state *state, virDomainPtr dom,
123 const char *path);
125 static void free_interface_devices(struct lv_read_state *state);
126 static int add_interface_device(struct lv_read_state *state, virDomainPtr dom,
127 const char *path, const char *address,
128 unsigned int number);
130 #define METADATA_VM_PARTITION_URI "http://ovirt.org/ovirtmap/tag/1.0"
131 #define METADATA_VM_PARTITION_ELEMENT "tag"
132 #define METADATA_VM_PARTITION_PREFIX "ovirtmap"
134 #define BUFFER_MAX_LEN 256
135 #define PARTITION_TAG_MAX_LEN 32
137 struct lv_read_instance {
138 struct lv_read_state read_state;
139 char tag[PARTITION_TAG_MAX_LEN];
140 size_t id;
141 };
143 struct lv_user_data {
144 struct lv_read_instance inst;
145 user_data_t ud;
146 };
148 #define NR_INSTANCES_DEFAULT 1
149 #define NR_INSTANCES_MAX 128
150 static int nr_instances = NR_INSTANCES_DEFAULT;
151 static struct lv_user_data lv_read_user_data[NR_INSTANCES_MAX];
153 /* HostnameFormat. */
154 #define HF_MAX_FIELDS 3
156 enum hf_field { hf_none = 0, hf_hostname, hf_name, hf_uuid };
158 static enum hf_field hostname_format[HF_MAX_FIELDS] = {hf_name};
160 /* PluginInstanceFormat */
161 #define PLGINST_MAX_FIELDS 2
163 enum plginst_field { plginst_none = 0, plginst_name, plginst_uuid };
165 static enum plginst_field plugin_instance_format[PLGINST_MAX_FIELDS] = {
166 plginst_none};
168 /* BlockDeviceFormat */
169 enum bd_field { target, source };
171 /* InterfaceFormat. */
172 enum if_field { if_address, if_name, if_number };
174 /* ExtraStats */
175 #define EX_STATS_MAX_FIELDS 8
176 enum ex_stats { ex_stats_none = 0, ex_stats_disk = 1, ex_stats_pcpu = 2 };
177 static unsigned int extra_stats = ex_stats_none;
179 struct ex_stats_item {
180 const char *name;
181 enum ex_stats flag;
182 };
183 static const struct ex_stats_item ex_stats_table[] = {
184 {"disk", ex_stats_disk}, {"pcpu", ex_stats_pcpu}, {NULL, ex_stats_none},
185 };
187 /* BlockDeviceFormatBasename */
188 _Bool blockdevice_format_basename = 0;
189 static enum bd_field blockdevice_format = target;
190 static enum if_field interface_format = if_name;
192 /* Time that we last refreshed. */
193 static time_t last_refresh = (time_t)0;
195 static int refresh_lists(struct lv_read_instance *inst);
197 struct lv_info {
198 virDomainInfo di;
199 unsigned long long total_user_cpu_time;
200 unsigned long long total_syst_cpu_time;
201 };
203 struct lv_block_info {
204 virDomainBlockStatsStruct bi;
206 long long rd_total_times;
207 long long wr_total_times;
209 long long fl_req;
210 long long fl_total_times;
211 };
213 static void init_block_info(struct lv_block_info *binfo) {
214 if (binfo == NULL)
215 return;
217 binfo->bi.rd_req = -1;
218 binfo->bi.wr_req = -1;
219 binfo->bi.rd_bytes = -1;
220 binfo->bi.wr_bytes = -1;
222 binfo->rd_total_times = -1;
223 binfo->wr_total_times = -1;
224 binfo->fl_req = -1;
225 binfo->fl_total_times = -1;
226 }
228 #ifdef HAVE_BLOCK_STATS_FLAGS
230 #define GET_BLOCK_INFO_VALUE(NAME, FIELD) \
231 do { \
232 if (!strcmp(param[i].field, NAME)) { \
233 binfo->FIELD = param[i].value.l; \
234 continue; \
235 } \
236 } while (0)
238 static int get_block_info(struct lv_block_info *binfo,
239 virTypedParameterPtr param, int nparams) {
240 if (binfo == NULL || param == NULL)
241 return -1;
243 for (int i = 0; i < nparams; ++i) {
244 /* ignore type. Everything must be LLONG anyway. */
245 GET_BLOCK_INFO_VALUE("rd_operations", bi.rd_req);
246 GET_BLOCK_INFO_VALUE("wr_operations", bi.wr_req);
247 GET_BLOCK_INFO_VALUE("rd_bytes", bi.rd_bytes);
248 GET_BLOCK_INFO_VALUE("wr_bytes", bi.wr_bytes);
249 GET_BLOCK_INFO_VALUE("rd_total_times", rd_total_times);
250 GET_BLOCK_INFO_VALUE("wr_total_times", wr_total_times);
251 GET_BLOCK_INFO_VALUE("flush_operations", fl_req);
252 GET_BLOCK_INFO_VALUE("flush_total_times", fl_total_times);
253 }
255 return 0;
256 }
258 #undef GET_BLOCK_INFO_VALUE
260 #endif /* HAVE_BLOCK_STATS_FLAGS */
262 /* ERROR(...) macro for virterrors. */
263 #define VIRT_ERROR(conn, s) \
264 do { \
265 virErrorPtr err; \
266 err = (conn) ? virConnGetLastError((conn)) : virGetLastError(); \
267 if (err) \
268 ERROR("%s: %s", (s), err->message); \
269 } while (0)
271 static void init_lv_info(struct lv_info *info) {
272 if (info != NULL)
273 memset(info, 0, sizeof(*info));
274 }
276 static int lv_domain_info(virDomainPtr dom, struct lv_info *info) {
277 #ifdef HAVE_CPU_STATS
278 virTypedParameterPtr param = NULL;
279 int nparams = 0;
280 #endif /* HAVE_CPU_STATS */
281 int ret = virDomainGetInfo(dom, &(info->di));
282 if (ret != 0) {
283 return ret;
284 }
286 #ifdef HAVE_CPU_STATS
287 nparams = virDomainGetCPUStats(dom, NULL, 0, -1, 1, 0);
288 if (nparams < 0) {
289 VIRT_ERROR(conn, "getting the CPU params count");
290 return -1;
291 }
293 param = calloc(nparams, sizeof(virTypedParameter));
294 if (param == NULL) {
295 ERROR("virt plugin: alloc(%i) for cpu parameters failed.", nparams);
296 return -1;
297 }
299 ret = virDomainGetCPUStats(dom, param, nparams, -1, 1, 0); // total stats.
300 if (ret < 0) {
301 virTypedParamsFree(param, nparams);
302 VIRT_ERROR(conn, "getting the disk params values");
303 return -1;
304 }
306 for (int i = 0; i < nparams; ++i) {
307 if (!strcmp(param[i].field, "user_time"))
308 info->total_user_cpu_time = param[i].value.ul;
309 else if (!strcmp(param[i].field, "system_time"))
310 info->total_syst_cpu_time = param[i].value.ul;
311 }
313 virTypedParamsFree(param, nparams);
314 #endif /* HAVE_CPU_STATS */
316 return 0;
317 }
319 static void init_value_list(value_list_t *vl, virDomainPtr dom) {
320 int n;
321 const char *name;
322 char uuid[VIR_UUID_STRING_BUFLEN];
324 sstrncpy(vl->plugin, PLUGIN_NAME, sizeof(vl->plugin));
326 vl->host[0] = '\0';
328 /* Construct the hostname field according to HostnameFormat. */
329 for (int i = 0; i < HF_MAX_FIELDS; ++i) {
330 if (hostname_format[i] == hf_none)
331 continue;
333 n = DATA_MAX_NAME_LEN - strlen(vl->host) - 2;
335 if (i > 0 && n >= 1) {
336 strncat(vl->host, ":", 1);
337 n--;
338 }
340 switch (hostname_format[i]) {
341 case hf_none:
342 break;
343 case hf_hostname:
344 strncat(vl->host, hostname_g, n);
345 break;
346 case hf_name:
347 name = virDomainGetName(dom);
348 if (name)
349 strncat(vl->host, name, n);
350 break;
351 case hf_uuid:
352 if (virDomainGetUUIDString(dom, uuid) == 0)
353 strncat(vl->host, uuid, n);
354 break;
355 }
356 }
358 vl->host[sizeof(vl->host) - 1] = '\0';
360 /* Construct the plugin instance field according to PluginInstanceFormat. */
361 for (int i = 0; i < PLGINST_MAX_FIELDS; ++i) {
362 if (plugin_instance_format[i] == plginst_none)
363 continue;
365 n = sizeof(vl->plugin_instance) - strlen(vl->plugin_instance) - 2;
367 if (i > 0 && n >= 1) {
368 strncat(vl->plugin_instance, ":", 1);
369 n--;
370 }
372 switch (plugin_instance_format[i]) {
373 case plginst_none:
374 break;
375 case plginst_name:
376 name = virDomainGetName(dom);
377 if (name)
378 strncat(vl->plugin_instance, name, n);
379 break;
380 case plginst_uuid:
381 if (virDomainGetUUIDString(dom, uuid) == 0)
382 strncat(vl->plugin_instance, uuid, n);
383 break;
384 }
385 }
387 vl->plugin_instance[sizeof(vl->plugin_instance) - 1] = '\0';
389 } /* void init_value_list */
391 static void submit(virDomainPtr dom, char const *type,
392 char const *type_instance, value_t *values,
393 size_t values_len) {
394 value_list_t vl = VALUE_LIST_INIT;
395 init_value_list(&vl, dom);
397 vl.values = values;
398 vl.values_len = values_len;
400 sstrncpy(vl.type, type, sizeof(vl.type));
401 if (type_instance != NULL)
402 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
404 plugin_dispatch_values(&vl);
405 }
407 static void memory_submit(gauge_t value, virDomainPtr dom) {
408 submit(dom, "memory", "total", &(value_t){.gauge = value}, 1);
409 }
411 static void memory_stats_submit(gauge_t value, virDomainPtr dom,
412 int tag_index) {
413 static const char *tags[] = {"swap_in", "swap_out", "major_fault",
414 "minor_fault", "unused", "available",
415 "actual_balloon", "rss"};
417 if ((tag_index < 0) || (tag_index >= STATIC_ARRAY_SIZE(tags))) {
418 ERROR("virt plugin: Array index out of bounds: tag_index = %d", tag_index);
419 return;
420 }
422 submit(dom, "memory", tags[tag_index], &(value_t){.gauge = value}, 1);
423 }
425 static void submit_derive2(const char *type, derive_t v0, derive_t v1,
426 virDomainPtr dom, const char *devname) {
427 value_t values[] = {
428 {.derive = v0}, {.derive = v1},
429 };
431 submit(dom, type, devname, values, STATIC_ARRAY_SIZE(values));
432 } /* void submit_derive2 */
434 static void pcpu_submit(virDomainPtr dom, struct lv_info *info) {
435 #ifdef HAVE_CPU_STATS
436 if (extra_stats & ex_stats_pcpu)
437 submit_derive2("ps_cputime", info->total_user_cpu_time,
438 info->total_syst_cpu_time, dom, NULL);
439 #endif /* HAVE_CPU_STATS */
440 }
442 static void cpu_submit(unsigned long long value, virDomainPtr dom,
443 const char *type) {
444 submit(dom, type, NULL, &(value_t){.derive = (derive_t)value}, 1);
445 }
447 static void vcpu_submit(derive_t value, virDomainPtr dom, int vcpu_nr,
448 const char *type) {
449 char type_instance[DATA_MAX_NAME_LEN];
451 ssnprintf(type_instance, sizeof(type_instance), "%d", vcpu_nr);
453 submit(dom, type, type_instance, &(value_t){.derive = value}, 1);
454 }
456 static void disk_submit(struct lv_block_info *binfo, virDomainPtr dom,
457 const char *type_instance) {
458 char flush_type_instance[DATA_MAX_NAME_LEN];
460 ssnprintf(flush_type_instance, sizeof(flush_type_instance), "flush-%s",
461 type_instance);
463 if ((binfo->bi.rd_req != -1) && (binfo->bi.wr_req != -1))
464 submit_derive2("disk_ops", (derive_t)binfo->bi.rd_req,
465 (derive_t)binfo->bi.wr_req, dom, type_instance);
467 if ((binfo->bi.rd_bytes != -1) && (binfo->bi.wr_bytes != -1))
468 submit_derive2("disk_octets", (derive_t)binfo->bi.rd_bytes,
469 (derive_t)binfo->bi.wr_bytes, dom, type_instance);
471 if (extra_stats & ex_stats_disk) {
472 if ((binfo->rd_total_times != -1) && (binfo->wr_total_times != -1))
473 submit_derive2("disk_time", (derive_t)binfo->rd_total_times,
474 (derive_t)binfo->wr_total_times, dom, type_instance);
476 if (binfo->fl_req != -1)
477 submit(dom, "total_requests", flush_type_instance,
478 &(value_t){.derive = (derive_t)binfo->fl_req}, 1);
479 if (binfo->fl_total_times != -1) {
480 derive_t value = binfo->fl_total_times / 1000; // ns -> ms
481 submit(dom, "total_time_in_ms", flush_type_instance,
482 &(value_t){.derive = value}, 1);
483 }
484 }
485 }
487 static unsigned int parse_ex_stats_flags(char **exstats, int numexstats) {
488 int extra_stats = ex_stats_none;
489 for (int i = 0; i < numexstats; i++) {
490 for (int j = 0; ex_stats_table[j].name != NULL; j++) {
491 if (strcasecmp(exstats[i], ex_stats_table[j].name) == 0) {
492 DEBUG(PLUGIN_NAME " plugin: enabling extra stats for '%s'",
493 ex_stats_table[j].name);
494 extra_stats |= ex_stats_table[j].flag;
495 break;
496 }
497 }
498 }
499 return extra_stats;
500 }
502 static int lv_config(const char *key, const char *value) {
503 if (virInitialize() != 0)
504 return 1;
506 if (il_domains == NULL)
507 il_domains = ignorelist_create(1);
508 if (il_block_devices == NULL)
509 il_block_devices = ignorelist_create(1);
510 if (il_interface_devices == NULL)
511 il_interface_devices = ignorelist_create(1);
513 if (strcasecmp(key, "Connection") == 0) {
514 char *tmp = strdup(value);
515 if (tmp == NULL) {
516 ERROR(PLUGIN_NAME " plugin: Connection strdup failed.");
517 return 1;
518 }
519 sfree(conn_string);
520 conn_string = tmp;
521 return 0;
522 }
524 if (strcasecmp(key, "RefreshInterval") == 0) {
525 char *eptr = NULL;
526 interval = strtol(value, &eptr, 10);
527 if (eptr == NULL || *eptr != '\0')
528 return 1;
529 return 0;
530 }
532 if (strcasecmp(key, "Domain") == 0) {
533 if (ignorelist_add(il_domains, value))
534 return 1;
535 return 0;
536 }
537 if (strcasecmp(key, "BlockDevice") == 0) {
538 if (ignorelist_add(il_block_devices, value))
539 return 1;
540 return 0;
541 }
543 if (strcasecmp(key, "BlockDeviceFormat") == 0) {
544 if (strcasecmp(value, "target") == 0)
545 blockdevice_format = target;
546 else if (strcasecmp(value, "source") == 0)
547 blockdevice_format = source;
548 else {
549 ERROR(PLUGIN_NAME " plugin: unknown BlockDeviceFormat: %s", value);
550 return -1;
551 }
552 return 0;
553 }
554 if (strcasecmp(key, "BlockDeviceFormatBasename") == 0) {
555 blockdevice_format_basename = IS_TRUE(value);
556 return 0;
557 }
558 if (strcasecmp(key, "InterfaceDevice") == 0) {
559 if (ignorelist_add(il_interface_devices, value))
560 return 1;
561 return 0;
562 }
564 if (strcasecmp(key, "IgnoreSelected") == 0) {
565 if (IS_TRUE(value)) {
566 ignorelist_set_invert(il_domains, 0);
567 ignorelist_set_invert(il_block_devices, 0);
568 ignorelist_set_invert(il_interface_devices, 0);
569 } else {
570 ignorelist_set_invert(il_domains, 1);
571 ignorelist_set_invert(il_block_devices, 1);
572 ignorelist_set_invert(il_interface_devices, 1);
573 }
574 return 0;
575 }
577 if (strcasecmp(key, "HostnameFormat") == 0) {
578 char *value_copy;
579 char *fields[HF_MAX_FIELDS];
580 int n;
582 value_copy = strdup(value);
583 if (value_copy == NULL) {
584 ERROR(PLUGIN_NAME " plugin: strdup failed.");
585 return -1;
586 }
588 n = strsplit(value_copy, fields, HF_MAX_FIELDS);
589 if (n < 1) {
590 sfree(value_copy);
591 ERROR(PLUGIN_NAME " plugin: HostnameFormat: no fields");
592 return -1;
593 }
595 for (int i = 0; i < n; ++i) {
596 if (strcasecmp(fields[i], "hostname") == 0)
597 hostname_format[i] = hf_hostname;
598 else if (strcasecmp(fields[i], "name") == 0)
599 hostname_format[i] = hf_name;
600 else if (strcasecmp(fields[i], "uuid") == 0)
601 hostname_format[i] = hf_uuid;
602 else {
603 ERROR(PLUGIN_NAME " plugin: unknown HostnameFormat field: %s",
604 fields[i]);
605 sfree(value_copy);
606 return -1;
607 }
608 }
609 sfree(value_copy);
611 for (int i = n; i < HF_MAX_FIELDS; ++i)
612 hostname_format[i] = hf_none;
614 return 0;
615 }
617 if (strcasecmp(key, "PluginInstanceFormat") == 0) {
618 char *value_copy;
619 char *fields[PLGINST_MAX_FIELDS];
620 int n;
622 value_copy = strdup(value);
623 if (value_copy == NULL) {
624 ERROR(PLUGIN_NAME " plugin: strdup failed.");
625 return -1;
626 }
628 n = strsplit(value_copy, fields, PLGINST_MAX_FIELDS);
629 if (n < 1) {
630 sfree(value_copy);
631 ERROR(PLUGIN_NAME " plugin: PluginInstanceFormat: no fields");
632 return -1;
633 }
635 for (int i = 0; i < n; ++i) {
636 if (strcasecmp(fields[i], "none") == 0) {
637 plugin_instance_format[i] = plginst_none;
638 break;
639 } else if (strcasecmp(fields[i], "name") == 0)
640 plugin_instance_format[i] = plginst_name;
641 else if (strcasecmp(fields[i], "uuid") == 0)
642 plugin_instance_format[i] = plginst_uuid;
643 else {
644 ERROR(PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s",
645 fields[i]);
646 sfree(value_copy);
647 return -1;
648 }
649 }
650 sfree(value_copy);
652 for (int i = n; i < PLGINST_MAX_FIELDS; ++i)
653 plugin_instance_format[i] = plginst_none;
655 return 0;
656 }
658 if (strcasecmp(key, "InterfaceFormat") == 0) {
659 if (strcasecmp(value, "name") == 0)
660 interface_format = if_name;
661 else if (strcasecmp(value, "address") == 0)
662 interface_format = if_address;
663 else if (strcasecmp(value, "number") == 0)
664 interface_format = if_number;
665 else {
666 ERROR(PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", value);
667 return -1;
668 }
669 return 0;
670 }
672 if (strcasecmp(key, "Instances") == 0) {
673 char *eptr = NULL;
674 double val = strtod(value, &eptr);
676 if (*eptr != '\0') {
677 ERROR(PLUGIN_NAME " plugin: Invalid value for Instances = '%s'", value);
678 return 1;
679 }
680 if (val <= 0) {
681 ERROR(PLUGIN_NAME " plugin: Instances <= 0 makes no sense.");
682 return 1;
683 }
684 if (val > NR_INSTANCES_MAX) {
685 ERROR(PLUGIN_NAME " plugin: Instances=%f > NR_INSTANCES_MAX=%i"
686 " use a lower setting or recompile the plugin.",
687 val, NR_INSTANCES_MAX);
688 return 1;
689 }
691 nr_instances = (int)val;
692 DEBUG(PLUGIN_NAME " plugin: configured %i instances", nr_instances);
693 return 0;
694 }
696 if (strcasecmp(key, "ExtraStats") == 0) {
697 char *localvalue = strdup(value);
698 if (localvalue != NULL) {
699 char *exstats[EX_STATS_MAX_FIELDS];
700 int numexstats =
701 strsplit(localvalue, exstats, STATIC_ARRAY_SIZE(exstats));
702 extra_stats = parse_ex_stats_flags(exstats, numexstats);
703 sfree(localvalue);
704 }
705 }
707 /* Unrecognised option. */
708 return -1;
709 }
711 static int lv_connect(void) {
712 if (conn == NULL) {
713 /* `conn_string == NULL' is acceptable. */
714 conn = virConnectOpenReadOnly(conn_string);
715 if (conn == NULL) {
716 c_complain(LOG_ERR, &conn_complain,
717 PLUGIN_NAME " plugin: Unable to connect: "
718 "virConnectOpenReadOnly failed.");
719 return -1;
720 }
721 }
722 c_release(LOG_NOTICE, &conn_complain,
723 PLUGIN_NAME " plugin: Connection established.");
724 return 0;
725 }
727 static void lv_disconnect(void) {
728 if (conn != NULL)
729 virConnectClose(conn);
730 conn = NULL;
731 WARNING(PLUGIN_NAME " plugin: closed connection to libvirt");
732 }
734 static int lv_domain_block_info(virDomainPtr dom, const char *path,
735 struct lv_block_info *binfo) {
736 #ifdef HAVE_BLOCK_STATS_FLAGS
737 virTypedParameterPtr params = NULL;
738 int nparams = 0;
739 int rc = -1;
740 int ret = virDomainBlockStatsFlags(dom, path, NULL, &nparams, 0);
741 if (ret < 0 || nparams == 0) {
742 VIRT_ERROR(conn, "getting the disk params count");
743 return -1;
744 }
746 params = calloc(nparams, sizeof(virTypedParameter));
747 if (params == NULL) {
748 ERROR("virt plugin: alloc(%i) for block=%s parameters failed.", nparams,
749 path);
750 return -1;
751 }
752 ret = virDomainBlockStatsFlags(dom, path, params, &nparams, 0);
753 if (ret < 0) {
754 VIRT_ERROR(conn, "getting the disk params values");
755 goto done;
756 }
758 rc = get_block_info(binfo, params, nparams);
760 done:
761 virTypedParamsFree(params, nparams);
762 return rc;
763 #else
764 return virDomainBlockStats(dom, path, &(binfo->bi), sizeof(binfo->bi));
765 #endif /* HAVE_BLOCK_STATS_FLAGS */
766 }
768 static int lv_read(user_data_t *ud) {
769 time_t t;
770 struct lv_read_instance *inst = NULL;
771 struct lv_read_state *state = NULL;
773 if (ud->data == NULL) {
774 ERROR(PLUGIN_NAME " plugin: NULL userdata");
775 return -1;
776 }
778 inst = ud->data;
779 state = &inst->read_state;
781 if (inst->id == 0) {
782 if (lv_connect() < 0)
783 return -1;
784 }
786 time(&t);
788 /* Need to refresh domain or device lists? */
789 if ((last_refresh == (time_t)0) ||
790 ((interval > 0) && ((last_refresh + interval) <= t))) {
791 if (refresh_lists(inst) != 0) {
792 if (inst->id == 0)
793 lv_disconnect();
794 return -1;
795 }
796 last_refresh = t;
797 }
799 #if 0
800 for (int i = 0; i < nr_domains; ++i)
801 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
802 for (int i = 0; i < nr_block_devices; ++i)
803 fprintf (stderr, "block device %d %s:%s\n",
804 i, virDomainGetName (block_devices[i].dom),
805 block_devices[i].path);
806 for (int i = 0; i < nr_interface_devices; ++i)
807 fprintf (stderr, "interface device %d %s:%s\n",
808 i, virDomainGetName (interface_devices[i].dom),
809 interface_devices[i].path);
810 #endif
812 /* Get CPU usage, memory, VCPU usage for each domain. */
813 for (int i = 0; i < state->nr_domains; ++i) {
814 struct lv_info info;
815 virVcpuInfoPtr vinfo = NULL;
816 virDomainMemoryStatPtr minfo = NULL;
817 int status;
819 init_lv_info(&info);
820 status = lv_domain_info(state->domains[i], &info);
821 if (status != 0) {
822 ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
823 status);
824 continue;
825 }
827 if (info.di.state != VIR_DOMAIN_RUNNING) {
828 /* only gather stats for running domains */
829 continue;
830 }
832 pcpu_submit(state->domains[i], &info);
833 cpu_submit(info.di.cpuTime, state->domains[i], "virt_cpu_total");
834 memory_submit((gauge_t)info.di.memory * 1024, state->domains[i]);
836 vinfo = malloc(info.di.nrVirtCpu * sizeof(vinfo[0]));
837 if (vinfo == NULL) {
838 ERROR(PLUGIN_NAME " plugin: malloc failed.");
839 continue;
840 }
842 status = virDomainGetVcpus(state->domains[i], vinfo, info.di.nrVirtCpu,
843 /* cpu map = */ NULL, /* cpu map length = */ 0);
844 if (status < 0) {
845 ERROR(PLUGIN_NAME " plugin: virDomainGetVcpus failed with status %i.",
846 status);
847 sfree(vinfo);
848 continue;
849 }
851 for (int j = 0; j < info.di.nrVirtCpu; ++j)
852 vcpu_submit(vinfo[j].cpuTime, state->domains[i], vinfo[j].number,
853 "virt_vcpu");
855 sfree(vinfo);
857 minfo =
858 malloc(VIR_DOMAIN_MEMORY_STAT_NR * sizeof(virDomainMemoryStatStruct));
859 if (minfo == NULL) {
860 ERROR("virt plugin: malloc failed.");
861 continue;
862 }
864 status = virDomainMemoryStats(state->domains[i], minfo,
865 VIR_DOMAIN_MEMORY_STAT_NR, 0);
867 if (status < 0) {
868 ERROR("virt plugin: virDomainMemoryStats failed with status %i.", status);
869 sfree(minfo);
870 continue;
871 }
873 for (int j = 0; j < status; j++) {
874 memory_stats_submit((gauge_t)minfo[j].val * 1024, state->domains[i],
875 minfo[j].tag);
876 }
878 sfree(minfo);
879 }
881 /* Get block device stats for each domain. */
882 for (int i = 0; i < state->nr_block_devices; ++i) {
883 struct block_device *bdev = &(state->block_devices[i]);
884 struct lv_block_info binfo;
885 init_block_info(&binfo);
887 if (lv_domain_block_info(bdev->dom, bdev->path, &binfo) < 0)
888 continue;
890 char *type_instance = bdev->path;
891 char *path = NULL;
892 if (blockdevice_format_basename && blockdevice_format == source) {
893 path = strdup(bdev->path);
894 if (path == NULL) {
895 WARNING(PLUGIN_NAME
896 " plugin: error extracting the basename for '%s', skipped",
897 bdev->path);
898 continue;
899 }
900 type_instance = basename(path);
901 }
903 disk_submit(&binfo, bdev->dom, type_instance);
905 sfree(path);
906 } /* for (nr_block_devices) */
908 /* Get interface stats for each domain. */
909 for (int i = 0; i < state->nr_interface_devices; ++i) {
910 struct _virDomainInterfaceStats stats;
911 char *display_name = NULL;
913 switch (interface_format) {
914 case if_address:
915 display_name = state->interface_devices[i].address;
916 break;
917 case if_number:
918 display_name = state->interface_devices[i].number;
919 break;
920 case if_name:
921 default:
922 display_name = state->interface_devices[i].path;
923 }
925 if (virDomainInterfaceStats(state->interface_devices[i].dom,
926 state->interface_devices[i].path, &stats,
927 sizeof stats) != 0)
928 continue;
930 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
931 submit_derive2("if_octets", (derive_t)stats.rx_bytes,
932 (derive_t)stats.tx_bytes, state->interface_devices[i].dom,
933 display_name);
935 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
936 submit_derive2("if_packets", (derive_t)stats.rx_packets,
937 (derive_t)stats.tx_packets,
938 state->interface_devices[i].dom, display_name);
940 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
941 submit_derive2("if_errors", (derive_t)stats.rx_errs,
942 (derive_t)stats.tx_errs, state->interface_devices[i].dom,
943 display_name);
945 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
946 submit_derive2("if_dropped", (derive_t)stats.rx_drop,
947 (derive_t)stats.tx_drop, state->interface_devices[i].dom,
948 display_name);
949 } /* for (nr_interface_devices) */
951 return 0;
952 }
954 static int lv_init_instance(size_t i, plugin_read_cb callback) {
955 struct lv_user_data *lv_ud = &(lv_read_user_data[i]);
956 struct lv_read_instance *inst = &(lv_ud->inst);
958 memset(lv_ud, 0, sizeof(*lv_ud));
960 ssnprintf(inst->tag, sizeof(inst->tag), "%s-%zu", PLUGIN_NAME, i);
961 inst->id = i;
963 user_data_t *ud = &(lv_ud->ud);
964 ud->data = inst;
965 ud->free_func = NULL;
967 INFO(PLUGIN_NAME " plugin: reader %s initialized", inst->tag);
968 return plugin_register_complex_read(NULL, inst->tag, callback, 0, ud);
969 }
971 static void lv_clean_read_state(struct lv_read_state *state) {
972 free_block_devices(state);
973 free_interface_devices(state);
974 free_domains(state);
975 }
977 static void lv_fini_instance(size_t i) {
978 struct lv_read_instance *inst = &(lv_read_user_data[i].inst);
979 struct lv_read_state *state = &(inst->read_state);
981 lv_clean_read_state(state);
982 INFO(PLUGIN_NAME " plugin: reader %s finalized", inst->tag);
983 }
985 static int lv_init(void) {
986 if (virInitialize() != 0)
987 return -1;
989 if (lv_connect() != 0)
990 return -1;
992 DEBUG(PLUGIN_NAME " plugin: starting %i instances", nr_instances);
994 for (int i = 0; i < nr_instances; ++i)
995 lv_init_instance(i, lv_read);
997 return 0;
998 }
1000 /*
1001 * returns 0 on success and <0 on error
1002 */
1003 static int lv_domain_get_tag(xmlXPathContextPtr xpath_ctx, const char *dom_name,
1004 char *dom_tag) {
1005 char xpath_str[BUFFER_MAX_LEN] = {'\0'};
1006 xmlXPathObjectPtr xpath_obj = NULL;
1007 xmlNodePtr xml_node = NULL;
1008 int ret = -1;
1009 int err;
1011 err = xmlXPathRegisterNs(xpath_ctx,
1012 (const xmlChar *)METADATA_VM_PARTITION_PREFIX,
1013 (const xmlChar *)METADATA_VM_PARTITION_URI);
1014 if (err) {
1015 ERROR(PLUGIN_NAME " plugin: xmlXpathRegisterNs(%s, %s) failed on domain %s",
1016 METADATA_VM_PARTITION_PREFIX, METADATA_VM_PARTITION_URI, dom_name);
1017 goto done;
1018 }
1020 ssnprintf(xpath_str, sizeof(xpath_str), "/domain/metadata/%s:%s/text()",
1021 METADATA_VM_PARTITION_PREFIX, METADATA_VM_PARTITION_ELEMENT);
1022 xpath_obj = xmlXPathEvalExpression((xmlChar *)xpath_str, xpath_ctx);
1023 if (xpath_obj == NULL) {
1024 ERROR(PLUGIN_NAME " plugin: xmlXPathEval(%s) failed on domain %s",
1025 xpath_str, dom_name);
1026 goto done;
1027 }
1029 if (xpath_obj->type != XPATH_NODESET) {
1030 ERROR(PLUGIN_NAME " plugin: xmlXPathEval(%s) unexpected return type %d "
1031 "(wanted %d) on domain %s",
1032 xpath_str, xpath_obj->type, XPATH_NODESET, dom_name);
1033 goto done;
1034 }
1036 /*
1037 * from now on there is no real error, it's ok if a domain
1038 * doesn't have the metadata partition tag.
1039 */
1040 ret = 0;
1041 if (xpath_obj->nodesetval == NULL || xpath_obj->nodesetval->nodeNr != 1) {
1042 DEBUG(PLUGIN_NAME " plugin: xmlXPathEval(%s) return nodeset size=%i "
1043 "expected=1 on domain %s",
1044 xpath_str,
1045 (xpath_obj->nodesetval == NULL) ? 0 : xpath_obj->nodesetval->nodeNr,
1046 dom_name);
1047 } else {
1048 xml_node = xpath_obj->nodesetval->nodeTab[0];
1049 sstrncpy(dom_tag, (const char *)xml_node->content, PARTITION_TAG_MAX_LEN);
1050 }
1052 done:
1053 /* deregister to clean up */
1054 err = xmlXPathRegisterNs(xpath_ctx,
1055 (const xmlChar *)METADATA_VM_PARTITION_PREFIX, NULL);
1056 if (err) {
1057 /* we can't really recover here */
1058 ERROR(PLUGIN_NAME
1059 " plugin: deregistration of namespace %s failed for domain %s",
1060 METADATA_VM_PARTITION_PREFIX, dom_name);
1061 }
1062 if (xpath_obj)
1063 xmlXPathFreeObject(xpath_obj);
1065 return ret;
1066 }
1068 static int is_known_tag(const char *dom_tag) {
1069 for (int i = 0; i < nr_instances; ++i)
1070 if (!strcmp(dom_tag, lv_read_user_data[i].inst.tag))
1071 return 1;
1072 return 0;
1073 }
1075 static int lv_instance_include_domain(struct lv_read_instance *inst,
1076 const char *dom_name,
1077 const char *dom_tag) {
1078 if ((dom_tag[0] != '\0') && (strcmp(dom_tag, inst->tag) == 0))
1079 return 1;
1081 /* instance#0 will always be there, so it is in charge of extra duties */
1082 if (inst->id == 0) {
1083 if (dom_tag[0] == '\0' || !is_known_tag(dom_tag)) {
1084 DEBUG(PLUGIN_NAME " plugin#%s: refreshing domain %s "
1085 "with unknown tag '%s'",
1086 inst->tag, dom_name, dom_tag);
1087 return 1;
1088 }
1089 }
1091 return 0;
1092 }
1094 static int refresh_lists(struct lv_read_instance *inst) {
1095 struct lv_read_state *state = &inst->read_state;
1096 int n;
1098 n = virConnectNumOfDomains(conn);
1099 if (n < 0) {
1100 VIRT_ERROR(conn, "reading number of domains");
1101 return -1;
1102 }
1104 lv_clean_read_state(state);
1106 if (n > 0) {
1107 int *domids;
1109 /* Get list of domains. */
1110 domids = malloc(sizeof(*domids) * n);
1111 if (domids == NULL) {
1112 ERROR(PLUGIN_NAME " plugin: malloc failed.");
1113 return -1;
1114 }
1116 n = virConnectListDomains(conn, domids, n);
1117 if (n < 0) {
1118 VIRT_ERROR(conn, "reading list of domains");
1119 sfree(domids);
1120 return -1;
1121 }
1123 /* Fetch each domain and add it to the list, unless ignore. */
1124 for (int i = 0; i < n; ++i) {
1125 virDomainPtr dom = NULL;
1126 const char *name;
1127 char *xml = NULL;
1128 xmlDocPtr xml_doc = NULL;
1129 xmlXPathContextPtr xpath_ctx = NULL;
1130 xmlXPathObjectPtr xpath_obj = NULL;
1131 char tag[PARTITION_TAG_MAX_LEN] = {'\0'};
1133 dom = virDomainLookupByID(conn, domids[i]);
1134 if (dom == NULL) {
1135 VIRT_ERROR(conn, "virDomainLookupByID");
1136 /* Could be that the domain went away -- ignore it anyway. */
1137 continue;
1138 }
1140 name = virDomainGetName(dom);
1141 if (name == NULL) {
1142 VIRT_ERROR(conn, "virDomainGetName");
1143 goto cont;
1144 }
1146 if (il_domains && ignorelist_match(il_domains, name) != 0)
1147 goto cont;
1149 /* Get a list of devices for this domain. */
1150 xml = virDomainGetXMLDesc(dom, 0);
1151 if (!xml) {
1152 VIRT_ERROR(conn, "virDomainGetXMLDesc");
1153 goto cont;
1154 }
1156 /* Yuck, XML. Parse out the devices. */
1157 xml_doc = xmlReadDoc((xmlChar *)xml, NULL, NULL, XML_PARSE_NONET);
1158 if (xml_doc == NULL) {
1159 VIRT_ERROR(conn, "xmlReadDoc");
1160 goto cont;
1161 }
1163 xpath_ctx = xmlXPathNewContext(xml_doc);
1165 if (lv_domain_get_tag(xpath_ctx, name, tag) < 0) {
1166 ERROR(PLUGIN_NAME " plugin: lv_domain_get_tag failed.");
1167 goto cont;
1168 }
1170 if (!lv_instance_include_domain(inst, name, tag))
1171 goto cont;
1173 if (add_domain(state, dom) < 0) {
1174 ERROR(PLUGIN_NAME " plugin: malloc failed.");
1175 goto cont;
1176 }
1178 /* Block devices. */
1179 const char *bd_xmlpath = "/domain/devices/disk/target[@dev]";
1180 if (blockdevice_format == source)
1181 bd_xmlpath = "/domain/devices/disk/source[@dev]";
1182 xpath_obj = xmlXPathEval((const xmlChar *)bd_xmlpath, xpath_ctx);
1184 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
1185 xpath_obj->nodesetval == NULL)
1186 goto cont;
1188 for (int j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
1189 xmlNodePtr node;
1190 char *path = NULL;
1192 node = xpath_obj->nodesetval->nodeTab[j];
1193 if (!node)
1194 continue;
1195 path = (char *)xmlGetProp(node, (xmlChar *)"dev");
1196 if (!path)
1197 continue;
1199 if (il_block_devices &&
1200 ignore_device_match(il_block_devices, name, path) != 0)
1201 goto cont2;
1203 add_block_device(state, dom, path);
1204 cont2:
1205 if (path)
1206 xmlFree(path);
1207 }
1208 xmlXPathFreeObject(xpath_obj);
1210 /* Network interfaces. */
1211 xpath_obj = xmlXPathEval(
1212 (xmlChar *)"/domain/devices/interface[target[@dev]]", xpath_ctx);
1213 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
1214 xpath_obj->nodesetval == NULL)
1215 goto cont;
1217 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
1219 for (int j = 0; j < xml_interfaces->nodeNr; ++j) {
1220 char *path = NULL;
1221 char *address = NULL;
1222 xmlNodePtr xml_interface;
1224 xml_interface = xml_interfaces->nodeTab[j];
1225 if (!xml_interface)
1226 continue;
1228 for (xmlNodePtr child = xml_interface->children; child;
1229 child = child->next) {
1230 if (child->type != XML_ELEMENT_NODE)
1231 continue;
1233 if (xmlStrEqual(child->name, (const xmlChar *)"target")) {
1234 path = (char *)xmlGetProp(child, (const xmlChar *)"dev");
1235 if (!path)
1236 continue;
1237 } else if (xmlStrEqual(child->name, (const xmlChar *)"mac")) {
1238 address = (char *)xmlGetProp(child, (const xmlChar *)"address");
1239 if (!address)
1240 continue;
1241 }
1242 }
1244 if (il_interface_devices &&
1245 (ignore_device_match(il_interface_devices, name, path) != 0 ||
1246 ignore_device_match(il_interface_devices, name, address) != 0))
1247 goto cont3;
1249 add_interface_device(state, dom, path, address, j + 1);
1250 cont3:
1251 if (path)
1252 xmlFree(path);
1253 if (address)
1254 xmlFree(address);
1255 }
1257 cont:
1258 if (xpath_obj)
1259 xmlXPathFreeObject(xpath_obj);
1260 if (xpath_ctx)
1261 xmlXPathFreeContext(xpath_ctx);
1262 if (xml_doc)
1263 xmlFreeDoc(xml_doc);
1264 sfree(xml);
1265 }
1267 sfree(domids);
1268 }
1270 DEBUG(PLUGIN_NAME " plugin#%s: refreshing"
1271 " domains=%i block_devices=%i iface_devices=%i",
1272 inst->tag, state->nr_domains, state->nr_block_devices,
1273 state->nr_interface_devices);
1275 return 0;
1276 }
1278 static void free_domains(struct lv_read_state *state) {
1279 if (state->domains) {
1280 for (int i = 0; i < state->nr_domains; ++i)
1281 virDomainFree(state->domains[i]);
1282 sfree(state->domains);
1283 }
1284 state->domains = NULL;
1285 state->nr_domains = 0;
1286 }
1288 static int add_domain(struct lv_read_state *state, virDomainPtr dom) {
1289 virDomainPtr *new_ptr;
1290 int new_size = sizeof(state->domains[0]) * (state->nr_domains + 1);
1292 if (state->domains)
1293 new_ptr = realloc(state->domains, new_size);
1294 else
1295 new_ptr = malloc(new_size);
1297 if (new_ptr == NULL)
1298 return -1;
1300 state->domains = new_ptr;
1301 state->domains[state->nr_domains] = dom;
1302 return state->nr_domains++;
1303 }
1305 static void free_block_devices(struct lv_read_state *state) {
1306 if (state->block_devices) {
1307 for (int i = 0; i < state->nr_block_devices; ++i)
1308 sfree(state->block_devices[i].path);
1309 sfree(state->block_devices);
1310 }
1311 state->block_devices = NULL;
1312 state->nr_block_devices = 0;
1313 }
1315 static int add_block_device(struct lv_read_state *state, virDomainPtr dom,
1316 const char *path) {
1317 struct block_device *new_ptr;
1318 int new_size =
1319 sizeof(state->block_devices[0]) * (state->nr_block_devices + 1);
1320 char *path_copy;
1322 path_copy = strdup(path);
1323 if (!path_copy)
1324 return -1;
1326 if (state->block_devices)
1327 new_ptr = realloc(state->block_devices, new_size);
1328 else
1329 new_ptr = malloc(new_size);
1331 if (new_ptr == NULL) {
1332 sfree(path_copy);
1333 return -1;
1334 }
1335 state->block_devices = new_ptr;
1336 state->block_devices[state->nr_block_devices].dom = dom;
1337 state->block_devices[state->nr_block_devices].path = path_copy;
1338 return state->nr_block_devices++;
1339 }
1341 static void free_interface_devices(struct lv_read_state *state) {
1342 if (state->interface_devices) {
1343 for (int i = 0; i < state->nr_interface_devices; ++i) {
1344 sfree(state->interface_devices[i].path);
1345 sfree(state->interface_devices[i].address);
1346 sfree(state->interface_devices[i].number);
1347 }
1348 sfree(state->interface_devices);
1349 }
1350 state->interface_devices = NULL;
1351 state->nr_interface_devices = 0;
1352 }
1354 static int add_interface_device(struct lv_read_state *state, virDomainPtr dom,
1355 const char *path, const char *address,
1356 unsigned int number) {
1357 struct interface_device *new_ptr;
1358 int new_size =
1359 sizeof(state->interface_devices[0]) * (state->nr_interface_devices + 1);
1360 char *path_copy, *address_copy, number_string[15];
1362 if ((path == NULL) || (address == NULL))
1363 return EINVAL;
1365 path_copy = strdup(path);
1366 if (!path_copy)
1367 return -1;
1369 address_copy = strdup(address);
1370 if (!address_copy) {
1371 sfree(path_copy);
1372 return -1;
1373 }
1375 snprintf(number_string, sizeof(number_string), "interface-%u", number);
1377 if (state->interface_devices)
1378 new_ptr = realloc(state->interface_devices, new_size);
1379 else
1380 new_ptr = malloc(new_size);
1382 if (new_ptr == NULL) {
1383 sfree(path_copy);
1384 sfree(address_copy);
1385 return -1;
1386 }
1387 state->interface_devices = new_ptr;
1388 state->interface_devices[state->nr_interface_devices].dom = dom;
1389 state->interface_devices[state->nr_interface_devices].path = path_copy;
1390 state->interface_devices[state->nr_interface_devices].address = address_copy;
1391 state->interface_devices[state->nr_interface_devices].number =
1392 strdup(number_string);
1393 return state->nr_interface_devices++;
1394 }
1396 static int ignore_device_match(ignorelist_t *il, const char *domname,
1397 const char *devpath) {
1398 char *name;
1399 int n, r;
1401 if ((domname == NULL) || (devpath == NULL))
1402 return 0;
1404 n = sizeof(char) * (strlen(domname) + strlen(devpath) + 2);
1405 name = malloc(n);
1406 if (name == NULL) {
1407 ERROR(PLUGIN_NAME " plugin: malloc failed.");
1408 return 0;
1409 }
1410 ssnprintf(name, n, "%s:%s", domname, devpath);
1411 r = ignorelist_match(il, name);
1412 sfree(name);
1413 return r;
1414 }
1416 static int lv_shutdown(void) {
1417 for (int i = 0; i < nr_instances; ++i) {
1418 lv_fini_instance(i);
1419 }
1421 lv_disconnect();
1423 ignorelist_free(il_domains);
1424 il_domains = NULL;
1425 ignorelist_free(il_block_devices);
1426 il_block_devices = NULL;
1427 ignorelist_free(il_interface_devices);
1428 il_interface_devices = NULL;
1430 return 0;
1431 }
1433 void module_register(void) {
1434 plugin_register_config(PLUGIN_NAME, lv_config, config_keys, NR_CONFIG_KEYS);
1435 plugin_register_init(PLUGIN_NAME, lv_init);
1436 plugin_register_shutdown(PLUGIN_NAME, lv_shutdown);
1437 }