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[] = {
49 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
52 static virConnectPtr conn = 0;
53 static char *conn_string = NULL;
54 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
56 /* Seconds between list refreshes, 0 disables completely. */
57 static int interval = 60;
59 /* List of domains, if specified. */
60 static ignorelist_t *il_domains = NULL;
61 /* List of block devices, if specified. */
62 static ignorelist_t *il_block_devices = NULL;
63 /* List of network interface devices, if specified. */
64 static ignorelist_t *il_interface_devices = NULL;
66 static int ignore_device_match (ignorelist_t *,
67 const char *domname, const char *devpath);
69 /* Actual list of domains found on last refresh. */
70 static virDomainPtr *domains = NULL;
71 static int nr_domains = 0;
73 static void free_domains (void);
74 static int add_domain (virDomainPtr dom);
76 /* Actual list of block devices found on last refresh. */
78 virDomainPtr dom; /* domain */
79 char *path; /* name of block device */
82 static struct block_device *block_devices = NULL;
83 static int nr_block_devices = 0;
85 static void free_block_devices (void);
86 static int add_block_device (virDomainPtr dom, const char *path);
88 /* Actual list of network interfaces found on last refresh. */
89 struct interface_device {
90 virDomainPtr dom; /* domain */
91 char *path; /* name of interface device */
94 static struct interface_device *interface_devices = NULL;
95 static int nr_interface_devices = 0;
97 static void free_interface_devices (void);
98 static int add_interface_device (virDomainPtr dom, const char *path);
100 /* HostnameFormat. */
101 #define HF_MAX_FIELDS 3
110 static enum hf_field hostname_format[HF_MAX_FIELDS] =
113 /* Time that we last refreshed. */
114 static time_t last_refresh = (time_t) 0;
116 static int refresh_lists (void);
118 /* Submit functions. */
119 static void cpu_submit (unsigned long long cpu_time,
121 virDomainPtr dom, const char *type);
122 static void vcpu_submit (unsigned long long cpu_time,
124 virDomainPtr dom, int vcpu_nr, const char *type);
125 static void submit_counter2 (const char *type, counter_t v0, counter_t v1,
127 virDomainPtr dom, const char *devname);
129 /* ERROR(...) macro for virterrors. */
130 #define VIRT_ERROR(conn,s) do { \
132 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
133 if (err) ERROR ("%s: %s", (s), err->message); \
139 if (virInitialize () != 0)
146 lv_config (const char *key, const char *value)
148 if (virInitialize () != 0)
151 if (il_domains == NULL)
152 il_domains = ignorelist_create (1);
153 if (il_block_devices == NULL)
154 il_block_devices = ignorelist_create (1);
155 if (il_interface_devices == NULL)
156 il_interface_devices = ignorelist_create (1);
158 if (strcasecmp (key, "Connection") == 0) {
159 char *tmp = strdup (value);
161 ERROR ("libvirt plugin: Connection strdup failed.");
169 if (strcasecmp (key, "RefreshInterval") == 0) {
171 interval = strtol (value, &eptr, 10);
172 if (eptr == NULL || *eptr != '\0') return 1;
176 if (strcasecmp (key, "Domain") == 0) {
177 if (ignorelist_add (il_domains, value)) return 1;
180 if (strcasecmp (key, "BlockDevice") == 0) {
181 if (ignorelist_add (il_block_devices, value)) return 1;
184 if (strcasecmp (key, "InterfaceDevice") == 0) {
185 if (ignorelist_add (il_interface_devices, value)) return 1;
189 if (strcasecmp (key, "IgnoreSelected") == 0) {
192 ignorelist_set_invert (il_domains, 0);
193 ignorelist_set_invert (il_block_devices, 0);
194 ignorelist_set_invert (il_interface_devices, 0);
198 ignorelist_set_invert (il_domains, 1);
199 ignorelist_set_invert (il_block_devices, 1);
200 ignorelist_set_invert (il_interface_devices, 1);
205 if (strcasecmp (key, "HostnameFormat") == 0) {
207 char *fields[HF_MAX_FIELDS];
210 value_copy = strdup (value);
211 if (value_copy == NULL) {
212 ERROR ("libvirt plugin: strdup failed.");
216 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
219 ERROR ("HostnameFormat: no fields");
223 for (i = 0; i < n; ++i) {
224 if (strcasecmp (fields[i], "hostname") == 0)
225 hostname_format[i] = hf_hostname;
226 else if (strcasecmp (fields[i], "name") == 0)
227 hostname_format[i] = hf_name;
228 else if (strcasecmp (fields[i], "uuid") == 0)
229 hostname_format[i] = hf_uuid;
232 ERROR ("unknown HostnameFormat field: %s", fields[i]);
238 for (i = n; i < HF_MAX_FIELDS; ++i)
239 hostname_format[i] = hf_none;
244 /* Unrecognised option. */
255 /* `conn_string == NULL' is acceptable. */
256 conn = virConnectOpenReadOnly (conn_string);
258 c_complain (LOG_ERR, &conn_complain,
259 "libvirt plugin: Unable to connect: "
260 "virConnectOpenReadOnly failed.");
264 c_release (LOG_NOTICE, &conn_complain,
265 "libvirt plugin: Connection established.");
269 /* Need to refresh domain or device lists? */
270 if ((last_refresh == (time_t) 0) ||
271 ((interval > 0) && ((last_refresh + interval) <= t))) {
272 if (refresh_lists () != 0) {
274 virConnectClose (conn);
282 for (i = 0; i < nr_domains; ++i)
283 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
284 for (i = 0; i < nr_block_devices; ++i)
285 fprintf (stderr, "block device %d %s:%s\n",
286 i, virDomainGetName (block_devices[i].dom),
287 block_devices[i].path);
288 for (i = 0; i < nr_interface_devices; ++i)
289 fprintf (stderr, "interface device %d %s:%s\n",
290 i, virDomainGetName (interface_devices[i].dom),
291 interface_devices[i].path);
294 /* Get CPU usage, VCPU usage for each domain. */
295 for (i = 0; i < nr_domains; ++i) {
297 virVcpuInfoPtr vinfo = NULL;
300 if (virDomainGetInfo (domains[i], &info) != 0)
303 cpu_submit (info.cpuTime, t, domains[i], "virt_cpu_total");
305 vinfo = malloc (info.nrVirtCpu * sizeof vinfo[0]);
307 ERROR ("libvirt plugin: malloc failed.");
311 if (virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
317 for (j = 0; j < info.nrVirtCpu; ++j)
318 vcpu_submit (vinfo[j].cpuTime,
319 t, domains[i], vinfo[j].number, "virt_vcpu");
324 /* Get block device stats for each domain. */
325 for (i = 0; i < nr_block_devices; ++i) {
326 struct _virDomainBlockStats stats;
328 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
329 &stats, sizeof stats) != 0)
332 if ((stats.rd_req != -1) && (stats.wr_req != -1))
333 submit_counter2 ("disk_ops",
334 (counter_t) stats.rd_req, (counter_t) stats.wr_req,
335 t, block_devices[i].dom, block_devices[i].path);
337 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
338 submit_counter2 ("disk_octets",
339 (counter_t) stats.rd_bytes, (counter_t) stats.wr_bytes,
340 t, block_devices[i].dom, block_devices[i].path);
341 } /* for (nr_block_devices) */
343 /* Get interface stats for each domain. */
344 for (i = 0; i < nr_interface_devices; ++i) {
345 struct _virDomainInterfaceStats stats;
347 if (virDomainInterfaceStats (interface_devices[i].dom,
348 interface_devices[i].path,
349 &stats, sizeof stats) != 0)
352 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
353 submit_counter2 ("if_octets",
354 (counter_t) stats.rx_bytes, (counter_t) stats.tx_bytes,
355 t, interface_devices[i].dom, interface_devices[i].path);
357 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
358 submit_counter2 ("if_packets",
359 (counter_t) stats.rx_packets, (counter_t) stats.tx_packets,
360 t, interface_devices[i].dom, interface_devices[i].path);
362 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
363 submit_counter2 ("if_errors",
364 (counter_t) stats.rx_errs, (counter_t) stats.tx_errs,
365 t, interface_devices[i].dom, interface_devices[i].path);
367 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
368 submit_counter2 ("if_dropped",
369 (counter_t) stats.rx_drop, (counter_t) stats.tx_drop,
370 t, interface_devices[i].dom, interface_devices[i].path);
371 } /* for (nr_interface_devices) */
381 n = virConnectNumOfDomains (conn);
383 VIRT_ERROR (conn, "reading number of domains");
391 /* Get list of domains. */
392 domids = malloc (sizeof (int) * n);
394 ERROR ("libvirt plugin: malloc failed.");
398 n = virConnectListDomains (conn, domids, n);
400 VIRT_ERROR (conn, "reading list of domains");
405 free_block_devices ();
406 free_interface_devices ();
409 /* Fetch each domain and add it to the list, unless ignore. */
410 for (i = 0; i < n; ++i) {
411 virDomainPtr dom = NULL;
414 xmlDocPtr xml_doc = NULL;
415 xmlXPathContextPtr xpath_ctx = NULL;
416 xmlXPathObjectPtr xpath_obj = NULL;
419 dom = virDomainLookupByID (conn, domids[i]);
421 VIRT_ERROR (conn, "virDomainLookupByID");
422 /* Could be that the domain went away -- ignore it anyway. */
426 name = virDomainGetName (dom);
428 VIRT_ERROR (conn, "virDomainGetName");
432 if (il_domains && ignorelist_match (il_domains, name) != 0)
435 if (add_domain (dom) < 0) {
436 ERROR ("libvirt plugin: malloc failed.");
440 /* Get a list of devices for this domain. */
441 xml = virDomainGetXMLDesc (dom, 0);
443 VIRT_ERROR (conn, "virDomainGetXMLDesc");
447 /* Yuck, XML. Parse out the devices. */
448 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
449 if (xml_doc == NULL) {
450 VIRT_ERROR (conn, "xmlReadDoc");
454 xpath_ctx = xmlXPathNewContext (xml_doc);
457 xpath_obj = xmlXPathEval
458 ((xmlChar *) "/domain/devices/disk/target[@dev]",
460 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
461 xpath_obj->nodesetval == NULL)
464 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
468 node = xpath_obj->nodesetval->nodeTab[j];
470 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
473 if (il_block_devices &&
474 ignore_device_match (il_block_devices, name, path) != 0)
477 add_block_device (dom, path);
479 if (path) xmlFree (path);
481 xmlXPathFreeObject (xpath_obj);
483 /* Network interfaces. */
484 xpath_obj = xmlXPathEval
485 ((xmlChar *) "/domain/devices/interface/target[@dev]",
487 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
488 xpath_obj->nodesetval == NULL)
491 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
495 node = xpath_obj->nodesetval->nodeTab[j];
497 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
500 if (il_interface_devices &&
501 ignore_device_match (il_interface_devices, name, path) != 0)
504 add_interface_device (dom, path);
506 if (path) xmlFree (path);
510 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
511 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
512 if (xml_doc) xmlFreeDoc (xml_doc);
528 for (i = 0; i < nr_domains; ++i)
529 virDomainFree (domains[i]);
537 add_domain (virDomainPtr dom)
539 virDomainPtr *new_ptr;
540 int new_size = sizeof (domains[0]) * (nr_domains+1);
543 new_ptr = realloc (domains, new_size);
545 new_ptr = malloc (new_size);
551 domains[nr_domains] = dom;
556 free_block_devices ()
561 for (i = 0; i < nr_block_devices; ++i)
562 free (block_devices[i].path);
563 free (block_devices);
565 block_devices = NULL;
566 nr_block_devices = 0;
570 add_block_device (virDomainPtr dom, const char *path)
572 struct block_device *new_ptr;
573 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
576 path_copy = strdup (path);
581 new_ptr = realloc (block_devices, new_size);
583 new_ptr = malloc (new_size);
585 if (new_ptr == NULL) {
589 block_devices = new_ptr;
590 block_devices[nr_block_devices].dom = dom;
591 block_devices[nr_block_devices].path = path_copy;
592 return nr_block_devices++;
596 free_interface_devices ()
600 if (interface_devices) {
601 for (i = 0; i < nr_interface_devices; ++i)
602 free (interface_devices[i].path);
603 free (interface_devices);
605 interface_devices = NULL;
606 nr_interface_devices = 0;
610 add_interface_device (virDomainPtr dom, const char *path)
612 struct interface_device *new_ptr;
613 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
616 path_copy = strdup (path);
617 if (!path_copy) return -1;
619 if (interface_devices)
620 new_ptr = realloc (interface_devices, new_size);
622 new_ptr = malloc (new_size);
624 if (new_ptr == NULL) {
628 interface_devices = new_ptr;
629 interface_devices[nr_interface_devices].dom = dom;
630 interface_devices[nr_interface_devices].path = path_copy;
631 return nr_interface_devices++;
635 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
640 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
643 ERROR ("libvirt plugin: malloc failed.");
646 ssnprintf (name, n, "%s:%s", domname, devpath);
647 r = ignorelist_match (il, name);
653 init_value_list (value_list_t *vl, time_t t, virDomainPtr dom)
657 char uuid[VIR_UUID_STRING_BUFLEN];
662 vl->interval = interval_g;
664 sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
668 host_len = sizeof (vl->host);
670 /* Construct the hostname field according to HostnameFormat. */
671 for (i = 0; i < HF_MAX_FIELDS; ++i) {
672 if (hostname_format[i] == hf_none)
675 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
677 if (i > 0 && n >= 1) {
678 strncat (vl->host, ":", 1);
682 switch (hostname_format[i]) {
685 strncat (vl->host, hostname_g, n);
688 name = virDomainGetName (dom);
690 strncat (vl->host, name, n);
693 if (virDomainGetUUIDString (dom, uuid) == 0)
694 strncat (vl->host, uuid, n);
699 vl->host[sizeof (vl->host) - 1] = '\0';
700 } /* void init_value_list */
703 cpu_submit (unsigned long long cpu_time,
705 virDomainPtr dom, const char *type)
708 value_list_t vl = VALUE_LIST_INIT;
710 init_value_list (&vl, t, dom);
712 values[0].counter = cpu_time;
717 sstrncpy (vl.type, type, sizeof (vl.type));
719 plugin_dispatch_values (&vl);
723 vcpu_submit (counter_t cpu_time,
725 virDomainPtr dom, int vcpu_nr, const char *type)
728 value_list_t vl = VALUE_LIST_INIT;
730 init_value_list (&vl, t, dom);
732 values[0].counter = cpu_time;
736 sstrncpy (vl.type, type, sizeof (vl.type));
737 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
739 plugin_dispatch_values (&vl);
743 submit_counter2 (const char *type, counter_t v0, counter_t v1,
745 virDomainPtr dom, const char *devname)
748 value_list_t vl = VALUE_LIST_INIT;
750 init_value_list (&vl, t, dom);
752 values[0].counter = v0;
753 values[1].counter = v1;
757 sstrncpy (vl.type, type, sizeof (vl.type));
758 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
760 plugin_dispatch_values (&vl);
761 } /* void submit_counter2 */
766 free_block_devices ();
767 free_interface_devices ();
771 virConnectClose (conn);
774 ignorelist_free (il_domains);
776 ignorelist_free (il_block_devices);
777 il_block_devices = NULL;
778 ignorelist_free (il_interface_devices);
779 il_interface_devices = NULL;
785 module_register (void)
787 plugin_register_config ("libvirt",
789 config_keys, NR_CONFIG_KEYS);
790 plugin_register_init ("libvirt", lv_init);
791 plugin_register_read ("libvirt", lv_read);
792 plugin_register_shutdown ("libvirt", lv_shutdown);
796 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker