2 * collectd - src/virt.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>
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>
36 #define PLUGIN_NAME "virt"
38 static const char *config_keys[] = {
51 "PluginInstanceFormat",
55 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
58 static virConnectPtr conn = 0;
59 static char *conn_string = NULL;
60 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
62 /* Seconds between list refreshes, 0 disables completely. */
63 static int interval = 60;
65 /* List of domains, if specified. */
66 static ignorelist_t *il_domains = NULL;
67 /* List of block devices, if specified. */
68 static ignorelist_t *il_block_devices = NULL;
69 /* List of network interface devices, if specified. */
70 static ignorelist_t *il_interface_devices = NULL;
72 static int ignore_device_match (ignorelist_t *,
73 const char *domname, const char *devpath);
75 /* Actual list of domains found on last refresh. */
76 static virDomainPtr *domains = NULL;
77 static int nr_domains = 0;
79 static void free_domains (void);
80 static int add_domain (virDomainPtr dom);
82 /* Actual list of block devices found on last refresh. */
84 virDomainPtr dom; /* domain */
85 char *path; /* name of block device */
88 static struct block_device *block_devices = NULL;
89 static int nr_block_devices = 0;
91 static void free_block_devices (void);
92 static int add_block_device (virDomainPtr dom, const char *path);
94 /* Actual list of network interfaces found on last refresh. */
95 struct interface_device {
96 virDomainPtr dom; /* domain */
97 char *path; /* name of interface device */
98 char *address; /* mac address of interface device */
99 char *number; /* interface device number */
102 static struct interface_device *interface_devices = NULL;
103 static int nr_interface_devices = 0;
105 static void free_interface_devices (void);
106 static int add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number);
108 /* HostnameFormat. */
109 #define HF_MAX_FIELDS 3
118 static enum hf_field hostname_format[HF_MAX_FIELDS] =
121 /* PluginInstanceFormat */
122 #define PLGINST_MAX_FIELDS 2
130 static enum plginst_field plugin_instance_format[PLGINST_MAX_FIELDS] =
133 /* InterfaceFormat. */
140 static enum if_field interface_format = if_name;
142 /* Time that we last refreshed. */
143 static time_t last_refresh = (time_t) 0;
145 static int refresh_lists (void);
147 /* ERROR(...) macro for virterrors. */
148 #define VIRT_ERROR(conn,s) do { \
150 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
151 if (err) ERROR ("%s: %s", (s), err->message); \
155 init_value_list (value_list_t *vl, virDomainPtr dom)
159 char uuid[VIR_UUID_STRING_BUFLEN];
161 sstrncpy (vl->plugin, PLUGIN_NAME, sizeof (vl->plugin));
165 /* Construct the hostname field according to HostnameFormat. */
166 for (int i = 0; i < HF_MAX_FIELDS; ++i) {
167 if (hostname_format[i] == hf_none)
170 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
172 if (i > 0 && n >= 1) {
173 strncat (vl->host, ":", 1);
177 switch (hostname_format[i]) {
180 strncat (vl->host, hostname_g, n);
183 name = virDomainGetName (dom);
185 strncat (vl->host, name, n);
188 if (virDomainGetUUIDString (dom, uuid) == 0)
189 strncat (vl->host, uuid, n);
194 vl->host[sizeof (vl->host) - 1] = '\0';
196 /* Construct the plugin instance field according to PluginInstanceFormat. */
197 for (int i = 0; i < PLGINST_MAX_FIELDS; ++i) {
198 if (plugin_instance_format[i] == plginst_none)
201 n = sizeof(vl->plugin_instance) - strlen (vl->plugin_instance) - 2;
203 if (i > 0 && n >= 1) {
204 strncat (vl->plugin_instance, ":", 1);
208 switch (plugin_instance_format[i]) {
209 case plginst_none: break;
211 name = virDomainGetName (dom);
213 strncat (vl->plugin_instance, name, n);
216 if (virDomainGetUUIDString (dom, uuid) == 0)
217 strncat (vl->plugin_instance, uuid, n);
222 vl->plugin_instance[sizeof (vl->plugin_instance) - 1] = '\0';
224 } /* void init_value_list */
227 memory_submit (gauge_t memory, virDomainPtr dom)
230 value_list_t vl = VALUE_LIST_INIT;
232 init_value_list (&vl, dom);
234 values[0].gauge = memory;
239 sstrncpy (vl.type, "memory", sizeof (vl.type));
240 sstrncpy (vl.type_instance, "total", sizeof (vl.type_instance));
242 plugin_dispatch_values (&vl);
246 memory_stats_submit (gauge_t memory, virDomainPtr dom, int tag_index)
248 static const char *tags[] = { "swap_in", "swap_out", "major_fault", "minor_fault",
249 "unused", "available", "actual_balloon", "rss"};
252 value_list_t vl = VALUE_LIST_INIT;
254 init_value_list (&vl, dom);
256 values[0].gauge = memory;
261 sstrncpy (vl.type, "memory", sizeof (vl.type));
262 sstrncpy (vl.type_instance, tags[tag_index], sizeof (vl.type_instance));
264 plugin_dispatch_values (&vl);
268 cpu_submit (unsigned long long cpu_time,
269 virDomainPtr dom, const char *type)
272 value_list_t vl = VALUE_LIST_INIT;
274 init_value_list (&vl, dom);
276 values[0].derive = cpu_time;
281 sstrncpy (vl.type, type, sizeof (vl.type));
283 plugin_dispatch_values (&vl);
287 vcpu_submit (derive_t cpu_time,
288 virDomainPtr dom, int vcpu_nr, const char *type)
291 value_list_t vl = VALUE_LIST_INIT;
293 init_value_list (&vl, dom);
295 values[0].derive = cpu_time;
299 sstrncpy (vl.type, type, sizeof (vl.type));
300 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
302 plugin_dispatch_values (&vl);
306 submit_derive2 (const char *type, derive_t v0, derive_t v1,
307 virDomainPtr dom, const char *devname)
310 value_list_t vl = VALUE_LIST_INIT;
312 init_value_list (&vl, dom);
314 values[0].derive = v0;
315 values[1].derive = v1;
319 sstrncpy (vl.type, type, sizeof (vl.type));
320 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
322 plugin_dispatch_values (&vl);
323 } /* void submit_derive2 */
328 if (virInitialize () != 0)
335 lv_config (const char *key, const char *value)
337 if (virInitialize () != 0)
340 if (il_domains == NULL)
341 il_domains = ignorelist_create (1);
342 if (il_block_devices == NULL)
343 il_block_devices = ignorelist_create (1);
344 if (il_interface_devices == NULL)
345 il_interface_devices = ignorelist_create (1);
347 if (strcasecmp (key, "Connection") == 0) {
348 char *tmp = strdup (value);
350 ERROR (PLUGIN_NAME " plugin: Connection strdup failed.");
358 if (strcasecmp (key, "RefreshInterval") == 0) {
360 interval = strtol (value, &eptr, 10);
361 if (eptr == NULL || *eptr != '\0') return 1;
365 if (strcasecmp (key, "Domain") == 0) {
366 if (ignorelist_add (il_domains, value)) return 1;
369 if (strcasecmp (key, "BlockDevice") == 0) {
370 if (ignorelist_add (il_block_devices, value)) return 1;
373 if (strcasecmp (key, "InterfaceDevice") == 0) {
374 if (ignorelist_add (il_interface_devices, value)) return 1;
378 if (strcasecmp (key, "IgnoreSelected") == 0) {
381 ignorelist_set_invert (il_domains, 0);
382 ignorelist_set_invert (il_block_devices, 0);
383 ignorelist_set_invert (il_interface_devices, 0);
387 ignorelist_set_invert (il_domains, 1);
388 ignorelist_set_invert (il_block_devices, 1);
389 ignorelist_set_invert (il_interface_devices, 1);
394 if (strcasecmp (key, "HostnameFormat") == 0) {
396 char *fields[HF_MAX_FIELDS];
399 value_copy = strdup (value);
400 if (value_copy == NULL) {
401 ERROR (PLUGIN_NAME " plugin: strdup failed.");
405 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
408 ERROR (PLUGIN_NAME " plugin: HostnameFormat: no fields");
412 for (int i = 0; i < n; ++i) {
413 if (strcasecmp (fields[i], "hostname") == 0)
414 hostname_format[i] = hf_hostname;
415 else if (strcasecmp (fields[i], "name") == 0)
416 hostname_format[i] = hf_name;
417 else if (strcasecmp (fields[i], "uuid") == 0)
418 hostname_format[i] = hf_uuid;
420 ERROR (PLUGIN_NAME " plugin: unknown HostnameFormat field: %s", fields[i]);
427 for (int i = n; i < HF_MAX_FIELDS; ++i)
428 hostname_format[i] = hf_none;
433 if (strcasecmp (key, "PluginInstanceFormat") == 0) {
435 char *fields[PLGINST_MAX_FIELDS];
438 value_copy = strdup (value);
439 if (value_copy == NULL) {
440 ERROR (PLUGIN_NAME " plugin: strdup failed.");
444 n = strsplit (value_copy, fields, PLGINST_MAX_FIELDS);
447 ERROR (PLUGIN_NAME " plugin: PluginInstanceFormat: no fields");
451 for (int i = 0; i < n; ++i) {
452 if (strcasecmp (fields[i], "none") == 0) {
453 plugin_instance_format[i] = plginst_none;
455 } else if (strcasecmp (fields[i], "name") == 0)
456 plugin_instance_format[i] = plginst_name;
457 else if (strcasecmp (fields[i], "uuid") == 0)
458 plugin_instance_format[i] = plginst_uuid;
460 ERROR (PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s", fields[i]);
467 for (int i = n; i < PLGINST_MAX_FIELDS; ++i)
468 plugin_instance_format[i] = plginst_none;
473 if (strcasecmp (key, "InterfaceFormat") == 0) {
474 if (strcasecmp (value, "name") == 0)
475 interface_format = if_name;
476 else if (strcasecmp (value, "address") == 0)
477 interface_format = if_address;
478 else if (strcasecmp (value, "number") == 0)
479 interface_format = if_number;
481 ERROR (PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", value);
487 /* Unrecognised option. */
497 /* `conn_string == NULL' is acceptable. */
498 conn = virConnectOpenReadOnly (conn_string);
500 c_complain (LOG_ERR, &conn_complain,
501 PLUGIN_NAME " plugin: Unable to connect: "
502 "virConnectOpenReadOnly failed.");
506 c_release (LOG_NOTICE, &conn_complain,
507 PLUGIN_NAME " plugin: Connection established.");
511 /* Need to refresh domain or device lists? */
512 if ((last_refresh == (time_t) 0) ||
513 ((interval > 0) && ((last_refresh + interval) <= t))) {
514 if (refresh_lists () != 0) {
516 virConnectClose (conn);
524 for (int i = 0; i < nr_domains; ++i)
525 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
526 for (int i = 0; i < nr_block_devices; ++i)
527 fprintf (stderr, "block device %d %s:%s\n",
528 i, virDomainGetName (block_devices[i].dom),
529 block_devices[i].path);
530 for (int i = 0; i < nr_interface_devices; ++i)
531 fprintf (stderr, "interface device %d %s:%s\n",
532 i, virDomainGetName (interface_devices[i].dom),
533 interface_devices[i].path);
536 /* Get CPU usage, memory, VCPU usage for each domain. */
537 for (int i = 0; i < nr_domains; ++i) {
539 virVcpuInfoPtr vinfo = NULL;
540 virDomainMemoryStatPtr minfo = NULL;
543 status = virDomainGetInfo (domains[i], &info);
546 ERROR (PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
551 if (info.state != VIR_DOMAIN_RUNNING)
553 /* only gather stats for running domains */
557 cpu_submit (info.cpuTime, domains[i], "virt_cpu_total");
558 memory_submit ((gauge_t) info.memory * 1024, domains[i]);
560 vinfo = malloc (info.nrVirtCpu * sizeof (vinfo[0]));
562 ERROR (PLUGIN_NAME " plugin: malloc failed.");
566 status = virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
567 /* cpu map = */ NULL, /* cpu map length = */ 0);
570 ERROR (PLUGIN_NAME " plugin: virDomainGetVcpus failed with status %i.",
576 for (int j = 0; j < info.nrVirtCpu; ++j)
577 vcpu_submit (vinfo[j].cpuTime,
578 domains[i], vinfo[j].number, "virt_vcpu");
582 minfo = malloc (VIR_DOMAIN_MEMORY_STAT_NR * sizeof (virDomainMemoryStatStruct));
584 ERROR ("virt plugin: malloc failed.");
588 status = virDomainMemoryStats (domains[i], minfo, VIR_DOMAIN_MEMORY_STAT_NR, 0);
591 ERROR ("virt plugin: virDomainMemoryStats failed with status %i.",
597 for (int j = 0; j < status; j++) {
598 memory_stats_submit ((gauge_t) minfo[j].val * 1024, domains[i], minfo[j].tag);
605 /* Get block device stats for each domain. */
606 for (int i = 0; i < nr_block_devices; ++i) {
607 struct _virDomainBlockStats stats;
609 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
610 &stats, sizeof stats) != 0)
613 if ((stats.rd_req != -1) && (stats.wr_req != -1))
614 submit_derive2 ("disk_ops",
615 (derive_t) stats.rd_req, (derive_t) stats.wr_req,
616 block_devices[i].dom, block_devices[i].path);
618 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
619 submit_derive2 ("disk_octets",
620 (derive_t) stats.rd_bytes, (derive_t) stats.wr_bytes,
621 block_devices[i].dom, block_devices[i].path);
622 } /* for (nr_block_devices) */
624 /* Get interface stats for each domain. */
625 for (int i = 0; i < nr_interface_devices; ++i) {
626 struct _virDomainInterfaceStats stats;
627 char *display_name = NULL;
630 switch (interface_format) {
632 display_name = interface_devices[i].address;
635 display_name = interface_devices[i].number;
639 display_name = interface_devices[i].path;
642 if (virDomainInterfaceStats (interface_devices[i].dom,
643 interface_devices[i].path,
644 &stats, sizeof stats) != 0)
647 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
648 submit_derive2 ("if_octets",
649 (derive_t) stats.rx_bytes, (derive_t) stats.tx_bytes,
650 interface_devices[i].dom, display_name);
652 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
653 submit_derive2 ("if_packets",
654 (derive_t) stats.rx_packets, (derive_t) stats.tx_packets,
655 interface_devices[i].dom, display_name);
657 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
658 submit_derive2 ("if_errors",
659 (derive_t) stats.rx_errs, (derive_t) stats.tx_errs,
660 interface_devices[i].dom, display_name);
662 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
663 submit_derive2 ("if_dropped",
664 (derive_t) stats.rx_drop, (derive_t) stats.tx_drop,
665 interface_devices[i].dom, display_name);
666 } /* for (nr_interface_devices) */
676 n = virConnectNumOfDomains (conn);
678 VIRT_ERROR (conn, "reading number of domains");
685 /* Get list of domains. */
686 domids = malloc (sizeof (*domids) * n);
687 if (domids == NULL) {
688 ERROR (PLUGIN_NAME " plugin: malloc failed.");
692 n = virConnectListDomains (conn, domids, n);
694 VIRT_ERROR (conn, "reading list of domains");
699 free_block_devices ();
700 free_interface_devices ();
703 /* Fetch each domain and add it to the list, unless ignore. */
704 for (int i = 0; i < n; ++i) {
705 virDomainPtr dom = NULL;
708 xmlDocPtr xml_doc = NULL;
709 xmlXPathContextPtr xpath_ctx = NULL;
710 xmlXPathObjectPtr xpath_obj = NULL;
712 dom = virDomainLookupByID (conn, domids[i]);
714 VIRT_ERROR (conn, "virDomainLookupByID");
715 /* Could be that the domain went away -- ignore it anyway. */
719 name = virDomainGetName (dom);
721 VIRT_ERROR (conn, "virDomainGetName");
725 if (il_domains && ignorelist_match (il_domains, name) != 0)
728 if (add_domain (dom) < 0) {
729 ERROR (PLUGIN_NAME " plugin: malloc failed.");
733 /* Get a list of devices for this domain. */
734 xml = virDomainGetXMLDesc (dom, 0);
736 VIRT_ERROR (conn, "virDomainGetXMLDesc");
740 /* Yuck, XML. Parse out the devices. */
741 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
742 if (xml_doc == NULL) {
743 VIRT_ERROR (conn, "xmlReadDoc");
747 xpath_ctx = xmlXPathNewContext (xml_doc);
750 xpath_obj = xmlXPathEval
751 ((xmlChar *) "/domain/devices/disk/target[@dev]",
753 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
754 xpath_obj->nodesetval == NULL)
757 for (int j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
761 node = xpath_obj->nodesetval->nodeTab[j];
763 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
766 if (il_block_devices &&
767 ignore_device_match (il_block_devices, name, path) != 0)
770 add_block_device (dom, path);
772 if (path) xmlFree (path);
774 xmlXPathFreeObject (xpath_obj);
776 /* Network interfaces. */
777 xpath_obj = xmlXPathEval
778 ((xmlChar *) "/domain/devices/interface[target[@dev]]",
780 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
781 xpath_obj->nodesetval == NULL)
784 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
786 for (int j = 0; j < xml_interfaces->nodeNr; ++j) {
788 char *address = NULL;
789 xmlNodePtr xml_interface;
791 xml_interface = xml_interfaces->nodeTab[j];
792 if (!xml_interface) continue;
794 for (xmlNodePtr child = xml_interface->children; child; child = child->next) {
795 if (child->type != XML_ELEMENT_NODE) continue;
797 if (xmlStrEqual(child->name, (const xmlChar *) "target")) {
798 path = (char *) xmlGetProp (child, (const xmlChar *) "dev");
800 } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) {
801 address = (char *) xmlGetProp (child, (const xmlChar *) "address");
802 if (!address) continue;
806 if (il_interface_devices &&
807 (ignore_device_match (il_interface_devices, name, path) != 0 ||
808 ignore_device_match (il_interface_devices, name, address) != 0))
811 add_interface_device (dom, path, address, j+1);
813 if (path) xmlFree (path);
814 if (address) xmlFree (address);
818 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
819 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
820 if (xml_doc) xmlFreeDoc (xml_doc);
834 for (int i = 0; i < nr_domains; ++i)
835 virDomainFree (domains[i]);
843 add_domain (virDomainPtr dom)
845 virDomainPtr *new_ptr;
846 int new_size = sizeof (domains[0]) * (nr_domains+1);
849 new_ptr = realloc (domains, new_size);
851 new_ptr = malloc (new_size);
857 domains[nr_domains] = dom;
862 free_block_devices (void)
865 for (int i = 0; i < nr_block_devices; ++i)
866 sfree (block_devices[i].path);
867 sfree (block_devices);
869 block_devices = NULL;
870 nr_block_devices = 0;
874 add_block_device (virDomainPtr dom, const char *path)
876 struct block_device *new_ptr;
877 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
880 path_copy = strdup (path);
885 new_ptr = realloc (block_devices, new_size);
887 new_ptr = malloc (new_size);
889 if (new_ptr == NULL) {
893 block_devices = new_ptr;
894 block_devices[nr_block_devices].dom = dom;
895 block_devices[nr_block_devices].path = path_copy;
896 return nr_block_devices++;
900 free_interface_devices (void)
902 if (interface_devices) {
903 for (int i = 0; i < nr_interface_devices; ++i) {
904 sfree (interface_devices[i].path);
905 sfree (interface_devices[i].address);
906 sfree (interface_devices[i].number);
908 sfree (interface_devices);
910 interface_devices = NULL;
911 nr_interface_devices = 0;
915 add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number)
917 struct interface_device *new_ptr;
918 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
919 char *path_copy, *address_copy, number_string[15];
921 if ((path == NULL) || (address == NULL))
924 path_copy = strdup (path);
925 if (!path_copy) return -1;
927 address_copy = strdup (address);
933 snprintf(number_string, sizeof (number_string), "interface-%u", number);
935 if (interface_devices)
936 new_ptr = realloc (interface_devices, new_size);
938 new_ptr = malloc (new_size);
940 if (new_ptr == NULL) {
942 sfree (address_copy);
945 interface_devices = new_ptr;
946 interface_devices[nr_interface_devices].dom = dom;
947 interface_devices[nr_interface_devices].path = path_copy;
948 interface_devices[nr_interface_devices].address = address_copy;
949 interface_devices[nr_interface_devices].number = strdup(number_string);
950 return nr_interface_devices++;
954 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
959 if ((domname == NULL) || (devpath == NULL))
962 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
965 ERROR (PLUGIN_NAME " plugin: malloc failed.");
968 ssnprintf (name, n, "%s:%s", domname, devpath);
969 r = ignorelist_match (il, name);
977 free_block_devices ();
978 free_interface_devices ();
982 virConnectClose (conn);
985 ignorelist_free (il_domains);
987 ignorelist_free (il_block_devices);
988 il_block_devices = NULL;
989 ignorelist_free (il_interface_devices);
990 il_interface_devices = NULL;
996 module_register (void)
998 plugin_register_config (PLUGIN_NAME,
1000 config_keys, NR_CONFIG_KEYS);
1001 plugin_register_init (PLUGIN_NAME, lv_init);
1002 plugin_register_read (PLUGIN_NAME, lv_read);
1003 plugin_register_shutdown (PLUGIN_NAME, lv_shutdown);
1007 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker