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 #ifdef LIBVIR_CHECK_VERSION
41 #if LIBVIR_CHECK_VERSION(0, 9, 5)
42 #define HAVE_BLOCK_STATS_FLAGS 1
46 static const char *config_keys[] = {"Connection",
53 "BlockDeviceFormatBasename",
60 "PluginInstanceFormat",
65 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
68 static virConnectPtr conn = 0;
69 static char *conn_string = NULL;
70 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
72 /* Seconds between list refreshes, 0 disables completely. */
73 static int interval = 60;
75 /* List of domains, if specified. */
76 static ignorelist_t *il_domains = NULL;
77 /* List of block devices, if specified. */
78 static ignorelist_t *il_block_devices = NULL;
79 /* List of network interface devices, if specified. */
80 static ignorelist_t *il_interface_devices = NULL;
82 static int ignore_device_match(ignorelist_t *, const char *domname,
85 /* Actual list of block devices found on last refresh. */
87 virDomainPtr dom; /* domain */
88 char *path; /* name of block device */
91 /* Actual list of network interfaces found on last refresh. */
92 struct interface_device {
93 virDomainPtr dom; /* domain */
94 char *path; /* name of interface device */
95 char *address; /* mac address of interface device */
96 char *number; /* interface device number */
99 struct lv_read_state {
100 /* Actual list of domains found on last refresh. */
101 virDomainPtr *domains;
104 struct block_device *block_devices;
105 int nr_block_devices;
107 struct interface_device *interface_devices;
108 int nr_interface_devices;
111 static void free_domains(struct lv_read_state *state);
112 static int add_domain(struct lv_read_state *state, virDomainPtr dom);
114 static void free_block_devices(struct lv_read_state *state);
115 static int add_block_device(struct lv_read_state *state, virDomainPtr dom,
118 static void free_interface_devices(struct lv_read_state *state);
119 static int add_interface_device(struct lv_read_state *state, virDomainPtr dom,
120 const char *path, const char *address,
121 unsigned int number);
123 #define METADATA_VM_PARTITION_URI "http://ovirt.org/ovirtmap/tag/1.0"
124 #define METADATA_VM_PARTITION_ELEMENT "tag"
125 #define METADATA_VM_PARTITION_PREFIX "ovirtmap"
127 #define BUFFER_MAX_LEN 256
128 #define PARTITION_TAG_MAX_LEN 32
130 struct lv_read_instance {
131 struct lv_read_state read_state;
132 char tag[PARTITION_TAG_MAX_LEN];
136 struct lv_user_data {
137 struct lv_read_instance inst;
141 #define NR_INSTANCES_DEFAULT 1
142 #define NR_INSTANCES_MAX 128
143 static int nr_instances = NR_INSTANCES_DEFAULT;
144 static struct lv_user_data lv_read_user_data[NR_INSTANCES_MAX];
146 /* HostnameFormat. */
147 #define HF_MAX_FIELDS 3
149 enum hf_field { hf_none = 0, hf_hostname, hf_name, hf_uuid };
151 static enum hf_field hostname_format[HF_MAX_FIELDS] = {hf_name};
153 /* PluginInstanceFormat */
154 #define PLGINST_MAX_FIELDS 2
156 enum plginst_field { plginst_none = 0, plginst_name, plginst_uuid };
158 static enum plginst_field plugin_instance_format[PLGINST_MAX_FIELDS] = {
161 /* BlockDeviceFormat */
162 enum bd_field { target, source };
164 /* InterfaceFormat. */
165 enum if_field { if_address, if_name, if_number };
167 /* BlockDeviceFormatBasename */
168 _Bool blockdevice_format_basename = 0;
169 static enum bd_field blockdevice_format = target;
170 static enum if_field interface_format = if_name;
172 /* Time that we last refreshed. */
173 static time_t last_refresh = (time_t)0;
175 static int refresh_lists(struct lv_read_instance *inst);
177 struct lv_block_info {
178 virDomainBlockStatsStruct bi;
180 long long rd_total_times;
181 long long wr_total_times;
184 long long fl_total_times;
187 static void init_block_info(struct lv_block_info *binfo) {
191 binfo->bi.rd_req = -1;
192 binfo->bi.wr_req = -1;
193 binfo->bi.rd_bytes = -1;
194 binfo->bi.wr_bytes = -1;
196 binfo->rd_total_times = -1;
197 binfo->wr_total_times = -1;
199 binfo->fl_total_times = -1;
202 #ifdef HAVE_BLOCK_STATS_FLAGS
204 #define GET_BLOCK_INFO_VALUE(NAME, FIELD) \
206 if (!strcmp(param[i].field, NAME)) { \
207 binfo->FIELD = param[i].value.l; \
212 static int get_block_info(struct lv_block_info *binfo,
213 virTypedParameterPtr param, int nparams) {
214 if (binfo == NULL || param == NULL)
217 for (int i = 0; i < nparams; ++i) {
218 /* ignore type. Everything must be LLONG anyway. */
219 GET_BLOCK_INFO_VALUE("rd_operations", bi.rd_req);
220 GET_BLOCK_INFO_VALUE("wr_operations", bi.wr_req);
221 GET_BLOCK_INFO_VALUE("rd_bytes", bi.rd_bytes);
222 GET_BLOCK_INFO_VALUE("wr_bytes", bi.wr_bytes);
223 GET_BLOCK_INFO_VALUE("rd_total_times", rd_total_times);
224 GET_BLOCK_INFO_VALUE("wr_total_times", wr_total_times);
225 GET_BLOCK_INFO_VALUE("flush_operations", fl_req);
226 GET_BLOCK_INFO_VALUE("flush_total_times", fl_total_times);
232 #undef GET_BLOCK_INFO_VALUE
234 #endif /* HAVE_BLOCK_STATS_FLAGS */
236 /* ERROR(...) macro for virterrors. */
237 #define VIRT_ERROR(conn, s) \
240 err = (conn) ? virConnGetLastError((conn)) : virGetLastError(); \
242 ERROR("%s: %s", (s), err->message); \
245 static void init_value_list(value_list_t *vl, virDomainPtr dom) {
248 char uuid[VIR_UUID_STRING_BUFLEN];
250 sstrncpy(vl->plugin, PLUGIN_NAME, sizeof(vl->plugin));
254 /* Construct the hostname field according to HostnameFormat. */
255 for (int i = 0; i < HF_MAX_FIELDS; ++i) {
256 if (hostname_format[i] == hf_none)
259 n = DATA_MAX_NAME_LEN - strlen(vl->host) - 2;
261 if (i > 0 && n >= 1) {
262 strncat(vl->host, ":", 1);
266 switch (hostname_format[i]) {
270 strncat(vl->host, hostname_g, n);
273 name = virDomainGetName(dom);
275 strncat(vl->host, name, n);
278 if (virDomainGetUUIDString(dom, uuid) == 0)
279 strncat(vl->host, uuid, n);
284 vl->host[sizeof(vl->host) - 1] = '\0';
286 /* Construct the plugin instance field according to PluginInstanceFormat. */
287 for (int i = 0; i < PLGINST_MAX_FIELDS; ++i) {
288 if (plugin_instance_format[i] == plginst_none)
291 n = sizeof(vl->plugin_instance) - strlen(vl->plugin_instance) - 2;
293 if (i > 0 && n >= 1) {
294 strncat(vl->plugin_instance, ":", 1);
298 switch (plugin_instance_format[i]) {
302 name = virDomainGetName(dom);
304 strncat(vl->plugin_instance, name, n);
307 if (virDomainGetUUIDString(dom, uuid) == 0)
308 strncat(vl->plugin_instance, uuid, n);
313 vl->plugin_instance[sizeof(vl->plugin_instance) - 1] = '\0';
315 } /* void init_value_list */
317 static void submit(virDomainPtr dom, char const *type,
318 char const *type_instance, value_t *values,
320 value_list_t vl = VALUE_LIST_INIT;
321 init_value_list(&vl, dom);
324 vl.values_len = values_len;
326 sstrncpy(vl.type, type, sizeof(vl.type));
327 if (type_instance != NULL)
328 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
330 plugin_dispatch_values(&vl);
333 static void memory_submit(gauge_t value, virDomainPtr dom) {
334 submit(dom, "memory", "total", &(value_t){.gauge = value}, 1);
337 static void memory_stats_submit(gauge_t value, virDomainPtr dom,
339 static const char *tags[] = {"swap_in", "swap_out", "major_fault",
340 "minor_fault", "unused", "available",
341 "actual_balloon", "rss"};
343 if ((tag_index < 0) || (tag_index >= STATIC_ARRAY_SIZE(tags))) {
344 ERROR("virt plugin: Array index out of bounds: tag_index = %d", tag_index);
348 submit(dom, "memory", tags[tag_index], &(value_t){.gauge = value}, 1);
351 static void cpu_submit(unsigned long long value, virDomainPtr dom,
353 submit(dom, type, NULL, &(value_t){.derive = (derive_t)value}, 1);
356 static void vcpu_submit(derive_t value, virDomainPtr dom, int vcpu_nr,
358 char type_instance[DATA_MAX_NAME_LEN];
360 ssnprintf(type_instance, sizeof(type_instance), "%d", vcpu_nr);
362 submit(dom, type, type_instance, &(value_t){.derive = value}, 1);
365 static void submit_derive2(const char *type, derive_t v0, derive_t v1,
366 virDomainPtr dom, const char *devname) {
368 {.derive = v0}, {.derive = v1},
371 submit(dom, type, devname, values, STATIC_ARRAY_SIZE(values));
372 } /* void submit_derive2 */
374 static void disk_submit(struct lv_block_info *binfo, virDomainPtr dom,
375 const char *type_instance) {
376 if ((binfo->bi.rd_req != -1) && (binfo->bi.wr_req != -1))
377 submit_derive2("disk_ops", (derive_t)binfo->bi.rd_req,
378 (derive_t)binfo->bi.wr_req, dom, type_instance);
380 if ((binfo->bi.rd_bytes != -1) && (binfo->bi.wr_bytes != -1))
381 submit_derive2("disk_octets", (derive_t)binfo->bi.rd_bytes,
382 (derive_t)binfo->bi.wr_bytes, dom, type_instance);
385 static int lv_config(const char *key, const char *value) {
386 if (virInitialize() != 0)
389 if (il_domains == NULL)
390 il_domains = ignorelist_create(1);
391 if (il_block_devices == NULL)
392 il_block_devices = ignorelist_create(1);
393 if (il_interface_devices == NULL)
394 il_interface_devices = ignorelist_create(1);
396 if (strcasecmp(key, "Connection") == 0) {
397 char *tmp = strdup(value);
399 ERROR(PLUGIN_NAME " plugin: Connection strdup failed.");
407 if (strcasecmp(key, "RefreshInterval") == 0) {
409 interval = strtol(value, &eptr, 10);
410 if (eptr == NULL || *eptr != '\0')
415 if (strcasecmp(key, "Domain") == 0) {
416 if (ignorelist_add(il_domains, value))
420 if (strcasecmp(key, "BlockDevice") == 0) {
421 if (ignorelist_add(il_block_devices, value))
426 if (strcasecmp(key, "BlockDeviceFormat") == 0) {
427 if (strcasecmp(value, "target") == 0)
428 blockdevice_format = target;
429 else if (strcasecmp(value, "source") == 0)
430 blockdevice_format = source;
432 ERROR(PLUGIN_NAME " plugin: unknown BlockDeviceFormat: %s", value);
437 if (strcasecmp(key, "BlockDeviceFormatBasename") == 0) {
438 blockdevice_format_basename = IS_TRUE(value);
441 if (strcasecmp(key, "InterfaceDevice") == 0) {
442 if (ignorelist_add(il_interface_devices, value))
447 if (strcasecmp(key, "IgnoreSelected") == 0) {
448 if (IS_TRUE(value)) {
449 ignorelist_set_invert(il_domains, 0);
450 ignorelist_set_invert(il_block_devices, 0);
451 ignorelist_set_invert(il_interface_devices, 0);
453 ignorelist_set_invert(il_domains, 1);
454 ignorelist_set_invert(il_block_devices, 1);
455 ignorelist_set_invert(il_interface_devices, 1);
460 if (strcasecmp(key, "HostnameFormat") == 0) {
462 char *fields[HF_MAX_FIELDS];
465 value_copy = strdup(value);
466 if (value_copy == NULL) {
467 ERROR(PLUGIN_NAME " plugin: strdup failed.");
471 n = strsplit(value_copy, fields, HF_MAX_FIELDS);
474 ERROR(PLUGIN_NAME " plugin: HostnameFormat: no fields");
478 for (int i = 0; i < n; ++i) {
479 if (strcasecmp(fields[i], "hostname") == 0)
480 hostname_format[i] = hf_hostname;
481 else if (strcasecmp(fields[i], "name") == 0)
482 hostname_format[i] = hf_name;
483 else if (strcasecmp(fields[i], "uuid") == 0)
484 hostname_format[i] = hf_uuid;
486 ERROR(PLUGIN_NAME " plugin: unknown HostnameFormat field: %s",
494 for (int i = n; i < HF_MAX_FIELDS; ++i)
495 hostname_format[i] = hf_none;
500 if (strcasecmp(key, "PluginInstanceFormat") == 0) {
502 char *fields[PLGINST_MAX_FIELDS];
505 value_copy = strdup(value);
506 if (value_copy == NULL) {
507 ERROR(PLUGIN_NAME " plugin: strdup failed.");
511 n = strsplit(value_copy, fields, PLGINST_MAX_FIELDS);
514 ERROR(PLUGIN_NAME " plugin: PluginInstanceFormat: no fields");
518 for (int i = 0; i < n; ++i) {
519 if (strcasecmp(fields[i], "none") == 0) {
520 plugin_instance_format[i] = plginst_none;
522 } else if (strcasecmp(fields[i], "name") == 0)
523 plugin_instance_format[i] = plginst_name;
524 else if (strcasecmp(fields[i], "uuid") == 0)
525 plugin_instance_format[i] = plginst_uuid;
527 ERROR(PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s",
535 for (int i = n; i < PLGINST_MAX_FIELDS; ++i)
536 plugin_instance_format[i] = plginst_none;
541 if (strcasecmp(key, "InterfaceFormat") == 0) {
542 if (strcasecmp(value, "name") == 0)
543 interface_format = if_name;
544 else if (strcasecmp(value, "address") == 0)
545 interface_format = if_address;
546 else if (strcasecmp(value, "number") == 0)
547 interface_format = if_number;
549 ERROR(PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", value);
555 if (strcasecmp(key, "Instances") == 0) {
557 double val = strtod(value, &eptr);
560 ERROR(PLUGIN_NAME " plugin: Invalid value for Instances = '%s'", value);
564 ERROR(PLUGIN_NAME " plugin: Instances <= 0 makes no sense.");
567 if (val > NR_INSTANCES_MAX) {
568 ERROR(PLUGIN_NAME " plugin: Instances=%f > NR_INSTANCES_MAX=%i"
569 " use a lower setting or recompile the plugin.",
570 val, NR_INSTANCES_MAX);
574 nr_instances = (int)val;
575 DEBUG(PLUGIN_NAME " plugin: configured %i instances", nr_instances);
579 /* Unrecognised option. */
583 static int lv_connect(void) {
585 /* `conn_string == NULL' is acceptable. */
586 conn = virConnectOpenReadOnly(conn_string);
588 c_complain(LOG_ERR, &conn_complain,
589 PLUGIN_NAME " plugin: Unable to connect: "
590 "virConnectOpenReadOnly failed.");
594 c_release(LOG_NOTICE, &conn_complain,
595 PLUGIN_NAME " plugin: Connection established.");
599 static void lv_disconnect(void) {
601 virConnectClose(conn);
603 WARNING(PLUGIN_NAME " plugin: closed connection to libvirt");
606 static int lv_domain_block_info(virDomainPtr dom, const char *path,
607 struct lv_block_info *binfo) {
608 #ifdef HAVE_BLOCK_STATS_FLAGS
609 virTypedParameterPtr params = NULL;
614 ret = virDomainBlockStatsFlags(dom, path, NULL, &nparams, 0);
615 if (ret < 0 || nparams == 0) {
616 VIRT_ERROR(conn, "getting the disk params count");
620 params = calloc(nparams, sizeof(virTypedParameter));
621 if (params == NULL) {
622 ERROR("virt plugin: alloc(%i) for block=%s parameters failed.", nparams,
626 ret = virDomainBlockStatsFlags(dom, path, params, &nparams, 0);
628 VIRT_ERROR(conn, "getting the disk params values");
632 rc = get_block_info(binfo, params, nparams);
635 virTypedParamsFree(params, nparams);
638 return virDomainBlockStats(dom, path, &(binfo->bi), sizeof(binfo->bi));
639 #endif /* HAVE_BLOCK_STATS_FLAGS */
642 static int lv_read(user_data_t *ud) {
644 struct lv_read_instance *inst = NULL;
645 struct lv_read_state *state = NULL;
647 if (ud->data == NULL) {
648 ERROR(PLUGIN_NAME " plugin: NULL userdata");
653 state = &inst->read_state;
656 if (lv_connect() < 0)
662 /* Need to refresh domain or device lists? */
663 if ((last_refresh == (time_t)0) ||
664 ((interval > 0) && ((last_refresh + interval) <= t))) {
665 if (refresh_lists(inst) != 0) {
674 for (int i = 0; i < nr_domains; ++i)
675 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
676 for (int i = 0; i < nr_block_devices; ++i)
677 fprintf (stderr, "block device %d %s:%s\n",
678 i, virDomainGetName (block_devices[i].dom),
679 block_devices[i].path);
680 for (int i = 0; i < nr_interface_devices; ++i)
681 fprintf (stderr, "interface device %d %s:%s\n",
682 i, virDomainGetName (interface_devices[i].dom),
683 interface_devices[i].path);
686 /* Get CPU usage, memory, VCPU usage for each domain. */
687 for (int i = 0; i < state->nr_domains; ++i) {
689 virVcpuInfoPtr vinfo = NULL;
690 virDomainMemoryStatPtr minfo = NULL;
693 status = virDomainGetInfo(state->domains[i], &info);
695 ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
700 if (info.state != VIR_DOMAIN_RUNNING) {
701 /* only gather stats for running domains */
705 cpu_submit(info.cpuTime, state->domains[i], "virt_cpu_total");
706 memory_submit((gauge_t)info.memory * 1024, state->domains[i]);
708 vinfo = malloc(info.nrVirtCpu * sizeof(vinfo[0]));
710 ERROR(PLUGIN_NAME " plugin: malloc failed.");
714 status = virDomainGetVcpus(state->domains[i], vinfo, info.nrVirtCpu,
715 /* cpu map = */ NULL, /* cpu map length = */ 0);
717 ERROR(PLUGIN_NAME " plugin: virDomainGetVcpus failed with status %i.",
723 for (int j = 0; j < info.nrVirtCpu; ++j)
724 vcpu_submit(vinfo[j].cpuTime, state->domains[i], vinfo[j].number,
730 malloc(VIR_DOMAIN_MEMORY_STAT_NR * sizeof(virDomainMemoryStatStruct));
732 ERROR("virt plugin: malloc failed.");
736 status = virDomainMemoryStats(state->domains[i], minfo,
737 VIR_DOMAIN_MEMORY_STAT_NR, 0);
740 ERROR("virt plugin: virDomainMemoryStats failed with status %i.", status);
745 for (int j = 0; j < status; j++) {
746 memory_stats_submit((gauge_t)minfo[j].val * 1024, state->domains[i],
753 /* Get block device stats for each domain. */
754 for (int i = 0; i < state->nr_block_devices; ++i) {
755 struct block_device *bdev = &(state->block_devices[i]);
756 struct lv_block_info binfo;
757 init_block_info(&binfo);
759 if (lv_domain_block_info(bdev->dom, bdev->path, &binfo) < 0)
762 char *type_instance = NULL;
763 if (blockdevice_format_basename && blockdevice_format == source)
764 type_instance = strdup(basename(bdev->path));
766 type_instance = strdup(bdev->path);
768 disk_submit(&binfo, bdev->dom, type_instance);
770 sfree(type_instance);
771 } /* for (nr_block_devices) */
773 /* Get interface stats for each domain. */
774 for (int i = 0; i < state->nr_interface_devices; ++i) {
775 struct _virDomainInterfaceStats stats;
776 char *display_name = NULL;
778 switch (interface_format) {
780 display_name = state->interface_devices[i].address;
783 display_name = state->interface_devices[i].number;
787 display_name = state->interface_devices[i].path;
790 if (virDomainInterfaceStats(state->interface_devices[i].dom,
791 state->interface_devices[i].path, &stats,
795 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
796 submit_derive2("if_octets", (derive_t)stats.rx_bytes,
797 (derive_t)stats.tx_bytes, state->interface_devices[i].dom,
800 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
801 submit_derive2("if_packets", (derive_t)stats.rx_packets,
802 (derive_t)stats.tx_packets,
803 state->interface_devices[i].dom, display_name);
805 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
806 submit_derive2("if_errors", (derive_t)stats.rx_errs,
807 (derive_t)stats.tx_errs, state->interface_devices[i].dom,
810 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
811 submit_derive2("if_dropped", (derive_t)stats.rx_drop,
812 (derive_t)stats.tx_drop, state->interface_devices[i].dom,
814 } /* for (nr_interface_devices) */
819 static int lv_init_instance(size_t i, plugin_read_cb callback) {
820 struct lv_user_data *lv_ud = &(lv_read_user_data[i]);
821 struct lv_read_instance *inst = &(lv_ud->inst);
823 memset(lv_ud, 0, sizeof(*lv_ud));
825 ssnprintf(inst->tag, sizeof(inst->tag), "%s-%zu", PLUGIN_NAME, i);
828 user_data_t *ud = &(lv_ud->ud);
830 ud->free_func = NULL;
832 INFO(PLUGIN_NAME " plugin: reader %s initialized", inst->tag);
833 return plugin_register_complex_read(NULL, inst->tag, callback, 0, ud);
836 static void lv_clean_read_state(struct lv_read_state *state) {
837 free_block_devices(state);
838 free_interface_devices(state);
842 static void lv_fini_instance(size_t i) {
843 struct lv_read_instance *inst = &(lv_read_user_data[i].inst);
844 struct lv_read_state *state = &(inst->read_state);
846 lv_clean_read_state(state);
847 INFO(PLUGIN_NAME " plugin: reader %s finalized", inst->tag);
850 static int lv_init(void) {
851 if (virInitialize() != 0)
854 if (lv_connect() != 0)
857 DEBUG(PLUGIN_NAME " plugin: starting %i instances", nr_instances);
859 for (int i = 0; i < nr_instances; ++i)
860 lv_init_instance(i, lv_read);
866 * returns 0 on success and <0 on error
868 static int lv_domain_get_tag(xmlXPathContextPtr xpath_ctx, const char *dom_name,
870 char xpath_str[BUFFER_MAX_LEN] = {'\0'};
871 xmlXPathObjectPtr xpath_obj = NULL;
872 xmlNodePtr xml_node = NULL;
876 err = xmlXPathRegisterNs(xpath_ctx,
877 (const xmlChar *)METADATA_VM_PARTITION_PREFIX,
878 (const xmlChar *)METADATA_VM_PARTITION_URI);
880 ERROR(PLUGIN_NAME " plugin: xmlXpathRegisterNs(%s, %s) failed on domain %s",
881 METADATA_VM_PARTITION_PREFIX, METADATA_VM_PARTITION_URI, dom_name);
885 ssnprintf(xpath_str, sizeof(xpath_str), "/domain/metadata/%s:%s/text()",
886 METADATA_VM_PARTITION_PREFIX, METADATA_VM_PARTITION_ELEMENT);
887 xpath_obj = xmlXPathEvalExpression((xmlChar *)xpath_str, xpath_ctx);
888 if (xpath_obj == NULL) {
889 ERROR(PLUGIN_NAME " plugin: xmlXPathEval(%s) failed on domain %s",
890 xpath_str, dom_name);
894 if (xpath_obj->type != XPATH_NODESET) {
895 ERROR(PLUGIN_NAME " plugin: xmlXPathEval(%s) unexpected return type %d "
896 "(wanted %d) on domain %s",
897 xpath_str, xpath_obj->type, XPATH_NODESET, dom_name);
902 * from now on there is no real error, it's ok if a domain
903 * doesn't have the metadata partition tag.
906 if (xpath_obj->nodesetval == NULL || xpath_obj->nodesetval->nodeNr != 1) {
907 DEBUG(PLUGIN_NAME " plugin: xmlXPathEval(%s) return nodeset size=%i "
908 "expected=1 on domain %s",
910 (xpath_obj->nodesetval == NULL) ? 0 : xpath_obj->nodesetval->nodeNr,
913 xml_node = xpath_obj->nodesetval->nodeTab[0];
914 sstrncpy(dom_tag, (const char *)xml_node->content, PARTITION_TAG_MAX_LEN);
918 /* deregister to clean up */
919 err = xmlXPathRegisterNs(xpath_ctx,
920 (const xmlChar *)METADATA_VM_PARTITION_PREFIX, NULL);
922 /* we can't really recover here */
924 " plugin: deregistration of namespace %s failed for domain %s",
925 METADATA_VM_PARTITION_PREFIX, dom_name);
928 xmlXPathFreeObject(xpath_obj);
933 static int is_known_tag(const char *dom_tag) {
934 for (int i = 0; i < nr_instances; ++i)
935 if (!strcmp(dom_tag, lv_read_user_data[i].inst.tag))
940 static int lv_instance_include_domain(struct lv_read_instance *inst,
941 const char *dom_name,
942 const char *dom_tag) {
943 if ((dom_tag[0] != '\0') && (strcmp(dom_tag, inst->tag) == 0))
946 /* instance#0 will always be there, so it is in charge of extra duties */
948 if (dom_tag[0] == '\0' || !is_known_tag(dom_tag)) {
949 DEBUG(PLUGIN_NAME " plugin#%s: refreshing domain %s "
950 "with unknown tag '%s'",
951 inst->tag, dom_name, dom_tag);
959 static int refresh_lists(struct lv_read_instance *inst) {
960 struct lv_read_state *state = &inst->read_state;
963 n = virConnectNumOfDomains(conn);
965 VIRT_ERROR(conn, "reading number of domains");
969 lv_clean_read_state(state);
974 /* Get list of domains. */
975 domids = malloc(sizeof(*domids) * n);
976 if (domids == NULL) {
977 ERROR(PLUGIN_NAME " plugin: malloc failed.");
981 n = virConnectListDomains(conn, domids, n);
983 VIRT_ERROR(conn, "reading list of domains");
988 /* Fetch each domain and add it to the list, unless ignore. */
989 for (int i = 0; i < n; ++i) {
990 virDomainPtr dom = NULL;
993 xmlDocPtr xml_doc = NULL;
994 xmlXPathContextPtr xpath_ctx = NULL;
995 xmlXPathObjectPtr xpath_obj = NULL;
996 char tag[PARTITION_TAG_MAX_LEN] = {'\0'};
998 dom = virDomainLookupByID(conn, domids[i]);
1000 VIRT_ERROR(conn, "virDomainLookupByID");
1001 /* Could be that the domain went away -- ignore it anyway. */
1005 name = virDomainGetName(dom);
1007 VIRT_ERROR(conn, "virDomainGetName");
1011 if (il_domains && ignorelist_match(il_domains, name) != 0)
1014 /* Get a list of devices for this domain. */
1015 xml = virDomainGetXMLDesc(dom, 0);
1017 VIRT_ERROR(conn, "virDomainGetXMLDesc");
1021 /* Yuck, XML. Parse out the devices. */
1022 xml_doc = xmlReadDoc((xmlChar *)xml, NULL, NULL, XML_PARSE_NONET);
1023 if (xml_doc == NULL) {
1024 VIRT_ERROR(conn, "xmlReadDoc");
1028 xpath_ctx = xmlXPathNewContext(xml_doc);
1030 if (lv_domain_get_tag(xpath_ctx, name, tag) < 0) {
1031 ERROR(PLUGIN_NAME " plugin: lv_domain_get_tag failed.");
1035 if (!lv_instance_include_domain(inst, name, tag))
1038 if (add_domain(state, dom) < 0) {
1039 ERROR(PLUGIN_NAME " plugin: malloc failed.");
1043 /* Block devices. */
1044 const char *bd_xmlpath = "/domain/devices/disk/target[@dev]";
1045 if (blockdevice_format == source)
1046 bd_xmlpath = "/domain/devices/disk/source[@dev]";
1047 xpath_obj = xmlXPathEval((const xmlChar *)bd_xmlpath, xpath_ctx);
1049 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
1050 xpath_obj->nodesetval == NULL)
1053 for (int j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
1057 node = xpath_obj->nodesetval->nodeTab[j];
1060 path = (char *)xmlGetProp(node, (xmlChar *)"dev");
1064 if (il_block_devices &&
1065 ignore_device_match(il_block_devices, name, path) != 0)
1068 add_block_device(state, dom, path);
1073 xmlXPathFreeObject(xpath_obj);
1075 /* Network interfaces. */
1076 xpath_obj = xmlXPathEval(
1077 (xmlChar *)"/domain/devices/interface[target[@dev]]", xpath_ctx);
1078 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
1079 xpath_obj->nodesetval == NULL)
1082 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
1084 for (int j = 0; j < xml_interfaces->nodeNr; ++j) {
1086 char *address = NULL;
1087 xmlNodePtr xml_interface;
1089 xml_interface = xml_interfaces->nodeTab[j];
1093 for (xmlNodePtr child = xml_interface->children; child;
1094 child = child->next) {
1095 if (child->type != XML_ELEMENT_NODE)
1098 if (xmlStrEqual(child->name, (const xmlChar *)"target")) {
1099 path = (char *)xmlGetProp(child, (const xmlChar *)"dev");
1102 } else if (xmlStrEqual(child->name, (const xmlChar *)"mac")) {
1103 address = (char *)xmlGetProp(child, (const xmlChar *)"address");
1109 if (il_interface_devices &&
1110 (ignore_device_match(il_interface_devices, name, path) != 0 ||
1111 ignore_device_match(il_interface_devices, name, address) != 0))
1114 add_interface_device(state, dom, path, address, j + 1);
1124 xmlXPathFreeObject(xpath_obj);
1126 xmlXPathFreeContext(xpath_ctx);
1128 xmlFreeDoc(xml_doc);
1135 DEBUG(PLUGIN_NAME " plugin#%s: refreshing"
1136 " domains=%i block_devices=%i iface_devices=%i",
1137 inst->tag, state->nr_domains, state->nr_block_devices,
1138 state->nr_interface_devices);
1143 static void free_domains(struct lv_read_state *state) {
1144 if (state->domains) {
1145 for (int i = 0; i < state->nr_domains; ++i)
1146 virDomainFree(state->domains[i]);
1147 sfree(state->domains);
1149 state->domains = NULL;
1150 state->nr_domains = 0;
1153 static int add_domain(struct lv_read_state *state, virDomainPtr dom) {
1154 virDomainPtr *new_ptr;
1155 int new_size = sizeof(state->domains[0]) * (state->nr_domains + 1);
1158 new_ptr = realloc(state->domains, new_size);
1160 new_ptr = malloc(new_size);
1162 if (new_ptr == NULL)
1165 state->domains = new_ptr;
1166 state->domains[state->nr_domains] = dom;
1167 return state->nr_domains++;
1170 static void free_block_devices(struct lv_read_state *state) {
1171 if (state->block_devices) {
1172 for (int i = 0; i < state->nr_block_devices; ++i)
1173 sfree(state->block_devices[i].path);
1174 sfree(state->block_devices);
1176 state->block_devices = NULL;
1177 state->nr_block_devices = 0;
1180 static int add_block_device(struct lv_read_state *state, virDomainPtr dom,
1182 struct block_device *new_ptr;
1184 sizeof(state->block_devices[0]) * (state->nr_block_devices + 1);
1187 path_copy = strdup(path);
1191 if (state->block_devices)
1192 new_ptr = realloc(state->block_devices, new_size);
1194 new_ptr = malloc(new_size);
1196 if (new_ptr == NULL) {
1200 state->block_devices = new_ptr;
1201 state->block_devices[state->nr_block_devices].dom = dom;
1202 state->block_devices[state->nr_block_devices].path = path_copy;
1203 return state->nr_block_devices++;
1206 static void free_interface_devices(struct lv_read_state *state) {
1207 if (state->interface_devices) {
1208 for (int i = 0; i < state->nr_interface_devices; ++i) {
1209 sfree(state->interface_devices[i].path);
1210 sfree(state->interface_devices[i].address);
1211 sfree(state->interface_devices[i].number);
1213 sfree(state->interface_devices);
1215 state->interface_devices = NULL;
1216 state->nr_interface_devices = 0;
1219 static int add_interface_device(struct lv_read_state *state, virDomainPtr dom,
1220 const char *path, const char *address,
1221 unsigned int number) {
1222 struct interface_device *new_ptr;
1224 sizeof(state->interface_devices[0]) * (state->nr_interface_devices + 1);
1225 char *path_copy, *address_copy, number_string[15];
1227 if ((path == NULL) || (address == NULL))
1230 path_copy = strdup(path);
1234 address_copy = strdup(address);
1235 if (!address_copy) {
1240 snprintf(number_string, sizeof(number_string), "interface-%u", number);
1242 if (state->interface_devices)
1243 new_ptr = realloc(state->interface_devices, new_size);
1245 new_ptr = malloc(new_size);
1247 if (new_ptr == NULL) {
1249 sfree(address_copy);
1252 state->interface_devices = new_ptr;
1253 state->interface_devices[state->nr_interface_devices].dom = dom;
1254 state->interface_devices[state->nr_interface_devices].path = path_copy;
1255 state->interface_devices[state->nr_interface_devices].address = address_copy;
1256 state->interface_devices[state->nr_interface_devices].number =
1257 strdup(number_string);
1258 return state->nr_interface_devices++;
1261 static int ignore_device_match(ignorelist_t *il, const char *domname,
1262 const char *devpath) {
1266 if ((domname == NULL) || (devpath == NULL))
1269 n = sizeof(char) * (strlen(domname) + strlen(devpath) + 2);
1272 ERROR(PLUGIN_NAME " plugin: malloc failed.");
1275 ssnprintf(name, n, "%s:%s", domname, devpath);
1276 r = ignorelist_match(il, name);
1281 static int lv_shutdown(void) {
1282 for (int i = 0; i < nr_instances; ++i) {
1283 lv_fini_instance(i);
1288 ignorelist_free(il_domains);
1290 ignorelist_free(il_block_devices);
1291 il_block_devices = NULL;
1292 ignorelist_free(il_interface_devices);
1293 il_interface_devices = NULL;
1298 void module_register(void) {
1299 plugin_register_config(PLUGIN_NAME, lv_config, config_keys, NR_CONFIG_KEYS);
1300 plugin_register_init(PLUGIN_NAME, lv_init);
1301 plugin_register_shutdown(PLUGIN_NAME, lv_shutdown);