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 */
96 static struct interface_device *interface_devices = NULL;
97 static int nr_interface_devices = 0;
99 static void free_interface_devices (void);
100 static int add_interface_device (virDomainPtr dom, const char *path, const char *address);
102 /* HostnameFormat. */
103 #define HF_MAX_FIELDS 3
112 static enum hf_field hostname_format[HF_MAX_FIELDS] =
115 /* InterfaceFormat. */
121 static enum if_field interface_format = if_name;
123 /* Time that we last refreshed. */
124 static time_t last_refresh = (time_t) 0;
126 static int refresh_lists (void);
128 /* ERROR(...) macro for virterrors. */
129 #define VIRT_ERROR(conn,s) do { \
131 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
132 if (err) ERROR ("%s: %s", (s), err->message); \
136 init_value_list (value_list_t *vl, virDomainPtr dom)
140 char uuid[VIR_UUID_STRING_BUFLEN];
142 sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
146 /* Construct the hostname field according to HostnameFormat. */
147 for (i = 0; i < HF_MAX_FIELDS; ++i) {
148 if (hostname_format[i] == hf_none)
151 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
153 if (i > 0 && n >= 1) {
154 strncat (vl->host, ":", 1);
158 switch (hostname_format[i]) {
161 strncat (vl->host, hostname_g, n);
164 name = virDomainGetName (dom);
166 strncat (vl->host, name, n);
169 if (virDomainGetUUIDString (dom, uuid) == 0)
170 strncat (vl->host, uuid, n);
175 vl->host[sizeof (vl->host) - 1] = '\0';
176 } /* void init_value_list */
179 cpu_submit (unsigned long long cpu_time,
180 virDomainPtr dom, const char *type)
183 value_list_t vl = VALUE_LIST_INIT;
185 init_value_list (&vl, dom);
187 values[0].derive = cpu_time;
192 sstrncpy (vl.type, type, sizeof (vl.type));
194 plugin_dispatch_values (&vl);
198 vcpu_submit (derive_t cpu_time,
199 virDomainPtr dom, int vcpu_nr, const char *type)
202 value_list_t vl = VALUE_LIST_INIT;
204 init_value_list (&vl, dom);
206 values[0].derive = cpu_time;
210 sstrncpy (vl.type, type, sizeof (vl.type));
211 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
213 plugin_dispatch_values (&vl);
217 submit_derive2 (const char *type, derive_t v0, derive_t v1,
218 virDomainPtr dom, const char *devname)
221 value_list_t vl = VALUE_LIST_INIT;
223 init_value_list (&vl, dom);
225 values[0].derive = v0;
226 values[1].derive = v1;
230 sstrncpy (vl.type, type, sizeof (vl.type));
231 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
233 plugin_dispatch_values (&vl);
234 } /* void submit_derive2 */
239 if (virInitialize () != 0)
246 lv_config (const char *key, const char *value)
248 if (virInitialize () != 0)
251 if (il_domains == NULL)
252 il_domains = ignorelist_create (1);
253 if (il_block_devices == NULL)
254 il_block_devices = ignorelist_create (1);
255 if (il_interface_devices == NULL)
256 il_interface_devices = ignorelist_create (1);
258 if (strcasecmp (key, "Connection") == 0) {
259 char *tmp = strdup (value);
261 ERROR ("libvirt plugin: Connection strdup failed.");
269 if (strcasecmp (key, "RefreshInterval") == 0) {
271 interval = strtol (value, &eptr, 10);
272 if (eptr == NULL || *eptr != '\0') return 1;
276 if (strcasecmp (key, "Domain") == 0) {
277 if (ignorelist_add (il_domains, value)) return 1;
280 if (strcasecmp (key, "BlockDevice") == 0) {
281 if (ignorelist_add (il_block_devices, value)) return 1;
284 if (strcasecmp (key, "InterfaceDevice") == 0) {
285 if (ignorelist_add (il_interface_devices, value)) return 1;
289 if (strcasecmp (key, "IgnoreSelected") == 0) {
292 ignorelist_set_invert (il_domains, 0);
293 ignorelist_set_invert (il_block_devices, 0);
294 ignorelist_set_invert (il_interface_devices, 0);
298 ignorelist_set_invert (il_domains, 1);
299 ignorelist_set_invert (il_block_devices, 1);
300 ignorelist_set_invert (il_interface_devices, 1);
305 if (strcasecmp (key, "HostnameFormat") == 0) {
307 char *fields[HF_MAX_FIELDS];
310 value_copy = strdup (value);
311 if (value_copy == NULL) {
312 ERROR ("libvirt plugin: strdup failed.");
316 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
319 ERROR ("HostnameFormat: no fields");
323 for (i = 0; i < n; ++i) {
324 if (strcasecmp (fields[i], "hostname") == 0)
325 hostname_format[i] = hf_hostname;
326 else if (strcasecmp (fields[i], "name") == 0)
327 hostname_format[i] = hf_name;
328 else if (strcasecmp (fields[i], "uuid") == 0)
329 hostname_format[i] = hf_uuid;
332 ERROR ("unknown HostnameFormat field: %s", fields[i]);
338 for (i = n; i < HF_MAX_FIELDS; ++i)
339 hostname_format[i] = hf_none;
344 if (strcasecmp (key, "InterfaceFormat") == 0) {
345 if (strcasecmp (value, "name") == 0)
346 interface_format = if_name;
347 else if (strcasecmp (value, "address") == 0)
348 interface_format = if_address;
350 ERROR ("unknown InterfaceFormat: %s", value);
356 /* Unrecognised option. */
367 /* `conn_string == NULL' is acceptable. */
368 conn = virConnectOpenReadOnly (conn_string);
370 c_complain (LOG_ERR, &conn_complain,
371 "libvirt plugin: Unable to connect: "
372 "virConnectOpenReadOnly failed.");
376 c_release (LOG_NOTICE, &conn_complain,
377 "libvirt plugin: Connection established.");
381 /* Need to refresh domain or device lists? */
382 if ((last_refresh == (time_t) 0) ||
383 ((interval > 0) && ((last_refresh + interval) <= t))) {
384 if (refresh_lists () != 0) {
386 virConnectClose (conn);
394 for (i = 0; i < nr_domains; ++i)
395 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
396 for (i = 0; i < nr_block_devices; ++i)
397 fprintf (stderr, "block device %d %s:%s\n",
398 i, virDomainGetName (block_devices[i].dom),
399 block_devices[i].path);
400 for (i = 0; i < nr_interface_devices; ++i)
401 fprintf (stderr, "interface device %d %s:%s\n",
402 i, virDomainGetName (interface_devices[i].dom),
403 interface_devices[i].path);
406 /* Get CPU usage, VCPU usage for each domain. */
407 for (i = 0; i < nr_domains; ++i) {
409 virVcpuInfoPtr vinfo = NULL;
413 status = virDomainGetInfo (domains[i], &info);
416 ERROR ("libvirt plugin: virDomainGetInfo failed with status %i.",
421 cpu_submit (info.cpuTime, domains[i], "virt_cpu_total");
423 vinfo = malloc (info.nrVirtCpu * sizeof (vinfo[0]));
425 ERROR ("libvirt plugin: malloc failed.");
429 status = virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
430 /* cpu map = */ NULL, /* cpu map length = */ 0);
433 ERROR ("libvirt plugin: virDomainGetVcpus failed with status %i.",
439 for (j = 0; j < info.nrVirtCpu; ++j)
440 vcpu_submit (vinfo[j].cpuTime,
441 domains[i], vinfo[j].number, "virt_vcpu");
446 /* Get block device stats for each domain. */
447 for (i = 0; i < nr_block_devices; ++i) {
448 struct _virDomainBlockStats stats;
450 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
451 &stats, sizeof stats) != 0)
454 if ((stats.rd_req != -1) && (stats.wr_req != -1))
455 submit_derive2 ("disk_ops",
456 (derive_t) stats.rd_req, (derive_t) stats.wr_req,
457 block_devices[i].dom, block_devices[i].path);
459 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
460 submit_derive2 ("disk_octets",
461 (derive_t) stats.rd_bytes, (derive_t) stats.wr_bytes,
462 block_devices[i].dom, block_devices[i].path);
463 } /* for (nr_block_devices) */
465 /* Get interface stats for each domain. */
466 for (i = 0; i < nr_interface_devices; ++i) {
467 struct _virDomainInterfaceStats stats;
468 char *display_name = interface_devices[i].path;
470 if (interface_format == if_address)
471 display_name = interface_devices[i].address;
473 if (virDomainInterfaceStats (interface_devices[i].dom,
474 interface_devices[i].path,
475 &stats, sizeof stats) != 0)
478 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
479 submit_derive2 ("if_octets",
480 (derive_t) stats.rx_bytes, (derive_t) stats.tx_bytes,
481 interface_devices[i].dom, display_name);
483 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
484 submit_derive2 ("if_packets",
485 (derive_t) stats.rx_packets, (derive_t) stats.tx_packets,
486 interface_devices[i].dom, display_name);
488 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
489 submit_derive2 ("if_errors",
490 (derive_t) stats.rx_errs, (derive_t) stats.tx_errs,
491 interface_devices[i].dom, display_name);
493 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
494 submit_derive2 ("if_dropped",
495 (derive_t) stats.rx_drop, (derive_t) stats.tx_drop,
496 interface_devices[i].dom, display_name);
497 } /* for (nr_interface_devices) */
507 n = virConnectNumOfDomains (conn);
509 VIRT_ERROR (conn, "reading number of domains");
517 /* Get list of domains. */
518 domids = malloc (sizeof (int) * n);
520 ERROR ("libvirt plugin: malloc failed.");
524 n = virConnectListDomains (conn, domids, n);
526 VIRT_ERROR (conn, "reading list of domains");
531 free_block_devices ();
532 free_interface_devices ();
535 /* Fetch each domain and add it to the list, unless ignore. */
536 for (i = 0; i < n; ++i) {
537 virDomainPtr dom = NULL;
540 xmlDocPtr xml_doc = NULL;
541 xmlXPathContextPtr xpath_ctx = NULL;
542 xmlXPathObjectPtr xpath_obj = NULL;
545 dom = virDomainLookupByID (conn, domids[i]);
547 VIRT_ERROR (conn, "virDomainLookupByID");
548 /* Could be that the domain went away -- ignore it anyway. */
552 name = virDomainGetName (dom);
554 VIRT_ERROR (conn, "virDomainGetName");
558 if (il_domains && ignorelist_match (il_domains, name) != 0)
561 if (add_domain (dom) < 0) {
562 ERROR ("libvirt plugin: malloc failed.");
566 /* Get a list of devices for this domain. */
567 xml = virDomainGetXMLDesc (dom, 0);
569 VIRT_ERROR (conn, "virDomainGetXMLDesc");
573 /* Yuck, XML. Parse out the devices. */
574 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
575 if (xml_doc == NULL) {
576 VIRT_ERROR (conn, "xmlReadDoc");
580 xpath_ctx = xmlXPathNewContext (xml_doc);
583 xpath_obj = xmlXPathEval
584 ((xmlChar *) "/domain/devices/disk/target[@dev]",
586 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
587 xpath_obj->nodesetval == NULL)
590 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
594 node = xpath_obj->nodesetval->nodeTab[j];
596 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
599 if (il_block_devices &&
600 ignore_device_match (il_block_devices, name, path) != 0)
603 add_block_device (dom, path);
605 if (path) xmlFree (path);
607 xmlXPathFreeObject (xpath_obj);
609 /* Network interfaces. */
610 xpath_obj = xmlXPathEval
611 ((xmlChar *) "/domain/devices/interface[target[@dev]]",
613 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
614 xpath_obj->nodesetval == NULL)
617 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
619 for (j = 0; j < xml_interfaces->nodeNr; ++j) {
621 char *address = NULL;
622 xmlNodePtr xml_interface;
624 xml_interface = xml_interfaces->nodeTab[j];
625 if (!xml_interface) continue;
626 xmlNodePtr child = NULL;
628 for (child = xml_interface->children; child; child = child->next) {
629 if (child->type != XML_ELEMENT_NODE) continue;
631 if (xmlStrEqual(child->name, (const xmlChar *) "target")) {
632 path = (char *) xmlGetProp (child, (const xmlChar *) "dev");
634 } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) {
635 address = (char *) xmlGetProp (child, (const xmlChar *) "address");
636 if (!address) continue;
640 if (il_interface_devices &&
641 (ignore_device_match (il_interface_devices, name, path) != 0 ||
642 ignore_device_match (il_interface_devices, name, address) != 0))
645 add_interface_device (dom, path, address);
647 if (path) xmlFree (path);
648 if (address) xmlFree (address);
652 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
653 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
654 if (xml_doc) xmlFreeDoc (xml_doc);
670 for (i = 0; i < nr_domains; ++i)
671 virDomainFree (domains[i]);
679 add_domain (virDomainPtr dom)
681 virDomainPtr *new_ptr;
682 int new_size = sizeof (domains[0]) * (nr_domains+1);
685 new_ptr = realloc (domains, new_size);
687 new_ptr = malloc (new_size);
693 domains[nr_domains] = dom;
698 free_block_devices ()
703 for (i = 0; i < nr_block_devices; ++i)
704 sfree (block_devices[i].path);
705 sfree (block_devices);
707 block_devices = NULL;
708 nr_block_devices = 0;
712 add_block_device (virDomainPtr dom, const char *path)
714 struct block_device *new_ptr;
715 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
718 path_copy = strdup (path);
723 new_ptr = realloc (block_devices, new_size);
725 new_ptr = malloc (new_size);
727 if (new_ptr == NULL) {
731 block_devices = new_ptr;
732 block_devices[nr_block_devices].dom = dom;
733 block_devices[nr_block_devices].path = path_copy;
734 return nr_block_devices++;
738 free_interface_devices ()
742 if (interface_devices) {
743 for (i = 0; i < nr_interface_devices; ++i) {
744 sfree (interface_devices[i].path);
745 sfree (interface_devices[i].address);
747 sfree (interface_devices);
749 interface_devices = NULL;
750 nr_interface_devices = 0;
754 add_interface_device (virDomainPtr dom, const char *path, const char *address)
756 struct interface_device *new_ptr;
757 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
758 char *path_copy, *address_copy;
760 path_copy = strdup (path);
761 if (!path_copy) return -1;
763 address_copy = strdup (address);
764 if (!address_copy) return -1;
766 if (interface_devices)
767 new_ptr = realloc (interface_devices, new_size);
769 new_ptr = malloc (new_size);
771 if (new_ptr == NULL) {
773 sfree (address_copy);
776 interface_devices = new_ptr;
777 interface_devices[nr_interface_devices].dom = dom;
778 interface_devices[nr_interface_devices].path = path_copy;
779 interface_devices[nr_interface_devices].address = address_copy;
780 return nr_interface_devices++;
784 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
789 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
792 ERROR ("libvirt plugin: malloc failed.");
795 ssnprintf (name, n, "%s:%s", domname, devpath);
796 r = ignorelist_match (il, name);
804 free_block_devices ();
805 free_interface_devices ();
809 virConnectClose (conn);
812 ignorelist_free (il_domains);
814 ignorelist_free (il_block_devices);
815 il_block_devices = NULL;
816 ignorelist_free (il_interface_devices);
817 il_interface_devices = NULL;
823 module_register (void)
825 plugin_register_config ("libvirt",
827 config_keys, NR_CONFIG_KEYS);
828 plugin_register_init ("libvirt", lv_init);
829 plugin_register_read ("libvirt", lv_read);
830 plugin_register_shutdown ("libvirt", lv_shutdown);
834 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker