1 /**
2 * collectd - src/libvirt.c
3 * Copyright (C) 2006-2008 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"
27 #include "utils_complain.h"
29 #include <libvirt/libvirt.h>
30 #include <libvirt/virterror.h>
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
33 #include <libxml/xpath.h>
35 static const char *config_keys[] = {
36 "Connection",
38 "RefreshInterval",
40 "Domain",
41 "BlockDevice",
42 "InterfaceDevice",
43 "IgnoreSelected",
45 "HostnameFormat",
47 NULL
48 };
49 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
51 /* Connection. */
52 static virConnectPtr conn = 0;
53 static char *conn_string = NULL;
54 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
56 /* Seconds between list refreshes, 0 disables completely. */
57 static int interval = 60;
59 /* List of domains, if specified. */
60 static ignorelist_t *il_domains = NULL;
61 /* List of block devices, if specified. */
62 static ignorelist_t *il_block_devices = NULL;
63 /* List of network interface devices, if specified. */
64 static ignorelist_t *il_interface_devices = NULL;
66 static int ignore_device_match (ignorelist_t *,
67 const char *domname, const char *devpath);
69 /* Actual list of domains found on last refresh. */
70 static virDomainPtr *domains = NULL;
71 static int nr_domains = 0;
73 static void free_domains (void);
74 static int add_domain (virDomainPtr dom);
76 /* Actual list of block devices found on last refresh. */
77 struct block_device {
78 virDomainPtr dom; /* domain */
79 char *path; /* name of block device */
80 };
82 static struct block_device *block_devices = NULL;
83 static int nr_block_devices = 0;
85 static void free_block_devices (void);
86 static int add_block_device (virDomainPtr dom, const char *path);
88 /* Actual list of network interfaces found on last refresh. */
89 struct interface_device {
90 virDomainPtr dom; /* domain */
91 char *path; /* name of interface device */
92 };
94 static struct interface_device *interface_devices = NULL;
95 static int nr_interface_devices = 0;
97 static void free_interface_devices (void);
98 static int add_interface_device (virDomainPtr dom, const char *path);
100 /* HostnameFormat. */
101 #define HF_MAX_FIELDS 3
103 enum hf_field {
104 hf_none = 0,
105 hf_hostname,
106 hf_name,
107 hf_uuid
108 };
110 static enum hf_field hostname_format[HF_MAX_FIELDS] =
111 { hf_name };
113 /* Time that we last refreshed. */
114 static time_t last_refresh = (time_t) 0;
116 static int refresh_lists (void);
118 /* Submit functions. */
119 static void cpu_submit (unsigned long long cpu_time,
120 time_t t,
121 virDomainPtr dom, const char *type);
122 static void vcpu_submit (unsigned long long cpu_time,
123 time_t t,
124 virDomainPtr dom, int vcpu_nr, const char *type);
125 static void submit_counter2 (const char *type, counter_t v0, counter_t v1,
126 time_t t,
127 virDomainPtr dom, const char *devname);
129 /* ERROR(...) macro for virterrors. */
130 #define VIRT_ERROR(conn,s) do { \
131 virErrorPtr err; \
132 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
133 if (err) ERROR ("%s: %s", (s), err->message); \
134 } while(0)
136 static int
137 lv_init (void)
138 {
139 if (virInitialize () != 0)
140 return -1;
142 return 0;
143 }
145 static int
146 lv_config (const char *key, const char *value)
147 {
148 if (virInitialize () != 0)
149 return 1;
151 if (il_domains == NULL)
152 il_domains = ignorelist_create (1);
153 if (il_block_devices == NULL)
154 il_block_devices = ignorelist_create (1);
155 if (il_interface_devices == NULL)
156 il_interface_devices = ignorelist_create (1);
158 if (strcasecmp (key, "Connection") == 0) {
159 char *tmp = strdup (value);
160 if (tmp == NULL) {
161 ERROR ("libvirt plugin: Connection strdup failed.");
162 return 1;
163 }
164 sfree (conn_string);
165 conn_string = tmp;
166 return 0;
167 }
169 if (strcasecmp (key, "RefreshInterval") == 0) {
170 char *eptr = NULL;
171 interval = strtol (value, &eptr, 10);
172 if (eptr == NULL || *eptr != '\0') return 1;
173 return 0;
174 }
176 if (strcasecmp (key, "Domain") == 0) {
177 if (ignorelist_add (il_domains, value)) return 1;
178 return 0;
179 }
180 if (strcasecmp (key, "BlockDevice") == 0) {
181 if (ignorelist_add (il_block_devices, value)) return 1;
182 return 0;
183 }
184 if (strcasecmp (key, "InterfaceDevice") == 0) {
185 if (ignorelist_add (il_interface_devices, value)) return 1;
186 return 0;
187 }
189 if (strcasecmp (key, "IgnoreSelected") == 0) {
190 if (strcasecmp (value, "True") == 0 ||
191 strcasecmp (value, "Yes") == 0 ||
192 strcasecmp (value, "On") == 0)
193 {
194 ignorelist_set_invert (il_domains, 0);
195 ignorelist_set_invert (il_block_devices, 0);
196 ignorelist_set_invert (il_interface_devices, 0);
197 }
198 else
199 {
200 ignorelist_set_invert (il_domains, 1);
201 ignorelist_set_invert (il_block_devices, 1);
202 ignorelist_set_invert (il_interface_devices, 1);
203 }
204 return 0;
205 }
207 if (strcasecmp (key, "HostnameFormat") == 0) {
208 char *value_copy;
209 char *fields[HF_MAX_FIELDS];
210 int i, n;
212 value_copy = strdup (value);
213 if (value_copy == NULL) {
214 ERROR ("libvirt plugin: strdup failed.");
215 return -1;
216 }
218 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
219 if (n < 1) {
220 free (value_copy);
221 ERROR ("HostnameFormat: no fields");
222 return -1;
223 }
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;
232 else {
233 free (value_copy);
234 ERROR ("unknown HostnameFormat field: %s", fields[i]);
235 return -1;
236 }
237 }
238 free (value_copy);
240 for (i = n; i < HF_MAX_FIELDS; ++i)
241 hostname_format[i] = hf_none;
243 return 0;
244 }
246 /* Unrecognised option. */
247 return -1;
248 }
250 static int
251 lv_read (void)
252 {
253 time_t t;
254 int i;
256 if (conn == NULL) {
257 /* `conn_string == NULL' is acceptable. */
258 conn = virConnectOpenReadOnly (conn_string);
259 if (conn == NULL) {
260 c_complain (LOG_ERR, &conn_complain,
261 "libvirt plugin: Unable to connect: "
262 "virConnectOpenReadOnly failed.");
263 return -1;
264 }
265 }
266 c_release (LOG_NOTICE, &conn_complain,
267 "libvirt plugin: Connection established.");
269 time (&t);
271 /* Need to refresh domain or device lists? */
272 if ((last_refresh == (time_t) 0) ||
273 ((interval > 0) && ((last_refresh + interval) <= t))) {
274 if (refresh_lists () != 0) {
275 if (conn != NULL)
276 virConnectClose (conn);
277 conn = NULL;
278 return -1;
279 }
280 last_refresh = t;
281 }
283 #if 0
284 for (i = 0; i < nr_domains; ++i)
285 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
286 for (i = 0; i < nr_block_devices; ++i)
287 fprintf (stderr, "block device %d %s:%s\n",
288 i, virDomainGetName (block_devices[i].dom),
289 block_devices[i].path);
290 for (i = 0; i < nr_interface_devices; ++i)
291 fprintf (stderr, "interface device %d %s:%s\n",
292 i, virDomainGetName (interface_devices[i].dom),
293 interface_devices[i].path);
294 #endif
296 /* Get CPU usage, VCPU usage for each domain. */
297 for (i = 0; i < nr_domains; ++i) {
298 virDomainInfo info;
299 virVcpuInfoPtr vinfo = NULL;
300 int j;
302 if (virDomainGetInfo (domains[i], &info) != 0)
303 continue;
305 cpu_submit (info.cpuTime, t, domains[i], "virt_cpu_total");
307 vinfo = malloc (info.nrVirtCpu * sizeof vinfo[0]);
308 if (vinfo == NULL) {
309 ERROR ("libvirt plugin: malloc failed.");
310 continue;
311 }
313 if (virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
314 NULL, 0) != 0) {
315 free (vinfo);
316 continue;
317 }
319 for (j = 0; j < info.nrVirtCpu; ++j)
320 vcpu_submit (vinfo[j].cpuTime,
321 t, domains[i], vinfo[j].number, "virt_vcpu");
323 free (vinfo);
324 }
326 /* Get block device stats for each domain. */
327 for (i = 0; i < nr_block_devices; ++i) {
328 struct _virDomainBlockStats stats;
330 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
331 &stats, sizeof stats) != 0)
332 continue;
334 if ((stats.rd_req != -1) && (stats.wr_req != -1))
335 submit_counter2 ("disk_ops",
336 (counter_t) stats.rd_req, (counter_t) stats.wr_req,
337 t, block_devices[i].dom, block_devices[i].path);
339 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
340 submit_counter2 ("disk_octets",
341 (counter_t) stats.rd_bytes, (counter_t) stats.wr_bytes,
342 t, block_devices[i].dom, block_devices[i].path);
343 } /* for (nr_block_devices) */
345 /* Get interface stats for each domain. */
346 for (i = 0; i < nr_interface_devices; ++i) {
347 struct _virDomainInterfaceStats stats;
349 if (virDomainInterfaceStats (interface_devices[i].dom,
350 interface_devices[i].path,
351 &stats, sizeof stats) != 0)
352 continue;
354 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
355 submit_counter2 ("if_octets",
356 (counter_t) stats.rx_bytes, (counter_t) stats.tx_bytes,
357 t, interface_devices[i].dom, interface_devices[i].path);
359 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
360 submit_counter2 ("if_packets",
361 (counter_t) stats.rx_packets, (counter_t) stats.tx_packets,
362 t, interface_devices[i].dom, interface_devices[i].path);
364 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
365 submit_counter2 ("if_errors",
366 (counter_t) stats.rx_errs, (counter_t) stats.tx_errs,
367 t, interface_devices[i].dom, interface_devices[i].path);
369 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
370 submit_counter2 ("if_dropped",
371 (counter_t) stats.rx_drop, (counter_t) stats.tx_drop,
372 t, interface_devices[i].dom, interface_devices[i].path);
373 } /* for (nr_interface_devices) */
375 return 0;
376 }
378 static int
379 refresh_lists (void)
380 {
381 int n;
383 n = virConnectNumOfDomains (conn);
384 if (n < 0) {
385 VIRT_ERROR (conn, "reading number of domains");
386 return -1;
387 }
389 if (n > 0) {
390 int i;
391 int *domids;
393 /* Get list of domains. */
394 domids = malloc (sizeof (int) * n);
395 if (domids == 0) {
396 ERROR ("libvirt plugin: malloc failed.");
397 return -1;
398 }
400 n = virConnectListDomains (conn, domids, n);
401 if (n < 0) {
402 VIRT_ERROR (conn, "reading list of domains");
403 free (domids);
404 return -1;
405 }
407 free_block_devices ();
408 free_interface_devices ();
409 free_domains ();
411 /* Fetch each domain and add it to the list, unless ignore. */
412 for (i = 0; i < n; ++i) {
413 virDomainPtr dom = NULL;
414 const char *name;
415 char *xml = NULL;
416 xmlDocPtr xml_doc = NULL;
417 xmlXPathContextPtr xpath_ctx = NULL;
418 xmlXPathObjectPtr xpath_obj = NULL;
419 int j;
421 dom = virDomainLookupByID (conn, domids[i]);
422 if (dom == NULL) {
423 VIRT_ERROR (conn, "virDomainLookupByID");
424 /* Could be that the domain went away -- ignore it anyway. */
425 continue;
426 }
428 name = virDomainGetName (dom);
429 if (name == NULL) {
430 VIRT_ERROR (conn, "virDomainGetName");
431 goto cont;
432 }
434 if (il_domains && ignorelist_match (il_domains, name) != 0)
435 goto cont;
437 if (add_domain (dom) < 0) {
438 ERROR ("libvirt plugin: malloc failed.");
439 goto cont;
440 }
442 /* Get a list of devices for this domain. */
443 xml = virDomainGetXMLDesc (dom, 0);
444 if (!xml) {
445 VIRT_ERROR (conn, "virDomainGetXMLDesc");
446 goto cont;
447 }
449 /* Yuck, XML. Parse out the devices. */
450 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
451 if (xml_doc == NULL) {
452 VIRT_ERROR (conn, "xmlReadDoc");
453 goto cont;
454 }
456 xpath_ctx = xmlXPathNewContext (xml_doc);
458 /* Block devices. */
459 xpath_obj = xmlXPathEval
460 ((xmlChar *) "/domain/devices/disk/target[@dev]",
461 xpath_ctx);
462 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
463 xpath_obj->nodesetval == NULL)
464 goto cont;
466 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
467 xmlNodePtr node;
468 char *path = NULL;
470 node = xpath_obj->nodesetval->nodeTab[j];
471 if (!node) continue;
472 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
473 if (!path) continue;
475 if (il_block_devices &&
476 ignore_device_match (il_block_devices, name, path) != 0)
477 goto cont2;
479 add_block_device (dom, path);
480 cont2:
481 if (path) xmlFree (path);
482 }
483 xmlXPathFreeObject (xpath_obj);
485 /* Network interfaces. */
486 xpath_obj = xmlXPathEval
487 ((xmlChar *) "/domain/devices/interface/target[@dev]",
488 xpath_ctx);
489 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
490 xpath_obj->nodesetval == NULL)
491 goto cont;
493 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
494 xmlNodePtr node;
495 char *path = NULL;
497 node = xpath_obj->nodesetval->nodeTab[j];
498 if (!node) continue;
499 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
500 if (!path) continue;
502 if (il_interface_devices &&
503 ignore_device_match (il_interface_devices, name, path) != 0)
504 goto cont3;
506 add_interface_device (dom, path);
507 cont3:
508 if (path) xmlFree (path);
509 }
511 cont:
512 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
513 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
514 if (xml_doc) xmlFreeDoc (xml_doc);
515 if (xml) free (xml);
516 }
518 free (domids);
519 }
521 return 0;
522 }
524 static void
525 free_domains ()
526 {
527 int i;
529 if (domains) {
530 for (i = 0; i < nr_domains; ++i)
531 virDomainFree (domains[i]);
532 free (domains);
533 }
534 domains = NULL;
535 nr_domains = 0;
536 }
538 static int
539 add_domain (virDomainPtr dom)
540 {
541 virDomainPtr *new_ptr;
542 int new_size = sizeof (domains[0]) * (nr_domains+1);
544 if (domains)
545 new_ptr = realloc (domains, new_size);
546 else
547 new_ptr = malloc (new_size);
549 if (new_ptr == NULL)
550 return -1;
552 domains = new_ptr;
553 domains[nr_domains] = dom;
554 return nr_domains++;
555 }
557 static void
558 free_block_devices ()
559 {
560 int i;
562 if (block_devices) {
563 for (i = 0; i < nr_block_devices; ++i)
564 free (block_devices[i].path);
565 free (block_devices);
566 }
567 block_devices = NULL;
568 nr_block_devices = 0;
569 }
571 static int
572 add_block_device (virDomainPtr dom, const char *path)
573 {
574 struct block_device *new_ptr;
575 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
576 char *path_copy;
578 path_copy = strdup (path);
579 if (!path_copy)
580 return -1;
582 if (block_devices)
583 new_ptr = realloc (block_devices, new_size);
584 else
585 new_ptr = malloc (new_size);
587 if (new_ptr == NULL) {
588 free (path_copy);
589 return -1;
590 }
591 block_devices = new_ptr;
592 block_devices[nr_block_devices].dom = dom;
593 block_devices[nr_block_devices].path = path_copy;
594 return nr_block_devices++;
595 }
597 static void
598 free_interface_devices ()
599 {
600 int i;
602 if (interface_devices) {
603 for (i = 0; i < nr_interface_devices; ++i)
604 free (interface_devices[i].path);
605 free (interface_devices);
606 }
607 interface_devices = NULL;
608 nr_interface_devices = 0;
609 }
611 static int
612 add_interface_device (virDomainPtr dom, const char *path)
613 {
614 struct interface_device *new_ptr;
615 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
616 char *path_copy;
618 path_copy = strdup (path);
619 if (!path_copy) return -1;
621 if (interface_devices)
622 new_ptr = realloc (interface_devices, new_size);
623 else
624 new_ptr = malloc (new_size);
626 if (new_ptr == NULL) {
627 free (path_copy);
628 return -1;
629 }
630 interface_devices = new_ptr;
631 interface_devices[nr_interface_devices].dom = dom;
632 interface_devices[nr_interface_devices].path = path_copy;
633 return nr_interface_devices++;
634 }
636 static int
637 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
638 {
639 char *name;
640 int n, r;
642 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
643 name = malloc (n);
644 if (name == NULL) {
645 ERROR ("libvirt plugin: malloc failed.");
646 return 0;
647 }
648 ssnprintf (name, n, "%s:%s", domname, devpath);
649 r = ignorelist_match (il, name);
650 free (name);
651 return r;
652 }
654 static void
655 init_value_list (value_list_t *vl, time_t t, virDomainPtr dom)
656 {
657 int i, n;
658 const char *name;
659 char uuid[VIR_UUID_STRING_BUFLEN];
660 char *host_ptr;
661 size_t host_len;
663 vl->time = t;
664 vl->interval = interval_g;
666 sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
668 vl->host[0] = '\0';
669 host_ptr = vl->host;
670 host_len = sizeof (vl->host);
672 /* Construct the hostname field according to HostnameFormat. */
673 for (i = 0; i < HF_MAX_FIELDS; ++i) {
674 if (hostname_format[i] == hf_none)
675 continue;
677 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
679 if (i > 0 && n >= 1) {
680 strncat (vl->host, ":", 1);
681 n--;
682 }
684 switch (hostname_format[i]) {
685 case hf_none: break;
686 case hf_hostname:
687 strncat (vl->host, hostname_g, n);
688 break;
689 case hf_name:
690 name = virDomainGetName (dom);
691 if (name)
692 strncat (vl->host, name, n);
693 break;
694 case hf_uuid:
695 if (virDomainGetUUIDString (dom, uuid) == 0)
696 strncat (vl->host, uuid, n);
697 break;
698 }
699 }
701 vl->host[sizeof (vl->host) - 1] = '\0';
702 } /* void init_value_list */
704 static void
705 cpu_submit (unsigned long long cpu_time,
706 time_t t,
707 virDomainPtr dom, const char *type)
708 {
709 value_t values[1];
710 value_list_t vl = VALUE_LIST_INIT;
712 init_value_list (&vl, t, dom);
714 values[0].counter = cpu_time;
716 vl.values = values;
717 vl.values_len = 1;
719 sstrncpy (vl.type, type, sizeof (vl.type));
721 plugin_dispatch_values (&vl);
722 }
724 static void
725 vcpu_submit (counter_t cpu_time,
726 time_t t,
727 virDomainPtr dom, int vcpu_nr, const char *type)
728 {
729 value_t values[1];
730 value_list_t vl = VALUE_LIST_INIT;
732 init_value_list (&vl, t, dom);
734 values[0].counter = cpu_time;
735 vl.values = values;
736 vl.values_len = 1;
738 sstrncpy (vl.type, type, sizeof (vl.type));
739 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
741 plugin_dispatch_values (&vl);
742 }
744 static void
745 submit_counter2 (const char *type, counter_t v0, counter_t v1,
746 time_t t,
747 virDomainPtr dom, const char *devname)
748 {
749 value_t values[2];
750 value_list_t vl = VALUE_LIST_INIT;
752 init_value_list (&vl, t, dom);
754 values[0].counter = v0;
755 values[1].counter = v1;
756 vl.values = values;
757 vl.values_len = 2;
759 sstrncpy (vl.type, type, sizeof (vl.type));
760 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
762 plugin_dispatch_values (&vl);
763 } /* void submit_counter2 */
765 static int
766 lv_shutdown (void)
767 {
768 free_block_devices ();
769 free_interface_devices ();
770 free_domains ();
772 if (conn != NULL)
773 virConnectClose (conn);
774 conn = NULL;
776 ignorelist_free (il_domains);
777 il_domains = NULL;
778 ignorelist_free (il_block_devices);
779 il_block_devices = NULL;
780 ignorelist_free (il_interface_devices);
781 il_interface_devices = NULL;
783 return 0;
784 }
786 void
787 module_register (void)
788 {
789 plugin_register_config ("libvirt",
790 lv_config,
791 config_keys, NR_CONFIG_KEYS);
792 plugin_register_init ("libvirt", lv_init);
793 plugin_register_read ("libvirt", lv_read);
794 plugin_register_shutdown ("libvirt", lv_shutdown);
795 }
797 /*
798 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker
799 */