src/utils_format_json.c: Correctly format the time of value lists.
[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  **/
21
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"
28
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>
34
35 static const char *config_keys[] = {
36     "Connection",
37
38     "RefreshInterval",
39
40     "Domain",
41     "BlockDevice",
42     "InterfaceDevice",
43     "IgnoreSelected",
44
45     "HostnameFormat",
46     "InterfaceFormat",
47
48     NULL
49 };
50 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
51
52 /* Connection. */
53 static virConnectPtr conn = 0;
54 static char *conn_string = NULL;
55 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
56
57 /* Seconds between list refreshes, 0 disables completely. */
58 static int interval = 60;
59
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;
66
67 static int ignore_device_match (ignorelist_t *,
68                                 const char *domname, const char *devpath);
69
70 /* Actual list of domains found on last refresh. */
71 static virDomainPtr *domains = NULL;
72 static int nr_domains = 0;
73
74 static void free_domains (void);
75 static int add_domain (virDomainPtr dom);
76
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 };
82
83 static struct block_device *block_devices = NULL;
84 static int nr_block_devices = 0;
85
86 static void free_block_devices (void);
87 static int add_block_device (virDomainPtr dom, const char *path);
88
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 };
95
96 static struct interface_device *interface_devices = NULL;
97 static int nr_interface_devices = 0;
98
99 static void free_interface_devices (void);
100 static int add_interface_device (virDomainPtr dom, const char *path, const char *address);
101
102 /* HostnameFormat. */
103 #define HF_MAX_FIELDS 3
104
105 enum hf_field {
106     hf_none = 0,
107     hf_hostname,
108     hf_name,
109     hf_uuid
110 };
111
112 static enum hf_field hostname_format[HF_MAX_FIELDS] =
113     { hf_name };
114
115 /* InterfaceFormat. */
116 enum if_field {
117     if_address,
118     if_name
119 };
120
121 static enum if_field interface_format = if_name;
122
123 /* Time that we last refreshed. */
124 static time_t last_refresh = (time_t) 0;
125
126 static int refresh_lists (void);
127
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)
134
135 static void
136 init_value_list (value_list_t *vl, virDomainPtr dom)
137 {
138     int i, n;
139     const char *name;
140     char uuid[VIR_UUID_STRING_BUFLEN];
141     char  *host_ptr;
142     size_t host_len;
143
144     vl->interval = interval_g;
145
146     sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
147
148     vl->host[0] = '\0';
149     host_ptr = vl->host;
150     host_len = sizeof (vl->host);
151
152     /* Construct the hostname field according to HostnameFormat. */
153     for (i = 0; i < HF_MAX_FIELDS; ++i) {
154         if (hostname_format[i] == hf_none)
155             continue;
156
157         n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
158
159         if (i > 0 && n >= 1) {
160             strncat (vl->host, ":", 1);
161             n--;
162         }
163
164         switch (hostname_format[i]) {
165         case hf_none: break;
166         case hf_hostname:
167             strncat (vl->host, hostname_g, n);
168             break;
169         case hf_name:
170             name = virDomainGetName (dom);
171             if (name)
172                 strncat (vl->host, name, n);
173             break;
174         case hf_uuid:
175             if (virDomainGetUUIDString (dom, uuid) == 0)
176                 strncat (vl->host, uuid, n);
177             break;
178         }
179     }
180
181     vl->host[sizeof (vl->host) - 1] = '\0';
182 } /* void init_value_list */
183
184 static void
185 cpu_submit (unsigned long long cpu_time,
186             virDomainPtr dom, const char *type)
187 {
188     value_t values[1];
189     value_list_t vl = VALUE_LIST_INIT;
190
191     init_value_list (&vl, dom);
192
193     values[0].derive = cpu_time;
194
195     vl.values = values;
196     vl.values_len = 1;
197
198     sstrncpy (vl.type, type, sizeof (vl.type));
199
200     plugin_dispatch_values (&vl);
201 }
202
203 static void
204 vcpu_submit (derive_t cpu_time,
205              virDomainPtr dom, int vcpu_nr, const char *type)
206 {
207     value_t values[1];
208     value_list_t vl = VALUE_LIST_INIT;
209
210     init_value_list (&vl, dom);
211
212     values[0].derive = cpu_time;
213     vl.values = values;
214     vl.values_len = 1;
215
216     sstrncpy (vl.type, type, sizeof (vl.type));
217     ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
218
219     plugin_dispatch_values (&vl);
220 }
221
222 static void
223 submit_derive2 (const char *type, derive_t v0, derive_t v1,
224              virDomainPtr dom, const char *devname)
225 {
226     value_t values[2];
227     value_list_t vl = VALUE_LIST_INIT;
228
229     init_value_list (&vl, dom);
230
231     values[0].derive = v0;
232     values[1].derive = v1;
233     vl.values = values;
234     vl.values_len = 2;
235
236     sstrncpy (vl.type, type, sizeof (vl.type));
237     sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
238
239     plugin_dispatch_values (&vl);
240 } /* void submit_derive2 */
241
242 static int
243 lv_init (void)
244 {
245     if (virInitialize () != 0)
246         return -1;
247
248         return 0;
249 }
250
251 static int
252 lv_config (const char *key, const char *value)
253 {
254     if (virInitialize () != 0)
255         return 1;
256
257     if (il_domains == NULL)
258         il_domains = ignorelist_create (1);
259     if (il_block_devices == NULL)
260         il_block_devices = ignorelist_create (1);
261     if (il_interface_devices == NULL)
262         il_interface_devices = ignorelist_create (1);
263
264     if (strcasecmp (key, "Connection") == 0) {
265         char *tmp = strdup (value);
266         if (tmp == NULL) {
267             ERROR ("libvirt plugin: Connection strdup failed.");
268             return 1;
269         }
270         sfree (conn_string);
271         conn_string = tmp;
272         return 0;
273     }
274
275     if (strcasecmp (key, "RefreshInterval") == 0) {
276         char *eptr = NULL;
277         interval = strtol (value, &eptr, 10);
278         if (eptr == NULL || *eptr != '\0') return 1;
279         return 0;
280     }
281
282     if (strcasecmp (key, "Domain") == 0) {
283         if (ignorelist_add (il_domains, value)) return 1;
284         return 0;
285     }
286     if (strcasecmp (key, "BlockDevice") == 0) {
287         if (ignorelist_add (il_block_devices, value)) return 1;
288         return 0;
289     }
290     if (strcasecmp (key, "InterfaceDevice") == 0) {
291         if (ignorelist_add (il_interface_devices, value)) return 1;
292         return 0;
293     }
294
295     if (strcasecmp (key, "IgnoreSelected") == 0) {
296         if (IS_TRUE (value))
297         {
298             ignorelist_set_invert (il_domains, 0);
299             ignorelist_set_invert (il_block_devices, 0);
300             ignorelist_set_invert (il_interface_devices, 0);
301         }
302         else
303         {
304             ignorelist_set_invert (il_domains, 1);
305             ignorelist_set_invert (il_block_devices, 1);
306             ignorelist_set_invert (il_interface_devices, 1);
307         }
308         return 0;
309     }
310
311     if (strcasecmp (key, "HostnameFormat") == 0) {
312         char *value_copy;
313         char *fields[HF_MAX_FIELDS];
314         int i, n;
315
316         value_copy = strdup (value);
317         if (value_copy == NULL) {
318             ERROR ("libvirt plugin: strdup failed.");
319             return -1;
320         }
321
322         n = strsplit (value_copy, fields, HF_MAX_FIELDS);
323         if (n < 1) {
324             sfree (value_copy);
325             ERROR ("HostnameFormat: no fields");
326             return -1;
327         }
328
329         for (i = 0; i < n; ++i) {
330             if (strcasecmp (fields[i], "hostname") == 0)
331                 hostname_format[i] = hf_hostname;
332             else if (strcasecmp (fields[i], "name") == 0)
333                 hostname_format[i] = hf_name;
334             else if (strcasecmp (fields[i], "uuid") == 0)
335                 hostname_format[i] = hf_uuid;
336             else {
337                 sfree (value_copy);
338                 ERROR ("unknown HostnameFormat field: %s", fields[i]);
339                 return -1;
340             }
341         }
342         sfree (value_copy);
343
344         for (i = n; i < HF_MAX_FIELDS; ++i)
345             hostname_format[i] = hf_none;
346
347         return 0;
348     }
349
350     if (strcasecmp (key, "InterfaceFormat") == 0) {
351         if (strcasecmp (value, "name") == 0)
352             interface_format = if_name;
353         else if (strcasecmp (value, "address") == 0)
354             interface_format = if_address;
355         else {
356             ERROR ("unknown InterfaceFormat: %s", value);
357             return -1;
358         }
359         return 0;
360     }
361
362     /* Unrecognised option. */
363     return -1;
364 }
365
366 static int
367 lv_read (void)
368 {
369     time_t t;
370     int i;
371
372     if (conn == NULL) {
373         /* `conn_string == NULL' is acceptable. */
374         conn = virConnectOpenReadOnly (conn_string);
375         if (conn == NULL) {
376             c_complain (LOG_ERR, &conn_complain,
377                     "libvirt plugin: Unable to connect: "
378                     "virConnectOpenReadOnly failed.");
379             return -1;
380         }
381     }
382     c_release (LOG_NOTICE, &conn_complain,
383             "libvirt plugin: Connection established.");
384
385     time (&t);
386
387     /* Need to refresh domain or device lists? */
388     if ((last_refresh == (time_t) 0) ||
389             ((interval > 0) && ((last_refresh + interval) <= t))) {
390         if (refresh_lists () != 0) {
391             if (conn != NULL)
392                 virConnectClose (conn);
393             conn = NULL;
394             return -1;
395         }
396         last_refresh = t;
397     }
398
399 #if 0
400     for (i = 0; i < nr_domains; ++i)
401         fprintf (stderr, "domain %s\n", virDomainGetName (domains[i]));
402     for (i = 0; i < nr_block_devices; ++i)
403         fprintf  (stderr, "block device %d %s:%s\n",
404                   i, virDomainGetName (block_devices[i].dom),
405                   block_devices[i].path);
406     for (i = 0; i < nr_interface_devices; ++i)
407         fprintf (stderr, "interface device %d %s:%s\n",
408                  i, virDomainGetName (interface_devices[i].dom),
409                  interface_devices[i].path);
410 #endif
411
412     /* Get CPU usage, VCPU usage for each domain. */
413     for (i = 0; i < nr_domains; ++i) {
414         virDomainInfo info;
415         virVcpuInfoPtr vinfo = NULL;
416         int status;
417         int j;
418
419         status = virDomainGetInfo (domains[i], &info);
420         if (status != 0)
421         {
422             ERROR ("libvirt plugin: virDomainGetInfo failed with status %i.",
423                     status);
424             continue;
425         }
426
427         cpu_submit (info.cpuTime, domains[i], "virt_cpu_total");
428
429         vinfo = malloc (info.nrVirtCpu * sizeof (vinfo[0]));
430         if (vinfo == NULL) {
431             ERROR ("libvirt plugin: malloc failed.");
432             continue;
433         }
434
435         status = virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
436                 /* cpu map = */ NULL, /* cpu map length = */ 0);
437         if (status < 0)
438         {
439             ERROR ("libvirt plugin: virDomainGetVcpus failed with status %i.",
440                     status);
441             free (vinfo);
442             continue;
443         }
444
445         for (j = 0; j < info.nrVirtCpu; ++j)
446             vcpu_submit (vinfo[j].cpuTime,
447                     domains[i], vinfo[j].number, "virt_vcpu");
448
449         sfree (vinfo);
450     }
451
452     /* Get block device stats for each domain. */
453     for (i = 0; i < nr_block_devices; ++i) {
454         struct _virDomainBlockStats stats;
455
456         if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
457                     &stats, sizeof stats) != 0)
458             continue;
459
460         if ((stats.rd_req != -1) && (stats.wr_req != -1))
461             submit_derive2 ("disk_ops",
462                     (derive_t) stats.rd_req, (derive_t) stats.wr_req,
463                     block_devices[i].dom, block_devices[i].path);
464
465         if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
466             submit_derive2 ("disk_octets",
467                     (derive_t) stats.rd_bytes, (derive_t) stats.wr_bytes,
468                     block_devices[i].dom, block_devices[i].path);
469     } /* for (nr_block_devices) */
470
471     /* Get interface stats for each domain. */
472     for (i = 0; i < nr_interface_devices; ++i) {
473         struct _virDomainInterfaceStats stats;
474         char *display_name = interface_devices[i].path;
475
476         if (interface_format == if_address)
477             display_name = interface_devices[i].address;
478
479         if (virDomainInterfaceStats (interface_devices[i].dom,
480                     interface_devices[i].path,
481                     &stats, sizeof stats) != 0)
482             continue;
483
484         if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
485             submit_derive2 ("if_octets",
486                     (derive_t) stats.rx_bytes, (derive_t) stats.tx_bytes,
487                     interface_devices[i].dom, display_name);
488
489         if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
490             submit_derive2 ("if_packets",
491                     (derive_t) stats.rx_packets, (derive_t) stats.tx_packets,
492                     interface_devices[i].dom, display_name);
493
494         if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
495             submit_derive2 ("if_errors",
496                     (derive_t) stats.rx_errs, (derive_t) stats.tx_errs,
497                     interface_devices[i].dom, display_name);
498
499         if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
500             submit_derive2 ("if_dropped",
501                     (derive_t) stats.rx_drop, (derive_t) stats.tx_drop,
502                     interface_devices[i].dom, display_name);
503     } /* for (nr_interface_devices) */
504
505     return 0;
506 }
507
508 static int
509 refresh_lists (void)
510 {
511     int n;
512
513     n = virConnectNumOfDomains (conn);
514     if (n < 0) {
515         VIRT_ERROR (conn, "reading number of domains");
516         return -1;
517     }
518
519     if (n > 0) {
520         int i;
521         int *domids;
522
523         /* Get list of domains. */
524         domids = malloc (sizeof (int) * n);
525         if (domids == 0) {
526             ERROR ("libvirt plugin: malloc failed.");
527             return -1;
528         }
529
530         n = virConnectListDomains (conn, domids, n);
531         if (n < 0) {
532             VIRT_ERROR (conn, "reading list of domains");
533             sfree (domids);
534             return -1;
535         }
536
537         free_block_devices ();
538         free_interface_devices ();
539         free_domains ();
540
541         /* Fetch each domain and add it to the list, unless ignore. */
542         for (i = 0; i < n; ++i) {
543             virDomainPtr dom = NULL;
544             const char *name;
545             char *xml = NULL;
546             xmlDocPtr xml_doc = NULL;
547             xmlXPathContextPtr xpath_ctx = NULL;
548             xmlXPathObjectPtr xpath_obj = NULL;
549             int j;
550
551             dom = virDomainLookupByID (conn, domids[i]);
552             if (dom == NULL) {
553                 VIRT_ERROR (conn, "virDomainLookupByID");
554                 /* Could be that the domain went away -- ignore it anyway. */
555                 continue;
556             }
557
558             name = virDomainGetName (dom);
559             if (name == NULL) {
560                 VIRT_ERROR (conn, "virDomainGetName");
561                 goto cont;
562             }
563
564             if (il_domains && ignorelist_match (il_domains, name) != 0)
565                 goto cont;
566
567             if (add_domain (dom) < 0) {
568                 ERROR ("libvirt plugin: malloc failed.");
569                 goto cont;
570             }
571
572             /* Get a list of devices for this domain. */
573             xml = virDomainGetXMLDesc (dom, 0);
574             if (!xml) {
575                 VIRT_ERROR (conn, "virDomainGetXMLDesc");
576                 goto cont;
577             }
578
579             /* Yuck, XML.  Parse out the devices. */
580             xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
581             if (xml_doc == NULL) {
582                 VIRT_ERROR (conn, "xmlReadDoc");
583                 goto cont;
584             }
585
586             xpath_ctx = xmlXPathNewContext (xml_doc);
587
588             /* Block devices. */
589             xpath_obj = xmlXPathEval
590                 ((xmlChar *) "/domain/devices/disk/target[@dev]",
591                  xpath_ctx);
592             if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
593                 xpath_obj->nodesetval == NULL)
594                 goto cont;
595
596             for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
597                 xmlNodePtr node;
598                 char *path = NULL;
599
600                 node = xpath_obj->nodesetval->nodeTab[j];
601                 if (!node) continue;
602                 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
603                 if (!path) continue;
604
605                 if (il_block_devices &&
606                     ignore_device_match (il_block_devices, name, path) != 0)
607                     goto cont2;
608
609                 add_block_device (dom, path);
610             cont2:
611                 if (path) xmlFree (path);
612             }
613             xmlXPathFreeObject (xpath_obj);
614
615             /* Network interfaces. */
616             xpath_obj = xmlXPathEval
617                 ((xmlChar *) "/domain/devices/interface[target[@dev]]",
618                  xpath_ctx);
619             if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
620                 xpath_obj->nodesetval == NULL)
621                 goto cont;
622
623             xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
624
625             for (j = 0; j < xml_interfaces->nodeNr; ++j) {
626                 char *path = NULL;
627                 char *address = NULL;
628                 xmlNodePtr xml_interface;
629
630                 xml_interface = xml_interfaces->nodeTab[j];
631                 if (!xml_interface) continue;
632                 xmlNodePtr child = NULL;
633
634                 for (child = xml_interface->children; child; child = child->next) {
635                     if (child->type != XML_ELEMENT_NODE) continue;
636
637                     if (xmlStrEqual(child->name, (const xmlChar *) "target")) {
638                         path = (char *) xmlGetProp (child, (const xmlChar *) "dev");
639                         if (!path) continue;
640                     } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) {
641                         address = (char *) xmlGetProp (child, (const xmlChar *) "address");
642                         if (!address) continue;
643                     }
644                 }
645
646                 if (il_interface_devices &&
647                     (ignore_device_match (il_interface_devices, name, path) != 0 ||
648                      ignore_device_match (il_interface_devices, name, address) != 0))
649                     goto cont3;
650
651                 add_interface_device (dom, path, address);
652                 cont3:
653                     if (path) xmlFree (path);
654                     if (address) xmlFree (address);
655             }
656
657         cont:
658             if (xpath_obj) xmlXPathFreeObject (xpath_obj);
659             if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
660             if (xml_doc) xmlFreeDoc (xml_doc);
661             sfree (xml);
662         }
663
664         sfree (domids);
665     }
666
667     return 0;
668 }
669
670 static void
671 free_domains ()
672 {
673     int i;
674
675     if (domains) {
676         for (i = 0; i < nr_domains; ++i)
677             virDomainFree (domains[i]);
678         sfree (domains);
679     }
680     domains = NULL;
681     nr_domains = 0;
682 }
683
684 static int
685 add_domain (virDomainPtr dom)
686 {
687     virDomainPtr *new_ptr;
688     int new_size = sizeof (domains[0]) * (nr_domains+1);
689
690     if (domains)
691         new_ptr = realloc (domains, new_size);
692     else
693         new_ptr = malloc (new_size);
694
695     if (new_ptr == NULL)
696         return -1;
697
698     domains = new_ptr;
699     domains[nr_domains] = dom;
700     return nr_domains++;
701 }
702
703 static void
704 free_block_devices ()
705 {
706     int i;
707
708     if (block_devices) {
709         for (i = 0; i < nr_block_devices; ++i)
710             sfree (block_devices[i].path);
711         sfree (block_devices);
712     }
713     block_devices = NULL;
714     nr_block_devices = 0;
715 }
716
717 static int
718 add_block_device (virDomainPtr dom, const char *path)
719 {
720     struct block_device *new_ptr;
721     int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
722     char *path_copy;
723
724     path_copy = strdup (path);
725     if (!path_copy)
726         return -1;
727
728     if (block_devices)
729         new_ptr = realloc (block_devices, new_size);
730     else
731         new_ptr = malloc (new_size);
732
733     if (new_ptr == NULL) {
734         sfree (path_copy);
735         return -1;
736     }
737     block_devices = new_ptr;
738     block_devices[nr_block_devices].dom = dom;
739     block_devices[nr_block_devices].path = path_copy;
740     return nr_block_devices++;
741 }
742
743 static void
744 free_interface_devices ()
745 {
746     int i;
747
748     if (interface_devices) {
749         for (i = 0; i < nr_interface_devices; ++i) {
750             sfree (interface_devices[i].path);
751             sfree (interface_devices[i].address);
752         }
753         sfree (interface_devices);
754     }
755     interface_devices = NULL;
756     nr_interface_devices = 0;
757 }
758
759 static int
760 add_interface_device (virDomainPtr dom, const char *path, const char *address)
761 {
762     struct interface_device *new_ptr;
763     int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
764     char *path_copy, *address_copy;
765
766     path_copy = strdup (path);
767     if (!path_copy) return -1;
768
769     address_copy = strdup (address);
770     if (!address_copy) return -1;
771
772     if (interface_devices)
773         new_ptr = realloc (interface_devices, new_size);
774     else
775         new_ptr = malloc (new_size);
776
777     if (new_ptr == NULL) {
778         sfree (path_copy);
779         sfree (address_copy);
780         return -1;
781     }
782     interface_devices = new_ptr;
783     interface_devices[nr_interface_devices].dom = dom;
784     interface_devices[nr_interface_devices].path = path_copy;
785     interface_devices[nr_interface_devices].address = address_copy;
786     return nr_interface_devices++;
787 }
788
789 static int
790 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
791 {
792     char *name;
793     int n, r;
794
795     n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
796     name = malloc (n);
797     if (name == NULL) {
798         ERROR ("libvirt plugin: malloc failed.");
799         return 0;
800     }
801     ssnprintf (name, n, "%s:%s", domname, devpath);
802     r = ignorelist_match (il, name);
803     sfree (name);
804     return r;
805 }
806
807 static int
808 lv_shutdown (void)
809 {
810     free_block_devices ();
811     free_interface_devices ();
812     free_domains ();
813
814     if (conn != NULL)
815         virConnectClose (conn);
816     conn = NULL;
817
818     ignorelist_free (il_domains);
819     il_domains = NULL;
820     ignorelist_free (il_block_devices);
821     il_block_devices = NULL;
822     ignorelist_free (il_interface_devices);
823     il_interface_devices = NULL;
824
825     return 0;
826 }
827
828 void
829 module_register (void)
830 {
831     plugin_register_config ("libvirt",
832             lv_config,
833             config_keys, NR_CONFIG_KEYS);
834     plugin_register_init ("libvirt", lv_init);
835     plugin_register_read ("libvirt", lv_read);
836     plugin_register_shutdown ("libvirt", lv_shutdown);
837 }
838
839 /*
840  * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker
841  */