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];
144 vl->interval = interval_g;
146 sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
150 host_len = sizeof (vl->host);
152 /* Construct the hostname field according to HostnameFormat. */
153 for (i = 0; i < HF_MAX_FIELDS; ++i) {
154 if (hostname_format[i] == hf_none)
157 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
159 if (i > 0 && n >= 1) {
160 strncat (vl->host, ":", 1);
164 switch (hostname_format[i]) {
167 strncat (vl->host, hostname_g, n);
170 name = virDomainGetName (dom);
172 strncat (vl->host, name, n);
175 if (virDomainGetUUIDString (dom, uuid) == 0)
176 strncat (vl->host, uuid, n);
181 vl->host[sizeof (vl->host) - 1] = '\0';
182 } /* void init_value_list */
185 cpu_submit (unsigned long long cpu_time,
186 virDomainPtr dom, const char *type)
189 value_list_t vl = VALUE_LIST_INIT;
191 init_value_list (&vl, dom);
193 values[0].counter = cpu_time;
198 sstrncpy (vl.type, type, sizeof (vl.type));
200 plugin_dispatch_values (&vl);
204 vcpu_submit (counter_t cpu_time,
205 virDomainPtr dom, int vcpu_nr, const char *type)
208 value_list_t vl = VALUE_LIST_INIT;
210 init_value_list (&vl, dom);
212 values[0].counter = cpu_time;
216 sstrncpy (vl.type, type, sizeof (vl.type));
217 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
219 plugin_dispatch_values (&vl);
223 submit_counter2 (const char *type, counter_t v0, counter_t v1,
224 virDomainPtr dom, const char *devname)
227 value_list_t vl = VALUE_LIST_INIT;
229 init_value_list (&vl, dom);
231 values[0].counter = v0;
232 values[1].counter = v1;
236 sstrncpy (vl.type, type, sizeof (vl.type));
237 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
239 plugin_dispatch_values (&vl);
240 } /* void submit_counter2 */
245 if (virInitialize () != 0)
252 lv_config (const char *key, const char *value)
254 if (virInitialize () != 0)
257 if (il_domains == NULL)
258 il_domains = ignorelist_create (1);
259 if (il_block_devices == NULL)
260 il_block_devices = ignorelist_create (1);
261 if (il_interface_devices == NULL)
262 il_interface_devices = ignorelist_create (1);
264 if (strcasecmp (key, "Connection") == 0) {
265 char *tmp = strdup (value);
267 ERROR ("libvirt plugin: Connection strdup failed.");
275 if (strcasecmp (key, "RefreshInterval") == 0) {
277 interval = strtol (value, &eptr, 10);
278 if (eptr == NULL || *eptr != '\0') return 1;
282 if (strcasecmp (key, "Domain") == 0) {
283 if (ignorelist_add (il_domains, value)) return 1;
286 if (strcasecmp (key, "BlockDevice") == 0) {
287 if (ignorelist_add (il_block_devices, value)) return 1;
290 if (strcasecmp (key, "InterfaceDevice") == 0) {
291 if (ignorelist_add (il_interface_devices, value)) return 1;
295 if (strcasecmp (key, "IgnoreSelected") == 0) {
298 ignorelist_set_invert (il_domains, 0);
299 ignorelist_set_invert (il_block_devices, 0);
300 ignorelist_set_invert (il_interface_devices, 0);
304 ignorelist_set_invert (il_domains, 1);
305 ignorelist_set_invert (il_block_devices, 1);
306 ignorelist_set_invert (il_interface_devices, 1);
311 if (strcasecmp (key, "HostnameFormat") == 0) {
313 char *fields[HF_MAX_FIELDS];
316 value_copy = strdup (value);
317 if (value_copy == NULL) {
318 ERROR ("libvirt plugin: strdup failed.");
322 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
325 ERROR ("HostnameFormat: no fields");
329 for (i = 0; i < n; ++i) {
330 if (strcasecmp (fields[i], "hostname") == 0)
331 hostname_format[i] = hf_hostname;
332 else if (strcasecmp (fields[i], "name") == 0)
333 hostname_format[i] = hf_name;
334 else if (strcasecmp (fields[i], "uuid") == 0)
335 hostname_format[i] = hf_uuid;
338 ERROR ("unknown HostnameFormat field: %s", fields[i]);
344 for (i = n; i < HF_MAX_FIELDS; ++i)
345 hostname_format[i] = hf_none;
350 if (strcasecmp (key, "InterfaceFormat") == 0) {
351 if (strcasecmp (value, "name") == 0)
352 interface_format = if_name;
353 else if (strcasecmp (value, "address") == 0)
354 interface_format = if_address;
356 ERROR ("unknown InterfaceFormat: %s", value);
362 /* Unrecognised option. */
373 /* `conn_string == NULL' is acceptable. */
374 conn = virConnectOpenReadOnly (conn_string);
376 c_complain (LOG_ERR, &conn_complain,
377 "libvirt plugin: Unable to connect: "
378 "virConnectOpenReadOnly failed.");
382 c_release (LOG_NOTICE, &conn_complain,
383 "libvirt plugin: Connection established.");
387 /* Need to refresh domain or device lists? */
388 if ((last_refresh == (time_t) 0) ||
389 ((interval > 0) && ((last_refresh + interval) <= t))) {
390 if (refresh_lists () != 0) {
392 virConnectClose (conn);
400 for (i = 0; i < nr_domains; ++i)
401 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
402 for (i = 0; i < nr_block_devices; ++i)
403 fprintf (stderr, "block device %d %s:%s\n",
404 i, virDomainGetName (block_devices[i].dom),
405 block_devices[i].path);
406 for (i = 0; i < nr_interface_devices; ++i)
407 fprintf (stderr, "interface device %d %s:%s\n",
408 i, virDomainGetName (interface_devices[i].dom),
409 interface_devices[i].path);
412 /* Get CPU usage, VCPU usage for each domain. */
413 for (i = 0; i < nr_domains; ++i) {
415 virVcpuInfoPtr vinfo = NULL;
418 if (virDomainGetInfo (domains[i], &info) != 0)
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 if (virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
435 for (j = 0; j < info.nrVirtCpu; ++j)
436 vcpu_submit (vinfo[j].cpuTime,
437 domains[i], vinfo[j].number, "virt_vcpu");
442 /* Get block device stats for each domain. */
443 for (i = 0; i < nr_block_devices; ++i) {
444 struct _virDomainBlockStats stats;
446 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
447 &stats, sizeof stats) != 0)
450 if ((stats.rd_req != -1) && (stats.wr_req != -1))
451 submit_counter2 ("disk_ops",
452 (counter_t) stats.rd_req, (counter_t) stats.wr_req,
453 block_devices[i].dom, block_devices[i].path);
455 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
456 submit_counter2 ("disk_octets",
457 (counter_t) stats.rd_bytes, (counter_t) stats.wr_bytes,
458 block_devices[i].dom, block_devices[i].path);
459 } /* for (nr_block_devices) */
461 /* Get interface stats for each domain. */
462 for (i = 0; i < nr_interface_devices; ++i) {
463 struct _virDomainInterfaceStats stats;
464 char *display_name = interface_devices[i].path;
466 if (interface_format == if_address)
467 display_name = interface_devices[i].address;
469 if (virDomainInterfaceStats (interface_devices[i].dom,
470 interface_devices[i].path,
471 &stats, sizeof stats) != 0)
474 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
475 submit_counter2 ("if_octets",
476 (counter_t) stats.rx_bytes, (counter_t) stats.tx_bytes,
477 interface_devices[i].dom, display_name);
479 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
480 submit_counter2 ("if_packets",
481 (counter_t) stats.rx_packets, (counter_t) stats.tx_packets,
482 interface_devices[i].dom, display_name);
484 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
485 submit_counter2 ("if_errors",
486 (counter_t) stats.rx_errs, (counter_t) stats.tx_errs,
487 interface_devices[i].dom, display_name);
489 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
490 submit_counter2 ("if_dropped",
491 (counter_t) stats.rx_drop, (counter_t) stats.tx_drop,
492 interface_devices[i].dom, display_name);
493 } /* for (nr_interface_devices) */
503 n = virConnectNumOfDomains (conn);
505 VIRT_ERROR (conn, "reading number of domains");
513 /* Get list of domains. */
514 domids = malloc (sizeof (int) * n);
516 ERROR ("libvirt plugin: malloc failed.");
520 n = virConnectListDomains (conn, domids, n);
522 VIRT_ERROR (conn, "reading list of domains");
527 free_block_devices ();
528 free_interface_devices ();
531 /* Fetch each domain and add it to the list, unless ignore. */
532 for (i = 0; i < n; ++i) {
533 virDomainPtr dom = NULL;
536 xmlDocPtr xml_doc = NULL;
537 xmlXPathContextPtr xpath_ctx = NULL;
538 xmlXPathObjectPtr xpath_obj = NULL;
541 dom = virDomainLookupByID (conn, domids[i]);
543 VIRT_ERROR (conn, "virDomainLookupByID");
544 /* Could be that the domain went away -- ignore it anyway. */
548 name = virDomainGetName (dom);
550 VIRT_ERROR (conn, "virDomainGetName");
554 if (il_domains && ignorelist_match (il_domains, name) != 0)
557 if (add_domain (dom) < 0) {
558 ERROR ("libvirt plugin: malloc failed.");
562 /* Get a list of devices for this domain. */
563 xml = virDomainGetXMLDesc (dom, 0);
565 VIRT_ERROR (conn, "virDomainGetXMLDesc");
569 /* Yuck, XML. Parse out the devices. */
570 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
571 if (xml_doc == NULL) {
572 VIRT_ERROR (conn, "xmlReadDoc");
576 xpath_ctx = xmlXPathNewContext (xml_doc);
579 xpath_obj = xmlXPathEval
580 ((xmlChar *) "/domain/devices/disk/target[@dev]",
582 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
583 xpath_obj->nodesetval == NULL)
586 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
590 node = xpath_obj->nodesetval->nodeTab[j];
592 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
595 if (il_block_devices &&
596 ignore_device_match (il_block_devices, name, path) != 0)
599 add_block_device (dom, path);
601 if (path) xmlFree (path);
603 xmlXPathFreeObject (xpath_obj);
605 /* Network interfaces. */
606 xpath_obj = xmlXPathEval
607 ((xmlChar *) "/domain/devices/interface[target[@dev]]",
609 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
610 xpath_obj->nodesetval == NULL)
613 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
615 for (j = 0; j < xml_interfaces->nodeNr; ++j) {
617 char *address = NULL;
618 xmlNodePtr xml_interface;
620 xml_interface = xml_interfaces->nodeTab[j];
621 if (!xml_interface) continue;
622 xmlNodePtr child = NULL;
624 for (child = xml_interface->children; child; child = child->next) {
625 if (child->type != XML_ELEMENT_NODE) continue;
627 if (xmlStrEqual(child->name, (const xmlChar *) "target")) {
628 path = (char *) xmlGetProp (child, (const xmlChar *) "dev");
630 } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) {
631 address = (char *) xmlGetProp (child, (const xmlChar *) "address");
632 if (!address) continue;
636 if (il_interface_devices &&
637 (ignore_device_match (il_interface_devices, name, path) != 0 ||
638 ignore_device_match (il_interface_devices, name, address) != 0))
641 add_interface_device (dom, path, address);
643 if (path) xmlFree (path);
644 if (address) xmlFree (address);
648 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
649 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
650 if (xml_doc) xmlFreeDoc (xml_doc);
666 for (i = 0; i < nr_domains; ++i)
667 virDomainFree (domains[i]);
675 add_domain (virDomainPtr dom)
677 virDomainPtr *new_ptr;
678 int new_size = sizeof (domains[0]) * (nr_domains+1);
681 new_ptr = realloc (domains, new_size);
683 new_ptr = malloc (new_size);
689 domains[nr_domains] = dom;
694 free_block_devices ()
699 for (i = 0; i < nr_block_devices; ++i)
700 sfree (block_devices[i].path);
701 sfree (block_devices);
703 block_devices = NULL;
704 nr_block_devices = 0;
708 add_block_device (virDomainPtr dom, const char *path)
710 struct block_device *new_ptr;
711 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
714 path_copy = strdup (path);
719 new_ptr = realloc (block_devices, new_size);
721 new_ptr = malloc (new_size);
723 if (new_ptr == NULL) {
727 block_devices = new_ptr;
728 block_devices[nr_block_devices].dom = dom;
729 block_devices[nr_block_devices].path = path_copy;
730 return nr_block_devices++;
734 free_interface_devices ()
738 if (interface_devices) {
739 for (i = 0; i < nr_interface_devices; ++i) {
740 sfree (interface_devices[i].path);
741 sfree (interface_devices[i].address);
743 sfree (interface_devices);
745 interface_devices = NULL;
746 nr_interface_devices = 0;
750 add_interface_device (virDomainPtr dom, const char *path, const char *address)
752 struct interface_device *new_ptr;
753 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
754 char *path_copy, *address_copy;
756 path_copy = strdup (path);
757 if (!path_copy) return -1;
759 address_copy = strdup (address);
760 if (!address_copy) return -1;
762 if (interface_devices)
763 new_ptr = realloc (interface_devices, new_size);
765 new_ptr = malloc (new_size);
767 if (new_ptr == NULL) {
769 sfree (address_copy);
772 interface_devices = new_ptr;
773 interface_devices[nr_interface_devices].dom = dom;
774 interface_devices[nr_interface_devices].path = path_copy;
775 interface_devices[nr_interface_devices].address = address_copy;
776 return nr_interface_devices++;
780 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
785 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
788 ERROR ("libvirt plugin: malloc failed.");
791 ssnprintf (name, n, "%s:%s", domname, devpath);
792 r = ignorelist_match (il, name);
800 free_block_devices ();
801 free_interface_devices ();
805 virConnectClose (conn);
808 ignorelist_free (il_domains);
810 ignorelist_free (il_block_devices);
811 il_block_devices = NULL;
812 ignorelist_free (il_interface_devices);
813 il_interface_devices = NULL;
819 module_register (void)
821 plugin_register_config ("libvirt",
823 config_keys, NR_CONFIG_KEYS);
824 plugin_register_init ("libvirt", lv_init);
825 plugin_register_read ("libvirt", lv_read);
826 plugin_register_shutdown ("libvirt", lv_shutdown);
830 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker