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