Code

Merge remote-tracking branch 'github/pr/387'
[collectd.git] / src / libvirt.c
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",
46     "InterfaceFormat",
48     NULL
49 };
50 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
52 /* Connection. */
53 static virConnectPtr conn = 0;
54 static char *conn_string = NULL;
55 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
57 /* Seconds between list refreshes, 0 disables completely. */
58 static int interval = 60;
60 /* List of domains, if specified. */
61 static ignorelist_t *il_domains = NULL;
62 /* List of block devices, if specified. */
63 static ignorelist_t *il_block_devices = NULL;
64 /* List of network interface devices, if specified. */
65 static ignorelist_t *il_interface_devices = NULL;
67 static int ignore_device_match (ignorelist_t *,
68                                 const char *domname, const char *devpath);
70 /* Actual list of domains found on last refresh. */
71 static virDomainPtr *domains = NULL;
72 static int nr_domains = 0;
74 static void free_domains (void);
75 static int add_domain (virDomainPtr dom);
77 /* Actual list of block devices found on last refresh. */
78 struct block_device {
79     virDomainPtr dom;           /* domain */
80     char *path;                 /* name of block device */
81 };
83 static struct block_device *block_devices = NULL;
84 static int nr_block_devices = 0;
86 static void free_block_devices (void);
87 static int add_block_device (virDomainPtr dom, const char *path);
89 /* Actual list of network interfaces found on last refresh. */
90 struct interface_device {
91     virDomainPtr dom;           /* domain */
92     char *path;                 /* name of interface device */
93     char *address;              /* mac address of interface device */
94     char *number;               /* interface device number */
95 };
97 static struct interface_device *interface_devices = NULL;
98 static int nr_interface_devices = 0;
100 static void free_interface_devices (void);
101 static int add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number);
103 /* HostnameFormat. */
104 #define HF_MAX_FIELDS 3
106 enum hf_field {
107     hf_none = 0,
108     hf_hostname,
109     hf_name,
110     hf_uuid
111 };
113 static enum hf_field hostname_format[HF_MAX_FIELDS] =
114     { hf_name };
116 /* InterfaceFormat. */
117 enum if_field {
118     if_address,
119     if_name,
120     if_number
121 };
123 static enum if_field interface_format = if_name;
125 /* Time that we last refreshed. */
126 static time_t last_refresh = (time_t) 0;
128 static int refresh_lists (void);
130 /* ERROR(...) macro for virterrors. */
131 #define VIRT_ERROR(conn,s) do {                 \
132         virErrorPtr err;                        \
133         err = (conn) ? virConnGetLastError ((conn)) : virGetLastError (); \
134         if (err) ERROR ("%s: %s", (s), err->message);                   \
135     } while(0)
137 static void
138 init_value_list (value_list_t *vl, virDomainPtr dom)
140     int i, n;
141     const char *name;
142     char uuid[VIR_UUID_STRING_BUFLEN];
144     sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
146     vl->host[0] = '\0';
148     /* Construct the hostname field according to HostnameFormat. */
149     for (i = 0; i < HF_MAX_FIELDS; ++i) {
150         if (hostname_format[i] == hf_none)
151             continue;
153         n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
155         if (i > 0 && n >= 1) {
156             strncat (vl->host, ":", 1);
157             n--;
158         }
160         switch (hostname_format[i]) {
161         case hf_none: break;
162         case hf_hostname:
163             strncat (vl->host, hostname_g, n);
164             break;
165         case hf_name:
166             name = virDomainGetName (dom);
167             if (name)
168                 strncat (vl->host, name, n);
169             break;
170         case hf_uuid:
171             if (virDomainGetUUIDString (dom, uuid) == 0)
172                 strncat (vl->host, uuid, n);
173             break;
174         }
175     }
177     vl->host[sizeof (vl->host) - 1] = '\0';
178 } /* void init_value_list */
180 static void
181 memory_submit (gauge_t memory, virDomainPtr dom)
183     value_t values[1];
184     value_list_t vl = VALUE_LIST_INIT;
186     init_value_list (&vl, dom);
188     values[0].gauge = memory;
190     vl.values = values;
191     vl.values_len = 1;
193     sstrncpy (vl.type, "memory", sizeof (vl.type));
194     sstrncpy (vl.type_instance, "total", sizeof (vl.type_instance));
196     plugin_dispatch_values (&vl);
199 static void
200 cpu_submit (unsigned long long cpu_time,
201             virDomainPtr dom, const char *type)
203     value_t values[1];
204     value_list_t vl = VALUE_LIST_INIT;
206     init_value_list (&vl, dom);
208     values[0].derive = cpu_time;
210     vl.values = values;
211     vl.values_len = 1;
213     sstrncpy (vl.type, type, sizeof (vl.type));
215     plugin_dispatch_values (&vl);
218 static void
219 vcpu_submit (derive_t cpu_time,
220              virDomainPtr dom, int vcpu_nr, const char *type)
222     value_t values[1];
223     value_list_t vl = VALUE_LIST_INIT;
225     init_value_list (&vl, dom);
227     values[0].derive = cpu_time;
228     vl.values = values;
229     vl.values_len = 1;
231     sstrncpy (vl.type, type, sizeof (vl.type));
232     ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
234     plugin_dispatch_values (&vl);
237 static void
238 submit_derive2 (const char *type, derive_t v0, derive_t v1,
239              virDomainPtr dom, const char *devname)
241     value_t values[2];
242     value_list_t vl = VALUE_LIST_INIT;
244     init_value_list (&vl, dom);
246     values[0].derive = v0;
247     values[1].derive = v1;
248     vl.values = values;
249     vl.values_len = 2;
251     sstrncpy (vl.type, type, sizeof (vl.type));
252     sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
254     plugin_dispatch_values (&vl);
255 } /* void submit_derive2 */
257 static int
258 lv_init (void)
260     if (virInitialize () != 0)
261         return -1;
263         return 0;
266 static int
267 lv_config (const char *key, const char *value)
269     if (virInitialize () != 0)
270         return 1;
272     if (il_domains == NULL)
273         il_domains = ignorelist_create (1);
274     if (il_block_devices == NULL)
275         il_block_devices = ignorelist_create (1);
276     if (il_interface_devices == NULL)
277         il_interface_devices = ignorelist_create (1);
279     if (strcasecmp (key, "Connection") == 0) {
280         char *tmp = strdup (value);
281         if (tmp == NULL) {
282             ERROR ("libvirt plugin: Connection strdup failed.");
283             return 1;
284         }
285         sfree (conn_string);
286         conn_string = tmp;
287         return 0;
288     }
290     if (strcasecmp (key, "RefreshInterval") == 0) {
291         char *eptr = NULL;
292         interval = strtol (value, &eptr, 10);
293         if (eptr == NULL || *eptr != '\0') return 1;
294         return 0;
295     }
297     if (strcasecmp (key, "Domain") == 0) {
298         if (ignorelist_add (il_domains, value)) return 1;
299         return 0;
300     }
301     if (strcasecmp (key, "BlockDevice") == 0) {
302         if (ignorelist_add (il_block_devices, value)) return 1;
303         return 0;
304     }
305     if (strcasecmp (key, "InterfaceDevice") == 0) {
306         if (ignorelist_add (il_interface_devices, value)) return 1;
307         return 0;
308     }
310     if (strcasecmp (key, "IgnoreSelected") == 0) {
311         if (IS_TRUE (value))
312         {
313             ignorelist_set_invert (il_domains, 0);
314             ignorelist_set_invert (il_block_devices, 0);
315             ignorelist_set_invert (il_interface_devices, 0);
316         }
317         else
318         {
319             ignorelist_set_invert (il_domains, 1);
320             ignorelist_set_invert (il_block_devices, 1);
321             ignorelist_set_invert (il_interface_devices, 1);
322         }
323         return 0;
324     }
326     if (strcasecmp (key, "HostnameFormat") == 0) {
327         char *value_copy;
328         char *fields[HF_MAX_FIELDS];
329         int i, n;
331         value_copy = strdup (value);
332         if (value_copy == NULL) {
333             ERROR ("libvirt plugin: strdup failed.");
334             return -1;
335         }
337         n = strsplit (value_copy, fields, HF_MAX_FIELDS);
338         if (n < 1) {
339             sfree (value_copy);
340             ERROR ("HostnameFormat: no fields");
341             return -1;
342         }
344         for (i = 0; i < n; ++i) {
345             if (strcasecmp (fields[i], "hostname") == 0)
346                 hostname_format[i] = hf_hostname;
347             else if (strcasecmp (fields[i], "name") == 0)
348                 hostname_format[i] = hf_name;
349             else if (strcasecmp (fields[i], "uuid") == 0)
350                 hostname_format[i] = hf_uuid;
351             else {
352                 sfree (value_copy);
353                 ERROR ("unknown HostnameFormat field: %s", fields[i]);
354                 return -1;
355             }
356         }
357         sfree (value_copy);
359         for (i = n; i < HF_MAX_FIELDS; ++i)
360             hostname_format[i] = hf_none;
362         return 0;
363     }
365     if (strcasecmp (key, "InterfaceFormat") == 0) {
366         if (strcasecmp (value, "name") == 0)
367             interface_format = if_name;
368         else if (strcasecmp (value, "address") == 0)
369             interface_format = if_address;
370         else if (strcasecmp (value, "number") == 0)
371             interface_format = if_number;
372         else {
373             ERROR ("unknown InterfaceFormat: %s", value);
374             return -1;
375         }
376         return 0;
377     }
379     /* Unrecognised option. */
380     return -1;
383 static int
384 lv_read (void)
386     time_t t;
387     int i;
389     if (conn == NULL) {
390         /* `conn_string == NULL' is acceptable. */
391         conn = virConnectOpenReadOnly (conn_string);
392         if (conn == NULL) {
393             c_complain (LOG_ERR, &conn_complain,
394                     "libvirt plugin: Unable to connect: "
395                     "virConnectOpenReadOnly failed.");
396             return -1;
397         }
398     }
399     c_release (LOG_NOTICE, &conn_complain,
400             "libvirt plugin: Connection established.");
402     time (&t);
404     /* Need to refresh domain or device lists? */
405     if ((last_refresh == (time_t) 0) ||
406             ((interval > 0) && ((last_refresh + interval) <= t))) {
407         if (refresh_lists () != 0) {
408             if (conn != NULL)
409                 virConnectClose (conn);
410             conn = NULL;
411             return -1;
412         }
413         last_refresh = t;
414     }
416 #if 0
417     for (i = 0; i < nr_domains; ++i)
418         fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
419     for (i = 0; i < nr_block_devices; ++i)
420         fprintf  (stderr, "block device %d %s:%s\n",
421                   i, virDomainGetName (block_devices[i].dom),
422                   block_devices[i].path);
423     for (i = 0; i < nr_interface_devices; ++i)
424         fprintf (stderr, "interface device %d %s:%s\n",
425                  i, virDomainGetName (interface_devices[i].dom),
426                  interface_devices[i].path);
427 #endif
429     /* Get CPU usage, memory, VCPU usage for each domain. */
430     for (i = 0; i < nr_domains; ++i) {
431         virDomainInfo info;
432         virVcpuInfoPtr vinfo = NULL;
433         int status;
434         int j;
436         status = virDomainGetInfo (domains[i], &info);
437         if (status != 0)
438         {
439             ERROR ("libvirt plugin: virDomainGetInfo failed with status %i.",
440                     status);
441             continue;
442         }
444         cpu_submit (info.cpuTime, domains[i], "virt_cpu_total");
445         memory_submit ((gauge_t) info.memory * 1024, domains[i]);
447         vinfo = malloc (info.nrVirtCpu * sizeof (vinfo[0]));
448         if (vinfo == NULL) {
449             ERROR ("libvirt plugin: malloc failed.");
450             continue;
451         }
453         status = virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
454                 /* cpu map = */ NULL, /* cpu map length = */ 0);
455         if (status < 0)
456         {
457             ERROR ("libvirt plugin: virDomainGetVcpus failed with status %i.",
458                     status);
459             free (vinfo);
460             continue;
461         }
463         for (j = 0; j < info.nrVirtCpu; ++j)
464             vcpu_submit (vinfo[j].cpuTime,
465                     domains[i], vinfo[j].number, "virt_vcpu");
467         sfree (vinfo);
468     }
470     /* Get block device stats for each domain. */
471     for (i = 0; i < nr_block_devices; ++i) {
472         struct _virDomainBlockStats stats;
474         if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
475                     &stats, sizeof stats) != 0)
476             continue;
478         if ((stats.rd_req != -1) && (stats.wr_req != -1))
479             submit_derive2 ("disk_ops",
480                     (derive_t) stats.rd_req, (derive_t) stats.wr_req,
481                     block_devices[i].dom, block_devices[i].path);
483         if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
484             submit_derive2 ("disk_octets",
485                     (derive_t) stats.rd_bytes, (derive_t) stats.wr_bytes,
486                     block_devices[i].dom, block_devices[i].path);
487     } /* for (nr_block_devices) */
489     /* Get interface stats for each domain. */
490     for (i = 0; i < nr_interface_devices; ++i) {
491         struct _virDomainInterfaceStats stats;
492         char *display_name = NULL;
495         switch (interface_format) {
496             case if_address:
497                 display_name = interface_devices[i].address;
498                 break;
499             case if_number:
500                 display_name = interface_devices[i].number;
501                 break;
502             case if_name:
503             default:
504                 display_name = interface_devices[i].path;
505         }
507         if (virDomainInterfaceStats (interface_devices[i].dom,
508                     interface_devices[i].path,
509                     &stats, sizeof stats) != 0)
510             continue;
512         if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
513             submit_derive2 ("if_octets",
514                     (derive_t) stats.rx_bytes, (derive_t) stats.tx_bytes,
515                     interface_devices[i].dom, display_name);
517         if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
518             submit_derive2 ("if_packets",
519                     (derive_t) stats.rx_packets, (derive_t) stats.tx_packets,
520                     interface_devices[i].dom, display_name);
522         if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
523             submit_derive2 ("if_errors",
524                     (derive_t) stats.rx_errs, (derive_t) stats.tx_errs,
525                     interface_devices[i].dom, display_name);
527         if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
528             submit_derive2 ("if_dropped",
529                     (derive_t) stats.rx_drop, (derive_t) stats.tx_drop,
530                     interface_devices[i].dom, display_name);
531     } /* for (nr_interface_devices) */
533     return 0;
536 static int
537 refresh_lists (void)
539     int n;
541     n = virConnectNumOfDomains (conn);
542     if (n < 0) {
543         VIRT_ERROR (conn, "reading number of domains");
544         return -1;
545     }
547     if (n > 0) {
548         int i;
549         int *domids;
551         /* Get list of domains. */
552         domids = malloc (sizeof (int) * n);
553         if (domids == 0) {
554             ERROR ("libvirt plugin: malloc failed.");
555             return -1;
556         }
558         n = virConnectListDomains (conn, domids, n);
559         if (n < 0) {
560             VIRT_ERROR (conn, "reading list of domains");
561             sfree (domids);
562             return -1;
563         }
565         free_block_devices ();
566         free_interface_devices ();
567         free_domains ();
569         /* Fetch each domain and add it to the list, unless ignore. */
570         for (i = 0; i < n; ++i) {
571             virDomainPtr dom = NULL;
572             const char *name;
573             char *xml = NULL;
574             xmlDocPtr xml_doc = NULL;
575             xmlXPathContextPtr xpath_ctx = NULL;
576             xmlXPathObjectPtr xpath_obj = NULL;
577             int j;
579             dom = virDomainLookupByID (conn, domids[i]);
580             if (dom == NULL) {
581                 VIRT_ERROR (conn, "virDomainLookupByID");
582                 /* Could be that the domain went away -- ignore it anyway. */
583                 continue;
584             }
586             name = virDomainGetName (dom);
587             if (name == NULL) {
588                 VIRT_ERROR (conn, "virDomainGetName");
589                 goto cont;
590             }
592             if (il_domains && ignorelist_match (il_domains, name) != 0)
593                 goto cont;
595             if (add_domain (dom) < 0) {
596                 ERROR ("libvirt plugin: malloc failed.");
597                 goto cont;
598             }
600             /* Get a list of devices for this domain. */
601             xml = virDomainGetXMLDesc (dom, 0);
602             if (!xml) {
603                 VIRT_ERROR (conn, "virDomainGetXMLDesc");
604                 goto cont;
605             }
607             /* Yuck, XML.  Parse out the devices. */
608             xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
609             if (xml_doc == NULL) {
610                 VIRT_ERROR (conn, "xmlReadDoc");
611                 goto cont;
612             }
614             xpath_ctx = xmlXPathNewContext (xml_doc);
616             /* Block devices. */
617             xpath_obj = xmlXPathEval
618                 ((xmlChar *) "/domain/devices/disk/target[@dev]",
619                  xpath_ctx);
620             if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
621                 xpath_obj->nodesetval == NULL)
622                 goto cont;
624             for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
625                 xmlNodePtr node;
626                 char *path = NULL;
628                 node = xpath_obj->nodesetval->nodeTab[j];
629                 if (!node) continue;
630                 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
631                 if (!path) continue;
633                 if (il_block_devices &&
634                     ignore_device_match (il_block_devices, name, path) != 0)
635                     goto cont2;
637                 add_block_device (dom, path);
638             cont2:
639                 if (path) xmlFree (path);
640             }
641             xmlXPathFreeObject (xpath_obj);
643             /* Network interfaces. */
644             xpath_obj = xmlXPathEval
645                 ((xmlChar *) "/domain/devices/interface[target[@dev]]",
646                  xpath_ctx);
647             if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
648                 xpath_obj->nodesetval == NULL)
649                 goto cont;
651             xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
653             for (j = 0; j < xml_interfaces->nodeNr; ++j) {
654                 char *path = NULL;
655                 char *address = NULL;
656                 xmlNodePtr xml_interface;
658                 xml_interface = xml_interfaces->nodeTab[j];
659                 if (!xml_interface) continue;
660                 xmlNodePtr child = NULL;
662                 for (child = xml_interface->children; child; child = child->next) {
663                     if (child->type != XML_ELEMENT_NODE) continue;
665                     if (xmlStrEqual(child->name, (const xmlChar *) "target")) {
666                         path = (char *) xmlGetProp (child, (const xmlChar *) "dev");
667                         if (!path) continue;
668                     } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) {
669                         address = (char *) xmlGetProp (child, (const xmlChar *) "address");
670                         if (!address) continue;
671                     }
672                 }
674                 if (il_interface_devices &&
675                     (ignore_device_match (il_interface_devices, name, path) != 0 ||
676                      ignore_device_match (il_interface_devices, name, address) != 0))
677                     goto cont3;
679                 add_interface_device (dom, path, address, j+1);
680                 cont3:
681                     if (path) xmlFree (path);
682                     if (address) xmlFree (address);
683             }
685         cont:
686             if (xpath_obj) xmlXPathFreeObject (xpath_obj);
687             if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
688             if (xml_doc) xmlFreeDoc (xml_doc);
689             sfree (xml);
690         }
692         sfree (domids);
693     }
695     return 0;
698 static void
699 free_domains ()
701     int i;
703     if (domains) {
704         for (i = 0; i < nr_domains; ++i)
705             virDomainFree (domains[i]);
706         sfree (domains);
707     }
708     domains = NULL;
709     nr_domains = 0;
712 static int
713 add_domain (virDomainPtr dom)
715     virDomainPtr *new_ptr;
716     int new_size = sizeof (domains[0]) * (nr_domains+1);
718     if (domains)
719         new_ptr = realloc (domains, new_size);
720     else
721         new_ptr = malloc (new_size);
723     if (new_ptr == NULL)
724         return -1;
726     domains = new_ptr;
727     domains[nr_domains] = dom;
728     return nr_domains++;
731 static void
732 free_block_devices ()
734     int i;
736     if (block_devices) {
737         for (i = 0; i < nr_block_devices; ++i)
738             sfree (block_devices[i].path);
739         sfree (block_devices);
740     }
741     block_devices = NULL;
742     nr_block_devices = 0;
745 static int
746 add_block_device (virDomainPtr dom, const char *path)
748     struct block_device *new_ptr;
749     int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
750     char *path_copy;
752     path_copy = strdup (path);
753     if (!path_copy)
754         return -1;
756     if (block_devices)
757         new_ptr = realloc (block_devices, new_size);
758     else
759         new_ptr = malloc (new_size);
761     if (new_ptr == NULL) {
762         sfree (path_copy);
763         return -1;
764     }
765     block_devices = new_ptr;
766     block_devices[nr_block_devices].dom = dom;
767     block_devices[nr_block_devices].path = path_copy;
768     return nr_block_devices++;
771 static void
772 free_interface_devices ()
774     int i;
776     if (interface_devices) {
777         for (i = 0; i < nr_interface_devices; ++i) {
778             sfree (interface_devices[i].path);
779             sfree (interface_devices[i].address);
780             sfree (interface_devices[i].number);
781         }
782         sfree (interface_devices);
783     }
784     interface_devices = NULL;
785     nr_interface_devices = 0;
788 static int
789 add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number)
791     struct interface_device *new_ptr;
792     int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
793     char *path_copy, *address_copy, number_string[15];
795     path_copy = strdup (path);
796     if (!path_copy) return -1;
798     address_copy = strdup (address);
799     if (!address_copy) {
800         sfree(path_copy);
801         return -1;
802     }
804     snprintf(number_string, sizeof (number_string), "interface-%u", number);
806     if (interface_devices)
807         new_ptr = realloc (interface_devices, new_size);
808     else
809         new_ptr = malloc (new_size);
811     if (new_ptr == NULL) {
812         sfree (path_copy);
813         sfree (address_copy);
814         return -1;
815     }
816     interface_devices = new_ptr;
817     interface_devices[nr_interface_devices].dom = dom;
818     interface_devices[nr_interface_devices].path = path_copy;
819     interface_devices[nr_interface_devices].address = address_copy;
820     interface_devices[nr_interface_devices].number = strdup(number_string);
821     return nr_interface_devices++;
824 static int
825 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
827     char *name;
828     int n, r;
830     n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
831     name = malloc (n);
832     if (name == NULL) {
833         ERROR ("libvirt plugin: malloc failed.");
834         return 0;
835     }
836     ssnprintf (name, n, "%s:%s", domname, devpath);
837     r = ignorelist_match (il, name);
838     sfree (name);
839     return r;
842 static int
843 lv_shutdown (void)
845     free_block_devices ();
846     free_interface_devices ();
847     free_domains ();
849     if (conn != NULL)
850         virConnectClose (conn);
851     conn = NULL;
853     ignorelist_free (il_domains);
854     il_domains = NULL;
855     ignorelist_free (il_block_devices);
856     il_block_devices = NULL;
857     ignorelist_free (il_interface_devices);
858     il_interface_devices = NULL;
860     return 0;
863 void
864 module_register (void)
866     plugin_register_config ("libvirt",
867     lv_config,
868     config_keys, NR_CONFIG_KEYS);
869     plugin_register_init ("libvirt", lv_init);
870     plugin_register_read ("libvirt", lv_read);
871     plugin_register_shutdown ("libvirt", lv_shutdown);
874 /*
875  * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker
876  */