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>
37 #define PLUGIN_NAME "virt"
39 static const char *config_keys[] = {"Connection",
46 "BlockDeviceFormatBasename",
53 "PluginInstanceFormat",
58 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
61 static virConnectPtr conn = 0;
62 static char *conn_string = NULL;
63 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
65 /* Seconds between list refreshes, 0 disables completely. */
66 static int interval = 60;
68 /* List of domains, if specified. */
69 static ignorelist_t *il_domains = NULL;
70 /* List of block devices, if specified. */
71 static ignorelist_t *il_block_devices = NULL;
72 /* List of network interface devices, if specified. */
73 static ignorelist_t *il_interface_devices = NULL;
75 static int ignore_device_match(ignorelist_t *, const char *domname,
78 /* Actual list of block devices found on last refresh. */
80 virDomainPtr dom; /* domain */
81 char *path; /* name of block device */
84 /* Actual list of network interfaces found on last refresh. */
85 struct interface_device {
86 virDomainPtr dom; /* domain */
87 char *path; /* name of interface device */
88 char *address; /* mac address of interface device */
89 char *number; /* interface device number */
92 struct lv_read_state {
93 /* Actual list of domains found on last refresh. */
94 virDomainPtr *domains;
97 struct block_device *block_devices;
100 struct interface_device *interface_devices;
101 int nr_interface_devices;
104 static void free_domains(struct lv_read_state *state);
105 static int add_domain(struct lv_read_state *state, virDomainPtr dom);
107 static void free_block_devices(struct lv_read_state *state);
108 static int add_block_device(struct lv_read_state *state, virDomainPtr dom,
111 static void free_interface_devices(struct lv_read_state *state);
112 static int add_interface_device(struct lv_read_state *state, virDomainPtr dom,
113 const char *path, const char *address,
114 unsigned int number);
116 #define PARTITION_TAG_MAX_LEN 32
117 struct lv_read_instance {
118 struct lv_read_state read_state;
119 char tag[PARTITION_TAG_MAX_LEN];
123 struct lv_user_data {
124 struct lv_read_instance inst;
128 #define NR_INSTANCES_DEFAULT 1
129 #define NR_INSTANCES_MAX 128
130 static size_t nr_instances = NR_INSTANCES_DEFAULT;
131 static struct lv_user_data lv_read_user_data[NR_INSTANCES_MAX];
133 /* HostnameFormat. */
134 #define HF_MAX_FIELDS 3
136 enum hf_field { hf_none = 0, hf_hostname, hf_name, hf_uuid };
138 static enum hf_field hostname_format[HF_MAX_FIELDS] = {hf_name};
140 /* PluginInstanceFormat */
141 #define PLGINST_MAX_FIELDS 2
143 enum plginst_field { plginst_none = 0, plginst_name, plginst_uuid };
145 static enum plginst_field plugin_instance_format[PLGINST_MAX_FIELDS] = {
148 /* BlockDeviceFormat */
149 enum bd_field { target, source };
151 /* InterfaceFormat. */
152 enum if_field { if_address, if_name, if_number };
154 /* BlockDeviceFormatBasename */
155 _Bool blockdevice_format_basename = 0;
156 static enum bd_field blockdevice_format = target;
157 static enum if_field interface_format = if_name;
159 /* Time that we last refreshed. */
160 static time_t last_refresh = (time_t)0;
162 static int refresh_lists(struct lv_read_state *state);
164 /* ERROR(...) macro for virterrors. */
165 #define VIRT_ERROR(conn, s) \
168 err = (conn) ? virConnGetLastError((conn)) : virGetLastError(); \
170 ERROR("%s: %s", (s), err->message); \
173 static void init_value_list(value_list_t *vl, virDomainPtr dom) {
176 char uuid[VIR_UUID_STRING_BUFLEN];
178 sstrncpy(vl->plugin, PLUGIN_NAME, sizeof(vl->plugin));
182 /* Construct the hostname field according to HostnameFormat. */
183 for (int i = 0; i < HF_MAX_FIELDS; ++i) {
184 if (hostname_format[i] == hf_none)
187 n = DATA_MAX_NAME_LEN - strlen(vl->host) - 2;
189 if (i > 0 && n >= 1) {
190 strncat(vl->host, ":", 1);
194 switch (hostname_format[i]) {
198 strncat(vl->host, hostname_g, n);
201 name = virDomainGetName(dom);
203 strncat(vl->host, name, n);
206 if (virDomainGetUUIDString(dom, uuid) == 0)
207 strncat(vl->host, uuid, n);
212 vl->host[sizeof(vl->host) - 1] = '\0';
214 /* Construct the plugin instance field according to PluginInstanceFormat. */
215 for (int i = 0; i < PLGINST_MAX_FIELDS; ++i) {
216 if (plugin_instance_format[i] == plginst_none)
219 n = sizeof(vl->plugin_instance) - strlen(vl->plugin_instance) - 2;
221 if (i > 0 && n >= 1) {
222 strncat(vl->plugin_instance, ":", 1);
226 switch (plugin_instance_format[i]) {
230 name = virDomainGetName(dom);
232 strncat(vl->plugin_instance, name, n);
235 if (virDomainGetUUIDString(dom, uuid) == 0)
236 strncat(vl->plugin_instance, uuid, n);
241 vl->plugin_instance[sizeof(vl->plugin_instance) - 1] = '\0';
243 } /* void init_value_list */
245 static void submit(virDomainPtr dom, char const *type,
246 char const *type_instance, value_t *values,
248 value_list_t vl = VALUE_LIST_INIT;
249 init_value_list(&vl, dom);
252 vl.values_len = values_len;
254 sstrncpy(vl.type, type, sizeof(vl.type));
255 if (type_instance != NULL)
256 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
258 plugin_dispatch_values(&vl);
261 static void memory_submit(gauge_t value, virDomainPtr dom) {
262 submit(dom, "memory", "total", &(value_t){.gauge = value}, 1);
265 static void memory_stats_submit(gauge_t value, virDomainPtr dom,
267 static const char *tags[] = {"swap_in", "swap_out", "major_fault",
268 "minor_fault", "unused", "available",
269 "actual_balloon", "rss"};
271 if ((tag_index < 0) || (tag_index >= STATIC_ARRAY_SIZE(tags))) {
272 ERROR("virt plugin: Array index out of bounds: tag_index = %d", tag_index);
276 submit(dom, "memory", tags[tag_index], &(value_t){.gauge = value}, 1);
279 static void cpu_submit(unsigned long long value, virDomainPtr dom,
281 submit(dom, type, NULL, &(value_t){.derive = (derive_t)value}, 1);
284 static void vcpu_submit(derive_t value, virDomainPtr dom, int vcpu_nr,
286 char type_instance[DATA_MAX_NAME_LEN];
288 ssnprintf(type_instance, sizeof(type_instance), "%d", vcpu_nr);
290 submit(dom, type, type_instance, &(value_t){.derive = value}, 1);
293 static void submit_derive2(const char *type, derive_t v0, derive_t v1,
294 virDomainPtr dom, const char *devname) {
296 {.derive = v0}, {.derive = v1},
299 submit(dom, type, devname, values, STATIC_ARRAY_SIZE(values));
300 } /* void submit_derive2 */
302 static int lv_config(const char *key, const char *value) {
303 if (virInitialize() != 0)
306 if (il_domains == NULL)
307 il_domains = ignorelist_create(1);
308 if (il_block_devices == NULL)
309 il_block_devices = ignorelist_create(1);
310 if (il_interface_devices == NULL)
311 il_interface_devices = ignorelist_create(1);
313 if (strcasecmp(key, "Connection") == 0) {
314 char *tmp = strdup(value);
316 ERROR(PLUGIN_NAME " plugin: Connection strdup failed.");
324 if (strcasecmp(key, "RefreshInterval") == 0) {
326 interval = strtol(value, &eptr, 10);
327 if (eptr == NULL || *eptr != '\0')
332 if (strcasecmp(key, "Domain") == 0) {
333 if (ignorelist_add(il_domains, value))
337 if (strcasecmp(key, "BlockDevice") == 0) {
338 if (ignorelist_add(il_block_devices, value))
343 if (strcasecmp(key, "BlockDeviceFormat") == 0) {
344 if (strcasecmp(value, "target") == 0)
345 blockdevice_format = target;
346 else if (strcasecmp(value, "source") == 0)
347 blockdevice_format = source;
349 ERROR(PLUGIN_NAME " plugin: unknown BlockDeviceFormat: %s", value);
354 if (strcasecmp(key, "BlockDeviceFormatBasename") == 0) {
355 blockdevice_format_basename = IS_TRUE(value);
358 if (strcasecmp(key, "InterfaceDevice") == 0) {
359 if (ignorelist_add(il_interface_devices, value))
364 if (strcasecmp(key, "IgnoreSelected") == 0) {
365 if (IS_TRUE(value)) {
366 ignorelist_set_invert(il_domains, 0);
367 ignorelist_set_invert(il_block_devices, 0);
368 ignorelist_set_invert(il_interface_devices, 0);
370 ignorelist_set_invert(il_domains, 1);
371 ignorelist_set_invert(il_block_devices, 1);
372 ignorelist_set_invert(il_interface_devices, 1);
377 if (strcasecmp(key, "HostnameFormat") == 0) {
379 char *fields[HF_MAX_FIELDS];
382 value_copy = strdup(value);
383 if (value_copy == NULL) {
384 ERROR(PLUGIN_NAME " plugin: strdup failed.");
388 n = strsplit(value_copy, fields, HF_MAX_FIELDS);
391 ERROR(PLUGIN_NAME " plugin: HostnameFormat: no fields");
395 for (int i = 0; i < n; ++i) {
396 if (strcasecmp(fields[i], "hostname") == 0)
397 hostname_format[i] = hf_hostname;
398 else if (strcasecmp(fields[i], "name") == 0)
399 hostname_format[i] = hf_name;
400 else if (strcasecmp(fields[i], "uuid") == 0)
401 hostname_format[i] = hf_uuid;
403 ERROR(PLUGIN_NAME " plugin: unknown HostnameFormat field: %s",
411 for (int i = n; i < HF_MAX_FIELDS; ++i)
412 hostname_format[i] = hf_none;
417 if (strcasecmp(key, "PluginInstanceFormat") == 0) {
419 char *fields[PLGINST_MAX_FIELDS];
422 value_copy = strdup(value);
423 if (value_copy == NULL) {
424 ERROR(PLUGIN_NAME " plugin: strdup failed.");
428 n = strsplit(value_copy, fields, PLGINST_MAX_FIELDS);
431 ERROR(PLUGIN_NAME " plugin: PluginInstanceFormat: no fields");
435 for (int i = 0; i < n; ++i) {
436 if (strcasecmp(fields[i], "none") == 0) {
437 plugin_instance_format[i] = plginst_none;
439 } else if (strcasecmp(fields[i], "name") == 0)
440 plugin_instance_format[i] = plginst_name;
441 else if (strcasecmp(fields[i], "uuid") == 0)
442 plugin_instance_format[i] = plginst_uuid;
444 ERROR(PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s",
452 for (int i = n; i < PLGINST_MAX_FIELDS; ++i)
453 plugin_instance_format[i] = plginst_none;
458 if (strcasecmp(key, "InterfaceFormat") == 0) {
459 if (strcasecmp(value, "name") == 0)
460 interface_format = if_name;
461 else if (strcasecmp(value, "address") == 0)
462 interface_format = if_address;
463 else if (strcasecmp(value, "number") == 0)
464 interface_format = if_number;
466 ERROR(PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", value);
472 if (strcasecmp(key, "Instances") == 0) {
474 long val = strtol(value, &eptr, 10);
475 if (eptr == NULL || *eptr != '\0')
478 ERROR(PLUGIN_NAME " plugin: Instances <= 0 makes no sense.");
481 if (val > NR_INSTANCES_MAX) {
482 ERROR(PLUGIN_NAME " plugin: Instances=%li > NR_INSTANCES_MAX=%i"
483 " use a lower setting or recompile the plugin.",
484 val, NR_INSTANCES_MAX);
487 nr_instances = (size_t)val;
491 /* Unrecognised option. */
495 static int lv_read(user_data_t *ud) {
497 struct lv_read_instance *inst = ud->data;
498 struct lv_read_state *state = &inst->read_state;
500 ERROR(PLUGIN_NAME " plugin: NULL userdata");
504 if (inst->id == 0 && conn == NULL) {
505 /* `conn_string == NULL' is acceptable. */
506 conn = virConnectOpenReadOnly(conn_string);
508 c_complain(LOG_ERR, &conn_complain,
509 PLUGIN_NAME " plugin: Unable to connect: "
510 "virConnectOpenReadOnly failed.");
514 c_release(LOG_NOTICE, &conn_complain,
515 PLUGIN_NAME " plugin: Connection established.");
519 /* Need to refresh domain or device lists? */
520 if ((last_refresh == (time_t)0) ||
521 ((interval > 0) && ((last_refresh + interval) <= t))) {
522 if (refresh_lists(state) != 0) {
524 virConnectClose(conn);
532 for (int i = 0; i < nr_domains; ++i)
533 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
534 for (int i = 0; i < nr_block_devices; ++i)
535 fprintf (stderr, "block device %d %s:%s\n",
536 i, virDomainGetName (block_devices[i].dom),
537 block_devices[i].path);
538 for (int i = 0; i < nr_interface_devices; ++i)
539 fprintf (stderr, "interface device %d %s:%s\n",
540 i, virDomainGetName (interface_devices[i].dom),
541 interface_devices[i].path);
544 /* Get CPU usage, memory, VCPU usage for each domain. */
545 for (int i = 0; i < state->nr_domains; ++i) {
547 virVcpuInfoPtr vinfo = NULL;
548 virDomainMemoryStatPtr minfo = NULL;
551 status = virDomainGetInfo(state->domains[i], &info);
553 ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
558 if (info.state != VIR_DOMAIN_RUNNING) {
559 /* only gather stats for running domains */
563 cpu_submit(info.cpuTime, state->domains[i], "virt_cpu_total");
564 memory_submit((gauge_t)info.memory * 1024, state->domains[i]);
566 vinfo = malloc(info.nrVirtCpu * sizeof(vinfo[0]));
568 ERROR(PLUGIN_NAME " plugin: malloc failed.");
572 status = virDomainGetVcpus(state->domains[i], vinfo, info.nrVirtCpu,
573 /* cpu map = */ NULL, /* cpu map length = */ 0);
575 ERROR(PLUGIN_NAME " plugin: virDomainGetVcpus failed with status %i.",
581 for (int j = 0; j < info.nrVirtCpu; ++j)
582 vcpu_submit(vinfo[j].cpuTime, state->domains[i], vinfo[j].number,
588 malloc(VIR_DOMAIN_MEMORY_STAT_NR * sizeof(virDomainMemoryStatStruct));
590 ERROR("virt plugin: malloc failed.");
594 status = virDomainMemoryStats(state->domains[i], minfo,
595 VIR_DOMAIN_MEMORY_STAT_NR, 0);
598 ERROR("virt plugin: virDomainMemoryStats failed with status %i.", status);
603 for (int j = 0; j < status; j++) {
604 memory_stats_submit((gauge_t)minfo[j].val * 1024, state->domains[i],
611 /* Get block device stats for each domain. */
612 for (int i = 0; i < state->nr_block_devices; ++i) {
613 struct _virDomainBlockStats stats;
615 if (virDomainBlockStats(state->block_devices[i].dom,
616 state->block_devices[i].path, &stats,
620 char *type_instance = NULL;
621 if (blockdevice_format_basename && blockdevice_format == source)
622 type_instance = strdup(basename(state->block_devices[i].path));
624 type_instance = strdup(state->block_devices[i].path);
626 if ((stats.rd_req != -1) && (stats.wr_req != -1))
627 submit_derive2("disk_ops", (derive_t)stats.rd_req, (derive_t)stats.wr_req,
628 state->block_devices[i].dom, type_instance);
630 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
631 submit_derive2("disk_octets", (derive_t)stats.rd_bytes,
632 (derive_t)stats.wr_bytes, state->block_devices[i].dom,
635 sfree(type_instance);
636 } /* for (nr_block_devices) */
638 /* Get interface stats for each domain. */
639 for (int i = 0; i < state->nr_interface_devices; ++i) {
640 struct _virDomainInterfaceStats stats;
641 char *display_name = NULL;
643 switch (interface_format) {
645 display_name = state->interface_devices[i].address;
648 display_name = state->interface_devices[i].number;
652 display_name = state->interface_devices[i].path;
655 if (virDomainInterfaceStats(state->interface_devices[i].dom,
656 state->interface_devices[i].path, &stats,
660 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
661 submit_derive2("if_octets", (derive_t)stats.rx_bytes,
662 (derive_t)stats.tx_bytes, state->interface_devices[i].dom,
665 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
666 submit_derive2("if_packets", (derive_t)stats.rx_packets,
667 (derive_t)stats.tx_packets,
668 state->interface_devices[i].dom, display_name);
670 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
671 submit_derive2("if_errors", (derive_t)stats.rx_errs,
672 (derive_t)stats.tx_errs, state->interface_devices[i].dom,
675 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
676 submit_derive2("if_dropped", (derive_t)stats.rx_drop,
677 (derive_t)stats.tx_drop, state->interface_devices[i].dom,
679 } /* for (nr_interface_devices) */
684 static int lv_init_instance(size_t i, plugin_read_cb callback) {
685 struct lv_user_data *lv_ud = &(lv_read_user_data[i]);
686 struct lv_read_instance *inst = &lv_ud->inst;
688 memset(lv_ud, 0, sizeof(*lv_ud));
690 ssnprintf(inst->tag, sizeof(inst->tag), "%s-%zu", PLUGIN_NAME, i);
693 user_data_t *ud = &lv_ud->ud;
695 ud->free_func = NULL;
697 INFO(PLUGIN_NAME "plugin: reader %s initialized", inst->tag);
698 return plugin_register_complex_read(NULL, inst->tag, callback, 0, ud);
701 static int lv_init(void) {
702 if (virInitialize() != 0)
705 for (size_t i = 0; i < nr_instances; ++i)
706 lv_init_instance(i, lv_read);
711 static int refresh_lists(struct lv_read_state *state) {
714 n = virConnectNumOfDomains(conn);
716 VIRT_ERROR(conn, "reading number of domains");
723 /* Get list of domains. */
724 domids = malloc(sizeof(*domids) * n);
725 if (domids == NULL) {
726 ERROR(PLUGIN_NAME " plugin: malloc failed.");
730 n = virConnectListDomains(conn, domids, n);
732 VIRT_ERROR(conn, "reading list of domains");
737 free_block_devices(state);
738 free_interface_devices(state);
741 /* Fetch each domain and add it to the list, unless ignore. */
742 for (int i = 0; i < n; ++i) {
743 virDomainPtr dom = NULL;
746 xmlDocPtr xml_doc = NULL;
747 xmlXPathContextPtr xpath_ctx = NULL;
748 xmlXPathObjectPtr xpath_obj = NULL;
750 dom = virDomainLookupByID(conn, domids[i]);
752 VIRT_ERROR(conn, "virDomainLookupByID");
753 /* Could be that the domain went away -- ignore it anyway. */
757 name = virDomainGetName(dom);
759 VIRT_ERROR(conn, "virDomainGetName");
763 if (il_domains && ignorelist_match(il_domains, name) != 0)
766 if (add_domain(state, dom) < 0) {
767 ERROR(PLUGIN_NAME " plugin: malloc failed.");
771 /* Get a list of devices for this domain. */
772 xml = virDomainGetXMLDesc(dom, 0);
774 VIRT_ERROR(conn, "virDomainGetXMLDesc");
778 /* Yuck, XML. Parse out the devices. */
779 xml_doc = xmlReadDoc((xmlChar *)xml, NULL, NULL, XML_PARSE_NONET);
780 if (xml_doc == NULL) {
781 VIRT_ERROR(conn, "xmlReadDoc");
785 xpath_ctx = xmlXPathNewContext(xml_doc);
788 char *bd_xmlpath = "/domain/devices/disk/target[@dev]";
789 if (blockdevice_format == source)
790 bd_xmlpath = "/domain/devices/disk/source[@dev]";
791 xpath_obj = xmlXPathEval((xmlChar *)bd_xmlpath, xpath_ctx);
793 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
794 xpath_obj->nodesetval == NULL)
797 for (int j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
801 node = xpath_obj->nodesetval->nodeTab[j];
804 path = (char *)xmlGetProp(node, (xmlChar *)"dev");
808 if (il_block_devices &&
809 ignore_device_match(il_block_devices, name, path) != 0)
812 add_block_device(state, dom, path);
817 xmlXPathFreeObject(xpath_obj);
819 /* Network interfaces. */
820 xpath_obj = xmlXPathEval(
821 (xmlChar *)"/domain/devices/interface[target[@dev]]", xpath_ctx);
822 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
823 xpath_obj->nodesetval == NULL)
826 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
828 for (int j = 0; j < xml_interfaces->nodeNr; ++j) {
830 char *address = NULL;
831 xmlNodePtr xml_interface;
833 xml_interface = xml_interfaces->nodeTab[j];
837 for (xmlNodePtr child = xml_interface->children; child;
838 child = child->next) {
839 if (child->type != XML_ELEMENT_NODE)
842 if (xmlStrEqual(child->name, (const xmlChar *)"target")) {
843 path = (char *)xmlGetProp(child, (const xmlChar *)"dev");
846 } else if (xmlStrEqual(child->name, (const xmlChar *)"mac")) {
847 address = (char *)xmlGetProp(child, (const xmlChar *)"address");
853 if (il_interface_devices &&
854 (ignore_device_match(il_interface_devices, name, path) != 0 ||
855 ignore_device_match(il_interface_devices, name, address) != 0))
858 add_interface_device(state, dom, path, address, j + 1);
868 xmlXPathFreeObject(xpath_obj);
870 xmlXPathFreeContext(xpath_ctx);
882 static void free_domains(struct lv_read_state *state) {
883 if (state->domains) {
884 for (int i = 0; i < state->nr_domains; ++i)
885 virDomainFree(state->domains[i]);
886 sfree(state->domains);
888 state->domains = NULL;
889 state->nr_domains = 0;
892 static int add_domain(struct lv_read_state *state, virDomainPtr dom) {
893 virDomainPtr *new_ptr;
894 int new_size = sizeof(state->domains[0]) * (state->nr_domains + 1);
897 new_ptr = realloc(state->domains, new_size);
899 new_ptr = malloc(new_size);
904 state->domains = new_ptr;
905 state->domains[state->nr_domains] = dom;
906 return state->nr_domains++;
909 static void free_block_devices(struct lv_read_state *state) {
910 if (state->block_devices) {
911 for (int i = 0; i < state->nr_block_devices; ++i)
912 sfree(state->block_devices[i].path);
913 sfree(state->block_devices);
915 state->block_devices = NULL;
916 state->nr_block_devices = 0;
919 static int add_block_device(struct lv_read_state *state, virDomainPtr dom,
921 struct block_device *new_ptr;
923 sizeof(state->block_devices[0]) * (state->nr_block_devices + 1);
926 path_copy = strdup(path);
930 if (state->block_devices)
931 new_ptr = realloc(state->block_devices, new_size);
933 new_ptr = malloc(new_size);
935 if (new_ptr == NULL) {
939 state->block_devices = new_ptr;
940 state->block_devices[state->nr_block_devices].dom = dom;
941 state->block_devices[state->nr_block_devices].path = path_copy;
942 return state->nr_block_devices++;
945 static void free_interface_devices(struct lv_read_state *state) {
946 if (state->interface_devices) {
947 for (int i = 0; i < state->nr_interface_devices; ++i) {
948 sfree(state->interface_devices[i].path);
949 sfree(state->interface_devices[i].address);
950 sfree(state->interface_devices[i].number);
952 sfree(state->interface_devices);
954 state->interface_devices = NULL;
955 state->nr_interface_devices = 0;
958 static int add_interface_device(struct lv_read_state *state, virDomainPtr dom,
959 const char *path, const char *address,
960 unsigned int number) {
961 struct interface_device *new_ptr;
963 sizeof(state->interface_devices[0]) * (state->nr_interface_devices + 1);
964 char *path_copy, *address_copy, number_string[15];
966 if ((path == NULL) || (address == NULL))
969 path_copy = strdup(path);
973 address_copy = strdup(address);
979 snprintf(number_string, sizeof(number_string), "interface-%u", number);
981 if (state->interface_devices)
982 new_ptr = realloc(state->interface_devices, new_size);
984 new_ptr = malloc(new_size);
986 if (new_ptr == NULL) {
991 state->interface_devices = new_ptr;
992 state->interface_devices[state->nr_interface_devices].dom = dom;
993 state->interface_devices[state->nr_interface_devices].path = path_copy;
994 state->interface_devices[state->nr_interface_devices].address = address_copy;
995 state->interface_devices[state->nr_interface_devices].number =
996 strdup(number_string);
997 return state->nr_interface_devices++;
1000 static int ignore_device_match(ignorelist_t *il, const char *domname,
1001 const char *devpath) {
1005 if ((domname == NULL) || (devpath == NULL))
1008 n = sizeof(char) * (strlen(domname) + strlen(devpath) + 2);
1011 ERROR(PLUGIN_NAME " plugin: malloc failed.");
1014 ssnprintf(name, n, "%s:%s", domname, devpath);
1015 r = ignorelist_match(il, name);
1020 static int lv_shutdown(void) {
1021 for (size_t i = 0; i < nr_instances; ++i) {
1022 struct lv_read_state *state = &(lv_read_user_data[i].inst.read_state);
1023 free_block_devices(state);
1024 free_interface_devices(state);
1025 free_domains(state);
1029 virConnectClose(conn);
1032 ignorelist_free(il_domains);
1034 ignorelist_free(il_block_devices);
1035 il_block_devices = NULL;
1036 ignorelist_free(il_interface_devices);
1037 il_interface_devices = NULL;
1042 void module_register(void) {
1043 plugin_register_config(PLUGIN_NAME, lv_config, config_keys, NR_CONFIG_KEYS);
1044 plugin_register_init(PLUGIN_NAME, lv_init);
1045 plugin_register_complex_read(NULL, PLUGIN_NAME, lv_read, 0, NULL);
1046 plugin_register_shutdown(PLUGIN_NAME, lv_shutdown);
1050 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker