X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Flibvirt.c;h=1f6287923fae47fbcd48bee83c660f66352da35c;hp=5acff29a578cd609dff2ba7de65bab1e13c4fc3b;hb=61a1fa91ba73e4fe3a34949f77c5f017056f2b7a;hpb=10075e6fe3c384b73c2dd398a1435f8d10e56654 diff --git a/src/libvirt.c b/src/libvirt.c index 5acff29a..1f628792 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -24,6 +24,7 @@ #include "plugin.h" #include "configfile.h" #include "utils_ignorelist.h" +#include "utils_complain.h" #include #include @@ -49,6 +50,8 @@ static const char *config_keys[] = { /* Connection. */ static virConnectPtr conn = 0; +static char *conn_string = NULL; +static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC; /* Seconds between list refreshes, 0 disables completely. */ static int interval = 60; @@ -153,15 +156,13 @@ lv_config (const char *key, const char *value) il_interface_devices = ignorelist_create (1); if (strcasecmp (key, "Connection") == 0) { - if (conn != 0) { - ERROR ("Connection may only be given once in config file"); - return 1; - } - conn = virConnectOpenReadOnly (value); - if (!conn) { - VIRT_ERROR (NULL, "connection failed"); + char *tmp = strdup (value); + if (tmp == NULL) { + ERROR ("libvirt plugin: Connection strdup failed."); return 1; } + sfree (conn_string); + conn_string = tmp; return 0; } @@ -186,9 +187,7 @@ lv_config (const char *key, const char *value) } if (strcasecmp (key, "IgnoreSelected") == 0) { - if (strcasecmp (value, "True") == 0 || - strcasecmp (value, "Yes") == 0 || - strcasecmp (value, "On") == 0) + if (IS_TRUE (value)) { ignorelist_set_invert (il_domains, 0); ignorelist_set_invert (il_block_devices, 0); @@ -253,19 +252,29 @@ lv_read (void) int i; if (conn == NULL) { - ERROR ("libvirt plugin: Not connected. Use Connection in " - "config file to supply connection URI. For more information " - "see "); - return -1; + /* `conn_string == NULL' is acceptable. */ + conn = virConnectOpenReadOnly (conn_string); + if (conn == NULL) { + c_complain (LOG_ERR, &conn_complain, + "libvirt plugin: Unable to connect: " + "virConnectOpenReadOnly failed."); + return -1; + } } + c_release (LOG_NOTICE, &conn_complain, + "libvirt plugin: Connection established."); time (&t); /* Need to refresh domain or device lists? */ if ((last_refresh == (time_t) 0) || ((interval > 0) && ((last_refresh + interval) <= t))) { - if (refresh_lists () != 0) + if (refresh_lists () != 0) { + if (conn != NULL) + virConnectClose (conn); + conn = NULL; return -1; + } last_refresh = t; } @@ -286,21 +295,31 @@ lv_read (void) 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; + } cpu_submit (info.cpuTime, t, domains[i], "virt_cpu_total"); - 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) { + 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; } @@ -646,8 +665,6 @@ 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; @@ -655,8 +672,6 @@ init_value_list (value_list_t *vl, time_t t, virDomainPtr dom) 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) {