#include "plugin.h"
#include "configfile.h"
#include "utils_ignorelist.h"
+#include "utils_complain.h"
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
/* 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;
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;
}
}
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);
int i;
if (conn == NULL) {
- ERROR ("libvirt plugin: Not connected. Use Connection in "
- "config file to supply connection URI. For more information "
- "see <http://libvirt.org/uri.html>");
- 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;
}
ERROR ("libvirt plugin: malloc failed.");
return 0;
}
- snprintf (name, n, "%s:%s", domname, devpath);
+ ssnprintf (name, n, "%s:%s", domname, devpath);
r = ignorelist_match (il, name);
free (name);
return r;
vl->time = t;
vl->interval = interval_g;
- strncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
- vl->plugin[sizeof (vl->plugin) - 1] = '\0';
+ sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
vl->host[0] = '\0';
host_ptr = vl->host;
n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
if (i > 0 && n >= 1) {
- strcat (vl->host, ":");
+ strncat (vl->host, ":", 1);
n--;
}
vl.values = values;
vl.values_len = 1;
- plugin_dispatch_values (type, &vl);
+ sstrncpy (vl.type, type, sizeof (vl.type));
+
+ plugin_dispatch_values (&vl);
}
static void
vl.values = values;
vl.values_len = 1;
- snprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
- vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+ sstrncpy (vl.type, type, sizeof (vl.type));
+ ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
- plugin_dispatch_values (type, &vl);
+ plugin_dispatch_values (&vl);
}
static void
vl.values = values;
vl.values_len = 2;
- strncpy (vl.type_instance, devname, sizeof (vl.type_instance));
- vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+ sstrncpy (vl.type, type, sizeof (vl.type));
+ sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
- plugin_dispatch_values (type, &vl);
+ plugin_dispatch_values (&vl);
} /* void submit_counter2 */
static int