X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fvirt.c;h=62b53dcca36606a10caefe4c0db6e2ba0ef14aec;hb=dc57f6e8f2394b51dd35d1d8231ef110c075b831;hp=431ac413c45a9d3a163a4352c55b0b5937a6e7ef;hpb=e14e8b7f5c5bf9d0fe5cc632c6383f304d4ac2ad;p=collectd.git diff --git a/src/virt.c b/src/virt.c index 431ac413..62b53dcc 100644 --- a/src/virt.c +++ b/src/virt.c @@ -31,6 +31,7 @@ #include #include #include +#include /* for basename(3) */ /* Plugin name */ #define PLUGIN_NAME "virt" @@ -42,6 +43,8 @@ static const char *config_keys[] = { "Domain", "BlockDevice", + "BlockDeviceFormat", + "BlockDeviceFormatBasename", "InterfaceDevice", "IgnoreSelected", @@ -130,6 +133,12 @@ enum plginst_field { static enum plginst_field plugin_instance_format[PLGINST_MAX_FIELDS] = { plginst_none }; +/* BlockDeviceFormat */ +enum bd_field { + target, + source +}; + /* InterfaceFormat. */ enum if_field { if_address, @@ -137,6 +146,9 @@ enum if_field { if_number }; +/* BlockDeviceFormatBasename */ +_Bool blockdevice_format_basename = 0; +static enum bd_field blockdevice_format = target; static enum if_field interface_format = if_name; /* Time that we last refreshed. */ @@ -282,20 +294,12 @@ static void submit_derive2 (const char *type, derive_t v0, derive_t v1, virDomainPtr dom, const char *devname) { - value_t values[2]; - value_list_t vl = VALUE_LIST_INIT; - - init_value_list (&vl, dom); - - values[0].derive = v0; - values[1].derive = v1; - vl.values = values; - vl.values_len = 2; + value_t values[] = { + { .derive = v0 }, + { .derive = v1 }, + }; - sstrncpy (vl.type, type, sizeof (vl.type)); - sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance)); - - plugin_dispatch_values (&vl); + submit (dom, type, devname, values, STATIC_ARRAY_SIZE (values)); } /* void submit_derive2 */ static int @@ -346,6 +350,22 @@ lv_config (const char *key, const char *value) if (ignorelist_add (il_block_devices, value)) return 1; return 0; } + + if (strcasecmp (key, "BlockDeviceFormat") == 0) { + if (strcasecmp (value, "target") == 0) + blockdevice_format = target; + else if (strcasecmp (value, "source") == 0) + blockdevice_format = source; + else { + ERROR (PLUGIN_NAME " plugin: unknown BlockDeviceFormat: %s", value); + return -1; + } + return 0; + } + if (strcasecmp (key, "BlockDeviceFormatBasename") == 0) { + blockdevice_format_basename = IS_TRUE (value); + return 0; + } if (strcasecmp (key, "InterfaceDevice") == 0) { if (ignorelist_add (il_interface_devices, value)) return 1; return 0; @@ -586,15 +606,23 @@ lv_read (void) &stats, sizeof stats) != 0) continue; + char *type_instance = NULL; + if (blockdevice_format_basename && blockdevice_format == source) + type_instance = strdup(basename(block_devices[i].path)); + else + type_instance = strdup(block_devices[i].path); + if ((stats.rd_req != -1) && (stats.wr_req != -1)) submit_derive2 ("disk_ops", (derive_t) stats.rd_req, (derive_t) stats.wr_req, - block_devices[i].dom, block_devices[i].path); + block_devices[i].dom, type_instance); if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1)) submit_derive2 ("disk_octets", (derive_t) stats.rd_bytes, (derive_t) stats.wr_bytes, - block_devices[i].dom, block_devices[i].path); + block_devices[i].dom, type_instance); + + sfree (type_instance); } /* for (nr_block_devices) */ /* Get interface stats for each domain. */ @@ -602,7 +630,6 @@ lv_read (void) struct _virDomainInterfaceStats stats; char *display_name = NULL; - switch (interface_format) { case if_address: display_name = interface_devices[i].address; @@ -723,9 +750,11 @@ refresh_lists (void) xpath_ctx = xmlXPathNewContext (xml_doc); /* Block devices. */ - xpath_obj = xmlXPathEval - ((xmlChar *) "/domain/devices/disk/target[@dev]", - xpath_ctx); + char *bd_xmlpath = "/domain/devices/disk/target[@dev]"; + if (blockdevice_format == source) + bd_xmlpath = "/domain/devices/disk/source[@dev]"; + xpath_obj = xmlXPathEval ((xmlChar *) bd_xmlpath, xpath_ctx); + if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET || xpath_obj->nodesetval == NULL) goto cont;