2 * collectd - src/libvirtstats.c
3 * Copyright (C) 2006,2007 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 #define LIBVIRTSTATS_DEBUG 0
36 static const char *config_keys[] = {
50 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
53 static virConnectPtr conn = 0;
55 /* Seconds between list refreshes, 0 disables completely. */
56 static int interval = 60;
58 /* List of domains, if specified. */
59 static ignorelist_t *il_domains = NULL;
60 /* List of block devices, if specified. */
61 static ignorelist_t *il_block_devices = NULL;
62 /* List of network interface devices, if specified. */
63 static ignorelist_t *il_interface_devices = NULL;
65 static int ignore_device_match (ignorelist_t *,
66 const char *domname, const char *devpath);
68 /* Actual list of domains found on last refresh. */
69 static virDomainPtr *domains = NULL;
70 static int nr_domains = 0;
72 static void free_domains (void);
73 static int add_domain (virDomainPtr dom);
75 /* Actual list of block devices found on last refresh. */
77 virDomainPtr dom; /* domain */
78 char *path; /* name of block device */
81 static struct block_device *block_devices = NULL;
82 static int nr_block_devices = 0;
84 static void free_block_devices (void);
85 static int add_block_device (virDomainPtr dom, const char *path);
87 /* Actual list of network interfaces found on last refresh. */
88 struct interface_device {
89 virDomainPtr dom; /* domain */
90 char *path; /* name of interface device */
93 static struct interface_device *interface_devices = NULL;
94 static int nr_interface_devices = 0;
96 static void free_interface_devices (void);
97 static int add_interface_device (virDomainPtr dom, const char *path);
100 #define HF_MAX_FIELDS 3
109 static enum hf_field hostname_format[HF_MAX_FIELDS] =
112 /* Time that we last refreshed. */
113 static time_t last_refresh = (time_t) 0;
115 static int refresh_lists (void);
117 /* Submit functions. */
118 static void cpu_submit (unsigned long long cpu_time,
120 virDomainPtr dom, const char *type);
121 static void vcpu_submit (unsigned long long cpu_time,
123 virDomainPtr dom, int vcpu_nr, const char *type);
124 static void disk_submit (long long read, long long write,
126 virDomainPtr dom, const char *devname,
128 static void if_submit (long long rx, long long tx,
130 virDomainPtr dom, const char *devname,
133 /* ERROR(...) macro for virterrors. */
134 #define VIRT_ERROR(conn,s) do { \
136 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
137 if (err) ERROR ("%s: %s", (s), err->message); \
141 libvirtstats_init (void)
143 if (virInitialize () == -1)
150 libvirtstats_config (const char *key, const char *value)
152 if (virInitialize () == -1)
155 if (il_domains == NULL)
156 il_domains = ignorelist_create (1);
157 if (il_block_devices == NULL)
158 il_block_devices = ignorelist_create (1);
159 if (il_interface_devices == NULL)
160 il_interface_devices = ignorelist_create (1);
162 if (strcasecmp (key, "Connection") == 0) {
164 ERROR ("Connection may only be given once in config file");
167 conn = virConnectOpenReadOnly (value);
169 VIRT_ERROR (NULL, "connection failed");
175 if (strcasecmp (key, "RefreshInterval") == 0) {
177 interval = strtol (value, &eptr, 10);
178 if (eptr == NULL || *eptr != '\0') return 1;
182 if (strcasecmp (key, "Domain") == 0) {
183 if (ignorelist_add (il_domains, value)) return 1;
186 if (strcasecmp (key, "BlockDevice") == 0) {
187 if (ignorelist_add (il_block_devices, value)) return 1;
190 if (strcasecmp (key, "InterfaceDevice") == 0) {
191 if (ignorelist_add (il_interface_devices, value)) return 1;
195 if (strcasecmp (key, "IgnoreSelected") == 0) {
196 if (strcasecmp (value, "True") == 0 ||
197 strcasecmp (value, "Yes") == 0 ||
198 strcasecmp (value, "On") == 0)
200 ignorelist_set_invert (il_domains, 0);
201 ignorelist_set_invert (il_block_devices, 0);
202 ignorelist_set_invert (il_interface_devices, 0);
206 ignorelist_set_invert (il_domains, 1);
207 ignorelist_set_invert (il_block_devices, 1);
208 ignorelist_set_invert (il_interface_devices, 1);
213 if (strcasecmp (key, "HostnameFormat") == 0) {
215 char *fields[HF_MAX_FIELDS];
218 value_copy = strdup (value);
219 if (value_copy == NULL) {
220 ERROR ("strdup: %s", strerror (errno));
224 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
227 ERROR ("HostnameFormat: no fields");
231 for (i = 0; i < n; ++i) {
232 if (strcasecmp (fields[i], "hostname") == 0)
233 hostname_format[i] = hf_hostname;
234 else if (strcasecmp (fields[i], "name") == 0)
235 hostname_format[i] = hf_name;
236 else if (strcasecmp (fields[i], "uuid") == 0)
237 hostname_format[i] = hf_uuid;
240 ERROR ("unknown HostnameFormat field: %s", fields[i]);
246 for (i = n; i < HF_MAX_FIELDS; ++i)
247 hostname_format[i] = hf_none;
252 /* Unrecognised option. */
257 libvirtstats_read (void)
263 ERROR ("Not connected. Use Connection in config file to supply connection URI. For more information see http://libvirt.org/uri.html");
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 () == -1) return -1;
276 #if LIBVIRTSTATS_DEBUG
277 for (i = 0; i < nr_domains; ++i)
278 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
279 for (i = 0; i < nr_block_devices; ++i)
280 fprintf (stderr, "block device %d %s:%s\n",
281 i, virDomainGetName (block_devices[i].dom),
282 block_devices[i].path);
283 for (i = 0; i < nr_interface_devices; ++i)
284 fprintf (stderr, "interface device %d %s:%s\n",
285 i, virDomainGetName (interface_devices[i].dom),
286 interface_devices[i].path);
289 /* Get CPU usage, VCPU usage for each domain. */
290 for (i = 0; i < nr_domains; ++i) {
292 virVcpuInfoPtr vinfo = NULL;
295 if (virDomainGetInfo (domains[i], &info) == -1) continue;
297 cpu_submit (info.cpuTime, t, domains[i], "virt_cpu_total");
299 vinfo = malloc (info.nrVirtCpu * sizeof vinfo[0]);
301 ERROR ("malloc: %s", strerror (errno));
305 if (virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
311 for (j = 0; j < info.nrVirtCpu; ++j)
312 vcpu_submit (vinfo[j].cpuTime,
313 t, domains[i], vinfo[j].number, "virt_vcpu");
318 /* Get block device stats for each domain. */
319 for (i = 0; i < nr_block_devices; ++i) {
320 struct _virDomainBlockStats stats;
322 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
323 &stats, sizeof stats) == -1)
326 disk_submit (stats.rd_req, stats.wr_req,
327 t, block_devices[i].dom, block_devices[i].path,
329 disk_submit (stats.rd_bytes, stats.wr_bytes,
330 t, block_devices[i].dom, block_devices[i].path,
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) == -1)
343 if_submit (stats.rx_bytes, stats.tx_bytes,
344 t, interface_devices[i].dom, interface_devices[i].path,
346 if_submit (stats.rx_packets, stats.tx_packets,
347 t, interface_devices[i].dom, interface_devices[i].path,
349 if_submit (stats.rx_errs, stats.tx_errs,
350 t, interface_devices[i].dom, interface_devices[i].path,
352 if_submit (stats.rx_drop, stats.tx_drop,
353 t, interface_devices[i].dom, interface_devices[i].path,
365 n = virConnectNumOfDomains (conn);
367 VIRT_ERROR (conn, "reading number of domains");
375 /* Get list of domains. */
376 domids = malloc (sizeof (int) * n);
378 ERROR ("malloc failed: %s", strerror (errno));
382 n = virConnectListDomains (conn, domids, n);
384 VIRT_ERROR (conn, "reading list of domains");
389 free_block_devices ();
390 free_interface_devices ();
393 /* Fetch each domain and add it to the list, unless ignore. */
394 for (i = 0; i < n; ++i) {
395 virDomainPtr dom = NULL;
398 xmlDocPtr xml_doc = NULL;
399 xmlXPathContextPtr xpath_ctx = NULL;
400 xmlXPathObjectPtr xpath_obj = NULL;
403 dom = virDomainLookupByID (conn, domids[i]);
405 VIRT_ERROR (conn, "virDomainLookupByID");
406 /* Could be that the domain went away -- ignore it anyway. */
410 name = virDomainGetName (dom);
412 VIRT_ERROR (conn, "virDomainGetName");
416 if (il_domains && ignorelist_match (il_domains, name) != 0)
419 if (add_domain (dom) == -1) {
420 ERROR ("malloc: %s", strerror (errno));
424 /* Get a list of devices for this domain. */
425 xml = virDomainGetXMLDesc (dom, 0);
427 VIRT_ERROR (conn, "virDomainGetXMLDesc");
431 /* Yuck, XML. Parse out the devices. */
432 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
433 if (xml_doc == NULL) {
434 VIRT_ERROR (conn, "xmlReadDoc");
438 xpath_ctx = xmlXPathNewContext (xml_doc);
441 xpath_obj = xmlXPathEval
442 ((xmlChar *) "/domain/devices/disk/target[@dev]",
444 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
445 xpath_obj->nodesetval == NULL)
448 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
452 node = xpath_obj->nodesetval->nodeTab[j];
454 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
457 if (il_block_devices &&
458 ignore_device_match (il_block_devices, name, path) != 0)
461 add_block_device (dom, path);
463 if (path) xmlFree (path);
465 xmlXPathFreeObject (xpath_obj);
467 /* Network interfaces. */
468 xpath_obj = xmlXPathEval
469 ((xmlChar *) "/domain/devices/interface/target[@dev]",
471 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
472 xpath_obj->nodesetval == NULL)
475 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
479 node = xpath_obj->nodesetval->nodeTab[j];
481 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
484 if (il_interface_devices &&
485 ignore_device_match (il_interface_devices, name, path) != 0)
488 add_interface_device (dom, path);
490 if (path) xmlFree (path);
494 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
495 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
496 if (xml_doc) xmlFreeDoc (xml_doc);
512 for (i = 0; i < nr_domains; ++i)
513 virDomainFree (domains[i]);
521 add_domain (virDomainPtr dom)
523 virDomainPtr *new_ptr;
524 int new_size = sizeof (domains[0]) * (nr_domains+1);
527 new_ptr = realloc (domains, new_size);
529 new_ptr = malloc (new_size);
531 if (new_ptr == NULL) return -1;
533 domains[nr_domains] = dom;
538 free_block_devices ()
543 for (i = 0; i < nr_block_devices; ++i)
544 free (block_devices[i].path);
545 free (block_devices);
547 block_devices = NULL;
548 nr_block_devices = 0;
552 add_block_device (virDomainPtr dom, const char *path)
554 struct block_device *new_ptr;
555 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
558 path_copy = strdup (path);
559 if (!path_copy) return -1;
562 new_ptr = realloc (block_devices, new_size);
564 new_ptr = malloc (new_size);
566 if (new_ptr == NULL) {
570 block_devices = new_ptr;
571 block_devices[nr_block_devices].dom = dom;
572 block_devices[nr_block_devices].path = path_copy;
573 return nr_block_devices++;
577 free_interface_devices ()
581 if (interface_devices) {
582 for (i = 0; i < nr_interface_devices; ++i)
583 free (interface_devices[i].path);
584 free (interface_devices);
586 interface_devices = NULL;
587 nr_interface_devices = 0;
591 add_interface_device (virDomainPtr dom, const char *path)
593 struct interface_device *new_ptr;
594 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
597 path_copy = strdup (path);
598 if (!path_copy) return -1;
600 if (interface_devices)
601 new_ptr = realloc (interface_devices, new_size);
603 new_ptr = malloc (new_size);
605 if (new_ptr == NULL) {
609 interface_devices = new_ptr;
610 interface_devices[nr_interface_devices].dom = dom;
611 interface_devices[nr_interface_devices].path = path_copy;
612 return nr_interface_devices++;
616 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
621 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
624 ERROR ("malloc: %s", strerror (errno));
627 snprintf (name, n, "%s:%s", domname, devpath);
628 r = ignorelist_match (il, name);
634 common_submit (value_list_t *vl, time_t t, virDomainPtr dom)
638 char uuid[VIR_UUID_STRING_BUFLEN];
641 vl->interval = interval_g;
642 strncpy (vl->plugin, "libvirtstats", DATA_MAX_NAME_LEN);
643 /*strncpy (vl->plugin_instance, ?, DATA_MAX_NAME_LEN);*/
647 /* Construct the hostname field according to HostnameFormat. */
648 for (i = 0; i < HF_MAX_FIELDS; ++i) {
649 if (hostname_format[i] == hf_none)
652 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
654 if (i > 0 && n >= 1) {
655 strcat (vl->host, ":");
659 switch (hostname_format[i]) {
662 strncat (vl->host, hostname_g, n);
665 name = virDomainGetName (dom);
667 strncat (vl->host, name, n);
670 if (virDomainGetUUIDString (dom, uuid) == 0)
671 strncat (vl->host, uuid, n);
676 vl->host[DATA_MAX_NAME_LEN-1] = '\0';
680 cpu_submit (unsigned long long cpu_time,
682 virDomainPtr dom, const char *type)
685 value_list_t vl = VALUE_LIST_INIT;
687 common_submit (&vl, t, dom);
689 values[0].counter = cpu_time;
694 plugin_dispatch_values (type, &vl);
698 vcpu_submit (unsigned long long cpu_time,
700 virDomainPtr dom, int vcpu_nr, const char *type)
703 value_list_t vl = VALUE_LIST_INIT;
705 common_submit (&vl, t, dom);
707 values[0].counter = cpu_time;
711 snprintf (vl.type_instance, DATA_MAX_NAME_LEN, "%d", vcpu_nr);
712 vl.type_instance[DATA_MAX_NAME_LEN-1] = '\0';
714 plugin_dispatch_values (type, &vl);
718 disk_submit (long long read, long long write,
720 virDomainPtr dom, const char *devname,
724 value_list_t vl = VALUE_LIST_INIT;
726 common_submit (&vl, t, dom);
728 values[0].counter = read >= 0 ? (unsigned long long) read : 0;
729 values[1].counter = write >= 0 ? (unsigned long long) write : 0;
733 strncpy (vl.type_instance, devname, DATA_MAX_NAME_LEN);
734 vl.type_instance[DATA_MAX_NAME_LEN-1] = '\0';
736 plugin_dispatch_values (type, &vl);
740 if_submit (long long rx, long long tx,
742 virDomainPtr dom, const char *devname,
746 value_list_t vl = VALUE_LIST_INIT;
748 common_submit (&vl, t, dom);
750 values[0].counter = rx >= 0 ? (unsigned long long) rx : 0;
751 values[1].counter = tx >= 0 ? (unsigned long long) tx : 0;
755 strncpy (vl.type_instance, devname, DATA_MAX_NAME_LEN);
756 vl.type_instance[DATA_MAX_NAME_LEN-1] = '\0';
758 plugin_dispatch_values (type, &vl);
762 libvirtstats_shutdown (void)
764 free_block_devices ();
765 free_interface_devices ();
768 if (conn) virConnectClose (conn);
771 ignorelist_free (il_domains);
773 ignorelist_free (il_block_devices);
774 il_block_devices = NULL;
775 ignorelist_free (il_interface_devices);
776 il_interface_devices = NULL;
782 module_register (void)
784 plugin_register_config ("libvirtstats",
786 config_keys, NR_CONFIG_KEYS);
787 plugin_register_init ("libvirtstats", libvirtstats_init);
788 plugin_register_read ("libvirtstats", libvirtstats_read);
789 plugin_register_shutdown ("libvirtstats", libvirtstats_shutdown);
793 * vim: set tabstop=4:
794 * vim: set shiftwidth=4:
795 * vim: set expandtab:
799 * indent-tabs-mode: nil