Merge pull request #1596 from rubenk/fix-a-few-more-prototypes
[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     char *number;               /* interface device number */
95 };
96
97 static struct interface_device *interface_devices = NULL;
98 static int nr_interface_devices = 0;
99
100 static void free_interface_devices (void);
101 static int add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number);
102
103 /* HostnameFormat. */
104 #define HF_MAX_FIELDS 3
105
106 enum hf_field {
107     hf_none = 0,
108     hf_hostname,
109     hf_name,
110     hf_uuid
111 };
112
113 static enum hf_field hostname_format[HF_MAX_FIELDS] =
114     { hf_name };
115
116 /* InterfaceFormat. */
117 enum if_field {
118     if_address,
119     if_name,
120     if_number
121 };
122
123 static enum if_field interface_format = if_name;
124
125 /* Time that we last refreshed. */
126 static time_t last_refresh = (time_t) 0;
127
128 static int refresh_lists (void);
129
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)
136
137 static void
138 init_value_list (value_list_t *vl, virDomainPtr dom)
139 {
140     int i, n;
141     const char *name;
142     char uuid[VIR_UUID_STRING_BUFLEN];
143
144     sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
145
146     vl->host[0] = '\0';
147
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;
152
153         n = DATA_MAX_NAME_LEN - strlen (vl->host) - 2;
154
155         if (i > 0 && n >= 1) {
156             strncat (vl->host, ":", 1);
157             n--;
158         }
159
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     }
176
177     vl->host[sizeof (vl->host) - 1] = '\0';
178 } /* void init_value_list */
179
180 static void
181 memory_submit (gauge_t memory, virDomainPtr dom)
182 {
183     value_t values[1];
184     value_list_t vl = VALUE_LIST_INIT;
185
186     init_value_list (&vl, dom);
187
188     values[0].gauge = memory;
189
190     vl.values = values;
191     vl.values_len = 1;
192
193     sstrncpy (vl.type, "memory", sizeof (vl.type));
194     sstrncpy (vl.type_instance, "total", sizeof (vl.type_instance));
195
196     plugin_dispatch_values (&vl);
197 }
198
199 static void
200 cpu_submit (unsigned long long cpu_time,
201             virDomainPtr dom, const char *type)
202 {
203     value_t values[1];
204     value_list_t vl = VALUE_LIST_INIT;
205
206     init_value_list (&vl, dom);
207
208     values[0].derive = cpu_time;
209
210     vl.values = values;
211     vl.values_len = 1;
212
213     sstrncpy (vl.type, type, sizeof (vl.type));
214
215     plugin_dispatch_values (&vl);
216 }
217
218 static void
219 vcpu_submit (derive_t cpu_time,
220              virDomainPtr dom, int vcpu_nr, const char *type)
221 {
222     value_t values[1];
223     value_list_t vl = VALUE_LIST_INIT;
224
225     init_value_list (&vl, dom);
226
227     values[0].derive = cpu_time;
228     vl.values = values;
229     vl.values_len = 1;
230
231     sstrncpy (vl.type, type, sizeof (vl.type));
232     ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
233
234     plugin_dispatch_values (&vl);
235 }
236
237 static void
238 submit_derive2 (const char *type, derive_t v0, derive_t v1,
239              virDomainPtr dom, const char *devname)
240 {
241     value_t values[2];
242     value_list_t vl = VALUE_LIST_INIT;
243
244     init_value_list (&vl, dom);
245
246     values[0].derive = v0;
247     values[1].derive = v1;
248     vl.values = values;
249     vl.values_len = 2;
250
251     sstrncpy (vl.type, type, sizeof (vl.type));
252     sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
253
254     plugin_dispatch_values (&vl);
255 } /* void submit_derive2 */
256
257 static int
258 lv_init (void)
259 {
260     if (virInitialize () != 0)
261         return -1;
262     else
263         return 0;
264 }
265
266 static int
267 lv_config (const char *key, const char *value)
268 {
269     if (virInitialize () != 0)
270         return 1;
271
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);
278
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     }
289
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     }
296
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     }
309
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     }
325
326     if (strcasecmp (key, "HostnameFormat") == 0) {
327         char *value_copy;
328         char *fields[HF_MAX_FIELDS];
329         int i, n;
330
331         value_copy = strdup (value);
332         if (value_copy == NULL) {
333             ERROR ("libvirt plugin: strdup failed.");
334             return -1;
335         }
336
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         }
343
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);
358
359         for (i = n; i < HF_MAX_FIELDS; ++i)
360             hostname_format[i] = hf_none;
361
362         return 0;
363     }
364
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     }
378
379     /* Unrecognised option. */
380     return -1;
381 }
382
383 static int
384 lv_read (void)
385 {
386     time_t t;
387     int i;
388
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.");
401
402     time (&t);
403
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     }
415
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
428
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;
435
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         }
443
444         if (info.state != VIR_DOMAIN_RUNNING)
445         {
446             /* only gather stats for running domains */
447             continue;
448         }
449
450         cpu_submit (info.cpuTime, domains[i], "virt_cpu_total");
451         memory_submit ((gauge_t) info.memory * 1024, domains[i]);
452
453         vinfo = malloc (info.nrVirtCpu * sizeof (vinfo[0]));
454         if (vinfo == NULL) {
455             ERROR ("libvirt plugin: malloc failed.");
456             continue;
457         }
458
459         status = virDomainGetVcpus (domains[i], vinfo, info.nrVirtCpu,
460                 /* cpu map = */ NULL, /* cpu map length = */ 0);
461         if (status < 0)
462         {
463             ERROR ("libvirt plugin: virDomainGetVcpus failed with status %i.",
464                     status);
465             free (vinfo);
466             continue;
467         }
468
469         for (j = 0; j < info.nrVirtCpu; ++j)
470             vcpu_submit (vinfo[j].cpuTime,
471                     domains[i], vinfo[j].number, "virt_vcpu");
472
473         sfree (vinfo);
474     }
475
476     /* Get block device stats for each domain. */
477     for (i = 0; i < nr_block_devices; ++i) {
478         struct _virDomainBlockStats stats;
479
480         if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path,
481                     &stats, sizeof stats) != 0)
482             continue;
483
484         if ((stats.rd_req != -1) && (stats.wr_req != -1))
485             submit_derive2 ("disk_ops",
486                     (derive_t) stats.rd_req, (derive_t) stats.wr_req,
487                     block_devices[i].dom, block_devices[i].path);
488
489         if ((stats.rd_bytes != -1) && (stats.wr_bytes != -1))
490             submit_derive2 ("disk_octets",
491                     (derive_t) stats.rd_bytes, (derive_t) stats.wr_bytes,
492                     block_devices[i].dom, block_devices[i].path);
493     } /* for (nr_block_devices) */
494
495     /* Get interface stats for each domain. */
496     for (i = 0; i < nr_interface_devices; ++i) {
497         struct _virDomainInterfaceStats stats;
498         char *display_name = NULL;
499
500
501         switch (interface_format) {
502             case if_address:
503                 display_name = interface_devices[i].address;
504                 break;
505             case if_number:
506                 display_name = interface_devices[i].number;
507                 break;
508             case if_name:
509             default:
510                 display_name = interface_devices[i].path;
511         }
512
513         if (virDomainInterfaceStats (interface_devices[i].dom,
514                     interface_devices[i].path,
515                     &stats, sizeof stats) != 0)
516             continue;
517
518         if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
519             submit_derive2 ("if_octets",
520                     (derive_t) stats.rx_bytes, (derive_t) stats.tx_bytes,
521                     interface_devices[i].dom, display_name);
522
523         if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
524             submit_derive2 ("if_packets",
525                     (derive_t) stats.rx_packets, (derive_t) stats.tx_packets,
526                     interface_devices[i].dom, display_name);
527
528         if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
529             submit_derive2 ("if_errors",
530                     (derive_t) stats.rx_errs, (derive_t) stats.tx_errs,
531                     interface_devices[i].dom, display_name);
532
533         if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
534             submit_derive2 ("if_dropped",
535                     (derive_t) stats.rx_drop, (derive_t) stats.tx_drop,
536                     interface_devices[i].dom, display_name);
537     } /* for (nr_interface_devices) */
538
539     return 0;
540 }
541
542 static int
543 refresh_lists (void)
544 {
545     int n;
546
547     n = virConnectNumOfDomains (conn);
548     if (n < 0) {
549         VIRT_ERROR (conn, "reading number of domains");
550         return -1;
551     }
552
553     if (n > 0) {
554         int i;
555         int *domids;
556
557         /* Get list of domains. */
558         domids = malloc (sizeof (int) * n);
559         if (domids == 0) {
560             ERROR ("libvirt plugin: malloc failed.");
561             return -1;
562         }
563
564         n = virConnectListDomains (conn, domids, n);
565         if (n < 0) {
566             VIRT_ERROR (conn, "reading list of domains");
567             sfree (domids);
568             return -1;
569         }
570
571         free_block_devices ();
572         free_interface_devices ();
573         free_domains ();
574
575         /* Fetch each domain and add it to the list, unless ignore. */
576         for (i = 0; i < n; ++i) {
577             virDomainPtr dom = NULL;
578             const char *name;
579             char *xml = NULL;
580             xmlDocPtr xml_doc = NULL;
581             xmlXPathContextPtr xpath_ctx = NULL;
582             xmlXPathObjectPtr xpath_obj = NULL;
583             int j;
584
585             dom = virDomainLookupByID (conn, domids[i]);
586             if (dom == NULL) {
587                 VIRT_ERROR (conn, "virDomainLookupByID");
588                 /* Could be that the domain went away -- ignore it anyway. */
589                 continue;
590             }
591
592             name = virDomainGetName (dom);
593             if (name == NULL) {
594                 VIRT_ERROR (conn, "virDomainGetName");
595                 goto cont;
596             }
597
598             if (il_domains && ignorelist_match (il_domains, name) != 0)
599                 goto cont;
600
601             if (add_domain (dom) < 0) {
602                 ERROR ("libvirt plugin: malloc failed.");
603                 goto cont;
604             }
605
606             /* Get a list of devices for this domain. */
607             xml = virDomainGetXMLDesc (dom, 0);
608             if (!xml) {
609                 VIRT_ERROR (conn, "virDomainGetXMLDesc");
610                 goto cont;
611             }
612
613             /* Yuck, XML.  Parse out the devices. */
614             xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET);
615             if (xml_doc == NULL) {
616                 VIRT_ERROR (conn, "xmlReadDoc");
617                 goto cont;
618             }
619
620             xpath_ctx = xmlXPathNewContext (xml_doc);
621
622             /* Block devices. */
623             xpath_obj = xmlXPathEval
624                 ((xmlChar *) "/domain/devices/disk/target[@dev]",
625                  xpath_ctx);
626             if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
627                 xpath_obj->nodesetval == NULL)
628                 goto cont;
629
630             for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
631                 xmlNodePtr node;
632                 char *path = NULL;
633
634                 node = xpath_obj->nodesetval->nodeTab[j];
635                 if (!node) continue;
636                 path = (char *) xmlGetProp (node, (xmlChar *) "dev");
637                 if (!path) continue;
638
639                 if (il_block_devices &&
640                     ignore_device_match (il_block_devices, name, path) != 0)
641                     goto cont2;
642
643                 add_block_device (dom, path);
644             cont2:
645                 if (path) xmlFree (path);
646             }
647             xmlXPathFreeObject (xpath_obj);
648
649             /* Network interfaces. */
650             xpath_obj = xmlXPathEval
651                 ((xmlChar *) "/domain/devices/interface[target[@dev]]",
652                  xpath_ctx);
653             if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
654                 xpath_obj->nodesetval == NULL)
655                 goto cont;
656
657             xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
658
659             for (j = 0; j < xml_interfaces->nodeNr; ++j) {
660                 char *path = NULL;
661                 char *address = NULL;
662                 xmlNodePtr xml_interface;
663
664                 xml_interface = xml_interfaces->nodeTab[j];
665                 if (!xml_interface) continue;
666                 xmlNodePtr child = NULL;
667
668                 for (child = xml_interface->children; child; child = child->next) {
669                     if (child->type != XML_ELEMENT_NODE) continue;
670
671                     if (xmlStrEqual(child->name, (const xmlChar *) "target")) {
672                         path = (char *) xmlGetProp (child, (const xmlChar *) "dev");
673                         if (!path) continue;
674                     } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) {
675                         address = (char *) xmlGetProp (child, (const xmlChar *) "address");
676                         if (!address) continue;
677                     }
678                 }
679
680                 if (il_interface_devices &&
681                     (ignore_device_match (il_interface_devices, name, path) != 0 ||
682                      ignore_device_match (il_interface_devices, name, address) != 0))
683                     goto cont3;
684
685                 add_interface_device (dom, path, address, j+1);
686                 cont3:
687                     if (path) xmlFree (path);
688                     if (address) xmlFree (address);
689             }
690
691         cont:
692             if (xpath_obj) xmlXPathFreeObject (xpath_obj);
693             if (xpath_ctx) xmlXPathFreeContext (xpath_ctx);
694             if (xml_doc) xmlFreeDoc (xml_doc);
695             sfree (xml);
696         }
697
698         sfree (domids);
699     }
700
701     return 0;
702 }
703
704 static void
705 free_domains ()
706 {
707     int i;
708
709     if (domains) {
710         for (i = 0; i < nr_domains; ++i)
711             virDomainFree (domains[i]);
712         sfree (domains);
713     }
714     domains = NULL;
715     nr_domains = 0;
716 }
717
718 static int
719 add_domain (virDomainPtr dom)
720 {
721     virDomainPtr *new_ptr;
722     int new_size = sizeof (domains[0]) * (nr_domains+1);
723
724     if (domains)
725         new_ptr = realloc (domains, new_size);
726     else
727         new_ptr = malloc (new_size);
728
729     if (new_ptr == NULL)
730         return -1;
731
732     domains = new_ptr;
733     domains[nr_domains] = dom;
734     return nr_domains++;
735 }
736
737 static void
738 free_block_devices ()
739 {
740     int i;
741
742     if (block_devices) {
743         for (i = 0; i < nr_block_devices; ++i)
744             sfree (block_devices[i].path);
745         sfree (block_devices);
746     }
747     block_devices = NULL;
748     nr_block_devices = 0;
749 }
750
751 static int
752 add_block_device (virDomainPtr dom, const char *path)
753 {
754     struct block_device *new_ptr;
755     int new_size = sizeof (block_devices[0]) * (nr_block_devices+1);
756     char *path_copy;
757
758     path_copy = strdup (path);
759     if (!path_copy)
760         return -1;
761
762     if (block_devices)
763         new_ptr = realloc (block_devices, new_size);
764     else
765         new_ptr = malloc (new_size);
766
767     if (new_ptr == NULL) {
768         sfree (path_copy);
769         return -1;
770     }
771     block_devices = new_ptr;
772     block_devices[nr_block_devices].dom = dom;
773     block_devices[nr_block_devices].path = path_copy;
774     return nr_block_devices++;
775 }
776
777 static void
778 free_interface_devices ()
779 {
780     int i;
781
782     if (interface_devices) {
783         for (i = 0; i < nr_interface_devices; ++i) {
784             sfree (interface_devices[i].path);
785             sfree (interface_devices[i].address);
786             sfree (interface_devices[i].number);
787         }
788         sfree (interface_devices);
789     }
790     interface_devices = NULL;
791     nr_interface_devices = 0;
792 }
793
794 static int
795 add_interface_device (virDomainPtr dom, const char *path, const char *address, unsigned int number)
796 {
797     struct interface_device *new_ptr;
798     int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1);
799     char *path_copy, *address_copy, number_string[15];
800
801     if ((path == NULL) || (address == NULL))
802         return EINVAL;
803
804     path_copy = strdup (path);
805     if (!path_copy) return -1;
806
807     address_copy = strdup (address);
808     if (!address_copy) {
809         sfree(path_copy);
810         return -1;
811     }
812
813     snprintf(number_string, sizeof (number_string), "interface-%u", number);
814
815     if (interface_devices)
816         new_ptr = realloc (interface_devices, new_size);
817     else
818         new_ptr = malloc (new_size);
819
820     if (new_ptr == NULL) {
821         sfree (path_copy);
822         sfree (address_copy);
823         return -1;
824     }
825     interface_devices = new_ptr;
826     interface_devices[nr_interface_devices].dom = dom;
827     interface_devices[nr_interface_devices].path = path_copy;
828     interface_devices[nr_interface_devices].address = address_copy;
829     interface_devices[nr_interface_devices].number = strdup(number_string);
830     return nr_interface_devices++;
831 }
832
833 static int
834 ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
835 {
836     char *name;
837     int n, r;
838
839     if ((domname == NULL) || (devpath == NULL))
840         return 0;
841
842     n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2);
843     name = malloc (n);
844     if (name == NULL) {
845         ERROR ("libvirt plugin: malloc failed.");
846         return 0;
847     }
848     ssnprintf (name, n, "%s:%s", domname, devpath);
849     r = ignorelist_match (il, name);
850     sfree (name);
851     return r;
852 }
853
854 static int
855 lv_shutdown (void)
856 {
857     free_block_devices ();
858     free_interface_devices ();
859     free_domains ();
860
861     if (conn != NULL)
862         virConnectClose (conn);
863     conn = NULL;
864
865     ignorelist_free (il_domains);
866     il_domains = NULL;
867     ignorelist_free (il_block_devices);
868     il_block_devices = NULL;
869     ignorelist_free (il_interface_devices);
870     il_interface_devices = NULL;
871
872     return 0;
873 }
874
875 void
876 module_register (void)
877 {
878     plugin_register_config ("libvirt",
879     lv_config,
880     config_keys, NR_CONFIG_KEYS);
881     plugin_register_init ("libvirt", lv_init);
882     plugin_register_read ("libvirt", lv_read);
883     plugin_register_shutdown ("libvirt", lv_shutdown);
884 }
885
886 /*
887  * vim: shiftwidth=4 tabstop=8 softtabstop=4 expandtab fdm=marker
888  */