1 /**
2 * collectd - src/virt.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"
24 #include "common.h"
25 #include "plugin.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 /* Plugin name */
36 #define PLUGIN_NAME "virt"
38 static const char *config_keys[] = {
39 "Connection",
41 "RefreshInterval",
43 "Domain",
44 "BlockDevice",
45 "InterfaceDevice",
46 "IgnoreSelected",
48 "HostnameFormat",
49 "InterfaceFormat",
51 "PluginInstanceFormat",
53 NULL
54 };
55 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
57 /* Connection. */
58 static virConnectPtr conn = 0;
59 static char *conn_string = NULL;
60 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
62 /* Seconds between list refreshes, 0 disables completely. */
63 static int interval = 60;
65 /* List of domains, if specified. */
66 static ignorelist_t *il_domains = NULL;
67 /* List of block devices, if specified. */
68 static ignorelist_t *il_block_devices = NULL;
69 /* List of network interface devices, if specified. */
70 static ignorelist_t *il_interface_devices = NULL;
72 static int ignore_device_match (ignorelist_t *,
73 const char *domname, const char *devpath);
75 /* Actual list of domains found on last refresh. */
76 static virDomainPtr *domains = NULL;
77 static int nr_domains = 0;
79 static void free_domains (void);
80 static int add_domain (virDomainPtr dom);
82 /* Actual list of block devices found on last refresh. */
83 struct block_device {
84 virDomainPtr dom; /* domain */
85 char *path; /* name of block device */
86 };
88 static struct block_device *block_devices = NULL;
89 static int nr_block_devices = 0;
91 static void free_block_devices (void);
92 static int add_block_device (virDomainPtr dom, const char *path);
94 /* Actual list of network interfaces found on last refresh. */
95 struct interface_device {
96 virDomainPtr dom; /* domain */
97 char *path; /* name of interface device */
98 char *address; /* mac address of interface device */
99 char *number; /* interface device number */
100 };
102 static struct interface_device *interface_devices = NULL;
103 static int nr_interface_devices = 0;
105 static void free_interface_devices (void);
106 static int add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number);
108 /* HostnameFormat. */
109 #define HF_MAX_FIELDS 3
111 enum hf_field {
112 hf_none = 0,
113 hf_hostname,
114 hf_name,
115 hf_uuid
116 };
118 static enum hf_field hostname_format[HF_MAX_FIELDS] =
119 { hf_name };
121 /* PluginInstanceFormat */
122 #define PLGINST_MAX_FIELDS 2
124 enum plginst_field {
125 plginst_none = 0,
126 plginst_name,
127 plginst_uuid
128 };
130 static enum plginst_field plugin_instance_format[PLGINST_MAX_FIELDS] =
131 { plginst_none };
133 /* InterfaceFormat. */
134 enum if_field {
135 if_address,
136 if_name,
137 if_number
138 };
140 static enum if_field interface_format = if_name;
142 /* Time that we last refreshed. */
143 static time_t last_refresh = (time_t) 0;
145 static int refresh_lists (void);
147 /* ERROR(...) macro for virterrors. */
148 #define VIRT_ERROR(conn,s) do { \
149 virErrorPtr err; \
150 err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
151 if (err) ERROR ("%s: %s", (s), err->message); \
152 } while(0)
154 static void
155 init_value_list (value_list_t *vl, virDomainPtr dom)
156 {
157 int n;
158 const char *name;
159 char uuid[VIR_UUID_STRING_BUFLEN];
161 sstrncpy (vl->plugin, PLUGIN_NAME, sizeof (vl->plugin));
163 vl->host[0] = '\0';
165 /* Construct the hostname field according to HostnameFormat. */
166 for (int i = 0; i < HF_MAX_FIELDS; ++i) {
167 if (hostname_format[i] == hf_none)
168 continue;
170 n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
172 if (i > 0 && n >= 1) {
173 strncat (vl->host, ":", 1);
174 n--;
175 }
177 switch (hostname_format[i]) {
178 case hf_none: break;
179 case hf_hostname:
180 strncat (vl->host, hostname_g, n);
181 break;
182 case hf_name:
183 name = virDomainGetName (dom);
184 if (name)
185 strncat (vl->host, name, n);
186 break;
187 case hf_uuid:
188 if (virDomainGetUUIDString (dom, uuid) == 0)
189 strncat (vl->host, uuid, n);
190 break;
191 }
192 }
194 vl->host[sizeof (vl->host) - 1] = '\0';
196 /* Construct the plugin instance field according to PluginInstanceFormat. */
197 for (int i = 0; i < PLGINST_MAX_FIELDS; ++i) {
198 if (plugin_instance_format[i] == plginst_none)
199 continue;
201 n = sizeof(vl->plugin_instance) - strlen (vl->plugin_instance) - 2;
203 if (i > 0 && n >= 1) {
204 strncat (vl->plugin_instance, ":", 1);
205 n--;
206 }
208 switch (plugin_instance_format[i]) {
209 case plginst_none: break;
210 case plginst_name:
211 name = virDomainGetName (dom);
212 if (name)
213 strncat (vl->plugin_instance, name, n);
214 break;
215 case plginst_uuid:
216 if (virDomainGetUUIDString (dom, uuid) == 0)
217 strncat (vl->plugin_instance, uuid, n);
218 break;
219 }
220 }
222 vl->plugin_instance[sizeof (vl->plugin_instance) - 1] = '\0';
224 } /* void init_value_list */
226 static void submit (virDomainPtr dom,
227 char const *type, char const *type_instance,
228 value_t *values, size_t values_len)
229 {
230 value_list_t vl = VALUE_LIST_INIT;
231 init_value_list (&vl, dom);
233 vl.values = values;
234 vl.values_len = values_len;
236 sstrncpy (vl.type, type, sizeof (vl.type));
237 if (type_instance != NULL)
238 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
240 plugin_dispatch_values (&vl);
241 }
243 static void
244 memory_submit (gauge_t value, virDomainPtr dom)
245 {
246 submit (dom, "memory", "total", &(value_t) { .gauge = value }, 1);
247 }
249 static void
250 memory_stats_submit (gauge_t value, virDomainPtr dom, int tag_index)
251 {
252 static const char *tags[] = { "swap_in", "swap_out", "major_fault", "minor_fault",
253 "unused", "available", "actual_balloon", "rss"};
255 if ((tag_index < 0) || (tag_index >= STATIC_ARRAY_SIZE (tags))) {
256 ERROR ("virt plugin: Array index out of bounds: tag_index = %d", tag_index);
257 return;
258 }
260 submit (dom, "memory", tags[tag_index], &(value_t) { .gauge = value }, 1);
261 }
263 static void
264 cpu_submit (unsigned long long value,
265 virDomainPtr dom, const char *type)
266 {
267 submit (dom, type, NULL, &(value_t) { .derive = (derive_t) value }, 1);
268 }
270 static void
271 vcpu_submit (derive_t value,
272 virDomainPtr dom, int vcpu_nr, const char *type)
273 {
274 char type_instance[DATA_MAX_NAME_LEN];
276 ssnprintf (type_instance, sizeof (type_instance), "%d", vcpu_nr);
278 submit (dom, type, type_instance, &(value_t) { .derive = value }, 1);
279 }
281 static void
282 submit_derive2 (const char *type, derive_t v0, derive_t v1,
283 virDomainPtr dom, const char *devname)
284 {
285 value_t values[2];
286 value_list_t vl = VALUE_LIST_INIT;
288 init_value_list (&vl, dom);
290 values[0].derive = v0;
291 values[1].derive = v1;
292 vl.values = values;
293 vl.values_len = 2;
295 sstrncpy (vl.type, type, sizeof (vl.type));
296 sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
298 plugin_dispatch_values (&vl);
299 } /* void submit_derive2 */
301 static int
302 lv_init (void)
303 {
304 if (virInitialize () != 0)
305 return -1;
306 else
307 return 0;
308 }
310 static int
311 lv_config (const char *key, const char *value)
312 {
313 if (virInitialize () != 0)
314 return 1;
316 if (il_domains == NULL)
317 il_domains = ignorelist_create (1);
318 if (il_block_devices == NULL)
319 il_block_devices = ignorelist_create (1);
320 if (il_interface_devices == NULL)
321 il_interface_devices = ignorelist_create (1);
323 if (strcasecmp (key, "Connection") == 0) {
324 char *tmp = strdup (value);
325 if (tmp == NULL) {
326 ERROR (PLUGIN_NAME " plugin: Connection strdup failed.");
327 return 1;
328 }
329 sfree (conn_string);
330 conn_string = tmp;
331 return 0;
332 }
334 if (strcasecmp (key, "RefreshInterval") == 0) {
335 char *eptr = NULL;
336 interval = strtol (value, &eptr, 10);
337 if (eptr == NULL || *eptr != '\0') return 1;
338 return 0;
339 }
341 if (strcasecmp (key, "Domain") == 0) {
342 if (ignorelist_add (il_domains, value)) return 1;
343 return 0;
344 }
345 if (strcasecmp (key, "BlockDevice") == 0) {
346 if (ignorelist_add (il_block_devices, value)) return 1;
347 return 0;
348 }
349 if (strcasecmp (key, "InterfaceDevice") == 0) {
350 if (ignorelist_add (il_interface_devices, value)) return 1;
351 return 0;
352 }
354 if (strcasecmp (key, "IgnoreSelected") == 0) {
355 if (IS_TRUE (value))
356 {
357 ignorelist_set_invert (il_domains, 0);
358 ignorelist_set_invert (il_block_devices, 0);
359 ignorelist_set_invert (il_interface_devices, 0);
360 }
361 else
362 {
363 ignorelist_set_invert (il_domains, 1);
364 ignorelist_set_invert (il_block_devices, 1);
365 ignorelist_set_invert (il_interface_devices, 1);
366 }
367 return 0;
368 }
370 if (strcasecmp (key, "HostnameFormat") == 0) {
371 char *value_copy;
372 char *fields[HF_MAX_FIELDS];
373 int n;
375 value_copy = strdup (value);
376 if (value_copy == NULL) {
377 ERROR (PLUGIN_NAME " plugin: strdup failed.");
378 return -1;
379 }
381 n = strsplit (value_copy, fields, HF_MAX_FIELDS);
382 if (n < 1) {
383 sfree (value_copy);
384 ERROR (PLUGIN_NAME " plugin: HostnameFormat: no fields");
385 return -1;
386 }
388 for (int i = 0; i < n; ++i) {
389 if (strcasecmp (fields[i], "hostname") == 0)
390 hostname_format[i] = hf_hostname;
391 else if (strcasecmp (fields[i], "name") == 0)
392 hostname_format[i] = hf_name;
393 else if (strcasecmp (fields[i], "uuid") == 0)
394 hostname_format[i] = hf_uuid;
395 else {
396 ERROR (PLUGIN_NAME " plugin: unknown HostnameFormat field: %s", fields[i]);
397 sfree (value_copy);
398 return -1;
399 }
400 }
401 sfree (value_copy);
403 for (int i = n; i < HF_MAX_FIELDS; ++i)
404 hostname_format[i] = hf_none;
406 return 0;
407 }
409 if (strcasecmp (key, "PluginInstanceFormat") == 0) {
410 char *value_copy;
411 char *fields[PLGINST_MAX_FIELDS];
412 int n;
414 value_copy = strdup (value);
415 if (value_copy == NULL) {
416 ERROR (PLUGIN_NAME " plugin: strdup failed.");
417 return -1;
418 }
420 n = strsplit (value_copy, fields, PLGINST_MAX_FIELDS);
421 if (n < 1) {
422 sfree (value_copy);
423 ERROR (PLUGIN_NAME " plugin: PluginInstanceFormat: no fields");
424 return -1;
425 }
427 for (int i = 0; i < n; ++i) {
428 if (strcasecmp (fields[i], "none") == 0) {
429 plugin_instance_format[i] = plginst_none;
430 break;
431 } else if (strcasecmp (fields[i], "name") == 0)
432 plugin_instance_format[i] = plginst_name;
433 else if (strcasecmp (fields[i], "uuid") == 0)
434 plugin_instance_format[i] = plginst_uuid;
435 else {
436 ERROR (PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s", fields[i]);
437 sfree (value_copy);
438 return -1;
439 }
440 }
441 sfree (value_copy);
443 for (int i = n; i < PLGINST_MAX_FIELDS; ++i)
444 plugin_instance_format[i] = plginst_none;
446 return 0;
447 }
449 if (strcasecmp (key, "InterfaceFormat") == 0) {
450 if (strcasecmp (value, "name") == 0)
451 interface_format = if_name;
452 else if (strcasecmp (value, "address") == 0)
453 interface_format = if_address;
454 else if (strcasecmp (value, "number") == 0)
455 interface_format = if_number;
456 else {
457 ERROR (PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", value);
458 return -1;
459 }
460 return 0;
461 }
463 /* Unrecognised option. */
464 return -1;
465 }
467 static int
468 lv_read (void)
469 {
470 time_t t;
472 if (conn == NULL) {
473 /* `conn_string == NULL' is acceptable. */
474 conn = virConnectOpenReadOnly (conn_string);
475 if (conn == NULL) {
476 c_complain (LOG_ERR, &conn_complain,
477 PLUGIN_NAME " plugin: Unable to connect: "
478 "virConnectOpenReadOnly failed.");
479 return -1;
480 }
481 }
482 c_release (LOG_NOTICE, &conn_complain,
483 PLUGIN_NAME " plugin: Connection established.");
485 time (&t);
487 /* Need to refresh domain or device lists? */
488 if ((last_refresh == (time_t) 0) ||
489 ((interval > 0) && ((last_refresh + interval) <= t))) {
490 if (refresh_lists () != 0) {
491 if (conn != NULL)
492 virConnectClose (conn);
493 conn = NULL;
494 return -1;
495 }
496 last_refresh = t;
497 }
499 #if 0
500 for (int i = 0; i < nr_domains; ++i)
501 fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
502 for (int i = 0; i < nr_block_devices; ++i)
503 fprintf (stderr, "block device %d %s:%s\n",
504 i, virDomainGetName (block_devices[i].dom),
505 block_devices[i].path);
506 for (int i = 0; i < nr_interface_devices; ++i)
507 fprintf (stderr, "interface device %d %s:%s\n",
508 i, virDomainGetName (interface_devices[i].dom),
509 interface_devices[i].path);
510 #endif
512 /* Get CPU usage, memory, VCPU usage for each domain. */
513 for (int i = 0; i < nr_domains; ++i) {
514 virDomainInfo info;
515 virVcpuInfoPtr vinfo = NULL;
516 virDomainMemoryStatPtr minfo = NULL;
517 int status;
519 status = virDomainGetInfo (domains[i], &info);
520 if (status != 0)
521 {
522 ERROR (PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
523 status);
524 continue;
525 }
527 if (info.state != VIR_DOMAIN_RUNNING)
528 {
529 /* only gather stats for running domains */
530 continue;
531 }
533 cpu_submit (info.cpuTime, domains[i], "virt_cpu_total");
534 memory_submit ((gauge_t) info.memory * 1024, domains[i]);
536 vinfo = malloc (info.nrVirtCpu * sizeof (vinfo[0]));
537 if (vinfo == NULL) {
538 ERROR (PLUGIN_NAME " plugin: malloc failed.");
539 continue;
540 }
542 status = virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
543 /* cpu map = */ NULL, /* cpu map length = */ 0);
544 if (status < 0)
545 {
546 ERROR (PLUGIN_NAME " plugin: virDomainGetVcpus failed with status %i.",
547 status);
548 sfree (vinfo);
549 continue;
550 }
552 for (int j = 0; j < info.nrVirtCpu; ++j)
553 vcpu_submit (vinfo[j].cpuTime,
554 domains[i], vinfo[j].number, "virt_vcpu");
556 sfree (vinfo);
558 minfo = malloc (VIR_DOMAIN_MEMORY_STAT_NR * sizeof (virDomainMemoryStatStruct));
559 if (minfo == NULL) {
560 ERROR ("virt plugin: malloc failed.");
561 continue;
562 }
564 status = virDomainMemoryStats (domains[i], minfo, VIR_DOMAIN_MEMORY_STAT_NR, 0);
566 if (status < 0) {
567 ERROR ("virt plugin: virDomainMemoryStats failed with status %i.",
568 status);
569 sfree (minfo);
570 continue;
571 }
573 for (int j = 0; j < status; j++) {
574 memory_stats_submit ((gauge_t) minfo[j].val * 1024, domains[i], minfo[j].tag);
575 }
577 sfree (minfo);
578 }
581 /* Get block device stats for each domain. */
582 for (int i = 0; i < nr_block_devices; ++i) {
583 struct _virDomainBlockStats stats;
585 if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
586 &stats, sizeof stats) != 0)
587 continue;
589 if ((stats.rd_req != -1) && (stats.wr_req != -1))
590 submit_derive2 ("disk_ops",
591 (derive_t) stats.rd_req, (derive_t) stats.wr_req,
592 block_devices[i].dom, block_devices[i].path);
594 if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
595 submit_derive2 ("disk_octets",
596 (derive_t) stats.rd_bytes, (derive_t) stats.wr_bytes,
597 block_devices[i].dom, block_devices[i].path);
598 } /* for (nr_block_devices) */
600 /* Get interface stats for each domain. */
601 for (int i = 0; i < nr_interface_devices; ++i) {
602 struct _virDomainInterfaceStats stats;
603 char *display_name = NULL;
606 switch (interface_format) {
607 case if_address:
608 display_name = interface_devices[i].address;
609 break;
610 case if_number:
611 display_name = interface_devices[i].number;
612 break;
613 case if_name:
614 default:
615 display_name = interface_devices[i].path;
616 }
618 if (virDomainInterfaceStats (interface_devices[i].dom,
619 interface_devices[i].path,
620 &stats, sizeof stats) != 0)
621 continue;
623 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
624 submit_derive2 ("if_octets",
625 (derive_t) stats.rx_bytes, (derive_t) stats.tx_bytes,
626 interface_devices[i].dom, display_name);
628 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
629 submit_derive2 ("if_packets",
630 (derive_t) stats.rx_packets, (derive_t) stats.tx_packets,
631 interface_devices[i].dom, display_name);
633 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
634 submit_derive2 ("if_errors",
635 (derive_t) stats.rx_errs, (derive_t) stats.tx_errs,
636 interface_devices[i].dom, display_name);
638 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
639 submit_derive2 ("if_dropped",
640 (derive_t) stats.rx_drop, (derive_t) stats.tx_drop,
641 interface_devices[i].dom, display_name);
642 } /* for (nr_interface_devices) */
644 return 0;
645 }
647 static int
648 refresh_lists (void)
649 {
650 int n;
652 n = virConnectNumOfDomains (conn);
653 if (n < 0) {
654 VIRT_ERROR (conn, "reading number of domains");
655 return -1;
656 }
658 if (n > 0) {
659 int *domids;
661 /* Get list of domains. */
662 domids = malloc (sizeof (*domids) * n);
663 if (domids == NULL) {
664 ERROR (PLUGIN_NAME " plugin: malloc failed.");
665 return -1;
666 }
668 n = virConnectListDomains (conn, domids, n);
669 if (n < 0) {
670 VIRT_ERROR (conn, "reading list of domains");
671 sfree (domids);
672 return -1;
673 }
675 free_block_devices ();
676 free_interface_devices ();
677 free_domains ();
679 /* Fetch each domain and add it to the list, unless ignore. */
680 for (int i = 0; i < n; ++i) {
681 virDomainPtr dom = NULL;
682 const char *name;
683 char *xml = NULL;
684 xmlDocPtr xml_doc = NULL;
685 xmlXPathContextPtr xpath_ctx = NULL;
686 xmlXPathObjectPtr xpath_obj = NULL;
688 dom = virDomainLookupByID (conn, domids[i]);
689 if (dom == NULL) {
690 VIRT_ERROR (conn, "virDomainLookupByID");
691 /* Could be that the domain went away -- ignore it anyway. */
692 continue;
693 }
695 name = virDomainGetName (dom);
696 if (name == NULL) {
697 VIRT_ERROR (conn, "virDomainGetName");
698 goto cont;
699 }
701 if (il_domains && ignorelist_match (il_domains, name) != 0)
702 goto cont;
704 if (add_domain (dom) < 0) {
705 ERROR (PLUGIN_NAME " plugin: malloc failed.");
706 goto cont;
707 }
709 /* Get a list of devices for this domain. */
710 xml = virDomainGetXMLDesc (dom, 0);
711 if (!xml) {
712 VIRT_ERROR (conn, "virDomainGetXMLDesc");
713 goto cont;
714 }
716 /* Yuck, XML. Parse out the devices. */
717 xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
718 if (xml_doc == NULL) {
719 VIRT_ERROR (conn, "xmlReadDoc");
720 goto cont;
721 }
723 xpath_ctx = xmlXPathNewContext (xml_doc);
725 /* Block devices. */
726 xpath_obj = xmlXPathEval
727 ((xmlChar *) "/domain/devices/disk/target[@dev]",
728 xpath_ctx);
729 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
730 xpath_obj->nodesetval == NULL)
731 goto cont;
733 for (int j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
734 xmlNodePtr node;
735 char *path = NULL;
737 node = xpath_obj->nodesetval->nodeTab[j];
738 if (!node) continue;
739 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
740 if (!path) continue;
742 if (il_block_devices &&
743 ignore_device_match (il_block_devices, name, path) != 0)
744 goto cont2;
746 add_block_device (dom, path);
747 cont2:
748 if (path) xmlFree (path);
749 }
750 xmlXPathFreeObject (xpath_obj);
752 /* Network interfaces. */
753 xpath_obj = xmlXPathEval
754 ((xmlChar *) "/domain/devices/interface[target[@dev]]",
755 xpath_ctx);
756 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
757 xpath_obj->nodesetval == NULL)
758 goto cont;
760 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
762 for (int j = 0; j < xml_interfaces->nodeNr; ++j) {
763 char *path = NULL;
764 char *address = NULL;
765 xmlNodePtr xml_interface;
767 xml_interface = xml_interfaces->nodeTab[j];
768 if (!xml_interface) continue;
770 for (xmlNodePtr child = xml_interface->children; child; child = child->next) {
771 if (child->type != XML_ELEMENT_NODE) continue;
773 if (xmlStrEqual(child->name, (const xmlChar *) "target")) {
774 path = (char *) xmlGetProp (child, (const xmlChar *) "dev");
775 if (!path) continue;
776 } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) {
777 address = (char *) xmlGetProp (child, (const xmlChar *) "address");
778 if (!address) continue;
779 }
780 }
782 if (il_interface_devices &&
783 (ignore_device_match (il_interface_devices, name, path) != 0 ||
784 ignore_device_match (il_interface_devices, name, address) != 0))
785 goto cont3;
787 add_interface_device (dom, path, address, j+1);
788 cont3:
789 if (path) xmlFree (path);
790 if (address) xmlFree (address);
791 }
793 cont:
794 if (xpath_obj) xmlXPathFreeObject (xpath_obj);
795 if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
796 if (xml_doc) xmlFreeDoc (xml_doc);
797 sfree (xml);
798 }
800 sfree (domids);
801 }
803 return 0;
804 }
806 static void
807 free_domains (void)
808 {
809 if (domains) {
810 for (int i = 0; i < nr_domains; ++i)
811 virDomainFree (domains[i]);
812 sfree (domains);
813 }
814 domains = NULL;
815 nr_domains = 0;
816 }
818 static int
819 add_domain (virDomainPtr dom)
820 {
821 virDomainPtr *new_ptr;
822 int new_size = sizeof (domains[0]) * (nr_domains+1);
824 if (domains)
825 new_ptr = realloc (domains, new_size);
826 else
827 new_ptr = malloc (new_size);
829 if (new_ptr == NULL)
830 return -1;
832 domains = new_ptr;
833 domains[nr_domains] = dom;
834 return nr_domains++;
835 }
837 static void
838 free_block_devices (void)
839 {
840 if (block_devices) {
841 for (int i = 0; i < nr_block_devices; ++i)
842 sfree (block_devices[i].path);
843 sfree (block_devices);
844 }
845 block_devices = NULL;
846 nr_block_devices = 0;
847 }
849 static int
850 add_block_device (virDomainPtr dom, const char *path)
851 {
852 struct block_device *new_ptr;
853 int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
854 char *path_copy;
856 path_copy = strdup (path);
857 if (!path_copy)
858 return -1;
860 if (block_devices)
861 new_ptr = realloc (block_devices, new_size);
862 else
863 new_ptr = malloc (new_size);
865 if (new_ptr == NULL) {
866 sfree (path_copy);
867 return -1;
868 }
869 block_devices = new_ptr;
870 block_devices[nr_block_devices].dom = dom;
871 block_devices[nr_block_devices].path = path_copy;
872 return nr_block_devices++;
873 }
875 static void
876 free_interface_devices (void)
877 {
878 if (interface_devices) {
879 for (int i = 0; i < nr_interface_devices; ++i) {
880 sfree (interface_devices[i].path);
881 sfree (interface_devices[i].address);
882 sfree (interface_devices[i].number);
883 }
884 sfree (interface_devices);
885 }
886 interface_devices = NULL;
887 nr_interface_devices = 0;
888 }
890 static int
891 add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number)
892 {
893 struct interface_device *new_ptr;
894 int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
895 char *path_copy, *address_copy, number_string[15];
897 if ((path == NULL) || (address == NULL))
898 return EINVAL;
900 path_copy = strdup (path);
901 if (!path_copy) return -1;
903 address_copy = strdup (address);
904 if (!address_copy) {
905 sfree(path_copy);
906 return -1;
907 }
909 snprintf(number_string, sizeof (number_string), "interface-%u", number);
911 if (interface_devices)
912 new_ptr = realloc (interface_devices, new_size);
913 else
914 new_ptr = malloc (new_size);
916 if (new_ptr == NULL) {
917 sfree (path_copy);
918 sfree (address_copy);
919 return -1;
920 }
921 interface_devices = new_ptr;
922 interface_devices[nr_interface_devices].dom = dom;
923 interface_devices[nr_interface_devices].path = path_copy;
924 interface_devices[nr_interface_devices].address = address_copy;
925 interface_devices[nr_interface_devices].number = strdup(number_string);
926 return nr_interface_devices++;
927 }
929 static int
930 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
931 {
932 char *name;
933 int n, r;
935 if ((domname == NULL) || (devpath == NULL))
936 return 0;
938 n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
939 name = malloc (n);
940 if (name == NULL) {
941 ERROR (PLUGIN_NAME " plugin: malloc failed.");
942 return 0;
943 }
944 ssnprintf (name, n, "%s:%s", domname, devpath);
945 r = ignorelist_match (il, name);
946 sfree (name);
947 return r;
948 }
950 static int
951 lv_shutdown (void)
952 {
953 free_block_devices ();
954 free_interface_devices ();
955 free_domains ();
957 if (conn != NULL)
958 virConnectClose (conn);
959 conn = NULL;
961 ignorelist_free (il_domains);
962 il_domains = NULL;
963 ignorelist_free (il_block_devices);
964 il_block_devices = NULL;
965 ignorelist_free (il_interface_devices);
966 il_interface_devices = NULL;
968 return 0;
969 }
971 void
972 module_register (void)
973 {
974 plugin_register_config (PLUGIN_NAME,
975 lv_config,
976 config_keys, NR_CONFIG_KEYS);
977 plugin_register_init (PLUGIN_NAME, lv_init);
978 plugin_register_read (PLUGIN_NAME, lv_read);
979 plugin_register_shutdown (PLUGIN_NAME, lv_shutdown);
980 }
982 /*
983 * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker
984 */