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