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 "configfile.h"
27 #include "utils_ignorelist.h"
28 #include "utils_complain.h"
30 #include <libvirt/libvirt.h>
31 #include <libvirt/virterror.h>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
34 #include <libxml/xpath.h>
37 #define PLUGIN_NAME "virt"
39 static const char *config_keys[] = {
52 "PluginInstanceFormat",
56 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
59 static virConnectPtr conn = 0;
60 static char *conn_string = NULL;
61 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
63 /* Seconds between list refreshes, 0 disables completely. */
64 static int interval = 60;
66 /* List of domains, if specified. */
67 static ignorelist_t *il_domains = NULL;
68 /* List of block devices, if specified. */
69 static ignorelist_t *il_block_devices = NULL;
70 /* List of network interface devices, if specified. */
71 static ignorelist_t *il_interface_devices = NULL;
73 static int ignore_device_match (ignorelist_t *,
74 const char *domname, const char *devpath);
76 /* Actual list of domains found on last refresh. */
77 static virDomainPtr *domains = NULL;
78 static int nr_domains = 0;
80 static void free_domains (void);
81 static int add_domain (virDomainPtr dom);
83 /* Actual list of block devices found on last refresh. */
85 virDomainPtr dom; /* domain */
86 char *path; /* name of block device */
89 static struct block_device *block_devices = NULL;
90 static int nr_block_devices = 0;
92 static void free_block_devices (void);
93 static int add_block_device (virDomainPtr dom, const char *path);
95 /* Actual list of network interfaces found on last refresh. */
96 struct interface_device {
97 virDomainPtr dom; /* domain */
98 char *path; /* name of interface device */
99 char *address; /* mac address of interface device */
100 char *number; /* interface device number */
103 static struct interface_device *interface_devices = NULL;
104 static int nr_interface_devices = 0;
106 static void free_interface_devices (void);
107 static int add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number);
109 /* HostnameFormat. */
110 #define HF_MAX_FIELDS 3
119 static enum hf_field hostname_format[HF_MAX_FIELDS] =
122 /* PluginInstanceFormat */
123 #define PLGINST_MAX_FIELDS 2
131 static enum plginst_field plugin_instance_format[PLGINST_MAX_FIELDS] =
134 /* InterfaceFormat. */
141 static enum if_field interface_format = if_name;
143 /* Time that we last refreshed. */
144 static time_t last_refresh = (time_t) 0;
146 static int refresh_lists (void);
148 /* ERROR(...) macro for virterrors. */
149 #define VIRT_ERROR(conn,s) do { \
151 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
152 if (err) ERROR ("%s: %s", (s), err->message); \
156 init_value_list (value_list_t *vl, virDomainPtr dom)
160 char uuid[VIR_UUID_STRING_BUFLEN];
162 sstrncpy (vl->plugin, PLUGIN_NAME, sizeof (vl->plugin));
166 /* Construct the hostname field according to HostnameFormat. */
167 for (i = 0; i < HF_MAX_FIELDS; ++i) {
168 if (hostname_format[i] == hf_none)
171 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
173 if (i > 0 && n >= 1) {
174 strncat (vl->host, ":", 1);
178 switch (hostname_format[i]) {
181 strncat (vl->host, hostname_g, n);
184 name = virDomainGetName (dom);
186 strncat (vl->host, name, n);
189 if (virDomainGetUUIDString (dom, uuid) == 0)
190 strncat (vl->host, uuid, n);
195 vl->host[sizeof (vl->host) - 1] = '\0';
197 /* Construct the plugin instance field according to PluginInstanceFormat. */
198 for (i = 0; i < PLGINST_MAX_FIELDS; ++i) {
199 if (plugin_instance_format[i] == plginst_none)
202 n = sizeof(vl->plugin_instance) - strlen (vl->plugin_instance) - 2;
204 if (i > 0 && n >= 1) {
205 strncat (vl->plugin_instance, ":", 1);
209 switch (plugin_instance_format[i]) {
210 case plginst_none: break;
212 name = virDomainGetName (dom);
214 strncat (vl->plugin_instance, name, n);
217 if (virDomainGetUUIDString (dom, uuid) == 0)
218 strncat (vl->plugin_instance, uuid, n);
223 vl->plugin_instance[sizeof (vl->plugin_instance) - 1] = '\0';
225 } /* void init_value_list */
228 memory_submit (gauge_t memory, virDomainPtr dom)
231 value_list_t vl = VALUE_LIST_INIT;
233 init_value_list (&vl, dom);
235 values[0].gauge = memory;
240 sstrncpy (vl.type, "memory", sizeof (vl.type));
241 sstrncpy (vl.type_instance, "total", sizeof (vl.type_instance));
243 plugin_dispatch_values (&vl);
247 memory_stats_submit (gauge_t memory, virDomainPtr dom, int tag_index)
249 static const char *tags[] = { "swap_in", "swap_out", "major_fault", "minor_fault",
250 "unused", "available", "actual_balloon", "rss"};
253 value_list_t vl = VALUE_LIST_INIT;
255 init_value_list (&vl, dom);
257 values[0].gauge = memory;
262 sstrncpy (vl.type, "memory", sizeof (vl.type));
263 sstrncpy (vl.type_instance, tags[tag_index], sizeof (vl.type_instance));
265 plugin_dispatch_values (&vl);
269 cpu_submit (unsigned long long cpu_time,
270 virDomainPtr dom, const char *type)
273 value_list_t vl = VALUE_LIST_INIT;
275 init_value_list (&vl, dom);
277 values[0].derive = cpu_time;
282 sstrncpy (vl.type, type, sizeof (vl.type));
284 plugin_dispatch_values (&vl);
288 vcpu_submit (derive_t cpu_time,
289 virDomainPtr dom, int vcpu_nr, const char *type)
292 value_list_t vl = VALUE_LIST_INIT;
294 init_value_list (&vl, dom);
296 values[0].derive = cpu_time;
300 sstrncpy (vl.type, type, sizeof (vl.type));
301 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
303 plugin_dispatch_values (&vl);
307 submit_derive2 (const char *type, derive_t v0, derive_t v1,
308 virDomainPtr dom, const char *devname)
311 value_list_t vl = VALUE_LIST_INIT;
313 init_value_list (&vl, dom);
315 values[0].derive = v0;
316 values[1].derive = v1;
320 sstrncpy (vl.type, type, sizeof (vl.type));
321 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
323 plugin_dispatch_values (&vl);
324 } /* void submit_derive2 */
329 if (virInitialize () != 0)
336 lv_config (const char *key, const char *value)
338 if (virInitialize () != 0)
341 if (il_domains == NULL)
342 il_domains = ignorelist_create (1);
343 if (il_block_devices == NULL)
344 il_block_devices = ignorelist_create (1);
345 if (il_interface_devices == NULL)
346 il_interface_devices = ignorelist_create (1);
348 if (strcasecmp (key, "Connection") == 0) {
349 char *tmp = strdup (value);
351 ERROR (PLUGIN_NAME " plugin: Connection strdup failed.");
359 if (strcasecmp (key, "RefreshInterval") == 0) {
361 interval = strtol (value, &eptr, 10);
362 if (eptr == NULL || *eptr != '\0') return 1;
366 if (strcasecmp (key, "Domain") == 0) {
367 if (ignorelist_add (il_domains, value)) return 1;
370 if (strcasecmp (key, "BlockDevice") == 0) {
371 if (ignorelist_add (il_block_devices, value)) return 1;
374 if (strcasecmp (key, "InterfaceDevice") == 0) {
375 if (ignorelist_add (il_interface_devices, value)) return 1;
379 if (strcasecmp (key, "IgnoreSelected") == 0) {
382 ignorelist_set_invert (il_domains, 0);
383 ignorelist_set_invert (il_block_devices, 0);
384 ignorelist_set_invert (il_interface_devices, 0);
388 ignorelist_set_invert (il_domains, 1);
389 ignorelist_set_invert (il_block_devices, 1);
390 ignorelist_set_invert (il_interface_devices, 1);
395 if (strcasecmp (key, "HostnameFormat") == 0) {
397 char *fields[HF_MAX_FIELDS];
400 value_copy = strdup (value);
401 if (value_copy == NULL) {
402 ERROR (PLUGIN_NAME " plugin: strdup failed.");
406 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
409 ERROR (PLUGIN_NAME " plugin: HostnameFormat: no fields");
413 for (i = 0; i < n; ++i) {
414 if (strcasecmp (fields[i], "hostname") == 0)
415 hostname_format[i] = hf_hostname;
416 else if (strcasecmp (fields[i], "name") == 0)
417 hostname_format[i] = hf_name;
418 else if (strcasecmp (fields[i], "uuid") == 0)
419 hostname_format[i] = hf_uuid;
421 ERROR (PLUGIN_NAME " plugin: unknown HostnameFormat field: %s", fields[i]);
428 for (i = n; i < HF_MAX_FIELDS; ++i)
429 hostname_format[i] = hf_none;
434 if (strcasecmp (key, "PluginInstanceFormat") == 0) {
436 char *fields[PLGINST_MAX_FIELDS];
439 value_copy = strdup (value);
440 if (value_copy == NULL) {
441 ERROR (PLUGIN_NAME " plugin: strdup failed.");
445 n = strsplit (value_copy, fields, PLGINST_MAX_FIELDS);
448 ERROR (PLUGIN_NAME " plugin: PluginInstanceFormat: no fields");
452 for (i = 0; i < n; ++i) {
453 if (strcasecmp (fields[i], "none") == 0) {
454 plugin_instance_format[i] = plginst_none;
456 } else if (strcasecmp (fields[i], "name") == 0)
457 plugin_instance_format[i] = plginst_name;
458 else if (strcasecmp (fields[i], "uuid") == 0)
459 plugin_instance_format[i] = plginst_uuid;
461 ERROR (PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s", fields[i]);
468 for (i = n; i < PLGINST_MAX_FIELDS; ++i)
469 plugin_instance_format[i] = plginst_none;
474 if (strcasecmp (key, "InterfaceFormat") == 0) {
475 if (strcasecmp (value, "name") == 0)
476 interface_format = if_name;
477 else if (strcasecmp (value, "address") == 0)
478 interface_format = if_address;
479 else if (strcasecmp (value, "number") == 0)
480 interface_format = if_number;
482 ERROR (PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", value);
488 /* Unrecognised option. */
499 /* `conn_string == NULL' is acceptable. */
500 conn = virConnectOpenReadOnly (conn_string);
502 c_complain (LOG_ERR, &conn_complain,
503 PLUGIN_NAME " plugin: Unable to connect: "
504 "virConnectOpenReadOnly failed.");
508 c_release (LOG_NOTICE, &conn_complain,
509 PLUGIN_NAME " plugin: Connection established.");
513 /* Need to refresh domain or device lists? */
514 if ((last_refresh == (time_t) 0) ||
515 ((interval > 0) && ((last_refresh + interval) <= t))) {
516 if (refresh_lists () != 0) {
518 virConnectClose (conn);
526 for (i = 0; i < nr_domains; ++i)
527 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
528 for (i = 0; i < nr_block_devices; ++i)
529 fprintf (stderr, "block device %d %s:%s\n",
530 i, virDomainGetName (block_devices[i].dom),
531 block_devices[i].path);
532 for (i = 0; i < nr_interface_devices; ++i)
533 fprintf (stderr, "interface device %d %s:%s\n",
534 i, virDomainGetName (interface_devices[i].dom),
535 interface_devices[i].path);
538 /* Get CPU usage, memory, VCPU usage for each domain. */
539 for (i = 0; i < nr_domains; ++i) {
541 virVcpuInfoPtr vinfo = NULL;
542 virDomainMemoryStatPtr minfo = NULL;
546 status = virDomainGetInfo (domains[i], &info);
549 ERROR (PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
554 if (info.state != VIR_DOMAIN_RUNNING)
556 /* only gather stats for running domains */
560 cpu_submit (info.cpuTime, domains[i], "virt_cpu_total");
561 memory_submit ((gauge_t) info.memory * 1024, domains[i]);
563 vinfo = malloc (info.nrVirtCpu * sizeof (vinfo[0]));
565 ERROR (PLUGIN_NAME " plugin: malloc failed.");
569 status = virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
570 /* cpu map = */ NULL, /* cpu map length = */ 0);
573 ERROR (PLUGIN_NAME " plugin: virDomainGetVcpus failed with status %i.",
579 for (j = 0; j < info.nrVirtCpu; ++j)
580 vcpu_submit (vinfo[j].cpuTime,
581 domains[i], vinfo[j].number, "virt_vcpu");
585 minfo = malloc (VIR_DOMAIN_MEMORY_STAT_NR * sizeof (virDomainMemoryStatStruct));
587 ERROR ("virt plugin: malloc failed.");
591 status = virDomainMemoryStats (domains[i], minfo, VIR_DOMAIN_MEMORY_STAT_NR, 0);
594 ERROR ("virt plugin: virDomainMemoryStats failed with status %i.",
600 for (j = 0; j < status; j++) {
601 memory_stats_submit ((gauge_t) minfo[j].val * 1024, domains[i], minfo[j].tag);
608 /* Get block device stats for each domain. */
609 for (i = 0; i < nr_block_devices; ++i) {
610 struct _virDomainBlockStats stats;
612 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
613 &stats, sizeof stats) != 0)
616 if ((stats.rd_req != -1) && (stats.wr_req != -1))
617 submit_derive2 ("disk_ops",
618 (derive_t) stats.rd_req, (derive_t) stats.wr_req,
619 block_devices[i].dom, block_devices[i].path);
621 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
622 submit_derive2 ("disk_octets",
623 (derive_t) stats.rd_bytes, (derive_t) stats.wr_bytes,
624 block_devices[i].dom, block_devices[i].path);
625 } /* for (nr_block_devices) */
627 /* Get interface stats for each domain. */
628 for (i = 0; i < nr_interface_devices; ++i) {
629 struct _virDomainInterfaceStats stats;
630 char *display_name = NULL;
633 switch (interface_format) {
635 display_name = interface_devices[i].address;
638 display_name = interface_devices[i].number;
642 display_name = interface_devices[i].path;
645 if (virDomainInterfaceStats (interface_devices[i].dom,
646 interface_devices[i].path,
647 &stats, sizeof stats) != 0)
650 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
651 submit_derive2 ("if_octets",
652 (derive_t) stats.rx_bytes, (derive_t) stats.tx_bytes,
653 interface_devices[i].dom, display_name);
655 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
656 submit_derive2 ("if_packets",
657 (derive_t) stats.rx_packets, (derive_t) stats.tx_packets,
658 interface_devices[i].dom, display_name);
660 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
661 submit_derive2 ("if_errors",
662 (derive_t) stats.rx_errs, (derive_t) stats.tx_errs,
663 interface_devices[i].dom, display_name);
665 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
666 submit_derive2 ("if_dropped",
667 (derive_t) stats.rx_drop, (derive_t) stats.tx_drop,
668 interface_devices[i].dom, display_name);
669 } /* for (nr_interface_devices) */
679 n = virConnectNumOfDomains (conn);
681 VIRT_ERROR (conn, "reading number of domains");
689 /* Get list of domains. */
690 domids = malloc (sizeof (*domids) * n);
691 if (domids == NULL) {
692 ERROR (PLUGIN_NAME " plugin: malloc failed.");
696 n = virConnectListDomains (conn, domids, n);
698 VIRT_ERROR (conn, "reading list of domains");
703 free_block_devices ();
704 free_interface_devices ();
707 /* Fetch each domain and add it to the list, unless ignore. */
708 for (i = 0; i < n; ++i) {
709 virDomainPtr dom = NULL;
712 xmlDocPtr xml_doc = NULL;
713 xmlXPathContextPtr xpath_ctx = NULL;
714 xmlXPathObjectPtr xpath_obj = NULL;
717 dom = virDomainLookupByID (conn, domids[i]);
719 VIRT_ERROR (conn, "virDomainLookupByID");
720 /* Could be that the domain went away -- ignore it anyway. */
724 name = virDomainGetName (dom);
726 VIRT_ERROR (conn, "virDomainGetName");
730 if (il_domains && ignorelist_match (il_domains, name) != 0)
733 if (add_domain (dom) < 0) {
734 ERROR (PLUGIN_NAME " plugin: malloc failed.");
738 /* Get a list of devices for this domain. */
739 xml = virDomainGetXMLDesc (dom, 0);
741 VIRT_ERROR (conn, "virDomainGetXMLDesc");
745 /* Yuck, XML. Parse out the devices. */
746 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
747 if (xml_doc == NULL) {
748 VIRT_ERROR (conn, "xmlReadDoc");
752 xpath_ctx = xmlXPathNewContext (xml_doc);
755 xpath_obj = xmlXPathEval
756 ((xmlChar *) "/domain/devices/disk/target[@dev]",
758 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
759 xpath_obj->nodesetval == NULL)
762 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
766 node = xpath_obj->nodesetval->nodeTab[j];
768 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
771 if (il_block_devices &&
772 ignore_device_match (il_block_devices, name, path) != 0)
775 add_block_device (dom, path);
777 if (path) xmlFree (path);
779 xmlXPathFreeObject (xpath_obj);
781 /* Network interfaces. */
782 xpath_obj = xmlXPathEval
783 ((xmlChar *) "/domain/devices/interface[target[@dev]]",
785 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
786 xpath_obj->nodesetval == NULL)
789 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
791 for (j = 0; j < xml_interfaces->nodeNr; ++j) {
793 char *address = NULL;
794 xmlNodePtr xml_interface;
796 xml_interface = xml_interfaces->nodeTab[j];
797 if (!xml_interface) continue;
798 xmlNodePtr child = NULL;
800 for (child = xml_interface->children; child; child = child->next) {
801 if (child->type != XML_ELEMENT_NODE) continue;
803 if (xmlStrEqual(child->name, (const xmlChar *) "target")) {
804 path = (char *) xmlGetProp (child, (const xmlChar *) "dev");
806 } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) {
807 address = (char *) xmlGetProp (child, (const xmlChar *) "address");
808 if (!address) continue;
812 if (il_interface_devices &&
813 (ignore_device_match (il_interface_devices, name, path) != 0 ||
814 ignore_device_match (il_interface_devices, name, address) != 0))
817 add_interface_device (dom, path, address, j+1);
819 if (path) xmlFree (path);
820 if (address) xmlFree (address);
824 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
825 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
826 if (xml_doc) xmlFreeDoc (xml_doc);
842 for (i = 0; i < nr_domains; ++i)
843 virDomainFree (domains[i]);
851 add_domain (virDomainPtr dom)
853 virDomainPtr *new_ptr;
854 int new_size = sizeof (domains[0]) * (nr_domains+1);
857 new_ptr = realloc (domains, new_size);
859 new_ptr = malloc (new_size);
865 domains[nr_domains] = dom;
870 free_block_devices (void)
875 for (i = 0; i < nr_block_devices; ++i)
876 sfree (block_devices[i].path);
877 sfree (block_devices);
879 block_devices = NULL;
880 nr_block_devices = 0;
884 add_block_device (virDomainPtr dom, const char *path)
886 struct block_device *new_ptr;
887 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
890 path_copy = strdup (path);
895 new_ptr = realloc (block_devices, new_size);
897 new_ptr = malloc (new_size);
899 if (new_ptr == NULL) {
903 block_devices = new_ptr;
904 block_devices[nr_block_devices].dom = dom;
905 block_devices[nr_block_devices].path = path_copy;
906 return nr_block_devices++;
910 free_interface_devices (void)
914 if (interface_devices) {
915 for (i = 0; i < nr_interface_devices; ++i) {
916 sfree (interface_devices[i].path);
917 sfree (interface_devices[i].address);
918 sfree (interface_devices[i].number);
920 sfree (interface_devices);
922 interface_devices = NULL;
923 nr_interface_devices = 0;
927 add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number)
929 struct interface_device *new_ptr;
930 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
931 char *path_copy, *address_copy, number_string[15];
933 if ((path == NULL) || (address == NULL))
936 path_copy = strdup (path);
937 if (!path_copy) return -1;
939 address_copy = strdup (address);
945 snprintf(number_string, sizeof (number_string), "interface-%u", number);
947 if (interface_devices)
948 new_ptr = realloc (interface_devices, new_size);
950 new_ptr = malloc (new_size);
952 if (new_ptr == NULL) {
954 sfree (address_copy);
957 interface_devices = new_ptr;
958 interface_devices[nr_interface_devices].dom = dom;
959 interface_devices[nr_interface_devices].path = path_copy;
960 interface_devices[nr_interface_devices].address = address_copy;
961 interface_devices[nr_interface_devices].number = strdup(number_string);
962 return nr_interface_devices++;
966 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
971 if ((domname == NULL) || (devpath == NULL))
974 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
977 ERROR (PLUGIN_NAME " plugin: malloc failed.");
980 ssnprintf (name, n, "%s:%s", domname, devpath);
981 r = ignorelist_match (il, name);
989 free_block_devices ();
990 free_interface_devices ();
994 virConnectClose (conn);
997 ignorelist_free (il_domains);
999 ignorelist_free (il_block_devices);
1000 il_block_devices = NULL;
1001 ignorelist_free (il_interface_devices);
1002 il_interface_devices = NULL;
1008 module_register (void)
1010 plugin_register_config (PLUGIN_NAME,
1012 config_keys, NR_CONFIG_KEYS);
1013 plugin_register_init (PLUGIN_NAME, lv_init);
1014 plugin_register_read (PLUGIN_NAME, lv_read);
1015 plugin_register_shutdown (PLUGIN_NAME, lv_shutdown);
1019 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker