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"
28 #include <libvirt/libvirt.h>
29 #include <libvirt/virterror.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32 #include <libxml/xpath.h>
34 static const char *config_keys[] = {
48 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
51 static virConnectPtr conn = 0;
52 static char *conn_string = NULL;
53 static int conn_count = 0;
55 /* Seconds between list refreshes, 0 disables completely. */
56 static int interval = 60;
58 /* List of domains, if specified. */
59 static ignorelist_t *il_domains = NULL;
60 /* List of block devices, if specified. */
61 static ignorelist_t *il_block_devices = NULL;
62 /* List of network interface devices, if specified. */
63 static ignorelist_t *il_interface_devices = NULL;
65 static int ignore_device_match (ignorelist_t *,
66 const char *domname, const char *devpath);
68 /* Actual list of domains found on last refresh. */
69 static virDomainPtr *domains = NULL;
70 static int nr_domains = 0;
72 static void free_domains (void);
73 static int add_domain (virDomainPtr dom);
75 /* Actual list of block devices found on last refresh. */
77 virDomainPtr dom; /* domain */
78 char *path; /* name of block device */
81 static struct block_device *block_devices = NULL;
82 static int nr_block_devices = 0;
84 static void free_block_devices (void);
85 static int add_block_device (virDomainPtr dom, const char *path);
87 /* Actual list of network interfaces found on last refresh. */
88 struct interface_device {
89 virDomainPtr dom; /* domain */
90 char *path; /* name of interface device */
93 static struct interface_device *interface_devices = NULL;
94 static int nr_interface_devices = 0;
96 static void free_interface_devices (void);
97 static int add_interface_device (virDomainPtr dom, const char *path);
100 #define HF_MAX_FIELDS 3
109 static enum hf_field hostname_format[HF_MAX_FIELDS] =
112 /* Time that we last refreshed. */
113 static time_t last_refresh = (time_t) 0;
115 static int refresh_lists (void);
117 /* Submit functions. */
118 static void cpu_submit (unsigned long long cpu_time,
120 virDomainPtr dom, const char *type);
121 static void vcpu_submit (unsigned long long cpu_time,
123 virDomainPtr dom, int vcpu_nr, const char *type);
124 static void submit_counter2 (const char *type, counter_t v0, counter_t v1,
126 virDomainPtr dom, const char *devname);
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); \
138 if (virInitialize () != 0)
145 lv_config (const char *key, const char *value)
147 if (virInitialize () != 0)
150 if (il_domains == NULL)
151 il_domains = ignorelist_create (1);
152 if (il_block_devices == NULL)
153 il_block_devices = ignorelist_create (1);
154 if (il_interface_devices == NULL)
155 il_interface_devices = ignorelist_create (1);
157 if (strcasecmp (key, "Connection") == 0) {
158 if (conn_count++ != 0) {
159 ERROR ("Connection may only be given once in config file");
162 conn_string = strdup(value);
163 if (conn_string == NULL) {
164 ERROR ("libvirt plugin: Connection strdup failed.");
170 if (strcasecmp (key, "RefreshInterval") == 0) {
172 interval = strtol (value, &eptr, 10);
173 if (eptr == NULL || *eptr != '\0') return 1;
177 if (strcasecmp (key, "Domain") == 0) {
178 if (ignorelist_add (il_domains, value)) return 1;
181 if (strcasecmp (key, "BlockDevice") == 0) {
182 if (ignorelist_add (il_block_devices, value)) return 1;
185 if (strcasecmp (key, "InterfaceDevice") == 0) {
186 if (ignorelist_add (il_interface_devices, value)) return 1;
190 if (strcasecmp (key, "IgnoreSelected") == 0) {
191 if (strcasecmp (value, "True") == 0 ||
192 strcasecmp (value, "Yes") == 0 ||
193 strcasecmp (value, "On") == 0)
195 ignorelist_set_invert (il_domains, 0);
196 ignorelist_set_invert (il_block_devices, 0);
197 ignorelist_set_invert (il_interface_devices, 0);
201 ignorelist_set_invert (il_domains, 1);
202 ignorelist_set_invert (il_block_devices, 1);
203 ignorelist_set_invert (il_interface_devices, 1);
208 if (strcasecmp (key, "HostnameFormat") == 0) {
210 char *fields[HF_MAX_FIELDS];
213 value_copy = strdup (value);
214 if (value_copy == NULL) {
215 ERROR ("libvirt plugin: strdup failed.");
219 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
222 ERROR ("HostnameFormat: no fields");
226 for (i = 0; i < n; ++i) {
227 if (strcasecmp (fields[i], "hostname") == 0)
228 hostname_format[i] = hf_hostname;
229 else if (strcasecmp (fields[i], "name") == 0)
230 hostname_format[i] = hf_name;
231 else if (strcasecmp (fields[i], "uuid") == 0)
232 hostname_format[i] = hf_uuid;
235 ERROR ("unknown HostnameFormat field: %s", fields[i]);
241 for (i = n; i < HF_MAX_FIELDS; ++i)
242 hostname_format[i] = hf_none;
247 /* Unrecognised option. */
258 conn = virConnectOpenReadOnly (conn_string);
260 ERROR ("libvirt plugin: Not connected.");
267 /* Need to refresh domain or device lists? */
268 if ((last_refresh == (time_t) 0) ||
269 ((interval > 0) && ((last_refresh + interval) <= t))) {
270 if (refresh_lists () != 0) {
272 virConnectClose (conn);
280 for (i = 0; i < nr_domains; ++i)
281 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
282 for (i = 0; i < nr_block_devices; ++i)
283 fprintf (stderr, "block device %d %s:%s\n",
284 i, virDomainGetName (block_devices[i].dom),
285 block_devices[i].path);
286 for (i = 0; i < nr_interface_devices; ++i)
287 fprintf (stderr, "interface device %d %s:%s\n",
288 i, virDomainGetName (interface_devices[i].dom),
289 interface_devices[i].path);
292 /* Get CPU usage, VCPU usage for each domain. */
293 for (i = 0; i < nr_domains; ++i) {
295 virVcpuInfoPtr vinfo = NULL;
298 if (virDomainGetInfo (domains[i], &info) != 0)
301 cpu_submit (info.cpuTime, t, domains[i], "virt_cpu_total");
303 vinfo = malloc (info.nrVirtCpu * sizeof vinfo[0]);
305 ERROR ("libvirt plugin: malloc failed.");
309 if (virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
315 for (j = 0; j < info.nrVirtCpu; ++j)
316 vcpu_submit (vinfo[j].cpuTime,
317 t, domains[i], vinfo[j].number, "virt_vcpu");
322 /* Get block device stats for each domain. */
323 for (i = 0; i < nr_block_devices; ++i) {
324 struct _virDomainBlockStats stats;
326 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
327 &stats, sizeof stats) != 0)
330 if ((stats.rd_req != -1) && (stats.wr_req != -1))
331 submit_counter2 ("disk_ops",
332 (counter_t) stats.rd_req, (counter_t) stats.wr_req,
333 t, block_devices[i].dom, block_devices[i].path);
335 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
336 submit_counter2 ("disk_octets",
337 (counter_t) stats.rd_bytes, (counter_t) stats.wr_bytes,
338 t, block_devices[i].dom, block_devices[i].path);
339 } /* for (nr_block_devices) */
341 /* Get interface stats for each domain. */
342 for (i = 0; i < nr_interface_devices; ++i) {
343 struct _virDomainInterfaceStats stats;
345 if (virDomainInterfaceStats (interface_devices[i].dom,
346 interface_devices[i].path,
347 &stats, sizeof stats) != 0)
350 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
351 submit_counter2 ("if_octets",
352 (counter_t) stats.rx_bytes, (counter_t) stats.tx_bytes,
353 t, interface_devices[i].dom, interface_devices[i].path);
355 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
356 submit_counter2 ("if_packets",
357 (counter_t) stats.rx_packets, (counter_t) stats.tx_packets,
358 t, interface_devices[i].dom, interface_devices[i].path);
360 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
361 submit_counter2 ("if_errors",
362 (counter_t) stats.rx_errs, (counter_t) stats.tx_errs,
363 t, interface_devices[i].dom, interface_devices[i].path);
365 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
366 submit_counter2 ("if_dropped",
367 (counter_t) stats.rx_drop, (counter_t) stats.tx_drop,
368 t, interface_devices[i].dom, interface_devices[i].path);
369 } /* for (nr_interface_devices) */
379 n = virConnectNumOfDomains (conn);
381 VIRT_ERROR (conn, "reading number of domains");
389 /* Get list of domains. */
390 domids = malloc (sizeof (int) * n);
392 ERROR ("libvirt plugin: malloc failed.");
396 n = virConnectListDomains (conn, domids, n);
398 VIRT_ERROR (conn, "reading list of domains");
403 free_block_devices ();
404 free_interface_devices ();
407 /* Fetch each domain and add it to the list, unless ignore. */
408 for (i = 0; i < n; ++i) {
409 virDomainPtr dom = NULL;
412 xmlDocPtr xml_doc = NULL;
413 xmlXPathContextPtr xpath_ctx = NULL;
414 xmlXPathObjectPtr xpath_obj = NULL;
417 dom = virDomainLookupByID (conn, domids[i]);
419 VIRT_ERROR (conn, "virDomainLookupByID");
420 /* Could be that the domain went away -- ignore it anyway. */
424 name = virDomainGetName (dom);
426 VIRT_ERROR (conn, "virDomainGetName");
430 if (il_domains && ignorelist_match (il_domains, name) != 0)
433 if (add_domain (dom) < 0) {
434 ERROR ("libvirt plugin: malloc failed.");
438 /* Get a list of devices for this domain. */
439 xml = virDomainGetXMLDesc (dom, 0);
441 VIRT_ERROR (conn, "virDomainGetXMLDesc");
445 /* Yuck, XML. Parse out the devices. */
446 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
447 if (xml_doc == NULL) {
448 VIRT_ERROR (conn, "xmlReadDoc");
452 xpath_ctx = xmlXPathNewContext (xml_doc);
455 xpath_obj = xmlXPathEval
456 ((xmlChar *) "/domain/devices/disk/target[@dev]",
458 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
459 xpath_obj->nodesetval == NULL)
462 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
466 node = xpath_obj->nodesetval->nodeTab[j];
468 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
471 if (il_block_devices &&
472 ignore_device_match (il_block_devices, name, path) != 0)
475 add_block_device (dom, path);
477 if (path) xmlFree (path);
479 xmlXPathFreeObject (xpath_obj);
481 /* Network interfaces. */
482 xpath_obj = xmlXPathEval
483 ((xmlChar *) "/domain/devices/interface/target[@dev]",
485 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
486 xpath_obj->nodesetval == NULL)
489 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
493 node = xpath_obj->nodesetval->nodeTab[j];
495 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
498 if (il_interface_devices &&
499 ignore_device_match (il_interface_devices, name, path) != 0)
502 add_interface_device (dom, path);
504 if (path) xmlFree (path);
508 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
509 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
510 if (xml_doc) xmlFreeDoc (xml_doc);
526 for (i = 0; i < nr_domains; ++i)
527 virDomainFree (domains[i]);
535 add_domain (virDomainPtr dom)
537 virDomainPtr *new_ptr;
538 int new_size = sizeof (domains[0]) * (nr_domains+1);
541 new_ptr = realloc (domains, new_size);
543 new_ptr = malloc (new_size);
549 domains[nr_domains] = dom;
554 free_block_devices ()
559 for (i = 0; i < nr_block_devices; ++i)
560 free (block_devices[i].path);
561 free (block_devices);
563 block_devices = NULL;
564 nr_block_devices = 0;
568 add_block_device (virDomainPtr dom, const char *path)
570 struct block_device *new_ptr;
571 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
574 path_copy = strdup (path);
579 new_ptr = realloc (block_devices, new_size);
581 new_ptr = malloc (new_size);
583 if (new_ptr == NULL) {
587 block_devices = new_ptr;
588 block_devices[nr_block_devices].dom = dom;
589 block_devices[nr_block_devices].path = path_copy;
590 return nr_block_devices++;
594 free_interface_devices ()
598 if (interface_devices) {
599 for (i = 0; i < nr_interface_devices; ++i)
600 free (interface_devices[i].path);
601 free (interface_devices);
603 interface_devices = NULL;
604 nr_interface_devices = 0;
608 add_interface_device (virDomainPtr dom, const char *path)
610 struct interface_device *new_ptr;
611 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
614 path_copy = strdup (path);
615 if (!path_copy) return -1;
617 if (interface_devices)
618 new_ptr = realloc (interface_devices, new_size);
620 new_ptr = malloc (new_size);
622 if (new_ptr == NULL) {
626 interface_devices = new_ptr;
627 interface_devices[nr_interface_devices].dom = dom;
628 interface_devices[nr_interface_devices].path = path_copy;
629 return nr_interface_devices++;
633 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
638 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
641 ERROR ("libvirt plugin: malloc failed.");
644 ssnprintf (name, n, "%s:%s", domname, devpath);
645 r = ignorelist_match (il, name);
651 init_value_list (value_list_t *vl, time_t t, virDomainPtr dom)
655 char uuid[VIR_UUID_STRING_BUFLEN];
660 vl->interval = interval_g;
662 sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
666 host_len = sizeof (vl->host);
668 /* Construct the hostname field according to HostnameFormat. */
669 for (i = 0; i < HF_MAX_FIELDS; ++i) {
670 if (hostname_format[i] == hf_none)
673 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
675 if (i > 0 && n >= 1) {
676 strncat (vl->host, ":", 1);
680 switch (hostname_format[i]) {
683 strncat (vl->host, hostname_g, n);
686 name = virDomainGetName (dom);
688 strncat (vl->host, name, n);
691 if (virDomainGetUUIDString (dom, uuid) == 0)
692 strncat (vl->host, uuid, n);
697 vl->host[sizeof (vl->host) - 1] = '\0';
698 } /* void init_value_list */
701 cpu_submit (unsigned long long cpu_time,
703 virDomainPtr dom, const char *type)
706 value_list_t vl = VALUE_LIST_INIT;
708 init_value_list (&vl, t, dom);
710 values[0].counter = cpu_time;
715 sstrncpy (vl.type, type, sizeof (vl.type));
717 plugin_dispatch_values (&vl);
721 vcpu_submit (counter_t cpu_time,
723 virDomainPtr dom, int vcpu_nr, const char *type)
726 value_list_t vl = VALUE_LIST_INIT;
728 init_value_list (&vl, t, dom);
730 values[0].counter = cpu_time;
734 sstrncpy (vl.type, type, sizeof (vl.type));
735 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
737 plugin_dispatch_values (&vl);
741 submit_counter2 (const char *type, counter_t v0, counter_t v1,
743 virDomainPtr dom, const char *devname)
746 value_list_t vl = VALUE_LIST_INIT;
748 init_value_list (&vl, t, dom);
750 values[0].counter = v0;
751 values[1].counter = v1;
755 sstrncpy (vl.type, type, sizeof (vl.type));
756 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
758 plugin_dispatch_values (&vl);
759 } /* void submit_counter2 */
764 free_block_devices ();
765 free_interface_devices ();
769 virConnectClose (conn);
772 ignorelist_free (il_domains);
774 ignorelist_free (il_block_devices);
775 il_block_devices = NULL;
776 ignorelist_free (il_interface_devices);
777 il_interface_devices = NULL;
783 module_register (void)
785 plugin_register_config ("libvirt",
787 config_keys, NR_CONFIG_KEYS);
788 plugin_register_init ("libvirt", lv_init);
789 plugin_register_read ("libvirt", lv_read);
790 plugin_register_shutdown ("libvirt", lv_shutdown);
794 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker