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 /* Submit functions. */
129 static void cpu_submit (unsigned long long cpu_time,
131 virDomainPtr dom, const char *type);
132 static void vcpu_submit (unsigned long long cpu_time,
134 virDomainPtr dom, int vcpu_nr, const char *type);
135 static void submit_counter2 (const char *type, counter_t v0, counter_t v1,
137 virDomainPtr dom, const char *devname);
139 /* ERROR(...) macro for virterrors. */
140 #define VIRT_ERROR(conn,s) do { \
142 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
143 if (err) ERROR ("%s: %s", (s), err->message); \
149 if (virInitialize () != 0)
156 lv_config (const char *key, const char *value)
158 if (virInitialize () != 0)
161 if (il_domains == NULL)
162 il_domains = ignorelist_create (1);
163 if (il_block_devices == NULL)
164 il_block_devices = ignorelist_create (1);
165 if (il_interface_devices == NULL)
166 il_interface_devices = ignorelist_create (1);
168 if (strcasecmp (key, "Connection") == 0) {
169 char *tmp = strdup (value);
171 ERROR ("libvirt plugin: Connection strdup failed.");
179 if (strcasecmp (key, "RefreshInterval") == 0) {
181 interval = strtol (value, &eptr, 10);
182 if (eptr == NULL || *eptr != '\0') return 1;
186 if (strcasecmp (key, "Domain") == 0) {
187 if (ignorelist_add (il_domains, value)) return 1;
190 if (strcasecmp (key, "BlockDevice") == 0) {
191 if (ignorelist_add (il_block_devices, value)) return 1;
194 if (strcasecmp (key, "InterfaceDevice") == 0) {
195 if (ignorelist_add (il_interface_devices, value)) return 1;
199 if (strcasecmp (key, "IgnoreSelected") == 0) {
202 ignorelist_set_invert (il_domains, 0);
203 ignorelist_set_invert (il_block_devices, 0);
204 ignorelist_set_invert (il_interface_devices, 0);
208 ignorelist_set_invert (il_domains, 1);
209 ignorelist_set_invert (il_block_devices, 1);
210 ignorelist_set_invert (il_interface_devices, 1);
215 if (strcasecmp (key, "HostnameFormat") == 0) {
217 char *fields[HF_MAX_FIELDS];
220 value_copy = strdup (value);
221 if (value_copy == NULL) {
222 ERROR ("libvirt plugin: strdup failed.");
226 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
229 ERROR ("HostnameFormat: no fields");
233 for (i = 0; i < n; ++i) {
234 if (strcasecmp (fields[i], "hostname") == 0)
235 hostname_format[i] = hf_hostname;
236 else if (strcasecmp (fields[i], "name") == 0)
237 hostname_format[i] = hf_name;
238 else if (strcasecmp (fields[i], "uuid") == 0)
239 hostname_format[i] = hf_uuid;
242 ERROR ("unknown HostnameFormat field: %s", fields[i]);
248 for (i = n; i < HF_MAX_FIELDS; ++i)
249 hostname_format[i] = hf_none;
254 if (strcasecmp (key, "InterfaceFormat") == 0) {
255 if (strcasecmp (value, "name") == 0)
256 interface_format = if_name;
257 else if (strcasecmp (value, "address") == 0)
258 interface_format = if_address;
260 ERROR ("unknown InterfaceFormat: %s", value);
266 /* Unrecognised option. */
277 /* `conn_string == NULL' is acceptable. */
278 conn = virConnectOpenReadOnly (conn_string);
280 c_complain (LOG_ERR, &conn_complain,
281 "libvirt plugin: Unable to connect: "
282 "virConnectOpenReadOnly failed.");
286 c_release (LOG_NOTICE, &conn_complain,
287 "libvirt plugin: Connection established.");
291 /* Need to refresh domain or device lists? */
292 if ((last_refresh == (time_t) 0) ||
293 ((interval > 0) && ((last_refresh + interval) <= t))) {
294 if (refresh_lists () != 0) {
296 virConnectClose (conn);
304 for (i = 0; i < nr_domains; ++i)
305 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
306 for (i = 0; i < nr_block_devices; ++i)
307 fprintf (stderr, "block device %d %s:%s\n",
308 i, virDomainGetName (block_devices[i].dom),
309 block_devices[i].path);
310 for (i = 0; i < nr_interface_devices; ++i)
311 fprintf (stderr, "interface device %d %s:%s\n",
312 i, virDomainGetName (interface_devices[i].dom),
313 interface_devices[i].path);
316 /* Get CPU usage, VCPU usage for each domain. */
317 for (i = 0; i < nr_domains; ++i) {
319 virVcpuInfoPtr vinfo = NULL;
322 if (virDomainGetInfo (domains[i], &info) != 0)
325 cpu_submit (info.cpuTime, t, domains[i], "virt_cpu_total");
327 vinfo = malloc (info.nrVirtCpu * sizeof vinfo[0]);
329 ERROR ("libvirt plugin: malloc failed.");
333 if (virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
339 for (j = 0; j < info.nrVirtCpu; ++j)
340 vcpu_submit (vinfo[j].cpuTime,
341 t, domains[i], vinfo[j].number, "virt_vcpu");
346 /* Get block device stats for each domain. */
347 for (i = 0; i < nr_block_devices; ++i) {
348 struct _virDomainBlockStats stats;
350 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
351 &stats, sizeof stats) != 0)
354 if ((stats.rd_req != -1) && (stats.wr_req != -1))
355 submit_counter2 ("disk_ops",
356 (counter_t) stats.rd_req, (counter_t) stats.wr_req,
357 t, block_devices[i].dom, block_devices[i].path);
359 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
360 submit_counter2 ("disk_octets",
361 (counter_t) stats.rd_bytes, (counter_t) stats.wr_bytes,
362 t, block_devices[i].dom, block_devices[i].path);
363 } /* for (nr_block_devices) */
365 /* Get interface stats for each domain. */
366 for (i = 0; i < nr_interface_devices; ++i) {
367 struct _virDomainInterfaceStats stats;
368 char *display_name = interface_devices[i].path;
370 if (interface_format == if_address)
371 display_name = interface_devices[i].address;
373 if (virDomainInterfaceStats (interface_devices[i].dom,
374 interface_devices[i].path,
375 &stats, sizeof stats) != 0)
378 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
379 submit_counter2 ("if_octets",
380 (counter_t) stats.rx_bytes, (counter_t) stats.tx_bytes,
381 t, interface_devices[i].dom, display_name);
383 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
384 submit_counter2 ("if_packets",
385 (counter_t) stats.rx_packets, (counter_t) stats.tx_packets,
386 t, interface_devices[i].dom, display_name);
388 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
389 submit_counter2 ("if_errors",
390 (counter_t) stats.rx_errs, (counter_t) stats.tx_errs,
391 t, interface_devices[i].dom, display_name);
393 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
394 submit_counter2 ("if_dropped",
395 (counter_t) stats.rx_drop, (counter_t) stats.tx_drop,
396 t, interface_devices[i].dom, display_name);
397 } /* for (nr_interface_devices) */
407 n = virConnectNumOfDomains (conn);
409 VIRT_ERROR (conn, "reading number of domains");
417 /* Get list of domains. */
418 domids = malloc (sizeof (int) * n);
420 ERROR ("libvirt plugin: malloc failed.");
424 n = virConnectListDomains (conn, domids, n);
426 VIRT_ERROR (conn, "reading list of domains");
431 free_block_devices ();
432 free_interface_devices ();
435 /* Fetch each domain and add it to the list, unless ignore. */
436 for (i = 0; i < n; ++i) {
437 virDomainPtr dom = NULL;
440 xmlDocPtr xml_doc = NULL;
441 xmlXPathContextPtr xpath_ctx = NULL;
442 xmlXPathObjectPtr xpath_obj = NULL;
445 dom = virDomainLookupByID (conn, domids[i]);
447 VIRT_ERROR (conn, "virDomainLookupByID");
448 /* Could be that the domain went away -- ignore it anyway. */
452 name = virDomainGetName (dom);
454 VIRT_ERROR (conn, "virDomainGetName");
458 if (il_domains && ignorelist_match (il_domains, name) != 0)
461 if (add_domain (dom) < 0) {
462 ERROR ("libvirt plugin: malloc failed.");
466 /* Get a list of devices for this domain. */
467 xml = virDomainGetXMLDesc (dom, 0);
469 VIRT_ERROR (conn, "virDomainGetXMLDesc");
473 /* Yuck, XML. Parse out the devices. */
474 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
475 if (xml_doc == NULL) {
476 VIRT_ERROR (conn, "xmlReadDoc");
480 xpath_ctx = xmlXPathNewContext (xml_doc);
483 xpath_obj = xmlXPathEval
484 ((xmlChar *) "/domain/devices/disk/target[@dev]",
486 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
487 xpath_obj->nodesetval == NULL)
490 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
494 node = xpath_obj->nodesetval->nodeTab[j];
496 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
499 if (il_block_devices &&
500 ignore_device_match (il_block_devices, name, path) != 0)
503 add_block_device (dom, path);
505 if (path) xmlFree (path);
507 xmlXPathFreeObject (xpath_obj);
509 /* Network interfaces. */
510 xpath_obj = xmlXPathEval
511 ((xmlChar *) "/domain/devices/interface[target[@dev]]",
513 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
514 xpath_obj->nodesetval == NULL)
517 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
519 for (j = 0; j < xml_interfaces->nodeNr; ++j) {
521 char *address = NULL;
522 xmlNodePtr xml_interface;
524 xml_interface = xml_interfaces->nodeTab[j];
525 if (!xml_interface) continue;
526 xmlNodePtr child = NULL;
528 for (child = xml_interface->children; child; child = child->next) {
529 if (child->type != XML_ELEMENT_NODE) continue;
531 if (xmlStrEqual(child->name, (const xmlChar *) "target")) {
532 path = (char *) xmlGetProp (child, (const xmlChar *) "dev");
534 } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) {
535 address = (char *) xmlGetProp (child, (const xmlChar *) "address");
536 if (!address) continue;
540 if (il_interface_devices &&
541 (ignore_device_match (il_interface_devices, name, path) != 0 ||
542 ignore_device_match (il_interface_devices, name, address) != 0))
545 add_interface_device (dom, path, address);
547 if (path) xmlFree (path);
548 if (address) xmlFree (address);
552 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
553 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
554 if (xml_doc) xmlFreeDoc (xml_doc);
570 for (i = 0; i < nr_domains; ++i)
571 virDomainFree (domains[i]);
579 add_domain (virDomainPtr dom)
581 virDomainPtr *new_ptr;
582 int new_size = sizeof (domains[0]) * (nr_domains+1);
585 new_ptr = realloc (domains, new_size);
587 new_ptr = malloc (new_size);
593 domains[nr_domains] = dom;
598 free_block_devices ()
603 for (i = 0; i < nr_block_devices; ++i)
604 sfree (block_devices[i].path);
605 sfree (block_devices);
607 block_devices = NULL;
608 nr_block_devices = 0;
612 add_block_device (virDomainPtr dom, const char *path)
614 struct block_device *new_ptr;
615 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
618 path_copy = strdup (path);
623 new_ptr = realloc (block_devices, new_size);
625 new_ptr = malloc (new_size);
627 if (new_ptr == NULL) {
631 block_devices = new_ptr;
632 block_devices[nr_block_devices].dom = dom;
633 block_devices[nr_block_devices].path = path_copy;
634 return nr_block_devices++;
638 free_interface_devices ()
642 if (interface_devices) {
643 for (i = 0; i < nr_interface_devices; ++i) {
644 sfree (interface_devices[i].path);
645 sfree (interface_devices[i].address);
647 sfree (interface_devices);
649 interface_devices = NULL;
650 nr_interface_devices = 0;
654 add_interface_device (virDomainPtr dom, const char *path, const char *address)
656 struct interface_device *new_ptr;
657 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
658 char *path_copy, *address_copy;
660 path_copy = strdup (path);
661 if (!path_copy) return -1;
663 address_copy = strdup (address);
664 if (!address_copy) return -1;
666 if (interface_devices)
667 new_ptr = realloc (interface_devices, new_size);
669 new_ptr = malloc (new_size);
671 if (new_ptr == NULL) {
673 sfree (address_copy);
676 interface_devices = new_ptr;
677 interface_devices[nr_interface_devices].dom = dom;
678 interface_devices[nr_interface_devices].path = path_copy;
679 interface_devices[nr_interface_devices].address = address_copy;
680 return nr_interface_devices++;
684 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
689 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
692 ERROR ("libvirt plugin: malloc failed.");
695 ssnprintf (name, n, "%s:%s", domname, devpath);
696 r = ignorelist_match (il, name);
702 init_value_list (value_list_t *vl, time_t t, virDomainPtr dom)
706 char uuid[VIR_UUID_STRING_BUFLEN];
711 vl->interval = interval_g;
713 sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
717 host_len = sizeof (vl->host);
719 /* Construct the hostname field according to HostnameFormat. */
720 for (i = 0; i < HF_MAX_FIELDS; ++i) {
721 if (hostname_format[i] == hf_none)
724 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
726 if (i > 0 && n >= 1) {
727 strncat (vl->host, ":", 1);
731 switch (hostname_format[i]) {
734 strncat (vl->host, hostname_g, n);
737 name = virDomainGetName (dom);
739 strncat (vl->host, name, n);
742 if (virDomainGetUUIDString (dom, uuid) == 0)
743 strncat (vl->host, uuid, n);
748 vl->host[sizeof (vl->host) - 1] = '\0';
749 } /* void init_value_list */
752 cpu_submit (unsigned long long cpu_time,
754 virDomainPtr dom, const char *type)
757 value_list_t vl = VALUE_LIST_INIT;
759 init_value_list (&vl, t, dom);
761 values[0].counter = cpu_time;
766 sstrncpy (vl.type, type, sizeof (vl.type));
768 plugin_dispatch_values (&vl);
772 vcpu_submit (counter_t cpu_time,
774 virDomainPtr dom, int vcpu_nr, const char *type)
777 value_list_t vl = VALUE_LIST_INIT;
779 init_value_list (&vl, t, dom);
781 values[0].counter = cpu_time;
785 sstrncpy (vl.type, type, sizeof (vl.type));
786 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
788 plugin_dispatch_values (&vl);
792 submit_counter2 (const char *type, counter_t v0, counter_t v1,
794 virDomainPtr dom, const char *devname)
797 value_list_t vl = VALUE_LIST_INIT;
799 init_value_list (&vl, t, dom);
801 values[0].counter = v0;
802 values[1].counter = v1;
806 sstrncpy (vl.type, type, sizeof (vl.type));
807 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
809 plugin_dispatch_values (&vl);
810 } /* void submit_counter2 */
815 free_block_devices ();
816 free_interface_devices ();
820 virConnectClose (conn);
823 ignorelist_free (il_domains);
825 ignorelist_free (il_block_devices);
826 il_block_devices = NULL;
827 ignorelist_free (il_interface_devices);
828 il_interface_devices = NULL;
834 module_register (void)
836 plugin_register_config ("libvirt",
838 config_keys, NR_CONFIG_KEYS);
839 plugin_register_init ("libvirt", lv_init);
840 plugin_register_read ("libvirt", lv_read);
841 plugin_register_shutdown ("libvirt", lv_shutdown);
845 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker