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