1 /**
2 * collectd - src/libvirtstats.c
3 * Copyright (C) 2006,2007 Red Hat Inc.
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Richard W.M. Jones <rjones@redhat.com>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
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[] = {
37 "Connection",
39 "RefreshInterval",
41 "Domain",
42 "BlockDevice",
43 "InterfaceDevice",
44 "IgnoreSelected",
46 "HostnameFormat",
48 NULL
49 };
50 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
52 /* Connection. */
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. */
76 struct block_device {
77 virDomainPtr dom; /* domain */
78 char *path; /* name of block device */
79 };
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 */
91 };
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);
99 /* HostnameFormat. */
100 #define HF_MAX_FIELDS 3
102 enum hf_field {
103 hf_none = 0,
104 hf_hostname,
105 hf_name,
106 hf_uuid
107 };
109 static enum hf_field hostname_format[HF_MAX_FIELDS] =
110 { hf_name };
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,
119 time_t t,
120 virDomainPtr dom, const char *type);
121 static void vcpu_submit (unsigned long long cpu_time,
122 time_t t,
123 virDomainPtr dom, int vcpu_nr, const char *type);
124 static void disk_submit (long long read, long long write,
125 time_t t,
126 virDomainPtr dom, const char *devname,
127 const char *type);
128 static void if_submit (long long rx, long long tx,
129 time_t t,
130 virDomainPtr dom, const char *devname,
131 const char *type);
133 /* ERROR(...) macro for virterrors. */
134 #define VIRT_ERROR(conn,s) do { \
135 virErrorPtr err; \
136 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
137 if (err) ERROR ("%s: %s", (s), err->message); \
138 } while(0)
140 static int
141 libvirtstats_init (void)
142 {
143 if (virInitialize () == -1)
144 return -1;
146 return 0;
147 }
149 static int
150 libvirtstats_config (const char *key, const char *value)
151 {
152 if (virInitialize () == -1)
153 return 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) {
163 if (conn != 0) {
164 ERROR ("Connection may only be given once in config file");
165 return 1;
166 }
167 conn = virConnectOpenReadOnly (value);
168 if (!conn) {
169 VIRT_ERROR (NULL, "connection failed");
170 return 1;
171 }
172 return 0;
173 }
175 if (strcasecmp (key, "RefreshInterval") == 0) {
176 char *eptr = NULL;
177 interval = strtol (value, &eptr, 10);
178 if (eptr == NULL || *eptr != '\0') return 1;
179 return 0;
180 }
182 if (strcasecmp (key, "Domain") == 0) {
183 if (ignorelist_add (il_domains, value)) return 1;
184 return 0;
185 }
186 if (strcasecmp (key, "BlockDevice") == 0) {
187 if (ignorelist_add (il_block_devices, value)) return 1;
188 return 0;
189 }
190 if (strcasecmp (key, "InterfaceDevice") == 0) {
191 if (ignorelist_add (il_interface_devices, value)) return 1;
192 return 0;
193 }
195 if (strcasecmp (key, "IgnoreSelected") == 0) {
196 if (strcasecmp (value, "True") == 0 ||
197 strcasecmp (value, "Yes") == 0 ||
198 strcasecmp (value, "On") == 0)
199 {
200 ignorelist_set_invert (il_domains, 0);
201 ignorelist_set_invert (il_block_devices, 0);
202 ignorelist_set_invert (il_interface_devices, 0);
203 }
204 else
205 {
206 ignorelist_set_invert (il_domains, 1);
207 ignorelist_set_invert (il_block_devices, 1);
208 ignorelist_set_invert (il_interface_devices, 1);
209 }
210 return 0;
211 }
213 if (strcasecmp (key, "HostnameFormat") == 0) {
214 char *value_copy;
215 char *fields[HF_MAX_FIELDS];
216 int i, n;
218 value_copy = strdup (value);
219 if (value_copy == NULL) {
220 ERROR ("strdup: %s", strerror (errno));
221 return -1;
222 }
224 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
225 if (n < 1) {
226 free (value_copy);
227 ERROR ("HostnameFormat: no fields");
228 return -1;
229 }
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;
238 else {
239 free (value_copy);
240 ERROR ("unknown HostnameFormat field: %s", fields[i]);
241 return -1;
242 }
243 }
244 free (value_copy);
246 for (i = n; i < HF_MAX_FIELDS; ++i)
247 hostname_format[i] = hf_none;
249 return 0;
250 }
252 /* Unrecognised option. */
253 return -1;
254 }
256 static int
257 libvirtstats_read (void)
258 {
259 time_t t;
260 int i;
262 if (conn == NULL) {
263 ERROR ("Not connected. Use Connection in config file to supply connection URI. For more information see http://libvirt.org/uri.html");
264 return -1;
265 }
267 time (&t);
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;
273 last_refresh = t;
274 }
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);
287 #endif
289 /* Get CPU usage, VCPU usage for each domain. */
290 for (i = 0; i < nr_domains; ++i) {
291 virDomainInfo info;
292 virVcpuInfoPtr vinfo = NULL;
293 int j;
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]);
300 if (vinfo == NULL) {
301 ERROR ("malloc: %s", strerror (errno));
302 continue;
303 }
305 if (virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
306 NULL, 0) == -1) {
307 free (vinfo);
308 continue;
309 }
311 for (j = 0; j < info.nrVirtCpu; ++j)
312 vcpu_submit (vinfo[j].cpuTime,
313 t, domains[i], vinfo[j].number, "virt_vcpu");
315 free (vinfo);
316 }
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)
324 continue;
326 disk_submit (stats.rd_req, stats.wr_req,
327 t, block_devices[i].dom, block_devices[i].path,
328 "disk_ops");
329 disk_submit (stats.rd_bytes, stats.wr_bytes,
330 t, block_devices[i].dom, block_devices[i].path,
331 "disk_octets");
332 }
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)
341 continue;
343 if_submit (stats.rx_bytes, stats.tx_bytes,
344 t, interface_devices[i].dom, interface_devices[i].path,
345 "if_octets");
346 if_submit (stats.rx_packets, stats.tx_packets,
347 t, interface_devices[i].dom, interface_devices[i].path,
348 "if_packets");
349 if_submit (stats.rx_errs, stats.tx_errs,
350 t, interface_devices[i].dom, interface_devices[i].path,
351 "if_errors");
352 if_submit (stats.rx_drop, stats.tx_drop,
353 t, interface_devices[i].dom, interface_devices[i].path,
354 "if_dropped");
355 }
357 return 0;
358 }
360 static int
361 refresh_lists (void)
362 {
363 int n;
365 n = virConnectNumOfDomains (conn);
366 if (n == -1) {
367 VIRT_ERROR (conn, "reading number of domains");
368 return -1;
369 }
371 if (n > 0) {
372 int i;
373 int *domids;
375 /* Get list of domains. */
376 domids = malloc (sizeof (int) * n);
377 if (domids == 0) {
378 ERROR ("malloc failed: %s", strerror (errno));
379 return -1;
380 }
382 n = virConnectListDomains (conn, domids, n);
383 if (n == -1) {
384 VIRT_ERROR (conn, "reading list of domains");
385 free (domids);
386 return -1;
387 }
389 free_block_devices ();
390 free_interface_devices ();
391 free_domains ();
393 /* Fetch each domain and add it to the list, unless ignore. */
394 for (i = 0; i < n; ++i) {
395 virDomainPtr dom = NULL;
396 const char *name;
397 char *xml = NULL;
398 xmlDocPtr xml_doc = NULL;
399 xmlXPathContextPtr xpath_ctx = NULL;
400 xmlXPathObjectPtr xpath_obj = NULL;
401 int j;
403 dom = virDomainLookupByID (conn, domids[i]);
404 if (dom == NULL) {
405 VIRT_ERROR (conn, "virDomainLookupByID");
406 /* Could be that the domain went away -- ignore it anyway. */
407 continue;
408 }
410 name = virDomainGetName (dom);
411 if (name == NULL) {
412 VIRT_ERROR (conn, "virDomainGetName");
413 goto cont;
414 }
416 if (il_domains && ignorelist_match (il_domains, name) != 0)
417 goto cont;
419 if (add_domain (dom) == -1) {
420 ERROR ("malloc: %s", strerror (errno));
421 goto cont;
422 }
424 /* Get a list of devices for this domain. */
425 xml = virDomainGetXMLDesc (dom, 0);
426 if (!xml) {
427 VIRT_ERROR (conn, "virDomainGetXMLDesc");
428 goto cont;
429 }
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");
435 goto cont;
436 }
438 xpath_ctx = xmlXPathNewContext (xml_doc);
440 /* Block devices. */
441 xpath_obj = xmlXPathEval
442 ((xmlChar *) "/domain/devices/disk/target[@dev]",
443 xpath_ctx);
444 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
445 xpath_obj->nodesetval == NULL)
446 goto cont;
448 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
449 xmlNodePtr node;
450 char *path = NULL;
452 node = xpath_obj->nodesetval->nodeTab[j];
453 if (!node) continue;
454 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
455 if (!path) continue;
457 if (il_block_devices &&
458 ignore_device_match (il_block_devices, name, path) != 0)
459 goto cont2;
461 add_block_device (dom, path);
462 cont2:
463 if (path) xmlFree (path);
464 }
465 xmlXPathFreeObject (xpath_obj);
467 /* Network interfaces. */
468 xpath_obj = xmlXPathEval
469 ((xmlChar *) "/domain/devices/interface/target[@dev]",
470 xpath_ctx);
471 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
472 xpath_obj->nodesetval == NULL)
473 goto cont;
475 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
476 xmlNodePtr node;
477 char *path = NULL;
479 node = xpath_obj->nodesetval->nodeTab[j];
480 if (!node) continue;
481 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
482 if (!path) continue;
484 if (il_interface_devices &&
485 ignore_device_match (il_interface_devices, name, path) != 0)
486 goto cont3;
488 add_interface_device (dom, path);
489 cont3:
490 if (path) xmlFree (path);
491 }
493 cont:
494 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
495 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
496 if (xml_doc) xmlFreeDoc (xml_doc);
497 if (xml) free (xml);
498 }
500 free (domids);
501 }
503 return 0;
504 }
506 static void
507 free_domains ()
508 {
509 int i;
511 if (domains) {
512 for (i = 0; i < nr_domains; ++i)
513 virDomainFree (domains[i]);
514 free (domains);
515 }
516 domains = NULL;
517 nr_domains = 0;
518 }
520 static int
521 add_domain (virDomainPtr dom)
522 {
523 virDomainPtr *new_ptr;
524 int new_size = sizeof (domains[0]) * (nr_domains+1);
526 if (domains)
527 new_ptr = realloc (domains, new_size);
528 else
529 new_ptr = malloc (new_size);
531 if (new_ptr == NULL) return -1;
532 domains = new_ptr;
533 domains[nr_domains] = dom;
534 return nr_domains++;
535 }
537 static void
538 free_block_devices ()
539 {
540 int i;
542 if (block_devices) {
543 for (i = 0; i < nr_block_devices; ++i)
544 free (block_devices[i].path);
545 free (block_devices);
546 }
547 block_devices = NULL;
548 nr_block_devices = 0;
549 }
551 static int
552 add_block_device (virDomainPtr dom, const char *path)
553 {
554 struct block_device *new_ptr;
555 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
556 char *path_copy;
558 path_copy = strdup (path);
559 if (!path_copy) return -1;
561 if (block_devices)
562 new_ptr = realloc (block_devices, new_size);
563 else
564 new_ptr = malloc (new_size);
566 if (new_ptr == NULL) {
567 free (path_copy);
568 return -1;
569 }
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++;
574 }
576 static void
577 free_interface_devices ()
578 {
579 int i;
581 if (interface_devices) {
582 for (i = 0; i < nr_interface_devices; ++i)
583 free (interface_devices[i].path);
584 free (interface_devices);
585 }
586 interface_devices = NULL;
587 nr_interface_devices = 0;
588 }
590 static int
591 add_interface_device (virDomainPtr dom, const char *path)
592 {
593 struct interface_device *new_ptr;
594 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
595 char *path_copy;
597 path_copy = strdup (path);
598 if (!path_copy) return -1;
600 if (interface_devices)
601 new_ptr = realloc (interface_devices, new_size);
602 else
603 new_ptr = malloc (new_size);
605 if (new_ptr == NULL) {
606 free (path_copy);
607 return -1;
608 }
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++;
613 }
615 static int
616 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
617 {
618 char *name;
619 int n, r;
621 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
622 name = malloc (n);
623 if (name == NULL) {
624 ERROR ("malloc: %s", strerror (errno));
625 return 0;
626 }
627 snprintf (name, n, "%s:%s", domname, devpath);
628 r = ignorelist_match (il, name);
629 free (name);
630 return r;
631 }
633 static void
634 common_submit (value_list_t *vl, time_t t, virDomainPtr dom)
635 {
636 int i, n;
637 const char *name;
638 char uuid[VIR_UUID_STRING_BUFLEN];
640 vl->time = t;
641 vl->interval = interval_g;
642 strncpy (vl->plugin, "libvirtstats", DATA_MAX_NAME_LEN);
643 /*strncpy (vl->plugin_instance, ?, DATA_MAX_NAME_LEN);*/
645 vl->host[0] = '\0';
647 /* Construct the hostname field according to HostnameFormat. */
648 for (i = 0; i < HF_MAX_FIELDS; ++i) {
649 if (hostname_format[i] == hf_none)
650 continue;
652 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
654 if (i > 0 && n >= 1) {
655 strcat (vl->host, ":");
656 n--;
657 }
659 switch (hostname_format[i]) {
660 case hf_none: break;
661 case hf_hostname:
662 strncat (vl->host, hostname_g, n);
663 break;
664 case hf_name:
665 name = virDomainGetName (dom);
666 if (name)
667 strncat (vl->host, name, n);
668 break;
669 case hf_uuid:
670 if (virDomainGetUUIDString (dom, uuid) == 0)
671 strncat (vl->host, uuid, n);
672 break;
673 }
674 }
676 vl->host[DATA_MAX_NAME_LEN-1] = '\0';
677 }
679 static void
680 cpu_submit (unsigned long long cpu_time,
681 time_t t,
682 virDomainPtr dom, const char *type)
683 {
684 value_t values[1];
685 value_list_t vl = VALUE_LIST_INIT;
687 common_submit (&vl, t, dom);
689 values[0].counter = cpu_time;
691 vl.values = values;
692 vl.values_len = 1;
694 plugin_dispatch_values (type, &vl);
695 }
697 static void
698 vcpu_submit (unsigned long long cpu_time,
699 time_t t,
700 virDomainPtr dom, int vcpu_nr, const char *type)
701 {
702 value_t values[1];
703 value_list_t vl = VALUE_LIST_INIT;
705 common_submit (&vl, t, dom);
707 values[0].counter = cpu_time;
709 vl.values = values;
710 vl.values_len = 1;
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);
715 }
717 static void
718 disk_submit (long long read, long long write,
719 time_t t,
720 virDomainPtr dom, const char *devname,
721 const char *type)
722 {
723 value_t values[2];
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;
731 vl.values = values;
732 vl.values_len = 2;
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);
737 }
739 static void
740 if_submit (long long rx, long long tx,
741 time_t t,
742 virDomainPtr dom, const char *devname,
743 const char *type)
744 {
745 value_t values[2];
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;
753 vl.values = values;
754 vl.values_len = 2;
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);
759 }
761 static int
762 libvirtstats_shutdown (void)
763 {
764 free_block_devices ();
765 free_interface_devices ();
766 free_domains ();
768 if (conn) virConnectClose (conn);
769 conn = NULL;
771 ignorelist_free (il_domains);
772 il_domains = NULL;
773 ignorelist_free (il_block_devices);
774 il_block_devices = NULL;
775 ignorelist_free (il_interface_devices);
776 il_interface_devices = NULL;
778 return 0;
779 }
781 void
782 module_register (void)
783 {
784 plugin_register_config ("libvirtstats",
785 libvirtstats_config,
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);
790 }
792 /*
793 * vim: set tabstop=4:
794 * vim: set shiftwidth=4:
795 * vim: set expandtab:
796 */
797 /*
798 * Local variables:
799 * indent-tabs-mode: nil
800 * c-indent-level: 4
801 * c-basic-offset: 4
802 * tab-width: 4
803 * End:
804 */