perl plugin: Removed commented code
[collectd.git] / src / libvirt.c
index 755d624..4cbe38d 100644 (file)
@@ -141,8 +141,6 @@ init_value_list (value_list_t *vl, virDomainPtr dom)
     const char *name;
     char uuid[VIR_UUID_STRING_BUFLEN];
 
-    vl->interval = interval_g;
-
     sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
 
     vl->host[0] = '\0';
@@ -180,6 +178,25 @@ init_value_list (value_list_t *vl, virDomainPtr dom)
 } /* 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)
 {
@@ -242,8 +259,8 @@ lv_init (void)
 {
     if (virInitialize () != 0)
         return -1;
-
-       return 0;
+    else
+        return 0;
 }
 
 static int
@@ -409,7 +426,7 @@ 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;
@@ -424,7 +441,14 @@ lv_read (void)
             continue;
         }
 
+        if (info.state != VIR_DOMAIN_RUNNING)
+        {
+            /* only gather stats for running domains */
+            continue;
+        }
+
         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]));
         if (vinfo == NULL) {
@@ -658,7 +682,7 @@ refresh_lists (void)
                      ignore_device_match (il_interface_devices, name, address) != 0))
                     goto cont3;
 
-               add_interface_device (dom, path, address, j+1);
+                add_interface_device (dom, path, address, j+1);
                 cont3:
                     if (path) xmlFree (path);
                     if (address) xmlFree (address);
@@ -772,17 +796,21 @@ add_interface_device (virDomainPtr dom, const char *path, const char *address, u
 {
     struct interface_device *new_ptr;
     int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
-    char *path_copy, *address_copy, *number_string;
+    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;
+    }
 
-    number_string = (char*) malloc (15);
-    if (!number_string) return -1;
-    snprintf(number_string, 15, "interface-%u", number);
+    snprintf(number_string, sizeof (number_string), "interface-%u", number);
 
     if (interface_devices)
         new_ptr = realloc (interface_devices, new_size);
@@ -798,7 +826,7 @@ add_interface_device (virDomainPtr dom, const char *path, const char *address, u
     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 = number_string;
+    interface_devices[nr_interface_devices].number = strdup(number_string);
     return nr_interface_devices++;
 }
 
@@ -808,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) {
@@ -828,7 +859,7 @@ lv_shutdown (void)
     free_domains ();
 
     if (conn != NULL)
-       virConnectClose (conn);
+        virConnectClose (conn);
     conn = NULL;
 
     ignorelist_free (il_domains);
@@ -845,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);