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 */
226 static void submit (virDomainPtr dom,
227 char const *type, char const *type_instance,
228 value_t *values, size_t values_len)
230 value_list_t vl = VALUE_LIST_INIT;
231 init_value_list (&vl, dom);
234 vl.values_len = values_len;
236 sstrncpy (vl.type, type, sizeof (vl.type));
237 if (type_instance != NULL)
238 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
240 plugin_dispatch_values (&vl);
244 memory_submit (gauge_t value, virDomainPtr dom)
246 submit (dom, "memory", "total", &(value_t) { .gauge = value }, 1);
250 memory_stats_submit (gauge_t value, virDomainPtr dom, int tag_index)
252 static const char *tags[] = { "swap_in", "swap_out", "major_fault", "minor_fault",
253 "unused", "available", "actual_balloon", "rss"};
255 if ((tag_index < 0) || (tag_index >= STATIC_ARRAY_SIZE (tags))) {
256 ERROR ("virt plugin: Array index out of bounds: tag_index = %d", tag_index);
260 submit (dom, "memory", tags[tag_index], &(value_t) { .gauge = value }, 1);
264 cpu_submit (unsigned long long value,
265 virDomainPtr dom, const char *type)
267 submit (dom, type, NULL, &(value_t) { .derive = (derive_t) value }, 1);
271 vcpu_submit (derive_t value,
272 virDomainPtr dom, int vcpu_nr, const char *type)
274 char type_instance[DATA_MAX_NAME_LEN];
276 ssnprintf (type_instance, sizeof (type_instance), "%d", vcpu_nr);
278 submit (dom, type, type_instance, &(value_t) { .derive = value }, 1);
282 submit_derive2 (const char *type, derive_t v0, derive_t v1,
283 virDomainPtr dom, const char *devname)
286 value_list_t vl = VALUE_LIST_INIT;
288 init_value_list (&vl, dom);
290 values[0].derive = v0;
291 values[1].derive = v1;
295 sstrncpy (vl.type, type, sizeof (vl.type));
296 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
298 plugin_dispatch_values (&vl);
299 } /* void submit_derive2 */
304 if (virInitialize () != 0)
311 lv_config (const char *key, const char *value)
313 if (virInitialize () != 0)
316 if (il_domains == NULL)
317 il_domains = ignorelist_create (1);
318 if (il_block_devices == NULL)
319 il_block_devices = ignorelist_create (1);
320 if (il_interface_devices == NULL)
321 il_interface_devices = ignorelist_create (1);
323 if (strcasecmp (key, "Connection") == 0) {
324 char *tmp = strdup (value);
326 ERROR (PLUGIN_NAME " plugin: Connection strdup failed.");
334 if (strcasecmp (key, "RefreshInterval") == 0) {
336 interval = strtol (value, &eptr, 10);
337 if (eptr == NULL || *eptr != '\0') return 1;
341 if (strcasecmp (key, "Domain") == 0) {
342 if (ignorelist_add (il_domains, value)) return 1;
345 if (strcasecmp (key, "BlockDevice") == 0) {
346 if (ignorelist_add (il_block_devices, value)) return 1;
349 if (strcasecmp (key, "InterfaceDevice") == 0) {
350 if (ignorelist_add (il_interface_devices, value)) return 1;
354 if (strcasecmp (key, "IgnoreSelected") == 0) {
357 ignorelist_set_invert (il_domains, 0);
358 ignorelist_set_invert (il_block_devices, 0);
359 ignorelist_set_invert (il_interface_devices, 0);
363 ignorelist_set_invert (il_domains, 1);
364 ignorelist_set_invert (il_block_devices, 1);
365 ignorelist_set_invert (il_interface_devices, 1);
370 if (strcasecmp (key, "HostnameFormat") == 0) {
372 char *fields[HF_MAX_FIELDS];
375 value_copy = strdup (value);
376 if (value_copy == NULL) {
377 ERROR (PLUGIN_NAME " plugin: strdup failed.");
381 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
384 ERROR (PLUGIN_NAME " plugin: HostnameFormat: no fields");
388 for (int i = 0; i < n; ++i) {
389 if (strcasecmp (fields[i], "hostname") == 0)
390 hostname_format[i] = hf_hostname;
391 else if (strcasecmp (fields[i], "name") == 0)
392 hostname_format[i] = hf_name;
393 else if (strcasecmp (fields[i], "uuid") == 0)
394 hostname_format[i] = hf_uuid;
396 ERROR (PLUGIN_NAME " plugin: unknown HostnameFormat field: %s", fields[i]);
403 for (int i = n; i < HF_MAX_FIELDS; ++i)
404 hostname_format[i] = hf_none;
409 if (strcasecmp (key, "PluginInstanceFormat") == 0) {
411 char *fields[PLGINST_MAX_FIELDS];
414 value_copy = strdup (value);
415 if (value_copy == NULL) {
416 ERROR (PLUGIN_NAME " plugin: strdup failed.");
420 n = strsplit (value_copy, fields, PLGINST_MAX_FIELDS);
423 ERROR (PLUGIN_NAME " plugin: PluginInstanceFormat: no fields");
427 for (int i = 0; i < n; ++i) {
428 if (strcasecmp (fields[i], "none") == 0) {
429 plugin_instance_format[i] = plginst_none;
431 } else if (strcasecmp (fields[i], "name") == 0)
432 plugin_instance_format[i] = plginst_name;
433 else if (strcasecmp (fields[i], "uuid") == 0)
434 plugin_instance_format[i] = plginst_uuid;
436 ERROR (PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s", fields[i]);
443 for (int i = n; i < PLGINST_MAX_FIELDS; ++i)
444 plugin_instance_format[i] = plginst_none;
449 if (strcasecmp (key, "InterfaceFormat") == 0) {
450 if (strcasecmp (value, "name") == 0)
451 interface_format = if_name;
452 else if (strcasecmp (value, "address") == 0)
453 interface_format = if_address;
454 else if (strcasecmp (value, "number") == 0)
455 interface_format = if_number;
457 ERROR (PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", value);
463 /* Unrecognised option. */
473 /* `conn_string == NULL' is acceptable. */
474 conn = virConnectOpenReadOnly (conn_string);
476 c_complain (LOG_ERR, &conn_complain,
477 PLUGIN_NAME " plugin: Unable to connect: "
478 "virConnectOpenReadOnly failed.");
482 c_release (LOG_NOTICE, &conn_complain,
483 PLUGIN_NAME " plugin: Connection established.");
487 /* Need to refresh domain or device lists? */
488 if ((last_refresh == (time_t) 0) ||
489 ((interval > 0) && ((last_refresh + interval) <= t))) {
490 if (refresh_lists () != 0) {
492 virConnectClose (conn);
500 for (int i = 0; i < nr_domains; ++i)
501 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
502 for (int i = 0; i < nr_block_devices; ++i)
503 fprintf (stderr, "block device %d %s:%s\n",
504 i, virDomainGetName (block_devices[i].dom),
505 block_devices[i].path);
506 for (int i = 0; i < nr_interface_devices; ++i)
507 fprintf (stderr, "interface device %d %s:%s\n",
508 i, virDomainGetName (interface_devices[i].dom),
509 interface_devices[i].path);
512 /* Get CPU usage, memory, VCPU usage for each domain. */
513 for (int i = 0; i < nr_domains; ++i) {
515 virVcpuInfoPtr vinfo = NULL;
516 virDomainMemoryStatPtr minfo = NULL;
519 status = virDomainGetInfo (domains[i], &info);
522 ERROR (PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
527 if (info.state != VIR_DOMAIN_RUNNING)
529 /* only gather stats for running domains */
533 cpu_submit (info.cpuTime, domains[i], "virt_cpu_total");
534 memory_submit ((gauge_t) info.memory * 1024, domains[i]);
536 vinfo = malloc (info.nrVirtCpu * sizeof (vinfo[0]));
538 ERROR (PLUGIN_NAME " plugin: malloc failed.");
542 status = virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
543 /* cpu map = */ NULL, /* cpu map length = */ 0);
546 ERROR (PLUGIN_NAME " plugin: virDomainGetVcpus failed with status %i.",
552 for (int j = 0; j < info.nrVirtCpu; ++j)
553 vcpu_submit (vinfo[j].cpuTime,
554 domains[i], vinfo[j].number, "virt_vcpu");
558 minfo = malloc (VIR_DOMAIN_MEMORY_STAT_NR * sizeof (virDomainMemoryStatStruct));
560 ERROR ("virt plugin: malloc failed.");
564 status = virDomainMemoryStats (domains[i], minfo, VIR_DOMAIN_MEMORY_STAT_NR, 0);
567 ERROR ("virt plugin: virDomainMemoryStats failed with status %i.",
573 for (int j = 0; j < status; j++) {
574 memory_stats_submit ((gauge_t) minfo[j].val * 1024, domains[i], minfo[j].tag);
581 /* Get block device stats for each domain. */
582 for (int i = 0; i < nr_block_devices; ++i) {
583 struct _virDomainBlockStats stats;
585 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
586 &stats, sizeof stats) != 0)
589 if ((stats.rd_req != -1) && (stats.wr_req != -1))
590 submit_derive2 ("disk_ops",
591 (derive_t) stats.rd_req, (derive_t) stats.wr_req,
592 block_devices[i].dom, block_devices[i].path);
594 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
595 submit_derive2 ("disk_octets",
596 (derive_t) stats.rd_bytes, (derive_t) stats.wr_bytes,
597 block_devices[i].dom, block_devices[i].path);
598 } /* for (nr_block_devices) */
600 /* Get interface stats for each domain. */
601 for (int i = 0; i < nr_interface_devices; ++i) {
602 struct _virDomainInterfaceStats stats;
603 char *display_name = NULL;
606 switch (interface_format) {
608 display_name = interface_devices[i].address;
611 display_name = interface_devices[i].number;
615 display_name = interface_devices[i].path;
618 if (virDomainInterfaceStats (interface_devices[i].dom,
619 interface_devices[i].path,
620 &stats, sizeof stats) != 0)
623 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
624 submit_derive2 ("if_octets",
625 (derive_t) stats.rx_bytes, (derive_t) stats.tx_bytes,
626 interface_devices[i].dom, display_name);
628 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
629 submit_derive2 ("if_packets",
630 (derive_t) stats.rx_packets, (derive_t) stats.tx_packets,
631 interface_devices[i].dom, display_name);
633 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
634 submit_derive2 ("if_errors",
635 (derive_t) stats.rx_errs, (derive_t) stats.tx_errs,
636 interface_devices[i].dom, display_name);
638 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
639 submit_derive2 ("if_dropped",
640 (derive_t) stats.rx_drop, (derive_t) stats.tx_drop,
641 interface_devices[i].dom, display_name);
642 } /* for (nr_interface_devices) */
652 n = virConnectNumOfDomains (conn);
654 VIRT_ERROR (conn, "reading number of domains");
661 /* Get list of domains. */
662 domids = malloc (sizeof (*domids) * n);
663 if (domids == NULL) {
664 ERROR (PLUGIN_NAME " plugin: malloc failed.");
668 n = virConnectListDomains (conn, domids, n);
670 VIRT_ERROR (conn, "reading list of domains");
675 free_block_devices ();
676 free_interface_devices ();
679 /* Fetch each domain and add it to the list, unless ignore. */
680 for (int i = 0; i < n; ++i) {
681 virDomainPtr dom = NULL;
684 xmlDocPtr xml_doc = NULL;
685 xmlXPathContextPtr xpath_ctx = NULL;
686 xmlXPathObjectPtr xpath_obj = NULL;
688 dom = virDomainLookupByID (conn, domids[i]);
690 VIRT_ERROR (conn, "virDomainLookupByID");
691 /* Could be that the domain went away -- ignore it anyway. */
695 name = virDomainGetName (dom);
697 VIRT_ERROR (conn, "virDomainGetName");
701 if (il_domains && ignorelist_match (il_domains, name) != 0)
704 if (add_domain (dom) < 0) {
705 ERROR (PLUGIN_NAME " plugin: malloc failed.");
709 /* Get a list of devices for this domain. */
710 xml = virDomainGetXMLDesc (dom, 0);
712 VIRT_ERROR (conn, "virDomainGetXMLDesc");
716 /* Yuck, XML. Parse out the devices. */
717 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
718 if (xml_doc == NULL) {
719 VIRT_ERROR (conn, "xmlReadDoc");
723 xpath_ctx = xmlXPathNewContext (xml_doc);
726 xpath_obj = xmlXPathEval
727 ((xmlChar *) "/domain/devices/disk/target[@dev]",
729 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
730 xpath_obj->nodesetval == NULL)
733 for (int j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
737 node = xpath_obj->nodesetval->nodeTab[j];
739 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
742 if (il_block_devices &&
743 ignore_device_match (il_block_devices, name, path) != 0)
746 add_block_device (dom, path);
748 if (path) xmlFree (path);
750 xmlXPathFreeObject (xpath_obj);
752 /* Network interfaces. */
753 xpath_obj = xmlXPathEval
754 ((xmlChar *) "/domain/devices/interface[target[@dev]]",
756 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
757 xpath_obj->nodesetval == NULL)
760 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
762 for (int j = 0; j < xml_interfaces->nodeNr; ++j) {
764 char *address = NULL;
765 xmlNodePtr xml_interface;
767 xml_interface = xml_interfaces->nodeTab[j];
768 if (!xml_interface) continue;
770 for (xmlNodePtr child = xml_interface->children; child; child = child->next) {
771 if (child->type != XML_ELEMENT_NODE) continue;
773 if (xmlStrEqual(child->name, (const xmlChar *) "target")) {
774 path = (char *) xmlGetProp (child, (const xmlChar *) "dev");
776 } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) {
777 address = (char *) xmlGetProp (child, (const xmlChar *) "address");
778 if (!address) continue;
782 if (il_interface_devices &&
783 (ignore_device_match (il_interface_devices, name, path) != 0 ||
784 ignore_device_match (il_interface_devices, name, address) != 0))
787 add_interface_device (dom, path, address, j+1);
789 if (path) xmlFree (path);
790 if (address) xmlFree (address);
794 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
795 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
796 if (xml_doc) xmlFreeDoc (xml_doc);
810 for (int i = 0; i < nr_domains; ++i)
811 virDomainFree (domains[i]);
819 add_domain (virDomainPtr dom)
821 virDomainPtr *new_ptr;
822 int new_size = sizeof (domains[0]) * (nr_domains+1);
825 new_ptr = realloc (domains, new_size);
827 new_ptr = malloc (new_size);
833 domains[nr_domains] = dom;
838 free_block_devices (void)
841 for (int i = 0; i < nr_block_devices; ++i)
842 sfree (block_devices[i].path);
843 sfree (block_devices);
845 block_devices = NULL;
846 nr_block_devices = 0;
850 add_block_device (virDomainPtr dom, const char *path)
852 struct block_device *new_ptr;
853 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
856 path_copy = strdup (path);
861 new_ptr = realloc (block_devices, new_size);
863 new_ptr = malloc (new_size);
865 if (new_ptr == NULL) {
869 block_devices = new_ptr;
870 block_devices[nr_block_devices].dom = dom;
871 block_devices[nr_block_devices].path = path_copy;
872 return nr_block_devices++;
876 free_interface_devices (void)
878 if (interface_devices) {
879 for (int i = 0; i < nr_interface_devices; ++i) {
880 sfree (interface_devices[i].path);
881 sfree (interface_devices[i].address);
882 sfree (interface_devices[i].number);
884 sfree (interface_devices);
886 interface_devices = NULL;
887 nr_interface_devices = 0;
891 add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number)
893 struct interface_device *new_ptr;
894 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
895 char *path_copy, *address_copy, number_string[15];
897 if ((path == NULL) || (address == NULL))
900 path_copy = strdup (path);
901 if (!path_copy) return -1;
903 address_copy = strdup (address);
909 snprintf(number_string, sizeof (number_string), "interface-%u", number);
911 if (interface_devices)
912 new_ptr = realloc (interface_devices, new_size);
914 new_ptr = malloc (new_size);
916 if (new_ptr == NULL) {
918 sfree (address_copy);
921 interface_devices = new_ptr;
922 interface_devices[nr_interface_devices].dom = dom;
923 interface_devices[nr_interface_devices].path = path_copy;
924 interface_devices[nr_interface_devices].address = address_copy;
925 interface_devices[nr_interface_devices].number = strdup(number_string);
926 return nr_interface_devices++;
930 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
935 if ((domname == NULL) || (devpath == NULL))
938 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
941 ERROR (PLUGIN_NAME " plugin: malloc failed.");
944 ssnprintf (name, n, "%s:%s", domname, devpath);
945 r = ignorelist_match (il, name);
953 free_block_devices ();
954 free_interface_devices ();
958 virConnectClose (conn);
961 ignorelist_free (il_domains);
963 ignorelist_free (il_block_devices);
964 il_block_devices = NULL;
965 ignorelist_free (il_interface_devices);
966 il_interface_devices = NULL;
972 module_register (void)
974 plugin_register_config (PLUGIN_NAME,
976 config_keys, NR_CONFIG_KEYS);
977 plugin_register_init (PLUGIN_NAME, lv_init);
978 plugin_register_read (PLUGIN_NAME, lv_read);
979 plugin_register_shutdown (PLUGIN_NAME, lv_shutdown);
983 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker