libvirt plugin: Re-connect to libvirtd if connecting fails.
[collectd.git] / src / libvirt.c
index 327536a..6ba08fb 100644 (file)
@@ -49,6 +49,8 @@ static const char *config_keys[] = {
 
 /* Connection. */
 static virConnectPtr conn = 0;
+static char *conn_string = NULL;
+static int conn_count = 0;
 
 /* Seconds between list refreshes, 0 disables completely. */
 static int interval = 60;
@@ -153,14 +155,14 @@ lv_config (const char *key, const char *value)
         il_interface_devices = ignorelist_create (1);
 
     if (strcasecmp (key, "Connection") == 0) {
-        if (conn != 0) {
+        if (conn_count++ != 0) {
             ERROR ("Connection may only be given once in config file");
             return 1;
         }
-        conn = virConnectOpenReadOnly (value);
-        if (!conn) {
-            VIRT_ERROR (NULL, "connection failed");
-            return 1;
+        conn_string = strdup(value);
+        if (conn_string == NULL) {
+            ERROR ("libvirt plugin: Connection strdup failed.");
+            return -1;
         }
         return 0;
     }
@@ -253,10 +255,11 @@ 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 <http://libvirt.org/uri.html>");
-        return -1;
+        conn = virConnectOpenReadOnly (conn_string);
+        if (conn == NULL) {
+            ERROR ("libvirt plugin: Not connected.");
+            return -1;
+        }
     }
 
     time (&t);
@@ -264,8 +267,12 @@ lv_read (void)
     /* 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;
     }
 
@@ -634,7 +641,7 @@ ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
         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;
@@ -652,8 +659,7 @@ init_value_list (value_list_t *vl, time_t t, virDomainPtr dom)
     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;
@@ -667,7 +673,7 @@ init_value_list (value_list_t *vl, time_t t, virDomainPtr dom)
         n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
 
         if (i > 0 && n >= 1) {
-            strcat (vl->host, ":");
+            strncat (vl->host, ":", 1);
             n--;
         }
 
@@ -706,7 +712,9 @@ cpu_submit (unsigned long long cpu_time,
     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
@@ -723,10 +731,10 @@ vcpu_submit (counter_t cpu_time,
     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
@@ -744,10 +752,10 @@ submit_counter2 (const char *type, counter_t v0, counter_t v1,
     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