2 * collectd - src/libvirt.c
3 * Copyright (C) 2006-2008 Red Hat Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the license is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Richard W.M. Jones <rjones@redhat.com>
25 #include "configfile.h"
26 #include "utils_ignorelist.h"
27 #include "utils_complain.h"
29 #include <libvirt/libvirt.h>
30 #include <libvirt/virterror.h>
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
33 #include <libxml/xpath.h>
35 static const char *config_keys[] = {
50 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
53 static virConnectPtr conn = 0;
54 static char *conn_string = NULL;
55 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
57 /* Seconds between list refreshes, 0 disables completely. */
58 static int interval = 60;
60 /* List of domains, if specified. */
61 static ignorelist_t *il_domains = NULL;
62 /* List of block devices, if specified. */
63 static ignorelist_t *il_block_devices = NULL;
64 /* List of network interface devices, if specified. */
65 static ignorelist_t *il_interface_devices = NULL;
67 static int ignore_device_match (ignorelist_t *,
68 const char *domname, const char *devpath);
70 /* Actual list of domains found on last refresh. */
71 static virDomainPtr *domains = NULL;
72 static int nr_domains = 0;
74 static void free_domains (void);
75 static int add_domain (virDomainPtr dom);
77 /* Actual list of block devices found on last refresh. */
79 virDomainPtr dom; /* domain */
80 char *path; /* name of block device */
83 static struct block_device *block_devices = NULL;
84 static int nr_block_devices = 0;
86 static void free_block_devices (void);
87 static int add_block_device (virDomainPtr dom, const char *path);
89 /* Actual list of network interfaces found on last refresh. */
90 struct interface_device {
91 virDomainPtr dom; /* domain */
92 char *path; /* name of interface device */
93 char *address; /* mac address of interface device */
94 char *number; /* interface device number */
97 static struct interface_device *interface_devices = NULL;
98 static int nr_interface_devices = 0;
100 static void free_interface_devices (void);
101 static int add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number);
103 /* HostnameFormat. */
104 #define HF_MAX_FIELDS 3
113 static enum hf_field hostname_format[HF_MAX_FIELDS] =
116 /* InterfaceFormat. */
123 static enum if_field interface_format = if_name;
125 /* Time that we last refreshed. */
126 static time_t last_refresh = (time_t) 0;
128 static int refresh_lists (void);
130 /* ERROR(...) macro for virterrors. */
131 #define VIRT_ERROR(conn,s) do { \
133 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
134 if (err) ERROR ("%s: %s", (s), err->message); \
138 init_value_list (value_list_t *vl, virDomainPtr dom)
142 char uuid[VIR_UUID_STRING_BUFLEN];
144 sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
148 /* Construct the hostname field according to HostnameFormat. */
149 for (i = 0; i < HF_MAX_FIELDS; ++i) {
150 if (hostname_format[i] == hf_none)
153 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
155 if (i > 0 && n >= 1) {
156 strncat (vl->host, ":", 1);
160 switch (hostname_format[i]) {
163 strncat (vl->host, hostname_g, n);
166 name = virDomainGetName (dom);
168 strncat (vl->host, name, n);
171 if (virDomainGetUUIDString (dom, uuid) == 0)
172 strncat (vl->host, uuid, n);
177 vl->host[sizeof (vl->host) - 1] = '\0';
178 } /* void init_value_list */
181 memory_submit (gauge_t memory, virDomainPtr dom)
184 value_list_t vl = VALUE_LIST_INIT;
186 init_value_list (&vl, dom);
188 values[0].gauge = memory;
193 sstrncpy (vl.type, "memory", sizeof (vl.type));
194 sstrncpy (vl.type_instance, "total", sizeof (vl.type_instance));
196 plugin_dispatch_values (&vl);
200 memory_stats_submit (gauge_t memory, virDomainPtr dom, int tag_index)
202 static const char *tags[] = { "swap_in", "swap_out", "major_fault", "minor_fault",
203 "unused", "available", "actual_balloon", "rss"};
206 value_list_t vl = VALUE_LIST_INIT;
208 init_value_list (&vl, dom);
210 values[0].gauge = memory;
215 sstrncpy (vl.type, "memory", sizeof (vl.type));
216 sstrncpy (vl.type_instance, tags[tag_index], sizeof (vl.type_instance));
218 plugin_dispatch_values (&vl);
222 cpu_submit (unsigned long long cpu_time,
223 virDomainPtr dom, const char *type)
226 value_list_t vl = VALUE_LIST_INIT;
228 init_value_list (&vl, dom);
230 values[0].derive = cpu_time;
235 sstrncpy (vl.type, type, sizeof (vl.type));
237 plugin_dispatch_values (&vl);
241 vcpu_submit (derive_t cpu_time,
242 virDomainPtr dom, int vcpu_nr, const char *type)
245 value_list_t vl = VALUE_LIST_INIT;
247 init_value_list (&vl, dom);
249 values[0].derive = cpu_time;
253 sstrncpy (vl.type, type, sizeof (vl.type));
254 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
256 plugin_dispatch_values (&vl);
260 submit_derive2 (const char *type, derive_t v0, derive_t v1,
261 virDomainPtr dom, const char *devname)
264 value_list_t vl = VALUE_LIST_INIT;
266 init_value_list (&vl, dom);
268 values[0].derive = v0;
269 values[1].derive = v1;
273 sstrncpy (vl.type, type, sizeof (vl.type));
274 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
276 plugin_dispatch_values (&vl);
277 } /* void submit_derive2 */
282 if (virInitialize () != 0)
289 lv_config (const char *key, const char *value)
291 if (virInitialize () != 0)
294 if (il_domains == NULL)
295 il_domains = ignorelist_create (1);
296 if (il_block_devices == NULL)
297 il_block_devices = ignorelist_create (1);
298 if (il_interface_devices == NULL)
299 il_interface_devices = ignorelist_create (1);
301 if (strcasecmp (key, "Connection") == 0) {
302 char *tmp = strdup (value);
304 ERROR ("libvirt plugin: Connection strdup failed.");
312 if (strcasecmp (key, "RefreshInterval") == 0) {
314 interval = strtol (value, &eptr, 10);
315 if (eptr == NULL || *eptr != '\0') return 1;
319 if (strcasecmp (key, "Domain") == 0) {
320 if (ignorelist_add (il_domains, value)) return 1;
323 if (strcasecmp (key, "BlockDevice") == 0) {
324 if (ignorelist_add (il_block_devices, value)) return 1;
327 if (strcasecmp (key, "InterfaceDevice") == 0) {
328 if (ignorelist_add (il_interface_devices, value)) return 1;
332 if (strcasecmp (key, "IgnoreSelected") == 0) {
335 ignorelist_set_invert (il_domains, 0);
336 ignorelist_set_invert (il_block_devices, 0);
337 ignorelist_set_invert (il_interface_devices, 0);
341 ignorelist_set_invert (il_domains, 1);
342 ignorelist_set_invert (il_block_devices, 1);
343 ignorelist_set_invert (il_interface_devices, 1);
348 if (strcasecmp (key, "HostnameFormat") == 0) {
350 char *fields[HF_MAX_FIELDS];
353 value_copy = strdup (value);
354 if (value_copy == NULL) {
355 ERROR ("libvirt plugin: strdup failed.");
359 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
362 ERROR ("HostnameFormat: no fields");
366 for (i = 0; i < n; ++i) {
367 if (strcasecmp (fields[i], "hostname") == 0)
368 hostname_format[i] = hf_hostname;
369 else if (strcasecmp (fields[i], "name") == 0)
370 hostname_format[i] = hf_name;
371 else if (strcasecmp (fields[i], "uuid") == 0)
372 hostname_format[i] = hf_uuid;
375 ERROR ("unknown HostnameFormat field: %s", fields[i]);
381 for (i = n; i < HF_MAX_FIELDS; ++i)
382 hostname_format[i] = hf_none;
387 if (strcasecmp (key, "InterfaceFormat") == 0) {
388 if (strcasecmp (value, "name") == 0)
389 interface_format = if_name;
390 else if (strcasecmp (value, "address") == 0)
391 interface_format = if_address;
392 else if (strcasecmp (value, "number") == 0)
393 interface_format = if_number;
395 ERROR ("unknown InterfaceFormat: %s", value);
401 /* Unrecognised option. */
412 /* `conn_string == NULL' is acceptable. */
413 conn = virConnectOpenReadOnly (conn_string);
415 c_complain (LOG_ERR, &conn_complain,
416 "libvirt plugin: Unable to connect: "
417 "virConnectOpenReadOnly failed.");
421 c_release (LOG_NOTICE, &conn_complain,
422 "libvirt plugin: Connection established.");
426 /* Need to refresh domain or device lists? */
427 if ((last_refresh == (time_t) 0) ||
428 ((interval > 0) && ((last_refresh + interval) <= t))) {
429 if (refresh_lists () != 0) {
431 virConnectClose (conn);
439 for (i = 0; i < nr_domains; ++i)
440 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
441 for (i = 0; i < nr_block_devices; ++i)
442 fprintf (stderr, "block device %d %s:%s\n",
443 i, virDomainGetName (block_devices[i].dom),
444 block_devices[i].path);
445 for (i = 0; i < nr_interface_devices; ++i)
446 fprintf (stderr, "interface device %d %s:%s\n",
447 i, virDomainGetName (interface_devices[i].dom),
448 interface_devices[i].path);
451 /* Get CPU usage, memory, VCPU usage for each domain. */
452 for (i = 0; i < nr_domains; ++i) {
454 virVcpuInfoPtr vinfo = NULL;
455 virDomainMemoryStatPtr minfo = NULL;
459 status = virDomainGetInfo (domains[i], &info);
462 ERROR ("libvirt plugin: virDomainGetInfo failed with status %i.",
467 cpu_submit (info.cpuTime, domains[i], "virt_cpu_total");
468 memory_submit ((gauge_t) info.memory * 1024, domains[i]);
470 vinfo = malloc (info.nrVirtCpu * sizeof (vinfo[0]));
472 ERROR ("libvirt plugin: malloc failed.");
476 status = virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
477 /* cpu map = */ NULL, /* cpu map length = */ 0);
480 ERROR ("libvirt plugin: virDomainGetVcpus failed with status %i.",
486 for (j = 0; j < info.nrVirtCpu; ++j)
487 vcpu_submit (vinfo[j].cpuTime,
488 domains[i], vinfo[j].number, "virt_vcpu");
491 minfo = malloc (VIR_DOMAIN_MEMORY_STAT_NR * sizeof (virDomainMemoryStatStruct));
493 ERROR ("libvirt plugin: malloc failed.");
497 status = virDomainMemoryStats (domains[i], minfo, VIR_DOMAIN_MEMORY_STAT_NR, 0);
500 ERROR ("libvirt plugin: virDomainMemoryStats failed with status %i.",
505 for (j = 0; j < status; j++) {
506 memory_stats_submit ((gauge_t) minfo[j].val, domains[i], minfo[j].tag);
514 /* Get block device stats for each domain. */
515 for (i = 0; i < nr_block_devices; ++i) {
516 struct _virDomainBlockStats stats;
518 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
519 &stats, sizeof stats) != 0)
522 if ((stats.rd_req != -1) && (stats.wr_req != -1))
523 submit_derive2 ("disk_ops",
524 (derive_t) stats.rd_req, (derive_t) stats.wr_req,
525 block_devices[i].dom, block_devices[i].path);
527 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
528 submit_derive2 ("disk_octets",
529 (derive_t) stats.rd_bytes, (derive_t) stats.wr_bytes,
530 block_devices[i].dom, block_devices[i].path);
531 } /* for (nr_block_devices) */
533 /* Get interface stats for each domain. */
534 for (i = 0; i < nr_interface_devices; ++i) {
535 struct _virDomainInterfaceStats stats;
536 char *display_name = NULL;
539 switch (interface_format) {
541 display_name = interface_devices[i].address;
544 display_name = interface_devices[i].number;
548 display_name = interface_devices[i].path;
551 if (virDomainInterfaceStats (interface_devices[i].dom,
552 interface_devices[i].path,
553 &stats, sizeof stats) != 0)
556 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
557 submit_derive2 ("if_octets",
558 (derive_t) stats.rx_bytes, (derive_t) stats.tx_bytes,
559 interface_devices[i].dom, display_name);
561 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
562 submit_derive2 ("if_packets",
563 (derive_t) stats.rx_packets, (derive_t) stats.tx_packets,
564 interface_devices[i].dom, display_name);
566 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
567 submit_derive2 ("if_errors",
568 (derive_t) stats.rx_errs, (derive_t) stats.tx_errs,
569 interface_devices[i].dom, display_name);
571 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
572 submit_derive2 ("if_dropped",
573 (derive_t) stats.rx_drop, (derive_t) stats.tx_drop,
574 interface_devices[i].dom, display_name);
575 } /* for (nr_interface_devices) */
585 n = virConnectNumOfDomains (conn);
587 VIRT_ERROR (conn, "reading number of domains");
595 /* Get list of domains. */
596 domids = malloc (sizeof (int) * n);
598 ERROR ("libvirt plugin: malloc failed.");
602 n = virConnectListDomains (conn, domids, n);
604 VIRT_ERROR (conn, "reading list of domains");
609 free_block_devices ();
610 free_interface_devices ();
613 /* Fetch each domain and add it to the list, unless ignore. */
614 for (i = 0; i < n; ++i) {
615 virDomainPtr dom = NULL;
618 xmlDocPtr xml_doc = NULL;
619 xmlXPathContextPtr xpath_ctx = NULL;
620 xmlXPathObjectPtr xpath_obj = NULL;
623 dom = virDomainLookupByID (conn, domids[i]);
625 VIRT_ERROR (conn, "virDomainLookupByID");
626 /* Could be that the domain went away -- ignore it anyway. */
630 name = virDomainGetName (dom);
632 VIRT_ERROR (conn, "virDomainGetName");
636 if (il_domains && ignorelist_match (il_domains, name) != 0)
639 if (add_domain (dom) < 0) {
640 ERROR ("libvirt plugin: malloc failed.");
644 /* Get a list of devices for this domain. */
645 xml = virDomainGetXMLDesc (dom, 0);
647 VIRT_ERROR (conn, "virDomainGetXMLDesc");
651 /* Yuck, XML. Parse out the devices. */
652 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
653 if (xml_doc == NULL) {
654 VIRT_ERROR (conn, "xmlReadDoc");
658 xpath_ctx = xmlXPathNewContext (xml_doc);
661 xpath_obj = xmlXPathEval
662 ((xmlChar *) "/domain/devices/disk/target[@dev]",
664 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
665 xpath_obj->nodesetval == NULL)
668 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
672 node = xpath_obj->nodesetval->nodeTab[j];
674 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
677 if (il_block_devices &&
678 ignore_device_match (il_block_devices, name, path) != 0)
681 add_block_device (dom, path);
683 if (path) xmlFree (path);
685 xmlXPathFreeObject (xpath_obj);
687 /* Network interfaces. */
688 xpath_obj = xmlXPathEval
689 ((xmlChar *) "/domain/devices/interface[target[@dev]]",
691 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
692 xpath_obj->nodesetval == NULL)
695 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
697 for (j = 0; j < xml_interfaces->nodeNr; ++j) {
699 char *address = NULL;
700 xmlNodePtr xml_interface;
702 xml_interface = xml_interfaces->nodeTab[j];
703 if (!xml_interface) continue;
704 xmlNodePtr child = NULL;
706 for (child = xml_interface->children; child; child = child->next) {
707 if (child->type != XML_ELEMENT_NODE) continue;
709 if (xmlStrEqual(child->name, (const xmlChar *) "target")) {
710 path = (char *) xmlGetProp (child, (const xmlChar *) "dev");
712 } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) {
713 address = (char *) xmlGetProp (child, (const xmlChar *) "address");
714 if (!address) continue;
718 if (il_interface_devices &&
719 (ignore_device_match (il_interface_devices, name, path) != 0 ||
720 ignore_device_match (il_interface_devices, name, address) != 0))
723 add_interface_device (dom, path, address, j+1);
725 if (path) xmlFree (path);
726 if (address) xmlFree (address);
730 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
731 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
732 if (xml_doc) xmlFreeDoc (xml_doc);
748 for (i = 0; i < nr_domains; ++i)
749 virDomainFree (domains[i]);
757 add_domain (virDomainPtr dom)
759 virDomainPtr *new_ptr;
760 int new_size = sizeof (domains[0]) * (nr_domains+1);
763 new_ptr = realloc (domains, new_size);
765 new_ptr = malloc (new_size);
771 domains[nr_domains] = dom;
776 free_block_devices ()
781 for (i = 0; i < nr_block_devices; ++i)
782 sfree (block_devices[i].path);
783 sfree (block_devices);
785 block_devices = NULL;
786 nr_block_devices = 0;
790 add_block_device (virDomainPtr dom, const char *path)
792 struct block_device *new_ptr;
793 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
796 path_copy = strdup (path);
801 new_ptr = realloc (block_devices, new_size);
803 new_ptr = malloc (new_size);
805 if (new_ptr == NULL) {
809 block_devices = new_ptr;
810 block_devices[nr_block_devices].dom = dom;
811 block_devices[nr_block_devices].path = path_copy;
812 return nr_block_devices++;
816 free_interface_devices ()
820 if (interface_devices) {
821 for (i = 0; i < nr_interface_devices; ++i) {
822 sfree (interface_devices[i].path);
823 sfree (interface_devices[i].address);
824 sfree (interface_devices[i].number);
826 sfree (interface_devices);
828 interface_devices = NULL;
829 nr_interface_devices = 0;
833 add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number)
835 struct interface_device *new_ptr;
836 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
837 char *path_copy, *address_copy, number_string[15];
839 path_copy = strdup (path);
840 if (!path_copy) return -1;
842 address_copy = strdup (address);
848 snprintf(number_string, sizeof (number_string), "interface-%u", number);
850 if (interface_devices)
851 new_ptr = realloc (interface_devices, new_size);
853 new_ptr = malloc (new_size);
855 if (new_ptr == NULL) {
857 sfree (address_copy);
860 interface_devices = new_ptr;
861 interface_devices[nr_interface_devices].dom = dom;
862 interface_devices[nr_interface_devices].path = path_copy;
863 interface_devices[nr_interface_devices].address = address_copy;
864 interface_devices[nr_interface_devices].number = strdup(number_string);
865 return nr_interface_devices++;
869 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
874 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
877 ERROR ("libvirt plugin: malloc failed.");
880 ssnprintf (name, n, "%s:%s", domname, devpath);
881 r = ignorelist_match (il, name);
889 free_block_devices ();
890 free_interface_devices ();
894 virConnectClose (conn);
897 ignorelist_free (il_domains);
899 ignorelist_free (il_block_devices);
900 il_block_devices = NULL;
901 ignorelist_free (il_interface_devices);
902 il_interface_devices = NULL;
908 module_register (void)
910 plugin_register_config ("libvirt",
912 config_keys, NR_CONFIG_KEYS);
913 plugin_register_init ("libvirt", lv_init);
914 plugin_register_read ("libvirt", lv_read);
915 plugin_register_shutdown ("libvirt", lv_shutdown);
919 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker