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[] = {
48 "PluginInstanceFormat",
52 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
55 static virConnectPtr conn = 0;
56 static char *conn_string = NULL;
57 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
59 /* Seconds between list refreshes, 0 disables completely. */
60 static int interval = 60;
62 /* List of domains, if specified. */
63 static ignorelist_t *il_domains = NULL;
64 /* List of block devices, if specified. */
65 static ignorelist_t *il_block_devices = NULL;
66 /* List of network interface devices, if specified. */
67 static ignorelist_t *il_interface_devices = NULL;
69 static int ignore_device_match (ignorelist_t *,
70 const char *domname, const char *devpath);
72 /* Actual list of domains found on last refresh. */
73 static virDomainPtr *domains = NULL;
74 static int nr_domains = 0;
76 static void free_domains (void);
77 static int add_domain (virDomainPtr dom);
79 /* Actual list of block devices found on last refresh. */
81 virDomainPtr dom; /* domain */
82 char *path; /* name of block device */
85 static struct block_device *block_devices = NULL;
86 static int nr_block_devices = 0;
88 static void free_block_devices (void);
89 static int add_block_device (virDomainPtr dom, const char *path);
91 /* Actual list of network interfaces found on last refresh. */
92 struct interface_device {
93 virDomainPtr dom; /* domain */
94 char *path; /* name of interface device */
95 char *address; /* mac address of interface device */
96 char *number; /* interface device number */
99 static struct interface_device *interface_devices = NULL;
100 static int nr_interface_devices = 0;
102 static void free_interface_devices (void);
103 static int add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number);
105 /* HostnameFormat. */
106 #define HF_MAX_FIELDS 3
115 static enum hf_field hostname_format[HF_MAX_FIELDS] =
118 /* PluginInstanceFormat */
119 #define PLGINST_MAX_FIELDS 2
127 static enum plginst_field plugin_instance_format[PLGINST_MAX_FIELDS] =
130 /* InterfaceFormat. */
137 static enum if_field interface_format = if_name;
139 /* Time that we last refreshed. */
140 static time_t last_refresh = (time_t) 0;
142 static int refresh_lists (void);
144 /* ERROR(...) macro for virterrors. */
145 #define VIRT_ERROR(conn,s) do { \
147 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
148 if (err) ERROR ("%s: %s", (s), err->message); \
152 init_value_list (value_list_t *vl, virDomainPtr dom)
156 char uuid[VIR_UUID_STRING_BUFLEN];
158 sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
162 /* Construct the hostname field according to HostnameFormat. */
163 for (i = 0; i < HF_MAX_FIELDS; ++i) {
164 if (hostname_format[i] == hf_none)
167 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
169 if (i > 0 && n >= 1) {
170 strncat (vl->host, ":", 1);
174 switch (hostname_format[i]) {
177 strncat (vl->host, hostname_g, n);
180 name = virDomainGetName (dom);
182 strncat (vl->host, name, n);
185 if (virDomainGetUUIDString (dom, uuid) == 0)
186 strncat (vl->host, uuid, n);
191 vl->host[sizeof (vl->host) - 1] = '\0';
193 /* Construct the plugin instance field according to PluginInstanceFormat. */
194 for (i = 0; i < PLGINST_MAX_FIELDS; ++i) {
195 if (plugin_instance_format[i] == plginst_none)
198 n = DATA_MAX_NAME_LEN - strlen (vl->plugin_instance) - 2;
200 if (i > 0 && n >= 1) {
201 strncat (vl->plugin_instance, ":", 1);
205 switch (plugin_instance_format[i]) {
206 case plginst_none: break;
208 name = virDomainGetName (dom);
210 strncat (vl->plugin_instance, name, n);
213 if (virDomainGetUUIDString (dom, uuid) == 0)
214 strncat (vl->plugin_instance, uuid, n);
219 vl->plugin_instance[sizeof (vl->plugin_instance) - 1] = '\0';
221 } /* void init_value_list */
224 memory_submit (gauge_t memory, virDomainPtr dom)
227 value_list_t vl = VALUE_LIST_INIT;
229 init_value_list (&vl, dom);
231 values[0].gauge = memory;
236 sstrncpy (vl.type, "memory", sizeof (vl.type));
237 sstrncpy (vl.type_instance, "total", sizeof (vl.type_instance));
239 plugin_dispatch_values (&vl);
243 cpu_submit (unsigned long long cpu_time,
244 virDomainPtr dom, const char *type)
247 value_list_t vl = VALUE_LIST_INIT;
249 init_value_list (&vl, dom);
251 values[0].derive = cpu_time;
256 sstrncpy (vl.type, type, sizeof (vl.type));
258 plugin_dispatch_values (&vl);
262 vcpu_submit (derive_t cpu_time,
263 virDomainPtr dom, int vcpu_nr, const char *type)
266 value_list_t vl = VALUE_LIST_INIT;
268 init_value_list (&vl, dom);
270 values[0].derive = cpu_time;
274 sstrncpy (vl.type, type, sizeof (vl.type));
275 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
277 plugin_dispatch_values (&vl);
281 submit_derive2 (const char *type, derive_t v0, derive_t v1,
282 virDomainPtr dom, const char *devname)
285 value_list_t vl = VALUE_LIST_INIT;
287 init_value_list (&vl, dom);
289 values[0].derive = v0;
290 values[1].derive = v1;
294 sstrncpy (vl.type, type, sizeof (vl.type));
295 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
297 plugin_dispatch_values (&vl);
298 } /* void submit_derive2 */
303 if (virInitialize () != 0)
310 lv_config (const char *key, const char *value)
312 if (virInitialize () != 0)
315 if (il_domains == NULL)
316 il_domains = ignorelist_create (1);
317 if (il_block_devices == NULL)
318 il_block_devices = ignorelist_create (1);
319 if (il_interface_devices == NULL)
320 il_interface_devices = ignorelist_create (1);
322 if (strcasecmp (key, "Connection") == 0) {
323 char *tmp = strdup (value);
325 ERROR ("libvirt plugin: Connection strdup failed.");
333 if (strcasecmp (key, "RefreshInterval") == 0) {
335 interval = strtol (value, &eptr, 10);
336 if (eptr == NULL || *eptr != '\0') return 1;
340 if (strcasecmp (key, "Domain") == 0) {
341 if (ignorelist_add (il_domains, value)) return 1;
344 if (strcasecmp (key, "BlockDevice") == 0) {
345 if (ignorelist_add (il_block_devices, value)) return 1;
348 if (strcasecmp (key, "InterfaceDevice") == 0) {
349 if (ignorelist_add (il_interface_devices, value)) return 1;
353 if (strcasecmp (key, "IgnoreSelected") == 0) {
356 ignorelist_set_invert (il_domains, 0);
357 ignorelist_set_invert (il_block_devices, 0);
358 ignorelist_set_invert (il_interface_devices, 0);
362 ignorelist_set_invert (il_domains, 1);
363 ignorelist_set_invert (il_block_devices, 1);
364 ignorelist_set_invert (il_interface_devices, 1);
369 if (strcasecmp (key, "HostnameFormat") == 0) {
371 char *fields[HF_MAX_FIELDS];
374 value_copy = strdup (value);
375 if (value_copy == NULL) {
376 ERROR ("libvirt plugin: strdup failed.");
380 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
383 ERROR ("HostnameFormat: no fields");
387 for (i = 0; i < n; ++i) {
388 if (strcasecmp (fields[i], "hostname") == 0)
389 hostname_format[i] = hf_hostname;
390 else if (strcasecmp (fields[i], "name") == 0)
391 hostname_format[i] = hf_name;
392 else if (strcasecmp (fields[i], "uuid") == 0)
393 hostname_format[i] = hf_uuid;
396 ERROR ("unknown HostnameFormat field: %s", fields[i]);
402 for (i = n; i < HF_MAX_FIELDS; ++i)
403 hostname_format[i] = hf_none;
408 if (strcasecmp (key, "PluginInstanceFormat") == 0) {
410 char *fields[PLGINST_MAX_FIELDS];
413 value_copy = strdup (value);
414 if (value_copy == NULL) {
415 ERROR ("libvirt plugin: strdup failed.");
419 n = strsplit (value_copy, fields, PLGINST_MAX_FIELDS);
422 ERROR ("PluginInstanceFormat: no fields");
426 for (i = 0; i < n; ++i) {
427 if (strcasecmp (fields[i], "name") == 0)
428 plugin_instance_format[i] = plginst_name;
429 else if (strcasecmp (fields[i], "uuid") == 0)
430 plugin_instance_format[i] = plginst_uuid;
433 ERROR ("unknown HostnameFormat field: %s", fields[i]);
439 for (i = n; i < PLGINST_MAX_FIELDS; ++i)
440 plugin_instance_format[i] = plginst_none;
445 if (strcasecmp (key, "InterfaceFormat") == 0) {
446 if (strcasecmp (value, "name") == 0)
447 interface_format = if_name;
448 else if (strcasecmp (value, "address") == 0)
449 interface_format = if_address;
450 else if (strcasecmp (value, "number") == 0)
451 interface_format = if_number;
453 ERROR ("unknown InterfaceFormat: %s", value);
459 /* Unrecognised option. */
470 /* `conn_string == NULL' is acceptable. */
471 conn = virConnectOpenReadOnly (conn_string);
473 c_complain (LOG_ERR, &conn_complain,
474 "libvirt plugin: Unable to connect: "
475 "virConnectOpenReadOnly failed.");
479 c_release (LOG_NOTICE, &conn_complain,
480 "libvirt plugin: Connection established.");
484 /* Need to refresh domain or device lists? */
485 if ((last_refresh == (time_t) 0) ||
486 ((interval > 0) && ((last_refresh + interval) <= t))) {
487 if (refresh_lists () != 0) {
489 virConnectClose (conn);
497 for (i = 0; i < nr_domains; ++i)
498 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
499 for (i = 0; i < nr_block_devices; ++i)
500 fprintf (stderr, "block device %d %s:%s\n",
501 i, virDomainGetName (block_devices[i].dom),
502 block_devices[i].path);
503 for (i = 0; i < nr_interface_devices; ++i)
504 fprintf (stderr, "interface device %d %s:%s\n",
505 i, virDomainGetName (interface_devices[i].dom),
506 interface_devices[i].path);
509 /* Get CPU usage, memory, VCPU usage for each domain. */
510 for (i = 0; i < nr_domains; ++i) {
512 virVcpuInfoPtr vinfo = NULL;
516 status = virDomainGetInfo (domains[i], &info);
519 ERROR ("libvirt plugin: virDomainGetInfo failed with status %i.",
524 cpu_submit (info.cpuTime, domains[i], "virt_cpu_total");
525 memory_submit ((gauge_t) info.memory * 1024, domains[i]);
527 vinfo = malloc (info.nrVirtCpu * sizeof (vinfo[0]));
529 ERROR ("libvirt plugin: malloc failed.");
533 status = virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
534 /* cpu map = */ NULL, /* cpu map length = */ 0);
537 ERROR ("libvirt plugin: virDomainGetVcpus failed with status %i.",
543 for (j = 0; j < info.nrVirtCpu; ++j)
544 vcpu_submit (vinfo[j].cpuTime,
545 domains[i], vinfo[j].number, "virt_vcpu");
550 /* Get block device stats for each domain. */
551 for (i = 0; i < nr_block_devices; ++i) {
552 struct _virDomainBlockStats stats;
554 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
555 &stats, sizeof stats) != 0)
558 if ((stats.rd_req != -1) && (stats.wr_req != -1))
559 submit_derive2 ("disk_ops",
560 (derive_t) stats.rd_req, (derive_t) stats.wr_req,
561 block_devices[i].dom, block_devices[i].path);
563 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
564 submit_derive2 ("disk_octets",
565 (derive_t) stats.rd_bytes, (derive_t) stats.wr_bytes,
566 block_devices[i].dom, block_devices[i].path);
567 } /* for (nr_block_devices) */
569 /* Get interface stats for each domain. */
570 for (i = 0; i < nr_interface_devices; ++i) {
571 struct _virDomainInterfaceStats stats;
572 char *display_name = NULL;
575 switch (interface_format) {
577 display_name = interface_devices[i].address;
580 display_name = interface_devices[i].number;
584 display_name = interface_devices[i].path;
587 if (virDomainInterfaceStats (interface_devices[i].dom,
588 interface_devices[i].path,
589 &stats, sizeof stats) != 0)
592 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
593 submit_derive2 ("if_octets",
594 (derive_t) stats.rx_bytes, (derive_t) stats.tx_bytes,
595 interface_devices[i].dom, display_name);
597 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
598 submit_derive2 ("if_packets",
599 (derive_t) stats.rx_packets, (derive_t) stats.tx_packets,
600 interface_devices[i].dom, display_name);
602 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
603 submit_derive2 ("if_errors",
604 (derive_t) stats.rx_errs, (derive_t) stats.tx_errs,
605 interface_devices[i].dom, display_name);
607 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
608 submit_derive2 ("if_dropped",
609 (derive_t) stats.rx_drop, (derive_t) stats.tx_drop,
610 interface_devices[i].dom, display_name);
611 } /* for (nr_interface_devices) */
621 n = virConnectNumOfDomains (conn);
623 VIRT_ERROR (conn, "reading number of domains");
631 /* Get list of domains. */
632 domids = malloc (sizeof (int) * n);
634 ERROR ("libvirt plugin: malloc failed.");
638 n = virConnectListDomains (conn, domids, n);
640 VIRT_ERROR (conn, "reading list of domains");
645 free_block_devices ();
646 free_interface_devices ();
649 /* Fetch each domain and add it to the list, unless ignore. */
650 for (i = 0; i < n; ++i) {
651 virDomainPtr dom = NULL;
654 xmlDocPtr xml_doc = NULL;
655 xmlXPathContextPtr xpath_ctx = NULL;
656 xmlXPathObjectPtr xpath_obj = NULL;
659 dom = virDomainLookupByID (conn, domids[i]);
661 VIRT_ERROR (conn, "virDomainLookupByID");
662 /* Could be that the domain went away -- ignore it anyway. */
666 name = virDomainGetName (dom);
668 VIRT_ERROR (conn, "virDomainGetName");
672 if (il_domains && ignorelist_match (il_domains, name) != 0)
675 if (add_domain (dom) < 0) {
676 ERROR ("libvirt plugin: malloc failed.");
680 /* Get a list of devices for this domain. */
681 xml = virDomainGetXMLDesc (dom, 0);
683 VIRT_ERROR (conn, "virDomainGetXMLDesc");
687 /* Yuck, XML. Parse out the devices. */
688 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
689 if (xml_doc == NULL) {
690 VIRT_ERROR (conn, "xmlReadDoc");
694 xpath_ctx = xmlXPathNewContext (xml_doc);
697 xpath_obj = xmlXPathEval
698 ((xmlChar *) "/domain/devices/disk/target[@dev]",
700 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
701 xpath_obj->nodesetval == NULL)
704 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
708 node = xpath_obj->nodesetval->nodeTab[j];
710 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
713 if (il_block_devices &&
714 ignore_device_match (il_block_devices, name, path) != 0)
717 add_block_device (dom, path);
719 if (path) xmlFree (path);
721 xmlXPathFreeObject (xpath_obj);
723 /* Network interfaces. */
724 xpath_obj = xmlXPathEval
725 ((xmlChar *) "/domain/devices/interface[target[@dev]]",
727 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
728 xpath_obj->nodesetval == NULL)
731 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
733 for (j = 0; j < xml_interfaces->nodeNr; ++j) {
735 char *address = NULL;
736 xmlNodePtr xml_interface;
738 xml_interface = xml_interfaces->nodeTab[j];
739 if (!xml_interface) continue;
740 xmlNodePtr child = NULL;
742 for (child = xml_interface->children; child; child = child->next) {
743 if (child->type != XML_ELEMENT_NODE) continue;
745 if (xmlStrEqual(child->name, (const xmlChar *) "target")) {
746 path = (char *) xmlGetProp (child, (const xmlChar *) "dev");
748 } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) {
749 address = (char *) xmlGetProp (child, (const xmlChar *) "address");
750 if (!address) continue;
754 if (il_interface_devices &&
755 (ignore_device_match (il_interface_devices, name, path) != 0 ||
756 ignore_device_match (il_interface_devices, name, address) != 0))
759 add_interface_device (dom, path, address, j+1);
761 if (path) xmlFree (path);
762 if (address) xmlFree (address);
766 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
767 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
768 if (xml_doc) xmlFreeDoc (xml_doc);
784 for (i = 0; i < nr_domains; ++i)
785 virDomainFree (domains[i]);
793 add_domain (virDomainPtr dom)
795 virDomainPtr *new_ptr;
796 int new_size = sizeof (domains[0]) * (nr_domains+1);
799 new_ptr = realloc (domains, new_size);
801 new_ptr = malloc (new_size);
807 domains[nr_domains] = dom;
812 free_block_devices ()
817 for (i = 0; i < nr_block_devices; ++i)
818 sfree (block_devices[i].path);
819 sfree (block_devices);
821 block_devices = NULL;
822 nr_block_devices = 0;
826 add_block_device (virDomainPtr dom, const char *path)
828 struct block_device *new_ptr;
829 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
832 path_copy = strdup (path);
837 new_ptr = realloc (block_devices, new_size);
839 new_ptr = malloc (new_size);
841 if (new_ptr == NULL) {
845 block_devices = new_ptr;
846 block_devices[nr_block_devices].dom = dom;
847 block_devices[nr_block_devices].path = path_copy;
848 return nr_block_devices++;
852 free_interface_devices ()
856 if (interface_devices) {
857 for (i = 0; i < nr_interface_devices; ++i) {
858 sfree (interface_devices[i].path);
859 sfree (interface_devices[i].address);
860 sfree (interface_devices[i].number);
862 sfree (interface_devices);
864 interface_devices = NULL;
865 nr_interface_devices = 0;
869 add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number)
871 struct interface_device *new_ptr;
872 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
873 char *path_copy, *address_copy, number_string[15];
875 path_copy = strdup (path);
876 if (!path_copy) return -1;
878 address_copy = strdup (address);
884 snprintf(number_string, sizeof (number_string), "interface-%u", number);
886 if (interface_devices)
887 new_ptr = realloc (interface_devices, new_size);
889 new_ptr = malloc (new_size);
891 if (new_ptr == NULL) {
893 sfree (address_copy);
896 interface_devices = new_ptr;
897 interface_devices[nr_interface_devices].dom = dom;
898 interface_devices[nr_interface_devices].path = path_copy;
899 interface_devices[nr_interface_devices].address = address_copy;
900 interface_devices[nr_interface_devices].number = strdup(number_string);
901 return nr_interface_devices++;
905 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
910 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
913 ERROR ("libvirt plugin: malloc failed.");
916 ssnprintf (name, n, "%s:%s", domname, devpath);
917 r = ignorelist_match (il, name);
925 free_block_devices ();
926 free_interface_devices ();
930 virConnectClose (conn);
933 ignorelist_free (il_domains);
935 ignorelist_free (il_block_devices);
936 il_block_devices = NULL;
937 ignorelist_free (il_interface_devices);
938 il_interface_devices = NULL;
944 module_register (void)
946 plugin_register_config ("libvirt",
948 config_keys, NR_CONFIG_KEYS);
949 plugin_register_init ("libvirt", lv_init);
950 plugin_register_read ("libvirt", lv_read);
951 plugin_register_shutdown ("libvirt", lv_shutdown);
955 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker