Many build fixes that turned up with GCC 4.6.
[collectd.git] / src / libvirt.c
index 0a99822..1f62879 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/libvirt.c
- * Copyright (C) 2006,2007  Red Hat Inc.
+ * Copyright (C) 2006-2008  Red Hat Inc.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -24,6 +24,7 @@
 #include "plugin.h"
 #include "configfile.h"
 #include "utils_ignorelist.h"
+#include "utils_complain.h"
 
 #include <libvirt/libvirt.h>
 #include <libvirt/virterror.h>
@@ -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 <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;
     }
 
@@ -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;
         }
@@ -634,7 +653,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;
@@ -643,67 +662,45 @@ ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
 static void
 init_value_list (value_list_t *vl, time_t t, virDomainPtr dom)
 {
-    int i;
-    char  *host_ptr;
-    size_t host_len;
+    int i, n;
+    const char *name;
+    char uuid[VIR_UUID_STRING_BUFLEN];
 
     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;
-    host_len = sizeof (vl->host);
 
     /* Construct the hostname field according to HostnameFormat. */
     for (i = 0; i < HF_MAX_FIELDS; ++i) {
-       int status = 0;
+        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:
-               /* do nothing */
-               break;
-
-           case hf_hostname:
-               status = snprintf (host_ptr, host_len, ":%s", hostname_g);
-               break;
-
-           case hf_name:
-           {
-               const char *name = virDomainGetName (dom);
-               if (name != NULL)
-                   status = snprintf (host_ptr, host_len, ":%s", name);
-               break;
-           }
-           case hf_uuid:
-           {
-               char uuid[VIR_UUID_STRING_BUFLEN];
-               if (virDomainGetUUIDString (dom, uuid) == 0) {
-                   uuid[sizeof (uuid) - 1] = '\0';
-                   status = snprintf (host_ptr, host_len, ":%s", uuid);
-               }
-               break;
-           }
-       } /* switch (hostname_format[i]) */
-
-       /* If status >= host_len
-        * => the buffer is full, there's no null-byte at the end and
-        *    continuing with this loop doesn't make any sense. */
-       if (status >= host_len) {
-           host_len = 0;
-           host_ptr = NULL;
-       }
-       /* else: Test if anything was added to the buffer */
-       else if (status > 0) {
-           host_len -= status;
-           host_ptr += status;
-       }
-
-       if (host_len <= 0)
-           break;
-    } /* for (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 */
@@ -723,7 +720,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
@@ -740,10 +739,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
@@ -761,10 +760,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