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 static const char *config_keys[] = {
48 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
51 static virConnectPtr conn = 0;
53 /* Seconds between list refreshes, 0 disables completely. */
54 static int interval = 60;
56 /* List of domains, if specified. */
57 static ignorelist_t *il_domains = NULL;
58 /* List of block devices, if specified. */
59 static ignorelist_t *il_block_devices = NULL;
60 /* List of network interface devices, if specified. */
61 static ignorelist_t *il_interface_devices = NULL;
63 static int ignore_device_match (ignorelist_t *,
64 const char *domname, const char *devpath);
66 /* Actual list of domains found on last refresh. */
67 static virDomainPtr *domains = NULL;
68 static int nr_domains = 0;
70 static void free_domains (void);
71 static int add_domain (virDomainPtr dom);
73 /* Actual list of block devices found on last refresh. */
75 virDomainPtr dom; /* domain */
76 char *path; /* name of block device */
79 static struct block_device *block_devices = NULL;
80 static int nr_block_devices = 0;
82 static void free_block_devices (void);
83 static int add_block_device (virDomainPtr dom, const char *path);
85 /* Actual list of network interfaces found on last refresh. */
86 struct interface_device {
87 virDomainPtr dom; /* domain */
88 char *path; /* name of interface device */
91 static struct interface_device *interface_devices = NULL;
92 static int nr_interface_devices = 0;
94 static void free_interface_devices (void);
95 static int add_interface_device (virDomainPtr dom, const char *path);
98 #define HF_MAX_FIELDS 3
107 static enum hf_field hostname_format[HF_MAX_FIELDS] =
110 /* Time that we last refreshed. */
111 static time_t last_refresh = (time_t) 0;
113 static int refresh_lists (void);
115 /* Submit functions. */
116 static void cpu_submit (unsigned long long cpu_time,
118 virDomainPtr dom, const char *type);
119 static void vcpu_submit (unsigned long long cpu_time,
121 virDomainPtr dom, int vcpu_nr, const char *type);
122 static void submit_counter2 (long long read, long long write,
124 virDomainPtr dom, const char *devname,
127 /* ERROR(...) macro for virterrors. */
128 #define VIRT_ERROR(conn,s) do { \
130 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
131 if (err) ERROR ("%s: %s", (s), err->message); \
135 libvirtstats_init (void)
137 if (virInitialize () != 0)
144 libvirtstats_config (const char *key, const char *value)
146 if (virInitialize () != 0)
149 if (il_domains == NULL)
150 il_domains = ignorelist_create (1);
151 if (il_block_devices == NULL)
152 il_block_devices = ignorelist_create (1);
153 if (il_interface_devices == NULL)
154 il_interface_devices = ignorelist_create (1);
156 if (strcasecmp (key, "Connection") == 0) {
158 ERROR ("Connection may only be given once in config file");
161 conn = virConnectOpenReadOnly (value);
163 VIRT_ERROR (NULL, "connection failed");
169 if (strcasecmp (key, "RefreshInterval") == 0) {
171 interval = strtol (value, &eptr, 10);
172 if (eptr == NULL || *eptr != '\0') return 1;
176 if (strcasecmp (key, "Domain") == 0) {
177 if (ignorelist_add (il_domains, value)) return 1;
180 if (strcasecmp (key, "BlockDevice") == 0) {
181 if (ignorelist_add (il_block_devices, value)) return 1;
184 if (strcasecmp (key, "InterfaceDevice") == 0) {
185 if (ignorelist_add (il_interface_devices, value)) return 1;
189 if (strcasecmp (key, "IgnoreSelected") == 0) {
190 if (strcasecmp (value, "True") == 0 ||
191 strcasecmp (value, "Yes") == 0 ||
192 strcasecmp (value, "On") == 0)
194 ignorelist_set_invert (il_domains, 0);
195 ignorelist_set_invert (il_block_devices, 0);
196 ignorelist_set_invert (il_interface_devices, 0);
200 ignorelist_set_invert (il_domains, 1);
201 ignorelist_set_invert (il_block_devices, 1);
202 ignorelist_set_invert (il_interface_devices, 1);
207 if (strcasecmp (key, "HostnameFormat") == 0) {
209 char *fields[HF_MAX_FIELDS];
212 value_copy = strdup (value);
213 if (value_copy == NULL) {
214 ERROR ("libvirtstats plugin: strdup failed.");
218 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
221 ERROR ("HostnameFormat: no fields");
225 for (i = 0; i < n; ++i) {
226 if (strcasecmp (fields[i], "hostname") == 0)
227 hostname_format[i] = hf_hostname;
228 else if (strcasecmp (fields[i], "name") == 0)
229 hostname_format[i] = hf_name;
230 else if (strcasecmp (fields[i], "uuid") == 0)
231 hostname_format[i] = hf_uuid;
234 ERROR ("unknown HostnameFormat field: %s", fields[i]);
240 for (i = n; i < HF_MAX_FIELDS; ++i)
241 hostname_format[i] = hf_none;
246 /* Unrecognised option. */
251 libvirtstats_read (void)
257 ERROR ("libvirtstats plugin: Not connected. Use Connection in "
258 "config file to supply connection URI. For more information "
259 "see <http://libvirt.org/uri.html>");
265 /* Need to refresh domain or device lists? */
266 if ((last_refresh == (time_t) 0) ||
267 ((interval > 0) && ((last_refresh + interval) <= t))) {
268 if (refresh_lists () != 0)
274 for (i = 0; i < nr_domains; ++i)
275 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
276 for (i = 0; i < nr_block_devices; ++i)
277 fprintf (stderr, "block device %d %s:%s\n",
278 i, virDomainGetName (block_devices[i].dom),
279 block_devices[i].path);
280 for (i = 0; i < nr_interface_devices; ++i)
281 fprintf (stderr, "interface device %d %s:%s\n",
282 i, virDomainGetName (interface_devices[i].dom),
283 interface_devices[i].path);
286 /* Get CPU usage, VCPU usage for each domain. */
287 for (i = 0; i < nr_domains; ++i) {
289 virVcpuInfoPtr vinfo = NULL;
292 if (virDomainGetInfo (domains[i], &info) != 0)
295 cpu_submit (info.cpuTime, t, domains[i], "virt_cpu_total");
297 vinfo = malloc (info.nrVirtCpu * sizeof vinfo[0]);
299 ERROR ("libvirtstats plugin: malloc failed.");
303 if (virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
309 for (j = 0; j < info.nrVirtCpu; ++j)
310 vcpu_submit (vinfo[j].cpuTime,
311 t, domains[i], vinfo[j].number, "virt_vcpu");
316 /* Get block device stats for each domain. */
317 for (i = 0; i < nr_block_devices; ++i) {
318 struct _virDomainBlockStats stats;
320 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
321 &stats, sizeof stats) != 0)
324 if ((stats.rd_req != -1) && (stats.wr_req != -1))
325 submit_counter2 ("disk_ops",
326 (counter_t) stats.rd_req, (counter_t) stats.wr_req,
327 t, block_devices[i].dom, block_devices[i].path);
329 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
330 submit_counter2 ("disk_octets",
331 (counter_t) stats.rd_bytes, (counter_t) stats.wr_bytes,
332 t, block_devices[i].dom, block_devices[i].path);
333 } /* for (nr_block_devices) */
335 /* Get interface stats for each domain. */
336 for (i = 0; i < nr_interface_devices; ++i) {
337 struct _virDomainInterfaceStats stats;
339 if (virDomainInterfaceStats (interface_devices[i].dom,
340 interface_devices[i].path,
341 &stats, sizeof stats) != 0)
344 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
345 submit_counter2 ("if_octets",
346 (counter_t) stats.rx_bytes, (counter_t) stats.tx_bytes,
347 t, interface_devices[i].dom, interface_devices[i].path);
349 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
350 submit_counter2 ("if_packets",
351 (counter_t) stats.rx_packets, (counter_t) stats.tx_packets,
352 t, interface_devices[i].dom, interface_devices[i].path);
354 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
355 submit_counter2 ("if_errors",
356 (counter_t) stats.rx_errs, (counter_t) stats.tx_errs,
357 t, interface_devices[i].dom, interface_devices[i].path);
359 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
360 submit_counter2 ("if_dropped",
361 (counter_t) stats.rx_drop, (counter_t) stats.tx_drop,
362 t, interface_devices[i].dom, interface_devices[i].path);
363 } /* for (nr_interface_devices) */
373 n = virConnectNumOfDomains (conn);
375 VIRT_ERROR (conn, "reading number of domains");
383 /* Get list of domains. */
384 domids = malloc (sizeof (int) * n);
386 ERROR ("libvirtstats plugin: malloc failed.");
390 n = virConnectListDomains (conn, domids, n);
392 VIRT_ERROR (conn, "reading list of domains");
397 free_block_devices ();
398 free_interface_devices ();
401 /* Fetch each domain and add it to the list, unless ignore. */
402 for (i = 0; i < n; ++i) {
403 virDomainPtr dom = NULL;
406 xmlDocPtr xml_doc = NULL;
407 xmlXPathContextPtr xpath_ctx = NULL;
408 xmlXPathObjectPtr xpath_obj = NULL;
411 dom = virDomainLookupByID (conn, domids[i]);
413 VIRT_ERROR (conn, "virDomainLookupByID");
414 /* Could be that the domain went away -- ignore it anyway. */
418 name = virDomainGetName (dom);
420 VIRT_ERROR (conn, "virDomainGetName");
424 if (il_domains && ignorelist_match (il_domains, name) != 0)
427 if (add_domain (dom) < 0) {
428 ERROR ("libvirtstats plugin: malloc failed.");
432 /* Get a list of devices for this domain. */
433 xml = virDomainGetXMLDesc (dom, 0);
435 VIRT_ERROR (conn, "virDomainGetXMLDesc");
439 /* Yuck, XML. Parse out the devices. */
440 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
441 if (xml_doc == NULL) {
442 VIRT_ERROR (conn, "xmlReadDoc");
446 xpath_ctx = xmlXPathNewContext (xml_doc);
449 xpath_obj = xmlXPathEval
450 ((xmlChar *) "/domain/devices/disk/target[@dev]",
452 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
453 xpath_obj->nodesetval == NULL)
456 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
460 node = xpath_obj->nodesetval->nodeTab[j];
462 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
465 if (il_block_devices &&
466 ignore_device_match (il_block_devices, name, path) != 0)
469 add_block_device (dom, path);
471 if (path) xmlFree (path);
473 xmlXPathFreeObject (xpath_obj);
475 /* Network interfaces. */
476 xpath_obj = xmlXPathEval
477 ((xmlChar *) "/domain/devices/interface/target[@dev]",
479 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
480 xpath_obj->nodesetval == NULL)
483 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
487 node = xpath_obj->nodesetval->nodeTab[j];
489 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
492 if (il_interface_devices &&
493 ignore_device_match (il_interface_devices, name, path) != 0)
496 add_interface_device (dom, path);
498 if (path) xmlFree (path);
502 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
503 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
504 if (xml_doc) xmlFreeDoc (xml_doc);
520 for (i = 0; i < nr_domains; ++i)
521 virDomainFree (domains[i]);
529 add_domain (virDomainPtr dom)
531 virDomainPtr *new_ptr;
532 int new_size = sizeof (domains[0]) * (nr_domains+1);
535 new_ptr = realloc (domains, new_size);
537 new_ptr = malloc (new_size);
543 domains[nr_domains] = dom;
548 free_block_devices ()
553 for (i = 0; i < nr_block_devices; ++i)
554 free (block_devices[i].path);
555 free (block_devices);
557 block_devices = NULL;
558 nr_block_devices = 0;
562 add_block_device (virDomainPtr dom, const char *path)
564 struct block_device *new_ptr;
565 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
568 path_copy = strdup (path);
573 new_ptr = realloc (block_devices, new_size);
575 new_ptr = malloc (new_size);
577 if (new_ptr == NULL) {
581 block_devices = new_ptr;
582 block_devices[nr_block_devices].dom = dom;
583 block_devices[nr_block_devices].path = path_copy;
584 return nr_block_devices++;
588 free_interface_devices ()
592 if (interface_devices) {
593 for (i = 0; i < nr_interface_devices; ++i)
594 free (interface_devices[i].path);
595 free (interface_devices);
597 interface_devices = NULL;
598 nr_interface_devices = 0;
602 add_interface_device (virDomainPtr dom, const char *path)
604 struct interface_device *new_ptr;
605 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
608 path_copy = strdup (path);
609 if (!path_copy) return -1;
611 if (interface_devices)
612 new_ptr = realloc (interface_devices, new_size);
614 new_ptr = malloc (new_size);
616 if (new_ptr == NULL) {
620 interface_devices = new_ptr;
621 interface_devices[nr_interface_devices].dom = dom;
622 interface_devices[nr_interface_devices].path = path_copy;
623 return nr_interface_devices++;
627 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
632 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
635 ERROR ("libvirtstats plugin: malloc failed.");
638 snprintf (name, n, "%s:%s", domname, devpath);
639 r = ignorelist_match (il, name);
645 init_value_list (value_list_t *vl, time_t t, virDomainPtr dom)
652 vl->interval = interval_g;
654 strncpy (vl->plugin, "libvirtstats", sizeof (vl->plugin));
655 vl->plugin[sizeof (vl->plugin) - 1] = '\0';
659 host_len = sizeof (vl->host);
661 /* Construct the hostname field according to HostnameFormat. */
662 for (i = 0; i < HF_MAX_FIELDS; ++i) {
665 switch (hostname_format[i]) {
671 status = snprintf (host_ptr, host_len, ":%s", hostname_g);
676 const char *name = virDomainGetName (dom);
678 status = snprintf (host_ptr, host_len, ":%s", name);
683 char uuid[VIR_UUID_STRING_BUFLEN];
684 if (virDomainGetUUIDString (dom, uuid) == 0) {
685 uuid[sizeof (uuid) - 1] = '\0';
686 status = snprintf (host_ptr, host_len, ":%s", uuid);
690 } /* switch (hostname_format[i]) */
692 /* If status >= host_len
693 * => the buffer is full, there's no null-byte at the end and
694 * continuing with this loop doesn't make any sense. */
695 if (status >= host_len) {
699 /* else: Test if anything was added to the buffer */
700 else if (status > 0) {
709 vl->host[sizeof (host) - 1] = '\0';
710 } /* void init_value_list */
713 cpu_submit (unsigned long long cpu_time,
715 virDomainPtr dom, const char *type)
718 value_list_t vl = VALUE_LIST_INIT;
720 init_value_list (&vl, t, dom);
722 values[0].counter = cpu_time;
727 plugin_dispatch_values (type, &vl);
731 vcpu_submit (counter_t cpu_time,
733 virDomainPtr dom, int vcpu_nr, const char *type)
736 value_list_t vl = VALUE_LIST_INIT;
738 init_value_list (&vl, t, dom);
740 values[0].counter = cpu_time;
744 snprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
745 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
747 plugin_dispatch_values (type, &vl);
751 submit_counter2 (const char *type, counter_t v0, counter_t v1,
753 virDomainPtr dom, const char *devname)
756 value_list_t vl = VALUE_LIST_INIT;
758 init_value_list (&vl, t, dom);
760 values[0].counter = v0;
761 values[1].counter = v1;
765 strncpy (vl.type_instance, devname, sizeof (vl.type_instance));
766 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
768 plugin_dispatch_values (type, &vl);
769 } /* void submit_counter2 */
772 libvirtstats_shutdown (void)
774 free_block_devices ();
775 free_interface_devices ();
779 virConnectClose (conn);
782 ignorelist_free (il_domains);
784 ignorelist_free (il_block_devices);
785 il_block_devices = NULL;
786 ignorelist_free (il_interface_devices);
787 il_interface_devices = NULL;
793 module_register (void)
795 plugin_register_config ("libvirtstats",
797 config_keys, NR_CONFIG_KEYS);
798 plugin_register_init ("libvirtstats", libvirtstats_init);
799 plugin_register_read ("libvirtstats", libvirtstats_read);
800 plugin_register_shutdown ("libvirtstats", libvirtstats_shutdown);
804 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker