X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Flibvirt.c;h=4cbe38d2c1f8a05d9fabd43a48c8dbb64c89c106;hb=8b4fed9940e02138b7e273e56863df03d1a39ef7;hp=44f3832089570eca2fbc74d2df6920be7c34c32c;hpb=37e18082cfb22491138e282074c5267df48dd8de;p=collectd.git diff --git a/src/libvirt.c b/src/libvirt.c index 44f38320..4cbe38d2 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; @@ -125,17 +127,6 @@ static time_t last_refresh = (time_t) 0; static int refresh_lists (void); -/* Submit functions. */ -static void cpu_submit (unsigned long long cpu_time, - time_t t, - virDomainPtr dom, const char *type); -static void vcpu_submit (unsigned long long cpu_time, - time_t t, - virDomainPtr dom, int vcpu_nr, const char *type); -static void submit_counter2 (const char *type, counter_t v0, counter_t v1, - time_t t, - virDomainPtr dom, const char *devname); - /* ERROR(...) macro for virterrors. */ #define VIRT_ERROR(conn,s) do { \ virErrorPtr err; \ @@ -143,13 +134,133 @@ static void submit_counter2 (const char *type, counter_t v0, counter_t v1, if (err) ERROR ("%s: %s", (s), err->message); \ } while(0) +static void +init_value_list (value_list_t *vl, virDomainPtr dom) +{ + int i, n; + const char *name; + char uuid[VIR_UUID_STRING_BUFLEN]; + + sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin)); + + vl->host[0] = '\0'; + + /* Construct the hostname field according to HostnameFormat. */ + for (i = 0; i < HF_MAX_FIELDS; ++i) { + if (hostname_format[i] == hf_none) + continue; + + n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2; + + if (i > 0 && n >= 1) { + strncat (vl->host, ":", 1); + n--; + } + + switch (hostname_format[i]) { + case hf_none: break; + case hf_hostname: + strncat (vl->host, hostname_g, n); + break; + case hf_name: + name = virDomainGetName (dom); + if (name) + strncat (vl->host, name, n); + break; + case hf_uuid: + if (virDomainGetUUIDString (dom, uuid) == 0) + strncat (vl->host, uuid, n); + break; + } + } + + vl->host[sizeof (vl->host) - 1] = '\0'; +} /* void init_value_list */ + +static void +memory_submit (gauge_t memory, virDomainPtr dom) +{ + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; + + init_value_list (&vl, dom); + + values[0].gauge = memory; + + vl.values = values; + vl.values_len = 1; + + sstrncpy (vl.type, "memory", sizeof (vl.type)); + sstrncpy (vl.type_instance, "total", sizeof (vl.type_instance)); + + plugin_dispatch_values (&vl); +} + +static void +cpu_submit (unsigned long long cpu_time, + virDomainPtr dom, const char *type) +{ + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; + + init_value_list (&vl, dom); + + values[0].derive = cpu_time; + + vl.values = values; + vl.values_len = 1; + + sstrncpy (vl.type, type, sizeof (vl.type)); + + plugin_dispatch_values (&vl); +} + +static void +vcpu_submit (derive_t cpu_time, + virDomainPtr dom, int vcpu_nr, const char *type) +{ + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; + + init_value_list (&vl, dom); + + values[0].derive = cpu_time; + vl.values = values; + vl.values_len = 1; + + sstrncpy (vl.type, type, sizeof (vl.type)); + ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr); + + plugin_dispatch_values (&vl); +} + +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; + + sstrncpy (vl.type, type, sizeof (vl.type)); + sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance)); + + plugin_dispatch_values (&vl); +} /* void submit_derive2 */ + static int lv_init (void) { if (virInitialize () != 0) return -1; - - return 0; + else + return 0; } static int @@ -256,6 +367,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; @@ -313,32 +426,49 @@ lv_read (void) interface_devices[i].path); #endif - /* Get CPU usage, VCPU usage for each domain. */ + /* Get CPU usage, memory, VCPU usage for each domain. */ for (i = 0; i < nr_domains; ++i) { virDomainInfo info; virVcpuInfoPtr vinfo = NULL; + int status; int j; - if (virDomainGetInfo (domains[i], &info) != 0) + status = virDomainGetInfo (domains[i], &info); + if (status != 0) + { + ERROR ("libvirt plugin: virDomainGetInfo failed with status %i.", + status); continue; + } + + if (info.state != VIR_DOMAIN_RUNNING) + { + /* only gather stats for running domains */ + continue; + } - cpu_submit (info.cpuTime, t, domains[i], "virt_cpu_total"); + cpu_submit (info.cpuTime, domains[i], "virt_cpu_total"); + memory_submit ((gauge_t) info.memory * 1024, domains[i]); - vinfo = malloc (info.nrVirtCpu * sizeof vinfo[0]); + vinfo = malloc (info.nrVirtCpu * sizeof (vinfo[0])); if (vinfo == NULL) { ERROR ("libvirt plugin: malloc failed."); continue; } - if (virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu, - NULL, 0) != 0) { - sfree (vinfo); + status = virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu, + /* cpu map = */ NULL, /* cpu map length = */ 0); + if (status < 0) + { + ERROR ("libvirt plugin: virDomainGetVcpus failed with status %i.", + status); + free (vinfo); continue; } for (j = 0; j < info.nrVirtCpu; ++j) vcpu_submit (vinfo[j].cpuTime, - t, domains[i], vinfo[j].number, "virt_vcpu"); + domains[i], vinfo[j].number, "virt_vcpu"); sfree (vinfo); } @@ -352,23 +482,33 @@ lv_read (void) continue; if ((stats.rd_req != -1) && (stats.wr_req != -1)) - submit_counter2 ("disk_ops", - (counter_t) stats.rd_req, (counter_t) stats.wr_req, - t, block_devices[i].dom, block_devices[i].path); + submit_derive2 ("disk_ops", + (derive_t) stats.rd_req, (derive_t) stats.wr_req, + block_devices[i].dom, block_devices[i].path); if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1)) - submit_counter2 ("disk_octets", - (counter_t) stats.rd_bytes, (counter_t) stats.wr_bytes, - t, block_devices[i].dom, block_devices[i].path); + submit_derive2 ("disk_octets", + (derive_t) stats.rd_bytes, (derive_t) stats.wr_bytes, + block_devices[i].dom, block_devices[i].path); } /* for (nr_block_devices) */ /* 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, @@ -376,24 +516,24 @@ lv_read (void) continue; if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1)) - submit_counter2 ("if_octets", - (counter_t) stats.rx_bytes, (counter_t) stats.tx_bytes, - t, interface_devices[i].dom, display_name); + submit_derive2 ("if_octets", + (derive_t) stats.rx_bytes, (derive_t) stats.tx_bytes, + interface_devices[i].dom, display_name); if ((stats.rx_packets != -1) && (stats.tx_packets != -1)) - submit_counter2 ("if_packets", - (counter_t) stats.rx_packets, (counter_t) stats.tx_packets, - t, interface_devices[i].dom, display_name); + submit_derive2 ("if_packets", + (derive_t) stats.rx_packets, (derive_t) stats.tx_packets, + interface_devices[i].dom, display_name); if ((stats.rx_errs != -1) && (stats.tx_errs != -1)) - submit_counter2 ("if_errors", - (counter_t) stats.rx_errs, (counter_t) stats.tx_errs, - t, interface_devices[i].dom, display_name); + submit_derive2 ("if_errors", + (derive_t) stats.rx_errs, (derive_t) stats.tx_errs, + interface_devices[i].dom, display_name); if ((stats.rx_drop != -1) && (stats.tx_drop != -1)) - submit_counter2 ("if_dropped", - (counter_t) stats.rx_drop, (counter_t) stats.tx_drop, - t, interface_devices[i].dom, display_name); + submit_derive2 ("if_dropped", + (derive_t) stats.rx_drop, (derive_t) stats.tx_drop, + interface_devices[i].dom, display_name); } /* for (nr_interface_devices) */ return 0; @@ -542,7 +682,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); @@ -643,6 +783,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); } @@ -651,17 +792,25 @@ 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]; + + if ((path == NULL) || (address == NULL)) + return EINVAL; 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); @@ -677,6 +826,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++; } @@ -686,6 +836,9 @@ ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath) char *name; int n, r; + if ((domname == NULL) || (devpath == NULL)) + return 0; + n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2); name = malloc (n); if (name == NULL) { @@ -698,117 +851,6 @@ ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath) return r; } -static void -init_value_list (value_list_t *vl, time_t t, virDomainPtr dom) -{ - int i, n; - const char *name; - char uuid[VIR_UUID_STRING_BUFLEN]; - char *host_ptr; - size_t host_len; - - vl->time = t; - vl->interval = interval_g; - - sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin)); - - vl->host[0] = '\0'; - host_ptr = vl->host; - host_len = sizeof (vl->host); - - /* Construct the hostname field according to HostnameFormat. */ - for (i = 0; i < HF_MAX_FIELDS; ++i) { - if (hostname_format[i] == hf_none) - continue; - - n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2; - - if (i > 0 && n >= 1) { - strncat (vl->host, ":", 1); - n--; - } - - switch (hostname_format[i]) { - case hf_none: break; - case hf_hostname: - strncat (vl->host, hostname_g, n); - break; - case hf_name: - name = virDomainGetName (dom); - if (name) - strncat (vl->host, name, n); - break; - case hf_uuid: - if (virDomainGetUUIDString (dom, uuid) == 0) - strncat (vl->host, uuid, n); - break; - } - } - - vl->host[sizeof (vl->host) - 1] = '\0'; -} /* void init_value_list */ - -static void -cpu_submit (unsigned long long cpu_time, - time_t t, - virDomainPtr dom, const char *type) -{ - value_t values[1]; - value_list_t vl = VALUE_LIST_INIT; - - init_value_list (&vl, t, dom); - - values[0].counter = cpu_time; - - vl.values = values; - vl.values_len = 1; - - sstrncpy (vl.type, type, sizeof (vl.type)); - - plugin_dispatch_values (&vl); -} - -static void -vcpu_submit (counter_t cpu_time, - time_t t, - virDomainPtr dom, int vcpu_nr, const char *type) -{ - value_t values[1]; - value_list_t vl = VALUE_LIST_INIT; - - init_value_list (&vl, t, dom); - - values[0].counter = cpu_time; - vl.values = values; - vl.values_len = 1; - - sstrncpy (vl.type, type, sizeof (vl.type)); - ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr); - - plugin_dispatch_values (&vl); -} - -static void -submit_counter2 (const char *type, counter_t v0, counter_t v1, - time_t t, - virDomainPtr dom, const char *devname) -{ - value_t values[2]; - value_list_t vl = VALUE_LIST_INIT; - - init_value_list (&vl, t, dom); - - values[0].counter = v0; - values[1].counter = v1; - vl.values = values; - vl.values_len = 2; - - sstrncpy (vl.type, type, sizeof (vl.type)); - sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance)); - - plugin_dispatch_values (&vl); -} /* void submit_counter2 */ - static int lv_shutdown (void) { @@ -817,7 +859,7 @@ lv_shutdown (void) free_domains (); if (conn != NULL) - virConnectClose (conn); + virConnectClose (conn); conn = NULL; ignorelist_free (il_domains); @@ -834,8 +876,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);