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[] = {
47 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
50 static virConnectPtr conn = 0;
52 /* Seconds between list refreshes, 0 disables completely. */
53 static int interval = 60;
55 /* List of domains, if specified. */
56 static ignorelist_t *il_domains = NULL;
57 /* List of block devices, if specified. */
58 static ignorelist_t *il_block_devices = NULL;
59 /* List of network interface devices, if specified. */
60 static ignorelist_t *il_interface_devices = NULL;
62 static int ignore_device_match (ignorelist_t *,
63 const char *domname, const char *devpath);
65 /* Actual list of domains found on last refresh. */
66 static virDomainPtr *domains = NULL;
67 static int nr_domains = 0;
69 static void free_domains (void);
70 static int add_domain (virDomainPtr dom);
72 /* Actual list of block devices found on last refresh. */
74 virDomainPtr dom; /* domain */
75 char *path; /* name of block device */
78 static struct block_device *block_devices = NULL;
79 static int nr_block_devices = 0;
81 static void free_block_devices (void);
82 static int add_block_device (virDomainPtr dom, const char *path);
84 /* Actual list of network interfaces found on last refresh. */
85 struct interface_device {
86 virDomainPtr dom; /* domain */
87 char *path; /* name of interface device */
90 static struct interface_device *interface_devices = NULL;
91 static int nr_interface_devices = 0;
93 static void free_interface_devices (void);
94 static int add_interface_device (virDomainPtr dom, const char *path);
96 /* Time that we last refreshed. */
97 static time_t last_refresh = (time_t) 0;
99 static int refresh_lists (void);
101 /* Submit functions. */
102 static void cpu_submit (unsigned long long cpu_time,
104 const char *domname, const char *type);
105 static void vcpu_submit (unsigned long long cpu_time,
107 const char *domname, int vcpu_nr, const char *type);
108 static void disk_submit (long long read, long long write,
110 const char *domname, const char *devname,
112 static void if_submit (long long rx, long long tx,
114 const char *domname, const char *devname,
117 /* ERROR(...) macro for virterrors. */
118 #define VIRT_ERROR(conn,s) do { \
120 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
121 if (err) ERROR ("%s: %s", (s), err->message); \
125 libvirtstats_init (void)
127 if (virInitialize () == -1)
134 libvirtstats_config (const char *key, const char *value)
136 if (virInitialize () == -1)
139 if (il_domains == NULL)
140 il_domains = ignorelist_create (1);
141 if (il_block_devices == NULL)
142 il_block_devices = ignorelist_create (1);
143 if (il_interface_devices == NULL)
144 il_interface_devices = ignorelist_create (1);
146 if (strcasecmp (key, "Connection") == 0) {
148 ERROR ("Connection may only be given once in config file");
151 conn = virConnectOpenReadOnly (value);
153 VIRT_ERROR (NULL, "connection failed");
159 if (strcasecmp (key, "RefreshInterval") == 0) {
161 interval = strtol (value, &eptr, 10);
162 if (eptr == NULL || *eptr != '\0') return 1;
166 if (strcasecmp (key, "Domain") == 0) {
167 if (ignorelist_add (il_domains, value)) return 1;
170 if (strcasecmp (key, "BlockDevice") == 0) {
171 if (ignorelist_add (il_block_devices, value)) return 1;
174 if (strcasecmp (key, "InterfaceDevice") == 0) {
175 if (ignorelist_add (il_interface_devices, value)) return 1;
179 if (strcasecmp (key, "IgnoreSelected") == 0) {
180 if (strcasecmp (value, "True") == 0 ||
181 strcasecmp (value, "Yes") == 0 ||
182 strcasecmp (value, "On") == 0)
184 ignorelist_set_invert (il_domains, 0);
185 ignorelist_set_invert (il_block_devices, 0);
186 ignorelist_set_invert (il_interface_devices, 0);
190 ignorelist_set_invert (il_domains, 1);
191 ignorelist_set_invert (il_block_devices, 1);
192 ignorelist_set_invert (il_interface_devices, 1);
196 /* Unrecognised option. */
201 libvirtstats_read (void)
207 ERROR ("Not connected. Use Connection in config file to supply connection URI. For more information see http://libvirt.org/uri.html");
213 /* Need to refresh domain or device lists? */
214 if (last_refresh == (time_t) 0 ||
215 (interval > 0 && last_refresh + interval <= t)) {
216 if (refresh_lists () == -1) return -1;
220 #if LIBVIRTSTATS_DEBUG
221 for (i = 0; i < nr_domains; ++i)
222 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
223 for (i = 0; i < nr_block_devices; ++i)
224 fprintf (stderr, "block device %d %s:%s\n",
225 i, virDomainGetName (block_devices[i].dom),
226 block_devices[i].path);
227 for (i = 0; i < nr_interface_devices; ++i)
228 fprintf (stderr, "interface device %d %s:%s\n",
229 i, virDomainGetName (interface_devices[i].dom),
230 interface_devices[i].path);
233 /* Get CPU usage, VCPU usage for each domain. */
234 for (i = 0; i < nr_domains; ++i) {
237 virVcpuInfoPtr vinfo = NULL;
240 name = virDomainGetName (domains[i]);
241 if (name == NULL) continue;
243 if (virDomainGetInfo (domains[i], &info) == -1) continue;
245 cpu_submit (info.cpuTime, t, name, "virt_cpu_total");
247 vinfo = malloc (info.nrVirtCpu * sizeof vinfo[0]);
249 ERROR ("malloc: %s", strerror (errno));
253 if (virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
259 for (j = 0; j < info.nrVirtCpu; ++j)
260 vcpu_submit (vinfo[j].cpuTime,
261 t, name, vinfo[j].number, "virt_vcpu");
266 /* Get block device stats for each domain. */
267 for (i = 0; i < nr_block_devices; ++i) {
269 struct _virDomainBlockStats stats;
271 name = virDomainGetName (block_devices[i].dom);
272 if (name == NULL) continue;
274 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
275 &stats, sizeof stats) == -1)
278 disk_submit (stats.rd_req, stats.wr_req,
279 t, name, block_devices[i].path,
281 disk_submit (stats.rd_bytes, stats.wr_bytes,
282 t, name, block_devices[i].path,
286 /* Get interface stats for each domain. */
287 for (i = 0; i < nr_interface_devices; ++i) {
289 struct _virDomainInterfaceStats stats;
291 name = virDomainGetName (interface_devices[i].dom);
292 if (name == NULL) continue;
294 if (virDomainInterfaceStats (interface_devices[i].dom,
295 interface_devices[i].path,
296 &stats, sizeof stats) == -1)
299 if_submit (stats.rx_bytes, stats.tx_bytes,
300 t, name, interface_devices[i].path,
302 if_submit (stats.rx_packets, stats.tx_packets,
303 t, name, interface_devices[i].path,
305 if_submit (stats.rx_errs, stats.tx_errs,
306 t, name, interface_devices[i].path,
308 if_submit (stats.rx_drop, stats.tx_drop,
309 t, name, interface_devices[i].path,
321 n = virConnectNumOfDomains (conn);
323 VIRT_ERROR (conn, "reading number of domains");
331 /* Get list of domains. */
332 domids = malloc (sizeof (int) * n);
334 ERROR ("malloc failed: %s", strerror (errno));
338 n = virConnectListDomains (conn, domids, n);
340 VIRT_ERROR (conn, "reading list of domains");
345 free_block_devices ();
346 free_interface_devices ();
349 /* Fetch each domain and add it to the list, unless ignore. */
350 for (i = 0; i < n; ++i) {
351 virDomainPtr dom = NULL;
354 xmlDocPtr xml_doc = NULL;
355 xmlXPathContextPtr xpath_ctx = NULL;
356 xmlXPathObjectPtr xpath_obj = NULL;
359 dom = virDomainLookupByID (conn, domids[i]);
361 VIRT_ERROR (conn, "virDomainLookupByID");
362 /* Could be that the domain went away -- ignore it anyway. */
366 name = virDomainGetName (dom);
368 VIRT_ERROR (conn, "virDomainGetName");
372 if (il_domains && ignorelist_match (il_domains, name) != 0)
375 if (add_domain (dom) == -1) {
376 ERROR ("malloc: %s", strerror (errno));
380 /* Get a list of devices for this domain. */
381 xml = virDomainGetXMLDesc (dom, 0);
383 VIRT_ERROR (conn, "virDomainGetXMLDesc");
387 /* Yuck, XML. Parse out the devices. */
388 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
389 if (xml_doc == NULL) {
390 VIRT_ERROR (conn, "xmlReadDoc");
394 xpath_ctx = xmlXPathNewContext (xml_doc);
397 xpath_obj = xmlXPathEval
398 ((xmlChar *) "/domain/devices/disk/target[@dev]",
400 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
401 xpath_obj->nodesetval == NULL)
404 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
408 node = xpath_obj->nodesetval->nodeTab[j];
410 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
413 if (il_block_devices &&
414 ignore_device_match (il_block_devices, name, path) != 0)
417 add_block_device (dom, path);
419 if (path) xmlFree (path);
421 xmlXPathFreeObject (xpath_obj);
423 /* Network interfaces. */
424 xpath_obj = xmlXPathEval
425 ((xmlChar *) "/domain/devices/interface/target[@dev]",
427 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
428 xpath_obj->nodesetval == NULL)
431 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
435 node = xpath_obj->nodesetval->nodeTab[j];
437 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
440 if (il_interface_devices &&
441 ignore_device_match (il_interface_devices, name, path) != 0)
444 add_interface_device (dom, path);
446 if (path) xmlFree (path);
450 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
451 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
452 if (xml_doc) xmlFreeDoc (xml_doc);
468 for (i = 0; i < nr_domains; ++i)
469 virDomainFree (domains[i]);
477 add_domain (virDomainPtr dom)
479 virDomainPtr *new_ptr;
480 int new_size = sizeof (domains[0]) * (nr_domains+1);
483 new_ptr = realloc (domains, new_size);
485 new_ptr = malloc (new_size);
487 if (new_ptr == NULL) return -1;
489 domains[nr_domains] = dom;
494 free_block_devices ()
499 for (i = 0; i < nr_block_devices; ++i)
500 free (block_devices[i].path);
501 free (block_devices);
503 block_devices = NULL;
504 nr_block_devices = 0;
508 add_block_device (virDomainPtr dom, const char *path)
510 struct block_device *new_ptr;
511 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
514 path_copy = strdup (path);
515 if (!path_copy) return -1;
518 new_ptr = realloc (block_devices, new_size);
520 new_ptr = malloc (new_size);
522 if (new_ptr == NULL) {
526 block_devices = new_ptr;
527 block_devices[nr_block_devices].dom = dom;
528 block_devices[nr_block_devices].path = path_copy;
529 return nr_block_devices++;
533 free_interface_devices ()
537 if (interface_devices) {
538 for (i = 0; i < nr_interface_devices; ++i)
539 free (interface_devices[i].path);
540 free (interface_devices);
542 interface_devices = NULL;
543 nr_interface_devices = 0;
547 add_interface_device (virDomainPtr dom, const char *path)
549 struct interface_device *new_ptr;
550 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
553 path_copy = strdup (path);
554 if (!path_copy) return -1;
556 if (interface_devices)
557 new_ptr = realloc (interface_devices, new_size);
559 new_ptr = malloc (new_size);
561 if (new_ptr == NULL) {
565 interface_devices = new_ptr;
566 interface_devices[nr_interface_devices].dom = dom;
567 interface_devices[nr_interface_devices].path = path_copy;
568 return nr_interface_devices++;
572 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
577 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
580 ERROR ("malloc: %s", strerror (errno));
583 snprintf (name, n, "%s:%s", domname, devpath);
584 r = ignorelist_match (il, name);
590 cpu_submit (unsigned long long cpu_time,
592 const char *domname, const char *type)
595 value_list_t vl = VALUE_LIST_INIT;
597 values[0].counter = cpu_time;
602 vl.interval = interval_g;
603 strncpy (vl.plugin, "libvirtstats", DATA_MAX_NAME_LEN);
604 strncpy (vl.host, domname, DATA_MAX_NAME_LEN);
605 /*strncpy (vl.type_instance, ?, DATA_MAX_NAME_LEN);*/
607 plugin_dispatch_values (type, &vl);
611 vcpu_submit (unsigned long long cpu_time,
613 const char *domname, int vcpu_nr, const char *type)
616 value_list_t vl = VALUE_LIST_INIT;
618 values[0].counter = cpu_time;
623 vl.interval = interval_g;
624 strncpy (vl.plugin, "libvirtstats", DATA_MAX_NAME_LEN);
625 strncpy (vl.host, domname, DATA_MAX_NAME_LEN);
626 snprintf (vl.type_instance, DATA_MAX_NAME_LEN, "%d", vcpu_nr);
628 plugin_dispatch_values (type, &vl);
632 disk_submit (long long read, long long write,
634 const char *domname, const char *devname,
638 value_list_t vl = VALUE_LIST_INIT;
640 values[0].counter = read >= 0 ? (unsigned long long) read : 0;
641 values[1].counter = write >= 0 ? (unsigned long long) write : 0;
646 vl.interval = interval_g;
647 strncpy (vl.plugin, "libvirtstats", DATA_MAX_NAME_LEN);
648 strncpy (vl.host, domname, DATA_MAX_NAME_LEN);
649 strncpy (vl.type_instance, devname, DATA_MAX_NAME_LEN);
651 plugin_dispatch_values (type, &vl);
655 if_submit (long long rx, long long tx,
657 const char *domname, const char *devname,
661 value_list_t vl = VALUE_LIST_INIT;
663 values[0].counter = rx >= 0 ? (unsigned long long) rx : 0;
664 values[1].counter = tx >= 0 ? (unsigned long long) tx : 0;
669 vl.interval = interval_g;
670 strncpy (vl.plugin, "libvirtstats", DATA_MAX_NAME_LEN);
671 strncpy (vl.host, domname, DATA_MAX_NAME_LEN);
672 strncpy (vl.type_instance, devname, DATA_MAX_NAME_LEN);
674 plugin_dispatch_values (type, &vl);
678 libvirtstats_shutdown (void)
680 free_block_devices ();
681 free_interface_devices ();
684 if (conn) virConnectClose (conn);
687 ignorelist_free (il_domains);
689 ignorelist_free (il_block_devices);
690 il_block_devices = NULL;
691 ignorelist_free (il_interface_devices);
692 il_interface_devices = NULL;
698 module_register (void)
700 plugin_register_config ("libvirtstats",
702 config_keys, NR_CONFIG_KEYS);
703 plugin_register_init ("libvirtstats", libvirtstats_init);
704 plugin_register_read ("libvirtstats", libvirtstats_read);
705 plugin_register_shutdown ("libvirtstats", libvirtstats_shutdown);
709 * vim: set tabstop=4:
710 * vim: set shiftwidth=4:
711 * vim: set expandtab:
715 * indent-tabs-mode: nil