X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Flibvirt.c;h=c53a81d4b5d1e4261667954b079d26e36a797e07;hb=dff1246579ac6bea70331e560d2e1da0c7429b11;hp=774067cdf016a09e1c9279f9ec2e86d987a3fd0b;hpb=b4c8f3f762d666742c774ab3b45815e5a416e5da;p=collectd.git diff --git a/src/libvirt.c b/src/libvirt.c index 774067cd..c53a81d4 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -91,13 +91,14 @@ struct interface_device { virDomainPtr dom; /* domain */ char *path; /* name of interface device */ char *address; /* mac address of interface device */ + char *number; /* interface device number */ }; static struct interface_device *interface_devices = NULL; static int nr_interface_devices = 0; static void free_interface_devices (void); -static int add_interface_device (virDomainPtr dom, const char *path, const char *address); +static int add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number); /* HostnameFormat. */ #define HF_MAX_FIELDS 3 @@ -115,7 +116,8 @@ static enum hf_field hostname_format[HF_MAX_FIELDS] = /* InterfaceFormat. */ enum if_field { if_address, - if_name + if_name, + if_number }; static enum if_field interface_format = if_name; @@ -139,8 +141,6 @@ init_value_list (value_list_t *vl, virDomainPtr dom) const char *name; char uuid[VIR_UUID_STRING_BUFLEN]; - vl->interval = interval_g; - sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin)); vl->host[0] = '\0'; @@ -348,6 +348,8 @@ lv_config (const char *key, const char *value) interface_format = if_name; else if (strcasecmp (value, "address") == 0) interface_format = if_address; + else if (strcasecmp (value, "number") == 0) + interface_format = if_number; else { ERROR ("unknown InterfaceFormat: %s", value); return -1; @@ -467,10 +469,20 @@ lv_read (void) /* Get interface stats for each domain. */ for (i = 0; i < nr_interface_devices; ++i) { struct _virDomainInterfaceStats stats; - char *display_name = interface_devices[i].path; - - if (interface_format == if_address) - display_name = interface_devices[i].address; + char *display_name = NULL; + + + switch (interface_format) { + case if_address: + display_name = interface_devices[i].address; + break; + case if_number: + display_name = interface_devices[i].number; + break; + case if_name: + default: + display_name = interface_devices[i].path; + } if (virDomainInterfaceStats (interface_devices[i].dom, interface_devices[i].path, @@ -644,7 +656,7 @@ refresh_lists (void) ignore_device_match (il_interface_devices, name, address) != 0)) goto cont3; - add_interface_device (dom, path, address); + add_interface_device (dom, path, address, j+1); cont3: if (path) xmlFree (path); if (address) xmlFree (address); @@ -745,6 +757,7 @@ free_interface_devices () for (i = 0; i < nr_interface_devices; ++i) { sfree (interface_devices[i].path); sfree (interface_devices[i].address); + sfree (interface_devices[i].number); } sfree (interface_devices); } @@ -753,17 +766,22 @@ free_interface_devices () } static int -add_interface_device (virDomainPtr dom, const char *path, const char *address) +add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number) { struct interface_device *new_ptr; int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1); - char *path_copy, *address_copy; + char *path_copy, *address_copy, number_string[15]; path_copy = strdup (path); if (!path_copy) return -1; address_copy = strdup (address); - if (!address_copy) return -1; + if (!address_copy) { + sfree(path_copy); + return -1; + } + + snprintf(number_string, sizeof (number_string), "interface-%u", number); if (interface_devices) new_ptr = realloc (interface_devices, new_size); @@ -779,6 +797,7 @@ add_interface_device (virDomainPtr dom, const char *path, const char *address) interface_devices[nr_interface_devices].dom = dom; interface_devices[nr_interface_devices].path = path_copy; interface_devices[nr_interface_devices].address = address_copy; + interface_devices[nr_interface_devices].number = strdup(number_string); return nr_interface_devices++; } @@ -808,7 +827,7 @@ lv_shutdown (void) free_domains (); if (conn != NULL) - virConnectClose (conn); + virConnectClose (conn); conn = NULL; ignorelist_free (il_domains); @@ -825,8 +844,8 @@ void module_register (void) { plugin_register_config ("libvirt", - lv_config, - config_keys, NR_CONFIG_KEYS); + lv_config, + config_keys, NR_CONFIG_KEYS); plugin_register_init ("libvirt", lv_init); plugin_register_read ("libvirt", lv_read); plugin_register_shutdown ("libvirt", lv_shutdown);