2 * collectd - src/virt.c
3 * Copyright (C) 2006-2008 Red Hat Inc.
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.
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.
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
19 * Richard W.M. Jones <rjones@redhat.com>
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>
38 #define PLUGIN_NAME "virt"
40 static const char *config_keys[] = {"Connection",
47 "BlockDeviceFormatBasename",
54 "PluginInstanceFormat",
59 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
62 static virConnectPtr conn = 0;
63 static char *conn_string = NULL;
64 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
66 /* Seconds between list refreshes, 0 disables completely. */
67 static int interval = 60;
69 /* List of domains, if specified. */
70 static ignorelist_t *il_domains = NULL;
71 /* List of block devices, if specified. */
72 static ignorelist_t *il_block_devices = NULL;
73 /* List of network interface devices, if specified. */
74 static ignorelist_t *il_interface_devices = NULL;
76 static int ignore_device_match(ignorelist_t *, const char *domname,
79 /* Actual list of block devices found on last refresh. */
81 virDomainPtr dom; /* domain */
82 char *path; /* name of block device */
85 /* Actual list of network interfaces found on last refresh. */
86 struct interface_device {
87 virDomainPtr dom; /* domain */
88 char *path; /* name of interface device */
89 char *address; /* mac address of interface device */
90 char *number; /* interface device number */
93 struct lv_read_state {
94 /* Actual list of domains found on last refresh. */
95 virDomainPtr *domains;
98 struct block_device *block_devices;
101 struct interface_device *interface_devices;
102 int nr_interface_devices;
105 static void free_domains(struct lv_read_state *state);
106 static int add_domain(struct lv_read_state *state, virDomainPtr dom);
108 static void free_block_devices(struct lv_read_state *state);
109 static int add_block_device(struct lv_read_state *state, virDomainPtr dom,
112 static void free_interface_devices(struct lv_read_state *state);
113 static int add_interface_device(struct lv_read_state *state, virDomainPtr dom,
114 const char *path, const char *address,
115 unsigned int number);
117 #define METADATA_VM_PARTITION_URI "http://ovirt.org/ovirtmap/tag/1.0"
118 #define METADATA_VM_PARTITION_ELEMENT "tag"
119 #define METADATA_VM_PARTITION_PREFIX "ovirtmap"
121 #define BUFFER_MAX_LEN 256
122 #define PARTITION_TAG_MAX_LEN 32
124 struct lv_read_instance {
125 struct lv_read_state read_state;
126 char tag[PARTITION_TAG_MAX_LEN];
130 struct lv_user_data {
131 struct lv_read_instance inst;
135 #define NR_INSTANCES_DEFAULT 1
136 #define NR_INSTANCES_MAX 128
137 static int nr_instances = NR_INSTANCES_DEFAULT;
138 static struct lv_user_data lv_read_user_data[NR_INSTANCES_MAX];
140 /* HostnameFormat. */
141 #define HF_MAX_FIELDS 3
143 enum hf_field { hf_none = 0, hf_hostname, hf_name, hf_uuid };
145 static enum hf_field hostname_format[HF_MAX_FIELDS] = {hf_name};
147 /* PluginInstanceFormat */
148 #define PLGINST_MAX_FIELDS 2
150 enum plginst_field { plginst_none = 0, plginst_name, plginst_uuid };
152 static enum plginst_field plugin_instance_format[PLGINST_MAX_FIELDS] = {
155 /* BlockDeviceFormat */
156 enum bd_field { target, source };
158 /* InterfaceFormat. */
159 enum if_field { if_address, if_name, if_number };
161 /* BlockDeviceFormatBasename */
162 _Bool blockdevice_format_basename = 0;
163 static enum bd_field blockdevice_format = target;
164 static enum if_field interface_format = if_name;
166 /* Time that we last refreshed. */
167 static time_t last_refresh = (time_t)0;
169 static int refresh_lists(struct lv_read_instance *inst);
171 /* ERROR(...) macro for virterrors. */
172 #define VIRT_ERROR(conn, s) \
175 err = (conn) ? virConnGetLastError((conn)) : virGetLastError(); \
177 ERROR("%s: %s", (s), err->message); \
180 static void init_value_list(value_list_t *vl, virDomainPtr dom) {
183 char uuid[VIR_UUID_STRING_BUFLEN];
185 sstrncpy(vl->plugin, PLUGIN_NAME, sizeof(vl->plugin));
189 /* Construct the hostname field according to HostnameFormat. */
190 for (int i = 0; i < HF_MAX_FIELDS; ++i) {
191 if (hostname_format[i] == hf_none)
194 n = DATA_MAX_NAME_LEN - strlen(vl->host) - 2;
196 if (i > 0 && n >= 1) {
197 strncat(vl->host, ":", 1);
201 switch (hostname_format[i]) {
205 strncat(vl->host, hostname_g, n);
208 name = virDomainGetName(dom);
210 strncat(vl->host, name, n);
213 if (virDomainGetUUIDString(dom, uuid) == 0)
214 strncat(vl->host, uuid, n);
219 vl->host[sizeof(vl->host) - 1] = '\0';
221 /* Construct the plugin instance field according to PluginInstanceFormat. */
222 for (int i = 0; i < PLGINST_MAX_FIELDS; ++i) {
223 if (plugin_instance_format[i] == plginst_none)
226 n = sizeof(vl->plugin_instance) - strlen(vl->plugin_instance) - 2;
228 if (i > 0 && n >= 1) {
229 strncat(vl->plugin_instance, ":", 1);
233 switch (plugin_instance_format[i]) {
237 name = virDomainGetName(dom);
239 strncat(vl->plugin_instance, name, n);
242 if (virDomainGetUUIDString(dom, uuid) == 0)
243 strncat(vl->plugin_instance, uuid, n);
248 vl->plugin_instance[sizeof(vl->plugin_instance) - 1] = '\0';
250 } /* void init_value_list */
252 static void submit(virDomainPtr dom, char const *type,
253 char const *type_instance, value_t *values,
255 value_list_t vl = VALUE_LIST_INIT;
256 init_value_list(&vl, dom);
259 vl.values_len = values_len;
261 sstrncpy(vl.type, type, sizeof(vl.type));
262 if (type_instance != NULL)
263 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
265 plugin_dispatch_values(&vl);
268 static void memory_submit(gauge_t value, virDomainPtr dom) {
269 submit(dom, "memory", "total", &(value_t){.gauge = value}, 1);
272 static void memory_stats_submit(gauge_t value, virDomainPtr dom,
274 static const char *tags[] = {"swap_in", "swap_out", "major_fault",
275 "minor_fault", "unused", "available",
276 "actual_balloon", "rss"};
278 if ((tag_index < 0) || (tag_index >= STATIC_ARRAY_SIZE(tags))) {
279 ERROR("virt plugin: Array index out of bounds: tag_index = %d", tag_index);
283 submit(dom, "memory", tags[tag_index], &(value_t){.gauge = value}, 1);
286 static void cpu_submit(unsigned long long value, virDomainPtr dom,
288 submit(dom, type, NULL, &(value_t){.derive = (derive_t)value}, 1);
291 static void vcpu_submit(derive_t value, virDomainPtr dom, int vcpu_nr,
293 char type_instance[DATA_MAX_NAME_LEN];
295 ssnprintf(type_instance, sizeof(type_instance), "%d", vcpu_nr);
297 submit(dom, type, type_instance, &(value_t){.derive = value}, 1);
300 static void submit_derive2(const char *type, derive_t v0, derive_t v1,
301 virDomainPtr dom, const char *devname) {
303 {.derive = v0}, {.derive = v1},
306 submit(dom, type, devname, values, STATIC_ARRAY_SIZE(values));
307 } /* void submit_derive2 */
309 static int lv_config(const char *key, const char *value) {
310 if (virInitialize() != 0)
313 if (il_domains == NULL)
314 il_domains = ignorelist_create(1);
315 if (il_block_devices == NULL)
316 il_block_devices = ignorelist_create(1);
317 if (il_interface_devices == NULL)
318 il_interface_devices = ignorelist_create(1);
320 if (strcasecmp(key, "Connection") == 0) {
321 char *tmp = strdup(value);
323 ERROR(PLUGIN_NAME " plugin: Connection strdup failed.");
331 if (strcasecmp(key, "RefreshInterval") == 0) {
333 interval = strtol(value, &eptr, 10);
334 if (eptr == NULL || *eptr != '\0')
339 if (strcasecmp(key, "Domain") == 0) {
340 if (ignorelist_add(il_domains, value))
344 if (strcasecmp(key, "BlockDevice") == 0) {
345 if (ignorelist_add(il_block_devices, value))
350 if (strcasecmp(key, "BlockDeviceFormat") == 0) {
351 if (strcasecmp(value, "target") == 0)
352 blockdevice_format = target;
353 else if (strcasecmp(value, "source") == 0)
354 blockdevice_format = source;
356 ERROR(PLUGIN_NAME " plugin: unknown BlockDeviceFormat: %s", value);
361 if (strcasecmp(key, "BlockDeviceFormatBasename") == 0) {
362 blockdevice_format_basename = IS_TRUE(value);
365 if (strcasecmp(key, "InterfaceDevice") == 0) {
366 if (ignorelist_add(il_interface_devices, value))
371 if (strcasecmp(key, "IgnoreSelected") == 0) {
372 if (IS_TRUE(value)) {
373 ignorelist_set_invert(il_domains, 0);
374 ignorelist_set_invert(il_block_devices, 0);
375 ignorelist_set_invert(il_interface_devices, 0);
377 ignorelist_set_invert(il_domains, 1);
378 ignorelist_set_invert(il_block_devices, 1);
379 ignorelist_set_invert(il_interface_devices, 1);
384 if (strcasecmp(key, "HostnameFormat") == 0) {
386 char *fields[HF_MAX_FIELDS];
389 value_copy = strdup(value);
390 if (value_copy == NULL) {
391 ERROR(PLUGIN_NAME " plugin: strdup failed.");
395 n = strsplit(value_copy, fields, HF_MAX_FIELDS);
398 ERROR(PLUGIN_NAME " plugin: HostnameFormat: no fields");
402 for (int i = 0; i < n; ++i) {
403 if (strcasecmp(fields[i], "hostname") == 0)
404 hostname_format[i] = hf_hostname;
405 else if (strcasecmp(fields[i], "name") == 0)
406 hostname_format[i] = hf_name;
407 else if (strcasecmp(fields[i], "uuid") == 0)
408 hostname_format[i] = hf_uuid;
410 ERROR(PLUGIN_NAME " plugin: unknown HostnameFormat field: %s",
418 for (int i = n; i < HF_MAX_FIELDS; ++i)
419 hostname_format[i] = hf_none;
424 if (strcasecmp(key, "PluginInstanceFormat") == 0) {
426 char *fields[PLGINST_MAX_FIELDS];
429 value_copy = strdup(value);
430 if (value_copy == NULL) {
431 ERROR(PLUGIN_NAME " plugin: strdup failed.");
435 n = strsplit(value_copy, fields, PLGINST_MAX_FIELDS);
438 ERROR(PLUGIN_NAME " plugin: PluginInstanceFormat: no fields");
442 for (int i = 0; i < n; ++i) {
443 if (strcasecmp(fields[i], "none") == 0) {
444 plugin_instance_format[i] = plginst_none;
446 } else if (strcasecmp(fields[i], "name") == 0)
447 plugin_instance_format[i] = plginst_name;
448 else if (strcasecmp(fields[i], "uuid") == 0)
449 plugin_instance_format[i] = plginst_uuid;
451 ERROR(PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s",
459 for (int i = n; i < PLGINST_MAX_FIELDS; ++i)
460 plugin_instance_format[i] = plginst_none;
465 if (strcasecmp(key, "InterfaceFormat") == 0) {
466 if (strcasecmp(value, "name") == 0)
467 interface_format = if_name;
468 else if (strcasecmp(value, "address") == 0)
469 interface_format = if_address;
470 else if (strcasecmp(value, "number") == 0)
471 interface_format = if_number;
473 ERROR(PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", value);
479 if (strcasecmp(key, "Instances") == 0) {
481 double val = strtod(value, &eptr);
484 ERROR(PLUGIN_NAME " plugin: Invalid value for Instances = '%s'", value);
488 ERROR(PLUGIN_NAME " plugin: Instances <= 0 makes no sense.");
491 if (val > NR_INSTANCES_MAX) {
492 ERROR(PLUGIN_NAME " plugin: Instances=%f > NR_INSTANCES_MAX=%i"
493 " use a lower setting or recompile the plugin.",
494 val, NR_INSTANCES_MAX);
498 nr_instances = (int)val;
499 DEBUG(PLUGIN_NAME " plugin: configured %i instances", nr_instances);
503 /* Unrecognised option. */
507 static int lv_connect(void) {
509 /* `conn_string == NULL' is acceptable. */
510 conn = virConnectOpenReadOnly(conn_string);
512 c_complain(LOG_ERR, &conn_complain,
513 PLUGIN_NAME " plugin: Unable to connect: "
514 "virConnectOpenReadOnly failed.");
518 c_release(LOG_NOTICE, &conn_complain,
519 PLUGIN_NAME " plugin: Connection established.");
523 static int lv_read(user_data_t *ud) {
525 struct lv_read_instance *inst = NULL;
526 struct lv_read_state *state = NULL;
528 if (ud->data == NULL) {
529 ERROR(PLUGIN_NAME " plugin: NULL userdata");
534 state = &inst->read_state;
537 if (lv_connect() < 0)
543 /* Need to refresh domain or device lists? */
544 if ((last_refresh == (time_t)0) ||
545 ((interval > 0) && ((last_refresh + interval) <= t))) {
546 if (refresh_lists(inst) != 0) {
548 virConnectClose(conn);
556 for (int i = 0; i < nr_domains; ++i)
557 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
558 for (int i = 0; i < nr_block_devices; ++i)
559 fprintf (stderr, "block device %d %s:%s\n",
560 i, virDomainGetName (block_devices[i].dom),
561 block_devices[i].path);
562 for (int i = 0; i < nr_interface_devices; ++i)
563 fprintf (stderr, "interface device %d %s:%s\n",
564 i, virDomainGetName (interface_devices[i].dom),
565 interface_devices[i].path);
568 /* Get CPU usage, memory, VCPU usage for each domain. */
569 for (int i = 0; i < state->nr_domains; ++i) {
571 virVcpuInfoPtr vinfo = NULL;
572 virDomainMemoryStatPtr minfo = NULL;
575 status = virDomainGetInfo(state->domains[i], &info);
577 ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
582 if (info.state != VIR_DOMAIN_RUNNING) {
583 /* only gather stats for running domains */
587 cpu_submit(info.cpuTime, state->domains[i], "virt_cpu_total");
588 memory_submit((gauge_t)info.memory * 1024, state->domains[i]);
590 vinfo = malloc(info.nrVirtCpu * sizeof(vinfo[0]));
592 ERROR(PLUGIN_NAME " plugin: malloc failed.");
596 status = virDomainGetVcpus(state->domains[i], vinfo, info.nrVirtCpu,
597 /* cpu map = */ NULL, /* cpu map length = */ 0);
599 ERROR(PLUGIN_NAME " plugin: virDomainGetVcpus failed with status %i.",
605 for (int j = 0; j < info.nrVirtCpu; ++j)
606 vcpu_submit(vinfo[j].cpuTime, state->domains[i], vinfo[j].number,
612 malloc(VIR_DOMAIN_MEMORY_STAT_NR * sizeof(virDomainMemoryStatStruct));
614 ERROR("virt plugin: malloc failed.");
618 status = virDomainMemoryStats(state->domains[i], minfo,
619 VIR_DOMAIN_MEMORY_STAT_NR, 0);
622 ERROR("virt plugin: virDomainMemoryStats failed with status %i.", status);
627 for (int j = 0; j < status; j++) {
628 memory_stats_submit((gauge_t)minfo[j].val * 1024, state->domains[i],
635 /* Get block device stats for each domain. */
636 for (int i = 0; i < state->nr_block_devices; ++i) {
637 struct _virDomainBlockStats stats;
639 if (virDomainBlockStats(state->block_devices[i].dom,
640 state->block_devices[i].path, &stats,
644 char *type_instance = NULL;
645 if (blockdevice_format_basename && blockdevice_format == source)
646 type_instance = strdup(basename(state->block_devices[i].path));
648 type_instance = strdup(state->block_devices[i].path);
650 if ((stats.rd_req != -1) && (stats.wr_req != -1))
651 submit_derive2("disk_ops", (derive_t)stats.rd_req, (derive_t)stats.wr_req,
652 state->block_devices[i].dom, type_instance);
654 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
655 submit_derive2("disk_octets", (derive_t)stats.rd_bytes,
656 (derive_t)stats.wr_bytes, state->block_devices[i].dom,
659 sfree(type_instance);
660 } /* for (nr_block_devices) */
662 /* Get interface stats for each domain. */
663 for (int i = 0; i < state->nr_interface_devices; ++i) {
664 struct _virDomainInterfaceStats stats;
665 char *display_name = NULL;
667 switch (interface_format) {
669 display_name = state->interface_devices[i].address;
672 display_name = state->interface_devices[i].number;
676 display_name = state->interface_devices[i].path;
679 if (virDomainInterfaceStats(state->interface_devices[i].dom,
680 state->interface_devices[i].path, &stats,
684 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
685 submit_derive2("if_octets", (derive_t)stats.rx_bytes,
686 (derive_t)stats.tx_bytes, state->interface_devices[i].dom,
689 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
690 submit_derive2("if_packets", (derive_t)stats.rx_packets,
691 (derive_t)stats.tx_packets,
692 state->interface_devices[i].dom, display_name);
694 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
695 submit_derive2("if_errors", (derive_t)stats.rx_errs,
696 (derive_t)stats.tx_errs, state->interface_devices[i].dom,
699 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
700 submit_derive2("if_dropped", (derive_t)stats.rx_drop,
701 (derive_t)stats.tx_drop, state->interface_devices[i].dom,
703 } /* for (nr_interface_devices) */
708 static int lv_init_instance(size_t i, plugin_read_cb callback) {
709 struct lv_user_data *lv_ud = &(lv_read_user_data[i]);
710 struct lv_read_instance *inst = &(lv_ud->inst);
712 memset(lv_ud, 0, sizeof(*lv_ud));
714 ssnprintf(inst->tag, sizeof(inst->tag), "%s-%zu", PLUGIN_NAME, i);
717 user_data_t *ud = &(lv_ud->ud);
719 ud->free_func = NULL;
721 INFO(PLUGIN_NAME " plugin: reader %s initialized", inst->tag);
722 return plugin_register_complex_read(NULL, inst->tag, callback, 0, ud);
725 static void lv_clean_read_state(struct lv_read_state *state) {
726 free_block_devices(state);
727 free_interface_devices(state);
731 static void lv_fini_instance(size_t i) {
732 struct lv_read_instance *inst = &(lv_read_user_data[i].inst);
733 struct lv_read_state *state = &(inst->read_state);
735 lv_clean_read_state(state);
736 INFO(PLUGIN_NAME " plugin: reader %s finalized", inst->tag);
739 static int lv_init(void) {
740 if (virInitialize() != 0)
743 if (lv_connect() != 0)
746 DEBUG(PLUGIN_NAME " plugin: starting %i instances", nr_instances);
748 for (int i = 0; i < nr_instances; ++i)
749 lv_init_instance(i, lv_read);
755 * returns 0 on success and <0 on error
757 static int lv_domain_get_tag(xmlXPathContextPtr xpath_ctx, const char *dom_name,
759 char xpath_str[BUFFER_MAX_LEN] = {'\0'};
760 xmlXPathObjectPtr xpath_obj = NULL;
761 xmlNodePtr xml_node = NULL;
765 err = xmlXPathRegisterNs(xpath_ctx,
766 (const xmlChar *)METADATA_VM_PARTITION_PREFIX,
767 (const xmlChar *)METADATA_VM_PARTITION_URI);
769 ERROR(PLUGIN_NAME " plugin: xmlXpathRegisterNs(%s, %s) failed on domain %s",
770 METADATA_VM_PARTITION_PREFIX, METADATA_VM_PARTITION_URI, dom_name);
774 ssnprintf(xpath_str, sizeof(xpath_str), "/domain/metadata/%s:%s/text()",
775 METADATA_VM_PARTITION_PREFIX, METADATA_VM_PARTITION_ELEMENT);
776 xpath_obj = xmlXPathEvalExpression((xmlChar *)xpath_str, xpath_ctx);
777 if (xpath_obj == NULL) {
778 ERROR(PLUGIN_NAME " plugin: xmlXPathEval(%s) failed on domain %s",
779 xpath_str, dom_name);
783 if (xpath_obj->type != XPATH_NODESET) {
784 ERROR(PLUGIN_NAME " plugin: xmlXPathEval(%s) unexpected return type %d "
785 "(wanted %d) on domain %s",
786 xpath_str, xpath_obj->type, XPATH_NODESET, dom_name);
791 * from now on there is no real error, it's ok if a domain
792 * doesn't have the metadata partition tag.
795 if (xpath_obj->nodesetval == NULL || xpath_obj->nodesetval->nodeNr != 1) {
796 DEBUG(PLUGIN_NAME " plugin: xmlXPathEval(%s) return nodeset size=%i "
797 "expected=1 on domain %s",
799 (xpath_obj->nodesetval == NULL) ? 0 : xpath_obj->nodesetval->nodeNr,
802 xml_node = xpath_obj->nodesetval->nodeTab[0];
803 sstrncpy(dom_tag, (const char *)xml_node->content, PARTITION_TAG_MAX_LEN);
807 /* deregister to clean up */
808 err = xmlXPathRegisterNs(xpath_ctx,
809 (const xmlChar *)METADATA_VM_PARTITION_PREFIX, NULL);
811 /* we can't really recover here */
813 " plugin: deregistration of namespace %s failed for domain %s",
814 METADATA_VM_PARTITION_PREFIX, dom_name);
817 xmlXPathFreeObject(xpath_obj);
822 static int is_known_tag(const char *dom_tag) {
823 for (int i = 0; i < nr_instances; ++i)
824 if (!strcmp(dom_tag, lv_read_user_data[i].inst.tag))
829 static int lv_instance_include_domain(struct lv_read_instance *inst,
830 const char *dom_name,
831 const char *dom_tag) {
832 if ((dom_tag[0] != '\0') && (strcmp(dom_tag, inst->tag) == 0))
835 /* instance#0 will always be there, so it is in charge of extra duties */
837 if (dom_tag[0] == '\0' || !is_known_tag(dom_tag)) {
838 DEBUG(PLUGIN_NAME " plugin#%s: refreshing domain %s "
839 "with unknown tag '%s'",
840 inst->tag, dom_name, dom_tag);
848 static int refresh_lists(struct lv_read_instance *inst) {
849 struct lv_read_state *state = &inst->read_state;
852 n = virConnectNumOfDomains(conn);
854 VIRT_ERROR(conn, "reading number of domains");
858 lv_clean_read_state(state);
863 /* Get list of domains. */
864 domids = malloc(sizeof(*domids) * n);
865 if (domids == NULL) {
866 ERROR(PLUGIN_NAME " plugin: malloc failed.");
870 n = virConnectListDomains(conn, domids, n);
872 VIRT_ERROR(conn, "reading list of domains");
877 /* Fetch each domain and add it to the list, unless ignore. */
878 for (int i = 0; i < n; ++i) {
879 virDomainPtr dom = NULL;
882 xmlDocPtr xml_doc = NULL;
883 xmlXPathContextPtr xpath_ctx = NULL;
884 xmlXPathObjectPtr xpath_obj = NULL;
885 char tag[PARTITION_TAG_MAX_LEN] = {'\0'};
887 dom = virDomainLookupByID(conn, domids[i]);
889 VIRT_ERROR(conn, "virDomainLookupByID");
890 /* Could be that the domain went away -- ignore it anyway. */
894 name = virDomainGetName(dom);
896 VIRT_ERROR(conn, "virDomainGetName");
900 if (il_domains && ignorelist_match(il_domains, name) != 0)
903 /* Get a list of devices for this domain. */
904 xml = virDomainGetXMLDesc(dom, 0);
906 VIRT_ERROR(conn, "virDomainGetXMLDesc");
910 /* Yuck, XML. Parse out the devices. */
911 xml_doc = xmlReadDoc((xmlChar *)xml, NULL, NULL, XML_PARSE_NONET);
912 if (xml_doc == NULL) {
913 VIRT_ERROR(conn, "xmlReadDoc");
917 xpath_ctx = xmlXPathNewContext(xml_doc);
919 if (lv_domain_get_tag(xpath_ctx, name, tag) < 0) {
920 ERROR(PLUGIN_NAME " plugin: lv_domain_get_tag failed.");
924 if (!lv_instance_include_domain(inst, name, tag))
927 if (add_domain(state, dom) < 0) {
928 ERROR(PLUGIN_NAME " plugin: malloc failed.");
933 char *bd_xmlpath = "/domain/devices/disk/target[@dev]";
934 if (blockdevice_format == source)
935 bd_xmlpath = "/domain/devices/disk/source[@dev]";
936 xpath_obj = xmlXPathEval((xmlChar *)bd_xmlpath, xpath_ctx);
938 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
939 xpath_obj->nodesetval == NULL)
942 for (int j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
946 node = xpath_obj->nodesetval->nodeTab[j];
949 path = (char *)xmlGetProp(node, (xmlChar *)"dev");
953 if (il_block_devices &&
954 ignore_device_match(il_block_devices, name, path) != 0)
957 add_block_device(state, dom, path);
962 xmlXPathFreeObject(xpath_obj);
964 /* Network interfaces. */
965 xpath_obj = xmlXPathEval(
966 (xmlChar *)"/domain/devices/interface[target[@dev]]", xpath_ctx);
967 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
968 xpath_obj->nodesetval == NULL)
971 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
973 for (int j = 0; j < xml_interfaces->nodeNr; ++j) {
975 char *address = NULL;
976 xmlNodePtr xml_interface;
978 xml_interface = xml_interfaces->nodeTab[j];
982 for (xmlNodePtr child = xml_interface->children; child;
983 child = child->next) {
984 if (child->type != XML_ELEMENT_NODE)
987 if (xmlStrEqual(child->name, (const xmlChar *)"target")) {
988 path = (char *)xmlGetProp(child, (const xmlChar *)"dev");
991 } else if (xmlStrEqual(child->name, (const xmlChar *)"mac")) {
992 address = (char *)xmlGetProp(child, (const xmlChar *)"address");
998 if (il_interface_devices &&
999 (ignore_device_match(il_interface_devices, name, path) != 0 ||
1000 ignore_device_match(il_interface_devices, name, address) != 0))
1003 add_interface_device(state, dom, path, address, j + 1);
1013 xmlXPathFreeObject(xpath_obj);
1015 xmlXPathFreeContext(xpath_ctx);
1017 xmlFreeDoc(xml_doc);
1024 DEBUG(PLUGIN_NAME " plugin#%s: refreshing"
1025 " domains=%i block_devices=%i iface_devices=%i",
1026 inst->tag, state->nr_domains, state->nr_block_devices,
1027 state->nr_interface_devices);
1032 static void free_domains(struct lv_read_state *state) {
1033 if (state->domains) {
1034 for (int i = 0; i < state->nr_domains; ++i)
1035 virDomainFree(state->domains[i]);
1036 sfree(state->domains);
1038 state->domains = NULL;
1039 state->nr_domains = 0;
1042 static int add_domain(struct lv_read_state *state, virDomainPtr dom) {
1043 virDomainPtr *new_ptr;
1044 int new_size = sizeof(state->domains[0]) * (state->nr_domains + 1);
1047 new_ptr = realloc(state->domains, new_size);
1049 new_ptr = malloc(new_size);
1051 if (new_ptr == NULL)
1054 state->domains = new_ptr;
1055 state->domains[state->nr_domains] = dom;
1056 return state->nr_domains++;
1059 static void free_block_devices(struct lv_read_state *state) {
1060 if (state->block_devices) {
1061 for (int i = 0; i < state->nr_block_devices; ++i)
1062 sfree(state->block_devices[i].path);
1063 sfree(state->block_devices);
1065 state->block_devices = NULL;
1066 state->nr_block_devices = 0;
1069 static int add_block_device(struct lv_read_state *state, virDomainPtr dom,
1071 struct block_device *new_ptr;
1073 sizeof(state->block_devices[0]) * (state->nr_block_devices + 1);
1076 path_copy = strdup(path);
1080 if (state->block_devices)
1081 new_ptr = realloc(state->block_devices, new_size);
1083 new_ptr = malloc(new_size);
1085 if (new_ptr == NULL) {
1089 state->block_devices = new_ptr;
1090 state->block_devices[state->nr_block_devices].dom = dom;
1091 state->block_devices[state->nr_block_devices].path = path_copy;
1092 return state->nr_block_devices++;
1095 static void free_interface_devices(struct lv_read_state *state) {
1096 if (state->interface_devices) {
1097 for (int i = 0; i < state->nr_interface_devices; ++i) {
1098 sfree(state->interface_devices[i].path);
1099 sfree(state->interface_devices[i].address);
1100 sfree(state->interface_devices[i].number);
1102 sfree(state->interface_devices);
1104 state->interface_devices = NULL;
1105 state->nr_interface_devices = 0;
1108 static int add_interface_device(struct lv_read_state *state, virDomainPtr dom,
1109 const char *path, const char *address,
1110 unsigned int number) {
1111 struct interface_device *new_ptr;
1113 sizeof(state->interface_devices[0]) * (state->nr_interface_devices + 1);
1114 char *path_copy, *address_copy, number_string[15];
1116 if ((path == NULL) || (address == NULL))
1119 path_copy = strdup(path);
1123 address_copy = strdup(address);
1124 if (!address_copy) {
1129 snprintf(number_string, sizeof(number_string), "interface-%u", number);
1131 if (state->interface_devices)
1132 new_ptr = realloc(state->interface_devices, new_size);
1134 new_ptr = malloc(new_size);
1136 if (new_ptr == NULL) {
1138 sfree(address_copy);
1141 state->interface_devices = new_ptr;
1142 state->interface_devices[state->nr_interface_devices].dom = dom;
1143 state->interface_devices[state->nr_interface_devices].path = path_copy;
1144 state->interface_devices[state->nr_interface_devices].address = address_copy;
1145 state->interface_devices[state->nr_interface_devices].number =
1146 strdup(number_string);
1147 return state->nr_interface_devices++;
1150 static int ignore_device_match(ignorelist_t *il, const char *domname,
1151 const char *devpath) {
1155 if ((domname == NULL) || (devpath == NULL))
1158 n = sizeof(char) * (strlen(domname) + strlen(devpath) + 2);
1161 ERROR(PLUGIN_NAME " plugin: malloc failed.");
1164 ssnprintf(name, n, "%s:%s", domname, devpath);
1165 r = ignorelist_match(il, name);
1170 static int lv_shutdown(void) {
1171 for (int i = 0; i < nr_instances; ++i) {
1172 lv_fini_instance(i);
1176 virConnectClose(conn);
1179 ignorelist_free(il_domains);
1181 ignorelist_free(il_block_devices);
1182 il_block_devices = NULL;
1183 ignorelist_free(il_interface_devices);
1184 il_interface_devices = NULL;
1189 void module_register(void) {
1190 plugin_register_config(PLUGIN_NAME, lv_config, config_keys, NR_CONFIG_KEYS);
1191 plugin_register_init(PLUGIN_NAME, lv_init);
1192 plugin_register_shutdown(PLUGIN_NAME, lv_shutdown);