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;
53 /* Seconds between list refreshes, 0 disables completely. */
54 static int interval = 60;
56 /* List of domains, if specified. */
57 static ignorelist_t *il_domains = NULL;
58 /* List of block devices, if specified. */
59 static ignorelist_t *il_block_devices = NULL;
60 /* List of network interface devices, if specified. */
61 static ignorelist_t *il_interface_devices = NULL;
63 static int ignore_device_match (ignorelist_t *,
64 const char *domname, const char *devpath);
66 /* Actual list of domains found on last refresh. */
67 static virDomainPtr *domains = NULL;
68 static int nr_domains = 0;
70 static void free_domains (void);
71 static int add_domain (virDomainPtr dom);
73 /* Actual list of block devices found on last refresh. */
75 virDomainPtr dom; /* domain */
76 char *path; /* name of block device */
79 static struct block_device *block_devices = NULL;
80 static int nr_block_devices = 0;
82 static void free_block_devices (void);
83 static int add_block_device (virDomainPtr dom, const char *path);
85 /* Actual list of network interfaces found on last refresh. */
86 struct interface_device {
87 virDomainPtr dom; /* domain */
88 char *path; /* name of interface device */
91 static struct interface_device *interface_devices = NULL;
92 static int nr_interface_devices = 0;
94 static void free_interface_devices (void);
95 static int add_interface_device (virDomainPtr dom, const char *path);
98 #define HF_MAX_FIELDS 3
107 static enum hf_field hostname_format[HF_MAX_FIELDS] =
110 /* Time that we last refreshed. */
111 static time_t last_refresh = (time_t) 0;
113 static int refresh_lists (void);
115 /* Submit functions. */
116 static void cpu_submit (unsigned long long cpu_time,
118 virDomainPtr dom, const char *type);
119 static void vcpu_submit (unsigned long long cpu_time,
121 virDomainPtr dom, int vcpu_nr, const char *type);
122 static void submit_counter2 (const char *type, counter_t v0, counter_t v1,
124 virDomainPtr dom, const char *devname);
126 /* ERROR(...) macro for virterrors. */
127 #define VIRT_ERROR(conn,s) do { \
129 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
130 if (err) ERROR ("%s: %s", (s), err->message); \
136 if (virInitialize () != 0)
143 lv_config (const char *key, const char *value)
145 if (virInitialize () != 0)
148 if (il_domains == NULL)
149 il_domains = ignorelist_create (1);
150 if (il_block_devices == NULL)
151 il_block_devices = ignorelist_create (1);
152 if (il_interface_devices == NULL)
153 il_interface_devices = ignorelist_create (1);
155 if (strcasecmp (key, "Connection") == 0) {
157 ERROR ("Connection may only be given once in config file");
160 conn = virConnectOpenReadOnly (value);
162 VIRT_ERROR (NULL, "connection failed");
168 if (strcasecmp (key, "RefreshInterval") == 0) {
170 interval = strtol (value, &eptr, 10);
171 if (eptr == NULL || *eptr != '\0') return 1;
175 if (strcasecmp (key, "Domain") == 0) {
176 if (ignorelist_add (il_domains, value)) return 1;
179 if (strcasecmp (key, "BlockDevice") == 0) {
180 if (ignorelist_add (il_block_devices, value)) return 1;
183 if (strcasecmp (key, "InterfaceDevice") == 0) {
184 if (ignorelist_add (il_interface_devices, value)) return 1;
188 if (strcasecmp (key, "IgnoreSelected") == 0) {
189 if (strcasecmp (value, "True") == 0 ||
190 strcasecmp (value, "Yes") == 0 ||
191 strcasecmp (value, "On") == 0)
193 ignorelist_set_invert (il_domains, 0);
194 ignorelist_set_invert (il_block_devices, 0);
195 ignorelist_set_invert (il_interface_devices, 0);
199 ignorelist_set_invert (il_domains, 1);
200 ignorelist_set_invert (il_block_devices, 1);
201 ignorelist_set_invert (il_interface_devices, 1);
206 if (strcasecmp (key, "HostnameFormat") == 0) {
208 char *fields[HF_MAX_FIELDS];
211 value_copy = strdup (value);
212 if (value_copy == NULL) {
213 ERROR ("libvirt plugin: strdup failed.");
217 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
220 ERROR ("HostnameFormat: no fields");
224 for (i = 0; i < n; ++i) {
225 if (strcasecmp (fields[i], "hostname") == 0)
226 hostname_format[i] = hf_hostname;
227 else if (strcasecmp (fields[i], "name") == 0)
228 hostname_format[i] = hf_name;
229 else if (strcasecmp (fields[i], "uuid") == 0)
230 hostname_format[i] = hf_uuid;
233 ERROR ("unknown HostnameFormat field: %s", fields[i]);
239 for (i = n; i < HF_MAX_FIELDS; ++i)
240 hostname_format[i] = hf_none;
245 /* Unrecognised option. */
256 ERROR ("libvirt plugin: Not connected. Use Connection in "
257 "config file to supply connection URI. For more information "
258 "see <http://libvirt.org/uri.html>");
264 /* Need to refresh domain or device lists? */
265 if ((last_refresh == (time_t) 0) ||
266 ((interval > 0) && ((last_refresh + interval) <= t))) {
267 if (refresh_lists () != 0)
273 for (i = 0; i < nr_domains; ++i)
274 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
275 for (i = 0; i < nr_block_devices; ++i)
276 fprintf (stderr, "block device %d %s:%s\n",
277 i, virDomainGetName (block_devices[i].dom),
278 block_devices[i].path);
279 for (i = 0; i < nr_interface_devices; ++i)
280 fprintf (stderr, "interface device %d %s:%s\n",
281 i, virDomainGetName (interface_devices[i].dom),
282 interface_devices[i].path);
285 /* Get CPU usage, VCPU usage for each domain. */
286 for (i = 0; i < nr_domains; ++i) {
288 virVcpuInfoPtr vinfo = NULL;
291 if (virDomainGetInfo (domains[i], &info) != 0)
294 cpu_submit (info.cpuTime, t, domains[i], "virt_cpu_total");
296 vinfo = malloc (info.nrVirtCpu * sizeof vinfo[0]);
298 ERROR ("libvirt plugin: malloc failed.");
302 if (virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
308 for (j = 0; j < info.nrVirtCpu; ++j)
309 vcpu_submit (vinfo[j].cpuTime,
310 t, domains[i], vinfo[j].number, "virt_vcpu");
315 /* Get block device stats for each domain. */
316 for (i = 0; i < nr_block_devices; ++i) {
317 struct _virDomainBlockStats stats;
319 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
320 &stats, sizeof stats) != 0)
323 if ((stats.rd_req != -1) && (stats.wr_req != -1))
324 submit_counter2 ("disk_ops",
325 (counter_t) stats.rd_req, (counter_t) stats.wr_req,
326 t, block_devices[i].dom, block_devices[i].path);
328 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
329 submit_counter2 ("disk_octets",
330 (counter_t) stats.rd_bytes, (counter_t) stats.wr_bytes,
331 t, block_devices[i].dom, block_devices[i].path);
332 } /* for (nr_block_devices) */
334 /* Get interface stats for each domain. */
335 for (i = 0; i < nr_interface_devices; ++i) {
336 struct _virDomainInterfaceStats stats;
338 if (virDomainInterfaceStats (interface_devices[i].dom,
339 interface_devices[i].path,
340 &stats, sizeof stats) != 0)
343 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
344 submit_counter2 ("if_octets",
345 (counter_t) stats.rx_bytes, (counter_t) stats.tx_bytes,
346 t, interface_devices[i].dom, interface_devices[i].path);
348 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
349 submit_counter2 ("if_packets",
350 (counter_t) stats.rx_packets, (counter_t) stats.tx_packets,
351 t, interface_devices[i].dom, interface_devices[i].path);
353 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
354 submit_counter2 ("if_errors",
355 (counter_t) stats.rx_errs, (counter_t) stats.tx_errs,
356 t, interface_devices[i].dom, interface_devices[i].path);
358 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
359 submit_counter2 ("if_dropped",
360 (counter_t) stats.rx_drop, (counter_t) stats.tx_drop,
361 t, interface_devices[i].dom, interface_devices[i].path);
362 } /* for (nr_interface_devices) */
372 n = virConnectNumOfDomains (conn);
374 VIRT_ERROR (conn, "reading number of domains");
382 /* Get list of domains. */
383 domids = malloc (sizeof (int) * n);
385 ERROR ("libvirt plugin: malloc failed.");
389 n = virConnectListDomains (conn, domids, n);
391 VIRT_ERROR (conn, "reading list of domains");
396 free_block_devices ();
397 free_interface_devices ();
400 /* Fetch each domain and add it to the list, unless ignore. */
401 for (i = 0; i < n; ++i) {
402 virDomainPtr dom = NULL;
405 xmlDocPtr xml_doc = NULL;
406 xmlXPathContextPtr xpath_ctx = NULL;
407 xmlXPathObjectPtr xpath_obj = NULL;
410 dom = virDomainLookupByID (conn, domids[i]);
412 VIRT_ERROR (conn, "virDomainLookupByID");
413 /* Could be that the domain went away -- ignore it anyway. */
417 name = virDomainGetName (dom);
419 VIRT_ERROR (conn, "virDomainGetName");
423 if (il_domains && ignorelist_match (il_domains, name) != 0)
426 if (add_domain (dom) < 0) {
427 ERROR ("libvirt plugin: malloc failed.");
431 /* Get a list of devices for this domain. */
432 xml = virDomainGetXMLDesc (dom, 0);
434 VIRT_ERROR (conn, "virDomainGetXMLDesc");
438 /* Yuck, XML. Parse out the devices. */
439 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
440 if (xml_doc == NULL) {
441 VIRT_ERROR (conn, "xmlReadDoc");
445 xpath_ctx = xmlXPathNewContext (xml_doc);
448 xpath_obj = xmlXPathEval
449 ((xmlChar *) "/domain/devices/disk/target[@dev]",
451 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
452 xpath_obj->nodesetval == NULL)
455 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
459 node = xpath_obj->nodesetval->nodeTab[j];
461 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
464 if (il_block_devices &&
465 ignore_device_match (il_block_devices, name, path) != 0)
468 add_block_device (dom, path);
470 if (path) xmlFree (path);
472 xmlXPathFreeObject (xpath_obj);
474 /* Network interfaces. */
475 xpath_obj = xmlXPathEval
476 ((xmlChar *) "/domain/devices/interface/target[@dev]",
478 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
479 xpath_obj->nodesetval == NULL)
482 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
486 node = xpath_obj->nodesetval->nodeTab[j];
488 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
491 if (il_interface_devices &&
492 ignore_device_match (il_interface_devices, name, path) != 0)
495 add_interface_device (dom, path);
497 if (path) xmlFree (path);
501 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
502 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
503 if (xml_doc) xmlFreeDoc (xml_doc);
519 for (i = 0; i < nr_domains; ++i)
520 virDomainFree (domains[i]);
528 add_domain (virDomainPtr dom)
530 virDomainPtr *new_ptr;
531 int new_size = sizeof (domains[0]) * (nr_domains+1);
534 new_ptr = realloc (domains, new_size);
536 new_ptr = malloc (new_size);
542 domains[nr_domains] = dom;
547 free_block_devices ()
552 for (i = 0; i < nr_block_devices; ++i)
553 free (block_devices[i].path);
554 free (block_devices);
556 block_devices = NULL;
557 nr_block_devices = 0;
561 add_block_device (virDomainPtr dom, const char *path)
563 struct block_device *new_ptr;
564 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
567 path_copy = strdup (path);
572 new_ptr = realloc (block_devices, new_size);
574 new_ptr = malloc (new_size);
576 if (new_ptr == NULL) {
580 block_devices = new_ptr;
581 block_devices[nr_block_devices].dom = dom;
582 block_devices[nr_block_devices].path = path_copy;
583 return nr_block_devices++;
587 free_interface_devices ()
591 if (interface_devices) {
592 for (i = 0; i < nr_interface_devices; ++i)
593 free (interface_devices[i].path);
594 free (interface_devices);
596 interface_devices = NULL;
597 nr_interface_devices = 0;
601 add_interface_device (virDomainPtr dom, const char *path)
603 struct interface_device *new_ptr;
604 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
607 path_copy = strdup (path);
608 if (!path_copy) return -1;
610 if (interface_devices)
611 new_ptr = realloc (interface_devices, new_size);
613 new_ptr = malloc (new_size);
615 if (new_ptr == NULL) {
619 interface_devices = new_ptr;
620 interface_devices[nr_interface_devices].dom = dom;
621 interface_devices[nr_interface_devices].path = path_copy;
622 return nr_interface_devices++;
626 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
631 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
634 ERROR ("libvirt plugin: malloc failed.");
637 snprintf (name, n, "%s:%s", domname, devpath);
638 r = ignorelist_match (il, name);
644 init_value_list (value_list_t *vl, time_t t, virDomainPtr dom)
648 char uuid[VIR_UUID_STRING_BUFLEN];
653 vl->interval = interval_g;
655 strncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
656 vl->plugin[sizeof (vl->plugin) - 1] = '\0';
660 host_len = sizeof (vl->host);
662 /* Construct the hostname field according to HostnameFormat. */
663 for (i = 0; i < HF_MAX_FIELDS; ++i) {
664 if (hostname_format[i] == hf_none)
667 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
669 if (i > 0 && n >= 1) {
670 strcat (vl->host, ":");
674 switch (hostname_format[i]) {
677 strncat (vl->host, hostname_g, n);
680 name = virDomainGetName (dom);
682 strncat (vl->host, name, n);
685 if (virDomainGetUUIDString (dom, uuid) == 0)
686 strncat (vl->host, uuid, n);
691 vl->host[sizeof (vl->host) - 1] = '\0';
692 } /* void init_value_list */
695 cpu_submit (unsigned long long cpu_time,
697 virDomainPtr dom, const char *type)
700 value_list_t vl = VALUE_LIST_INIT;
702 init_value_list (&vl, t, dom);
704 values[0].counter = cpu_time;
709 plugin_dispatch_values (type, &vl);
713 vcpu_submit (counter_t cpu_time,
715 virDomainPtr dom, int vcpu_nr, const char *type)
718 value_list_t vl = VALUE_LIST_INIT;
720 init_value_list (&vl, t, dom);
722 values[0].counter = cpu_time;
726 snprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
727 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
729 plugin_dispatch_values (type, &vl);
733 submit_counter2 (const char *type, counter_t v0, counter_t v1,
735 virDomainPtr dom, const char *devname)
738 value_list_t vl = VALUE_LIST_INIT;
740 init_value_list (&vl, t, dom);
742 values[0].counter = v0;
743 values[1].counter = v1;
747 strncpy (vl.type_instance, devname, sizeof (vl.type_instance));
748 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
750 plugin_dispatch_values (type, &vl);
751 } /* void submit_counter2 */
756 free_block_devices ();
757 free_interface_devices ();
761 virConnectClose (conn);
764 ignorelist_free (il_domains);
766 ignorelist_free (il_block_devices);
767 il_block_devices = NULL;
768 ignorelist_free (il_interface_devices);
769 il_interface_devices = NULL;
775 module_register (void)
777 plugin_register_config ("libvirt",
779 config_keys, NR_CONFIG_KEYS);
780 plugin_register_init ("libvirt", lv_init);
781 plugin_register_read ("libvirt", lv_read);
782 plugin_register_shutdown ("libvirt", lv_shutdown);
786 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker