6ba08fbca5ebc28249c5246e7a9030b49b94b242
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"
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[] = {
35 "Connection",
37 "RefreshInterval",
39 "Domain",
40 "BlockDevice",
41 "InterfaceDevice",
42 "IgnoreSelected",
44 "HostnameFormat",
46 NULL
47 };
48 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
50 /* Connection. */
51 static virConnectPtr conn = 0;
52 static char *conn_string = NULL;
53 static int conn_count = 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 submit_counter2 (const char *type, counter_t v0, counter_t v1,
125 time_t t,
126 virDomainPtr dom, const char *devname);
128 /* ERROR(...) macro for virterrors. */
129 #define VIRT_ERROR(conn,s) do { \
130 virErrorPtr err; \
131 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
132 if (err) ERROR ("%s: %s", (s), err->message); \
133 } while(0)
135 static int
136 lv_init (void)
137 {
138 if (virInitialize () != 0)
139 return -1;
141 return 0;
142 }
144 static int
145 lv_config (const char *key, const char *value)
146 {
147 if (virInitialize () != 0)
148 return 1;
150 if (il_domains == NULL)
151 il_domains = ignorelist_create (1);
152 if (il_block_devices == NULL)
153 il_block_devices = ignorelist_create (1);
154 if (il_interface_devices == NULL)
155 il_interface_devices = ignorelist_create (1);
157 if (strcasecmp (key, "Connection") == 0) {
158 if (conn_count++ != 0) {
159 ERROR ("Connection may only be given once in config file");
160 return 1;
161 }
162 conn_string = strdup(value);
163 if (conn_string == NULL) {
164 ERROR ("libvirt plugin: Connection strdup failed.");
165 return -1;
166 }
167 return 0;
168 }
170 if (strcasecmp (key, "RefreshInterval") == 0) {
171 char *eptr = NULL;
172 interval = strtol (value, &eptr, 10);
173 if (eptr == NULL || *eptr != '\0') return 1;
174 return 0;
175 }
177 if (strcasecmp (key, "Domain") == 0) {
178 if (ignorelist_add (il_domains, value)) return 1;
179 return 0;
180 }
181 if (strcasecmp (key, "BlockDevice") == 0) {
182 if (ignorelist_add (il_block_devices, value)) return 1;
183 return 0;
184 }
185 if (strcasecmp (key, "InterfaceDevice") == 0) {
186 if (ignorelist_add (il_interface_devices, value)) return 1;
187 return 0;
188 }
190 if (strcasecmp (key, "IgnoreSelected") == 0) {
191 if (strcasecmp (value, "True") == 0 ||
192 strcasecmp (value, "Yes") == 0 ||
193 strcasecmp (value, "On") == 0)
194 {
195 ignorelist_set_invert (il_domains, 0);
196 ignorelist_set_invert (il_block_devices, 0);
197 ignorelist_set_invert (il_interface_devices, 0);
198 }
199 else
200 {
201 ignorelist_set_invert (il_domains, 1);
202 ignorelist_set_invert (il_block_devices, 1);
203 ignorelist_set_invert (il_interface_devices, 1);
204 }
205 return 0;
206 }
208 if (strcasecmp (key, "HostnameFormat") == 0) {
209 char *value_copy;
210 char *fields[HF_MAX_FIELDS];
211 int i, n;
213 value_copy = strdup (value);
214 if (value_copy == NULL) {
215 ERROR ("libvirt plugin: strdup failed.");
216 return -1;
217 }
219 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
220 if (n < 1) {
221 free (value_copy);
222 ERROR ("HostnameFormat: no fields");
223 return -1;
224 }
226 for (i = 0; i < n; ++i) {
227 if (strcasecmp (fields[i], "hostname") == 0)
228 hostname_format[i] = hf_hostname;
229 else if (strcasecmp (fields[i], "name") == 0)
230 hostname_format[i] = hf_name;
231 else if (strcasecmp (fields[i], "uuid") == 0)
232 hostname_format[i] = hf_uuid;
233 else {
234 free (value_copy);
235 ERROR ("unknown HostnameFormat field: %s", fields[i]);
236 return -1;
237 }
238 }
239 free (value_copy);
241 for (i = n; i < HF_MAX_FIELDS; ++i)
242 hostname_format[i] = hf_none;
244 return 0;
245 }
247 /* Unrecognised option. */
248 return -1;
249 }
251 static int
252 lv_read (void)
253 {
254 time_t t;
255 int i;
257 if (conn == NULL) {
258 conn = virConnectOpenReadOnly (conn_string);
259 if (conn == NULL) {
260 ERROR ("libvirt plugin: Not connected.");
261 return -1;
262 }
263 }
265 time (&t);
267 /* Need to refresh domain or device lists? */
268 if ((last_refresh == (time_t) 0) ||
269 ((interval > 0) && ((last_refresh + interval) <= t))) {
270 if (refresh_lists () != 0) {
271 if (conn != NULL)
272 virConnectClose (conn);
273 conn = NULL;
274 return -1;
275 }
276 last_refresh = t;
277 }
279 #if 0
280 for (i = 0; i < nr_domains; ++i)
281 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
282 for (i = 0; i < nr_block_devices; ++i)
283 fprintf (stderr, "block device %d %s:%s\n",
284 i, virDomainGetName (block_devices[i].dom),
285 block_devices[i].path);
286 for (i = 0; i < nr_interface_devices; ++i)
287 fprintf (stderr, "interface device %d %s:%s\n",
288 i, virDomainGetName (interface_devices[i].dom),
289 interface_devices[i].path);
290 #endif
292 /* Get CPU usage, VCPU usage for each domain. */
293 for (i = 0; i < nr_domains; ++i) {
294 virDomainInfo info;
295 virVcpuInfoPtr vinfo = NULL;
296 int j;
298 if (virDomainGetInfo (domains[i], &info) != 0)
299 continue;
301 cpu_submit (info.cpuTime, t, domains[i], "virt_cpu_total");
303 vinfo = malloc (info.nrVirtCpu * sizeof vinfo[0]);
304 if (vinfo == NULL) {
305 ERROR ("libvirt plugin: malloc failed.");
306 continue;
307 }
309 if (virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
310 NULL, 0) != 0) {
311 free (vinfo);
312 continue;
313 }
315 for (j = 0; j < info.nrVirtCpu; ++j)
316 vcpu_submit (vinfo[j].cpuTime,
317 t, domains[i], vinfo[j].number, "virt_vcpu");
319 free (vinfo);
320 }
322 /* Get block device stats for each domain. */
323 for (i = 0; i < nr_block_devices; ++i) {
324 struct _virDomainBlockStats stats;
326 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
327 &stats, sizeof stats) != 0)
328 continue;
330 if ((stats.rd_req != -1) && (stats.wr_req != -1))
331 submit_counter2 ("disk_ops",
332 (counter_t) stats.rd_req, (counter_t) stats.wr_req,
333 t, block_devices[i].dom, block_devices[i].path);
335 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
336 submit_counter2 ("disk_octets",
337 (counter_t) stats.rd_bytes, (counter_t) stats.wr_bytes,
338 t, block_devices[i].dom, block_devices[i].path);
339 } /* for (nr_block_devices) */
341 /* Get interface stats for each domain. */
342 for (i = 0; i < nr_interface_devices; ++i) {
343 struct _virDomainInterfaceStats stats;
345 if (virDomainInterfaceStats (interface_devices[i].dom,
346 interface_devices[i].path,
347 &stats, sizeof stats) != 0)
348 continue;
350 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
351 submit_counter2 ("if_octets",
352 (counter_t) stats.rx_bytes, (counter_t) stats.tx_bytes,
353 t, interface_devices[i].dom, interface_devices[i].path);
355 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
356 submit_counter2 ("if_packets",
357 (counter_t) stats.rx_packets, (counter_t) stats.tx_packets,
358 t, interface_devices[i].dom, interface_devices[i].path);
360 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
361 submit_counter2 ("if_errors",
362 (counter_t) stats.rx_errs, (counter_t) stats.tx_errs,
363 t, interface_devices[i].dom, interface_devices[i].path);
365 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
366 submit_counter2 ("if_dropped",
367 (counter_t) stats.rx_drop, (counter_t) stats.tx_drop,
368 t, interface_devices[i].dom, interface_devices[i].path);
369 } /* for (nr_interface_devices) */
371 return 0;
372 }
374 static int
375 refresh_lists (void)
376 {
377 int n;
379 n = virConnectNumOfDomains (conn);
380 if (n < 0) {
381 VIRT_ERROR (conn, "reading number of domains");
382 return -1;
383 }
385 if (n > 0) {
386 int i;
387 int *domids;
389 /* Get list of domains. */
390 domids = malloc (sizeof (int) * n);
391 if (domids == 0) {
392 ERROR ("libvirt plugin: malloc failed.");
393 return -1;
394 }
396 n = virConnectListDomains (conn, domids, n);
397 if (n < 0) {
398 VIRT_ERROR (conn, "reading list of domains");
399 free (domids);
400 return -1;
401 }
403 free_block_devices ();
404 free_interface_devices ();
405 free_domains ();
407 /* Fetch each domain and add it to the list, unless ignore. */
408 for (i = 0; i < n; ++i) {
409 virDomainPtr dom = NULL;
410 const char *name;
411 char *xml = NULL;
412 xmlDocPtr xml_doc = NULL;
413 xmlXPathContextPtr xpath_ctx = NULL;
414 xmlXPathObjectPtr xpath_obj = NULL;
415 int j;
417 dom = virDomainLookupByID (conn, domids[i]);
418 if (dom == NULL) {
419 VIRT_ERROR (conn, "virDomainLookupByID");
420 /* Could be that the domain went away -- ignore it anyway. */
421 continue;
422 }
424 name = virDomainGetName (dom);
425 if (name == NULL) {
426 VIRT_ERROR (conn, "virDomainGetName");
427 goto cont;
428 }
430 if (il_domains && ignorelist_match (il_domains, name) != 0)
431 goto cont;
433 if (add_domain (dom) < 0) {
434 ERROR ("libvirt plugin: malloc failed.");
435 goto cont;
436 }
438 /* Get a list of devices for this domain. */
439 xml = virDomainGetXMLDesc (dom, 0);
440 if (!xml) {
441 VIRT_ERROR (conn, "virDomainGetXMLDesc");
442 goto cont;
443 }
445 /* Yuck, XML. Parse out the devices. */
446 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
447 if (xml_doc == NULL) {
448 VIRT_ERROR (conn, "xmlReadDoc");
449 goto cont;
450 }
452 xpath_ctx = xmlXPathNewContext (xml_doc);
454 /* Block devices. */
455 xpath_obj = xmlXPathEval
456 ((xmlChar *) "/domain/devices/disk/target[@dev]",
457 xpath_ctx);
458 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
459 xpath_obj->nodesetval == NULL)
460 goto cont;
462 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
463 xmlNodePtr node;
464 char *path = NULL;
466 node = xpath_obj->nodesetval->nodeTab[j];
467 if (!node) continue;
468 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
469 if (!path) continue;
471 if (il_block_devices &&
472 ignore_device_match (il_block_devices, name, path) != 0)
473 goto cont2;
475 add_block_device (dom, path);
476 cont2:
477 if (path) xmlFree (path);
478 }
479 xmlXPathFreeObject (xpath_obj);
481 /* Network interfaces. */
482 xpath_obj = xmlXPathEval
483 ((xmlChar *) "/domain/devices/interface/target[@dev]",
484 xpath_ctx);
485 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
486 xpath_obj->nodesetval == NULL)
487 goto cont;
489 for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
490 xmlNodePtr node;
491 char *path = NULL;
493 node = xpath_obj->nodesetval->nodeTab[j];
494 if (!node) continue;
495 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
496 if (!path) continue;
498 if (il_interface_devices &&
499 ignore_device_match (il_interface_devices, name, path) != 0)
500 goto cont3;
502 add_interface_device (dom, path);
503 cont3:
504 if (path) xmlFree (path);
505 }
507 cont:
508 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
509 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
510 if (xml_doc) xmlFreeDoc (xml_doc);
511 if (xml) free (xml);
512 }
514 free (domids);
515 }
517 return 0;
518 }
520 static void
521 free_domains ()
522 {
523 int i;
525 if (domains) {
526 for (i = 0; i < nr_domains; ++i)
527 virDomainFree (domains[i]);
528 free (domains);
529 }
530 domains = NULL;
531 nr_domains = 0;
532 }
534 static int
535 add_domain (virDomainPtr dom)
536 {
537 virDomainPtr *new_ptr;
538 int new_size = sizeof (domains[0]) * (nr_domains+1);
540 if (domains)
541 new_ptr = realloc (domains, new_size);
542 else
543 new_ptr = malloc (new_size);
545 if (new_ptr == NULL)
546 return -1;
548 domains = new_ptr;
549 domains[nr_domains] = dom;
550 return nr_domains++;
551 }
553 static void
554 free_block_devices ()
555 {
556 int i;
558 if (block_devices) {
559 for (i = 0; i < nr_block_devices; ++i)
560 free (block_devices[i].path);
561 free (block_devices);
562 }
563 block_devices = NULL;
564 nr_block_devices = 0;
565 }
567 static int
568 add_block_device (virDomainPtr dom, const char *path)
569 {
570 struct block_device *new_ptr;
571 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
572 char *path_copy;
574 path_copy = strdup (path);
575 if (!path_copy)
576 return -1;
578 if (block_devices)
579 new_ptr = realloc (block_devices, new_size);
580 else
581 new_ptr = malloc (new_size);
583 if (new_ptr == NULL) {
584 free (path_copy);
585 return -1;
586 }
587 block_devices = new_ptr;
588 block_devices[nr_block_devices].dom = dom;
589 block_devices[nr_block_devices].path = path_copy;
590 return nr_block_devices++;
591 }
593 static void
594 free_interface_devices ()
595 {
596 int i;
598 if (interface_devices) {
599 for (i = 0; i < nr_interface_devices; ++i)
600 free (interface_devices[i].path);
601 free (interface_devices);
602 }
603 interface_devices = NULL;
604 nr_interface_devices = 0;
605 }
607 static int
608 add_interface_device (virDomainPtr dom, const char *path)
609 {
610 struct interface_device *new_ptr;
611 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
612 char *path_copy;
614 path_copy = strdup (path);
615 if (!path_copy) return -1;
617 if (interface_devices)
618 new_ptr = realloc (interface_devices, new_size);
619 else
620 new_ptr = malloc (new_size);
622 if (new_ptr == NULL) {
623 free (path_copy);
624 return -1;
625 }
626 interface_devices = new_ptr;
627 interface_devices[nr_interface_devices].dom = dom;
628 interface_devices[nr_interface_devices].path = path_copy;
629 return nr_interface_devices++;
630 }
632 static int
633 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
634 {
635 char *name;
636 int n, r;
638 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
639 name = malloc (n);
640 if (name == NULL) {
641 ERROR ("libvirt plugin: malloc failed.");
642 return 0;
643 }
644 ssnprintf (name, n, "%s:%s", domname, devpath);
645 r = ignorelist_match (il, name);
646 free (name);
647 return r;
648 }
650 static void
651 init_value_list (value_list_t *vl, time_t t, virDomainPtr dom)
652 {
653 int i, n;
654 const char *name;
655 char uuid[VIR_UUID_STRING_BUFLEN];
656 char *host_ptr;
657 size_t host_len;
659 vl->time = t;
660 vl->interval = interval_g;
662 sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
664 vl->host[0] = '\0';
665 host_ptr = vl->host;
666 host_len = sizeof (vl->host);
668 /* Construct the hostname field according to HostnameFormat. */
669 for (i = 0; i < HF_MAX_FIELDS; ++i) {
670 if (hostname_format[i] == hf_none)
671 continue;
673 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
675 if (i > 0 && n >= 1) {
676 strncat (vl->host, ":", 1);
677 n--;
678 }
680 switch (hostname_format[i]) {
681 case hf_none: break;
682 case hf_hostname:
683 strncat (vl->host, hostname_g, n);
684 break;
685 case hf_name:
686 name = virDomainGetName (dom);
687 if (name)
688 strncat (vl->host, name, n);
689 break;
690 case hf_uuid:
691 if (virDomainGetUUIDString (dom, uuid) == 0)
692 strncat (vl->host, uuid, n);
693 break;
694 }
695 }
697 vl->host[sizeof (vl->host) - 1] = '\0';
698 } /* void init_value_list */
700 static void
701 cpu_submit (unsigned long long cpu_time,
702 time_t t,
703 virDomainPtr dom, const char *type)
704 {
705 value_t values[1];
706 value_list_t vl = VALUE_LIST_INIT;
708 init_value_list (&vl, t, dom);
710 values[0].counter = cpu_time;
712 vl.values = values;
713 vl.values_len = 1;
715 sstrncpy (vl.type, type, sizeof (vl.type));
717 plugin_dispatch_values (&vl);
718 }
720 static void
721 vcpu_submit (counter_t cpu_time,
722 time_t t,
723 virDomainPtr dom, int vcpu_nr, const char *type)
724 {
725 value_t values[1];
726 value_list_t vl = VALUE_LIST_INIT;
728 init_value_list (&vl, t, dom);
730 values[0].counter = cpu_time;
731 vl.values = values;
732 vl.values_len = 1;
734 sstrncpy (vl.type, type, sizeof (vl.type));
735 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
737 plugin_dispatch_values (&vl);
738 }
740 static void
741 submit_counter2 (const char *type, counter_t v0, counter_t v1,
742 time_t t,
743 virDomainPtr dom, const char *devname)
744 {
745 value_t values[2];
746 value_list_t vl = VALUE_LIST_INIT;
748 init_value_list (&vl, t, dom);
750 values[0].counter = v0;
751 values[1].counter = v1;
752 vl.values = values;
753 vl.values_len = 2;
755 sstrncpy (vl.type, type, sizeof (vl.type));
756 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
758 plugin_dispatch_values (&vl);
759 } /* void submit_counter2 */
761 static int
762 lv_shutdown (void)
763 {
764 free_block_devices ();
765 free_interface_devices ();
766 free_domains ();
768 if (conn != NULL)
769 virConnectClose (conn);
770 conn = NULL;
772 ignorelist_free (il_domains);
773 il_domains = NULL;
774 ignorelist_free (il_block_devices);
775 il_block_devices = NULL;
776 ignorelist_free (il_interface_devices);
777 il_interface_devices = NULL;
779 return 0;
780 }
782 void
783 module_register (void)
784 {
785 plugin_register_config ("libvirt",
786 lv_config,
787 config_keys, NR_CONFIG_KEYS);
788 plugin_register_init ("libvirt", lv_init);
789 plugin_register_read ("libvirt", lv_read);
790 plugin_register_shutdown ("libvirt", lv_shutdown);
791 }
793 /*
794 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker
795 */