2 * collectd - src/virt.c
3 * Copyright (C) 2006-2008 Red Hat Inc.
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.
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.
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
19 * Richard W.M. Jones <rjones@redhat.com>
20 * Przemyslaw Szczerbik <przemyslawx.szczerbik@intel.com>
27 #include "utils_complain.h"
28 #include "utils_ignorelist.h"
30 #include <libgen.h> /* for basename(3) */
31 #include <libvirt/libvirt.h>
32 #include <libvirt/virterror.h>
33 #include <libxml/parser.h>
34 #include <libxml/tree.h>
35 #include <libxml/xpath.h>
36 #include <libxml/xpathInternals.h>
39 #define PLUGIN_NAME "virt"
41 #ifdef LIBVIR_CHECK_VERSION
43 #if LIBVIR_CHECK_VERSION(0, 9, 2)
44 #define HAVE_DOM_REASON 1
47 #if LIBVIR_CHECK_VERSION(0, 9, 5)
48 #define HAVE_BLOCK_STATS_FLAGS 1
49 #define HAVE_DOM_REASON_PAUSED_SHUTTING_DOWN 1
52 #if LIBVIR_CHECK_VERSION(0, 9, 10)
53 #define HAVE_DISK_ERR 1
56 #if LIBVIR_CHECK_VERSION(0, 9, 11)
57 #define HAVE_CPU_STATS 1
58 #define HAVE_DOM_STATE_PMSUSPENDED 1
59 #define HAVE_DOM_REASON_RUNNING_WAKEUP 1
63 virConnectListAllDomains() appeared in 0.10.2
64 Note that LIBVIR_CHECK_VERSION appeared a year later, so
65 in some systems which actually have virConnectListAllDomains()
68 #ifdef LIBVIR_CHECK_VERSION
69 #if LIBVIR_CHECK_VERSION(0, 10, 2)
70 #define HAVE_LIST_ALL_DOMAINS 1
74 #if LIBVIR_CHECK_VERSION(1, 0, 1)
75 #define HAVE_DOM_REASON_PAUSED_SNAPSHOT 1
78 #if LIBVIR_CHECK_VERSION(1, 1, 1)
79 #define HAVE_DOM_REASON_PAUSED_CRASHED 1
82 #if LIBVIR_CHECK_VERSION(1, 2, 9)
83 #define HAVE_JOB_STATS 1
86 #if LIBVIR_CHECK_VERSION(1, 2, 10)
87 #define HAVE_DOM_REASON_CRASHED 1
90 #if LIBVIR_CHECK_VERSION(1, 2, 11)
91 #define HAVE_FS_INFO 1
94 #if LIBVIR_CHECK_VERSION(1, 2, 15)
95 #define HAVE_DOM_REASON_PAUSED_STARTING_UP 1
98 #if LIBVIR_CHECK_VERSION(1, 3, 3)
99 #define HAVE_PERF_STATS 1
100 #define HAVE_DOM_REASON_POSTCOPY 1
103 #endif /* LIBVIR_CHECK_VERSION */
105 static const char *config_keys[] = {"Connection",
112 "BlockDeviceFormatBasename",
119 "PluginInstanceFormat",
123 "PersistentNotification",
126 /* PersistentNotification is false by default */
127 static _Bool persistent_notification = 0;
129 /* libvirt event loop */
130 static pthread_t event_loop_tid;
132 static int domain_event_cb_id;
134 const char *domain_states[] = {
135 [VIR_DOMAIN_NOSTATE] = "no state",
136 [VIR_DOMAIN_RUNNING] = "the domain is running",
137 [VIR_DOMAIN_BLOCKED] = "the domain is blocked on resource",
138 [VIR_DOMAIN_PAUSED] = "the domain is paused by user",
139 [VIR_DOMAIN_SHUTDOWN] = "the domain is being shut down",
140 [VIR_DOMAIN_SHUTOFF] = "the domain is shut off",
141 [VIR_DOMAIN_CRASHED] = "the domain is crashed",
142 #ifdef HAVE_DOM_STATE_PMSUSPENDED
143 [VIR_DOMAIN_PMSUSPENDED] =
144 "the domain is suspended by guest power management",
148 static int map_domain_event_to_state(int event) {
151 case VIR_DOMAIN_EVENT_STARTED:
152 ret = VIR_DOMAIN_RUNNING;
154 case VIR_DOMAIN_EVENT_SUSPENDED:
155 ret = VIR_DOMAIN_PAUSED;
157 case VIR_DOMAIN_EVENT_RESUMED:
158 ret = VIR_DOMAIN_RUNNING;
160 case VIR_DOMAIN_EVENT_STOPPED:
161 ret = VIR_DOMAIN_SHUTOFF;
163 case VIR_DOMAIN_EVENT_SHUTDOWN:
164 ret = VIR_DOMAIN_SHUTDOWN;
166 case VIR_DOMAIN_EVENT_PMSUSPENDED:
167 ret = VIR_DOMAIN_PMSUSPENDED;
169 case VIR_DOMAIN_EVENT_CRASHED:
170 ret = VIR_DOMAIN_CRASHED;
173 ret = VIR_DOMAIN_NOSTATE;
178 static int map_domain_event_detail_to_reason(int event, int detail) {
181 case VIR_DOMAIN_EVENT_STARTED:
183 case VIR_DOMAIN_EVENT_STARTED_BOOTED: /* Normal startup from boot */
184 ret = VIR_DOMAIN_RUNNING_BOOTED;
186 case VIR_DOMAIN_EVENT_STARTED_MIGRATED: /* Incoming migration from another
188 ret = VIR_DOMAIN_RUNNING_MIGRATED;
190 case VIR_DOMAIN_EVENT_STARTED_RESTORED: /* Restored from a state file */
191 ret = VIR_DOMAIN_RUNNING_RESTORED;
193 case VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT: /* Restored from snapshot */
194 ret = VIR_DOMAIN_RUNNING_FROM_SNAPSHOT;
196 case VIR_DOMAIN_EVENT_STARTED_WAKEUP: /* Started due to wakeup event */
197 ret = VIR_DOMAIN_RUNNING_WAKEUP;
200 ret = VIR_DOMAIN_RUNNING_UNKNOWN;
203 case VIR_DOMAIN_EVENT_SUSPENDED:
205 case VIR_DOMAIN_EVENT_SUSPENDED_PAUSED: /* Normal suspend due to admin
207 ret = VIR_DOMAIN_PAUSED_USER;
209 case VIR_DOMAIN_EVENT_SUSPENDED_MIGRATED: /* Suspended for offline
211 ret = VIR_DOMAIN_PAUSED_MIGRATION;
213 case VIR_DOMAIN_EVENT_SUSPENDED_IOERROR: /* Suspended due to a disk I/O
215 ret = VIR_DOMAIN_PAUSED_IOERROR;
217 case VIR_DOMAIN_EVENT_SUSPENDED_WATCHDOG: /* Suspended due to a watchdog
219 ret = VIR_DOMAIN_PAUSED_WATCHDOG;
221 case VIR_DOMAIN_EVENT_SUSPENDED_RESTORED: /* Restored from paused state
223 ret = VIR_DOMAIN_PAUSED_UNKNOWN;
225 case VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT: /* Restored from paused
227 ret = VIR_DOMAIN_PAUSED_FROM_SNAPSHOT;
229 case VIR_DOMAIN_EVENT_SUSPENDED_API_ERROR: /* Suspended after failure during
231 ret = VIR_DOMAIN_PAUSED_UNKNOWN;
233 case VIR_DOMAIN_EVENT_SUSPENDED_POSTCOPY: /* Suspended for post-copy
235 ret = VIR_DOMAIN_PAUSED_POSTCOPY;
237 case VIR_DOMAIN_EVENT_SUSPENDED_POSTCOPY_FAILED: /* Suspended after failed
239 ret = VIR_DOMAIN_PAUSED_POSTCOPY_FAILED;
242 ret = VIR_DOMAIN_PAUSED_UNKNOWN;
245 case VIR_DOMAIN_EVENT_RESUMED:
247 case VIR_DOMAIN_EVENT_RESUMED_UNPAUSED: /* Normal resume due to admin
249 ret = VIR_DOMAIN_RUNNING_UNPAUSED;
251 case VIR_DOMAIN_EVENT_RESUMED_MIGRATED: /* Resumed for completion of
253 ret = VIR_DOMAIN_RUNNING_MIGRATED;
255 case VIR_DOMAIN_EVENT_RESUMED_FROM_SNAPSHOT: /* Resumed from snapshot */
256 ret = VIR_DOMAIN_RUNNING_FROM_SNAPSHOT;
258 case VIR_DOMAIN_EVENT_RESUMED_POSTCOPY: /* Resumed, but migration is still
259 running in post-copy mode */
260 ret = VIR_DOMAIN_RUNNING_POSTCOPY;
263 ret = VIR_DOMAIN_RUNNING_UNKNOWN;
266 case VIR_DOMAIN_EVENT_STOPPED:
268 case VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN: /* Normal shutdown */
269 ret = VIR_DOMAIN_SHUTOFF_SHUTDOWN;
271 case VIR_DOMAIN_EVENT_STOPPED_DESTROYED: /* Forced poweroff from host */
272 ret = VIR_DOMAIN_SHUTOFF_DESTROYED;
274 case VIR_DOMAIN_EVENT_STOPPED_CRASHED: /* Guest crashed */
275 ret = VIR_DOMAIN_SHUTOFF_CRASHED;
277 case VIR_DOMAIN_EVENT_STOPPED_MIGRATED: /* Migrated off to another host */
278 ret = VIR_DOMAIN_SHUTOFF_MIGRATED;
280 case VIR_DOMAIN_EVENT_STOPPED_SAVED: /* Saved to a state file */
281 ret = VIR_DOMAIN_SHUTOFF_SAVED;
283 case VIR_DOMAIN_EVENT_STOPPED_FAILED: /* Host emulator/mgmt failed */
284 ret = VIR_DOMAIN_SHUTOFF_FAILED;
286 case VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT: /* Offline snapshot loaded */
287 ret = VIR_DOMAIN_SHUTOFF_FROM_SNAPSHOT;
290 ret = VIR_DOMAIN_SHUTOFF_UNKNOWN;
293 case VIR_DOMAIN_EVENT_SHUTDOWN:
295 case VIR_DOMAIN_EVENT_SHUTDOWN_FINISHED: /* Guest finished shutdown
297 ret = VIR_DOMAIN_SHUTDOWN_USER;
300 ret = VIR_DOMAIN_SHUTDOWN_UNKNOWN;
303 case VIR_DOMAIN_EVENT_PMSUSPENDED:
305 case VIR_DOMAIN_EVENT_PMSUSPENDED_MEMORY: /* Guest was PM suspended to
307 ret = VIR_DOMAIN_PMSUSPENDED_UNKNOWN;
309 case VIR_DOMAIN_EVENT_PMSUSPENDED_DISK: /* Guest was PM suspended to disk */
310 ret = VIR_DOMAIN_PMSUSPENDED_DISK_UNKNOWN;
313 ret = VIR_DOMAIN_PMSUSPENDED_UNKNOWN;
316 case VIR_DOMAIN_EVENT_CRASHED:
318 case VIR_DOMAIN_EVENT_CRASHED_PANICKED: /* Guest was panicked */
319 ret = VIR_DOMAIN_CRASHED_PANICKED;
322 ret = VIR_DOMAIN_CRASHED_UNKNOWN;
326 ret = VIR_DOMAIN_NOSTATE_UNKNOWN;
331 #ifdef HAVE_DOM_REASON
332 #define DOMAIN_STATE_REASON_MAX_SIZE 20
333 const char *domain_reasons[][DOMAIN_STATE_REASON_MAX_SIZE] = {
334 [VIR_DOMAIN_NOSTATE][VIR_DOMAIN_NOSTATE_UNKNOWN] =
335 "the reason is unknown",
337 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_UNKNOWN] =
338 "the reason is unknown",
339 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_BOOTED] =
340 "normal startup from boot",
341 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_MIGRATED] =
342 "migrated from another host",
343 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_RESTORED] =
344 "restored from a state file",
345 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_FROM_SNAPSHOT] =
346 "restored from snapshot",
347 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_UNPAUSED] =
348 "returned from paused state",
349 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_MIGRATION_CANCELED] =
350 "returned from migration",
351 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_SAVE_CANCELED] =
352 "returned from failed save process",
353 #ifdef HAVE_DOM_REASON_RUNNING_WAKEUP
354 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_WAKEUP] =
355 "returned from pmsuspended due to wakeup event",
357 #ifdef HAVE_DOM_REASON_CRASHED
358 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_CRASHED] =
359 "resumed from crashed",
361 #ifdef HAVE_DOM_REASON_POSTCOPY
362 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_POSTCOPY] =
363 "running in post-copy migration mode",
366 [VIR_DOMAIN_BLOCKED][VIR_DOMAIN_BLOCKED_UNKNOWN] =
367 "the reason is unknown",
369 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_UNKNOWN] =
370 "the reason is unknown",
371 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_USER] = "paused on user request",
372 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_MIGRATION] =
373 "paused for offline migration",
374 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_SAVE] = "paused for save",
375 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_DUMP] =
376 "paused for offline core dump",
377 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_IOERROR] =
378 "paused due to a disk I/O error",
379 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_WATCHDOG] =
380 "paused due to a watchdog event",
381 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_FROM_SNAPSHOT] =
382 "paused after restoring from snapshot",
383 #ifdef HAVE_DOM_REASON_PAUSED_SHUTTING_DOWN
384 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_SHUTTING_DOWN] =
385 "paused during shutdown process",
387 #ifdef HAVE_DOM_REASON_PAUSED_SNAPSHOT
388 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_SNAPSHOT] =
389 "paused while creating a snapshot",
391 #ifdef HAVE_DOM_REASON_PAUSED_CRASHED
392 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_CRASHED] =
393 "paused due to a guest crash",
395 #ifdef HAVE_DOM_REASON_PAUSED_STARTING_UP
396 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_STARTING_UP] =
397 "the domain is being started",
399 #ifdef HAVE_DOM_REASON_POSTCOPY
400 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_POSTCOPY] =
401 "paused for post-copy migration",
402 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_POSTCOPY_FAILED] =
403 "paused after failed post-copy",
406 [VIR_DOMAIN_SHUTDOWN][VIR_DOMAIN_SHUTDOWN_UNKNOWN] =
407 "the reason is unknown",
408 [VIR_DOMAIN_SHUTDOWN][VIR_DOMAIN_SHUTDOWN_USER] =
409 "shutting down on user request",
411 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_UNKNOWN] =
412 "the reason is unknown",
413 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_SHUTDOWN] = "normal shutdown",
414 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_DESTROYED] = "forced poweroff",
415 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_CRASHED] = "domain crashed",
416 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_MIGRATED] =
417 "migrated to another host",
418 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_SAVED] = "saved to a file",
419 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_FAILED] =
420 "domain failed to start",
421 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_FROM_SNAPSHOT] =
422 "restored from a snapshot which was taken while domain was shutoff",
424 [VIR_DOMAIN_CRASHED][VIR_DOMAIN_CRASHED_UNKNOWN] =
425 "the reason is unknown",
426 #ifdef VIR_DOMAIN_CRASHED_PANICKED
427 [VIR_DOMAIN_CRASHED][VIR_DOMAIN_CRASHED_PANICKED] = "domain panicked",
430 #ifdef HAVE_DOM_STATE_PMSUSPENDED
431 [VIR_DOMAIN_PMSUSPENDED][VIR_DOMAIN_PMSUSPENDED_UNKNOWN] =
432 "the reason is unknown",
435 #endif /* HAVE_DOM_REASON */
437 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
438 #define NANOSEC_IN_SEC 1e9
440 #define GET_STATS(_f, _name, ...) \
442 status = _f(__VA_ARGS__); \
444 ERROR(PLUGIN_NAME ": Failed to get " _name); \
448 static virConnectPtr conn = 0;
449 static char *conn_string = NULL;
450 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
452 /* Node information required for %CPU */
453 static virNodeInfo nodeinfo;
455 /* Seconds between list refreshes, 0 disables completely. */
456 static int interval = 60;
458 /* List of domains, if specified. */
459 static ignorelist_t *il_domains = NULL;
460 /* List of block devices, if specified. */
461 static ignorelist_t *il_block_devices = NULL;
462 /* List of network interface devices, if specified. */
463 static ignorelist_t *il_interface_devices = NULL;
465 static int ignore_device_match(ignorelist_t *, const char *domname,
466 const char *devpath);
468 /* Actual list of block devices found on last refresh. */
469 struct block_device {
470 virDomainPtr dom; /* domain */
471 char *path; /* name of block device */
474 /* Actual list of network interfaces found on last refresh. */
475 struct interface_device {
476 virDomainPtr dom; /* domain */
477 char *path; /* name of interface device */
478 char *address; /* mac address of interface device */
479 char *number; /* interface device number */
482 typedef struct domain_s {
488 struct lv_read_state {
489 /* Actual list of domains found on last refresh. */
493 struct block_device *block_devices;
494 int nr_block_devices;
496 struct interface_device *interface_devices;
497 int nr_interface_devices;
500 static void free_domains(struct lv_read_state *state);
501 static int add_domain(struct lv_read_state *state, virDomainPtr dom,
504 static void free_block_devices(struct lv_read_state *state);
505 static int add_block_device(struct lv_read_state *state, virDomainPtr dom,
508 static void free_interface_devices(struct lv_read_state *state);
509 static int add_interface_device(struct lv_read_state *state, virDomainPtr dom,
510 const char *path, const char *address,
511 unsigned int number);
513 #define METADATA_VM_PARTITION_URI "http://ovirt.org/ovirtmap/tag/1.0"
514 #define METADATA_VM_PARTITION_ELEMENT "tag"
515 #define METADATA_VM_PARTITION_PREFIX "ovirtmap"
517 #define BUFFER_MAX_LEN 256
518 #define PARTITION_TAG_MAX_LEN 32
520 struct lv_read_instance {
521 struct lv_read_state read_state;
522 char tag[PARTITION_TAG_MAX_LEN];
526 struct lv_user_data {
527 struct lv_read_instance inst;
531 #define NR_INSTANCES_DEFAULT 1
532 #define NR_INSTANCES_MAX 128
533 static int nr_instances = NR_INSTANCES_DEFAULT;
534 static struct lv_user_data lv_read_user_data[NR_INSTANCES_MAX];
536 /* HostnameFormat. */
537 #define HF_MAX_FIELDS 3
539 enum hf_field { hf_none = 0, hf_hostname, hf_name, hf_uuid };
541 static enum hf_field hostname_format[HF_MAX_FIELDS] = {hf_name};
543 /* PluginInstanceFormat */
544 #define PLGINST_MAX_FIELDS 2
546 enum plginst_field { plginst_none = 0, plginst_name, plginst_uuid };
548 static enum plginst_field plugin_instance_format[PLGINST_MAX_FIELDS] = {
551 /* BlockDeviceFormat */
552 enum bd_field { target, source };
554 /* InterfaceFormat. */
555 enum if_field { if_address, if_name, if_number };
558 #define EX_STATS_MAX_FIELDS 15
561 ex_stats_disk = 1 << 0,
562 ex_stats_pcpu = 1 << 1,
563 ex_stats_cpu_util = 1 << 2,
564 ex_stats_domain_state = 1 << 3,
565 #ifdef HAVE_PERF_STATS
566 ex_stats_perf = 1 << 4,
568 ex_stats_vcpupin = 1 << 5,
570 ex_stats_disk_err = 1 << 6,
573 ex_stats_fs_info = 1 << 7,
575 #ifdef HAVE_JOB_STATS
576 ex_stats_job_stats_completed = 1 << 8,
577 ex_stats_job_stats_background = 1 << 9,
581 static unsigned int extra_stats = ex_stats_none;
583 struct ex_stats_item {
587 static const struct ex_stats_item ex_stats_table[] = {
588 {"disk", ex_stats_disk},
589 {"pcpu", ex_stats_pcpu},
590 {"cpu_util", ex_stats_cpu_util},
591 {"domain_state", ex_stats_domain_state},
592 #ifdef HAVE_PERF_STATS
593 {"perf", ex_stats_perf},
595 {"vcpupin", ex_stats_vcpupin},
597 {"disk_err", ex_stats_disk_err},
600 {"fs_info", ex_stats_fs_info},
602 #ifdef HAVE_JOB_STATS
603 {"job_stats_completed", ex_stats_job_stats_completed},
604 {"job_stats_background", ex_stats_job_stats_background},
606 {NULL, ex_stats_none},
609 /* BlockDeviceFormatBasename */
610 _Bool blockdevice_format_basename = 0;
611 static enum bd_field blockdevice_format = target;
612 static enum if_field interface_format = if_name;
614 /* Time that we last refreshed. */
615 static time_t last_refresh = (time_t)0;
617 static int refresh_lists(struct lv_read_instance *inst);
621 unsigned long long total_user_cpu_time;
622 unsigned long long total_syst_cpu_time;
625 struct lv_block_info {
626 virDomainBlockStatsStruct bi;
628 long long rd_total_times;
629 long long wr_total_times;
632 long long fl_total_times;
635 static void init_block_info(struct lv_block_info *binfo) {
639 binfo->bi.rd_req = -1;
640 binfo->bi.wr_req = -1;
641 binfo->bi.rd_bytes = -1;
642 binfo->bi.wr_bytes = -1;
644 binfo->rd_total_times = -1;
645 binfo->wr_total_times = -1;
647 binfo->fl_total_times = -1;
650 #ifdef HAVE_BLOCK_STATS_FLAGS
652 #define GET_BLOCK_INFO_VALUE(NAME, FIELD) \
653 if (!strcmp(param[i].field, NAME)) { \
654 binfo->FIELD = param[i].value.l; \
658 static int get_block_info(struct lv_block_info *binfo,
659 virTypedParameterPtr param, int nparams) {
660 if (binfo == NULL || param == NULL)
663 for (int i = 0; i < nparams; ++i) {
664 /* ignore type. Everything must be LLONG anyway. */
665 GET_BLOCK_INFO_VALUE("rd_operations", bi.rd_req);
666 GET_BLOCK_INFO_VALUE("wr_operations", bi.wr_req);
667 GET_BLOCK_INFO_VALUE("rd_bytes", bi.rd_bytes);
668 GET_BLOCK_INFO_VALUE("wr_bytes", bi.wr_bytes);
669 GET_BLOCK_INFO_VALUE("rd_total_times", rd_total_times);
670 GET_BLOCK_INFO_VALUE("wr_total_times", wr_total_times);
671 GET_BLOCK_INFO_VALUE("flush_operations", fl_req);
672 GET_BLOCK_INFO_VALUE("flush_total_times", fl_total_times);
678 #undef GET_BLOCK_INFO_VALUE
680 #endif /* HAVE_BLOCK_STATS_FLAGS */
682 /* ERROR(...) macro for virterrors. */
683 #define VIRT_ERROR(conn, s) \
686 err = (conn) ? virConnGetLastError((conn)) : virGetLastError(); \
688 ERROR("%s: %s", (s), err->message); \
691 static void init_lv_info(struct lv_info *info) {
693 memset(info, 0, sizeof(*info));
696 static int lv_domain_info(virDomainPtr dom, struct lv_info *info) {
697 #ifdef HAVE_CPU_STATS
698 virTypedParameterPtr param = NULL;
700 #endif /* HAVE_CPU_STATS */
701 int ret = virDomainGetInfo(dom, &(info->di));
706 #ifdef HAVE_CPU_STATS
707 nparams = virDomainGetCPUStats(dom, NULL, 0, -1, 1, 0);
709 VIRT_ERROR(conn, "getting the CPU params count");
713 param = calloc(nparams, sizeof(virTypedParameter));
715 ERROR("virt plugin: alloc(%i) for cpu parameters failed.", nparams);
719 ret = virDomainGetCPUStats(dom, param, nparams, -1, 1, 0); // total stats.
721 virTypedParamsClear(param, nparams);
723 VIRT_ERROR(conn, "getting the disk params values");
727 for (int i = 0; i < nparams; ++i) {
728 if (!strcmp(param[i].field, "user_time"))
729 info->total_user_cpu_time = param[i].value.ul;
730 else if (!strcmp(param[i].field, "system_time"))
731 info->total_syst_cpu_time = param[i].value.ul;
734 virTypedParamsClear(param, nparams);
736 #endif /* HAVE_CPU_STATS */
741 static void init_value_list(value_list_t *vl, virDomainPtr dom) {
744 char uuid[VIR_UUID_STRING_BUFLEN];
746 sstrncpy(vl->plugin, PLUGIN_NAME, sizeof(vl->plugin));
750 /* Construct the hostname field according to HostnameFormat. */
751 for (int i = 0; i < HF_MAX_FIELDS; ++i) {
752 if (hostname_format[i] == hf_none)
755 n = DATA_MAX_NAME_LEN - strlen(vl->host) - 2;
757 if (i > 0 && n >= 1) {
758 strncat(vl->host, ":", 1);
762 switch (hostname_format[i]) {
766 strncat(vl->host, hostname_g, n);
769 name = virDomainGetName(dom);
771 strncat(vl->host, name, n);
774 if (virDomainGetUUIDString(dom, uuid) == 0)
775 strncat(vl->host, uuid, n);
780 vl->host[sizeof(vl->host) - 1] = '\0';
782 /* Construct the plugin instance field according to PluginInstanceFormat. */
783 for (int i = 0; i < PLGINST_MAX_FIELDS; ++i) {
784 if (plugin_instance_format[i] == plginst_none)
787 n = sizeof(vl->plugin_instance) - strlen(vl->plugin_instance) - 2;
789 if (i > 0 && n >= 1) {
790 strncat(vl->plugin_instance, ":", 1);
794 switch (plugin_instance_format[i]) {
798 name = virDomainGetName(dom);
800 strncat(vl->plugin_instance, name, n);
803 if (virDomainGetUUIDString(dom, uuid) == 0)
804 strncat(vl->plugin_instance, uuid, n);
809 vl->plugin_instance[sizeof(vl->plugin_instance) - 1] = '\0';
811 } /* void init_value_list */
813 static int init_notif(notification_t *notif, const virDomainPtr domain,
814 int severity, const char *msg, const char *type,
815 const char *type_instance) {
816 value_list_t vl = VALUE_LIST_INIT;
819 ERROR(PLUGIN_NAME ": init_notif: NULL pointer");
823 init_value_list(&vl, domain);
824 notification_init(notif, severity, msg, vl.host, vl.plugin,
825 vl.plugin_instance, type, type_instance);
826 notif->time = cdtime();
830 static void submit_notif(const virDomainPtr domain, int severity,
831 const char *msg, const char *type,
832 const char *type_instance) {
833 notification_t notif;
835 init_notif(¬if, domain, severity, msg, type, type_instance);
836 plugin_dispatch_notification(¬if);
838 plugin_notification_meta_free(notif.meta);
841 static void submit(virDomainPtr dom, char const *type,
842 char const *type_instance, value_t *values,
844 value_list_t vl = VALUE_LIST_INIT;
845 init_value_list(&vl, dom);
848 vl.values_len = values_len;
850 sstrncpy(vl.type, type, sizeof(vl.type));
851 if (type_instance != NULL)
852 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
854 plugin_dispatch_values(&vl);
857 static void memory_submit(virDomainPtr dom, gauge_t value) {
858 submit(dom, "memory", "total", &(value_t){.gauge = value}, 1);
861 static void memory_stats_submit(gauge_t value, virDomainPtr dom,
863 static const char *tags[] = {"swap_in", "swap_out", "major_fault",
864 "minor_fault", "unused", "available",
865 "actual_balloon", "rss", "usable",
868 if ((tag_index < 0) || (tag_index >= (int)STATIC_ARRAY_SIZE(tags))) {
869 ERROR("virt plugin: Array index out of bounds: tag_index = %d", tag_index);
873 submit(dom, "memory", tags[tag_index], &(value_t){.gauge = value}, 1);
876 static void submit_derive2(const char *type, derive_t v0, derive_t v1,
877 virDomainPtr dom, const char *devname) {
879 {.derive = v0}, {.derive = v1},
882 submit(dom, type, devname, values, STATIC_ARRAY_SIZE(values));
883 } /* void submit_derive2 */
885 static void pcpu_submit(virDomainPtr dom, struct lv_info *info) {
886 #ifdef HAVE_CPU_STATS
887 if (extra_stats & ex_stats_pcpu)
888 submit_derive2("ps_cputime", info->total_user_cpu_time,
889 info->total_syst_cpu_time, dom, NULL);
890 #endif /* HAVE_CPU_STATS */
893 static double cpu_ns_to_percent(unsigned int node_cpus,
894 unsigned long long cpu_time_old,
895 unsigned long long cpu_time_new) {
896 double percent = 0.0;
897 unsigned long long cpu_time_diff = 0;
898 double time_diff_sec = CDTIME_T_TO_DOUBLE(plugin_get_interval());
900 if (node_cpus != 0 && time_diff_sec != 0 && cpu_time_old != 0) {
901 cpu_time_diff = cpu_time_new - cpu_time_old;
902 percent = ((double)(100 * cpu_time_diff)) /
903 (time_diff_sec * node_cpus * NANOSEC_IN_SEC);
906 DEBUG(PLUGIN_NAME ": node_cpus=%u cpu_time_old=%" PRIu64
907 " cpu_time_new=%" PRIu64 "cpu_time_diff=%" PRIu64
908 " time_diff_sec=%f percent=%f",
909 node_cpus, (uint64_t)cpu_time_old, (uint64_t)cpu_time_new,
910 (uint64_t)cpu_time_diff, time_diff_sec, percent);
915 static void cpu_submit(const domain_t *dom, unsigned long long cpuTime_new) {
920 if (extra_stats & ex_stats_cpu_util) {
921 /* Computing %CPU requires 2 samples of cpuTime */
922 if (dom->info.cpuTime != 0 && cpuTime_new != 0) {
924 submit(dom->ptr, "percent", "virt_cpu_total",
925 &(value_t){.gauge = cpu_ns_to_percent(
926 nodeinfo.cpus, dom->info.cpuTime, cpuTime_new)},
931 submit(dom->ptr, "virt_cpu_total", NULL, &(value_t){.derive = cpuTime_new},
935 static void vcpu_submit(derive_t value, virDomainPtr dom, int vcpu_nr,
937 char type_instance[DATA_MAX_NAME_LEN];
939 snprintf(type_instance, sizeof(type_instance), "%d", vcpu_nr);
940 submit(dom, type, type_instance, &(value_t){.derive = value}, 1);
943 static void disk_submit(struct lv_block_info *binfo, virDomainPtr dom,
945 char *dev_copy = strdup(dev);
946 const char *type_instance = dev_copy;
951 if (blockdevice_format_basename && blockdevice_format == source)
952 type_instance = basename(dev_copy);
954 if (!type_instance) {
959 char flush_type_instance[DATA_MAX_NAME_LEN];
960 snprintf(flush_type_instance, sizeof(flush_type_instance), "flush-%s",
963 if ((binfo->bi.rd_req != -1) && (binfo->bi.wr_req != -1))
964 submit_derive2("disk_ops", (derive_t)binfo->bi.rd_req,
965 (derive_t)binfo->bi.wr_req, dom, type_instance);
967 if ((binfo->bi.rd_bytes != -1) && (binfo->bi.wr_bytes != -1))
968 submit_derive2("disk_octets", (derive_t)binfo->bi.rd_bytes,
969 (derive_t)binfo->bi.wr_bytes, dom, type_instance);
971 if (extra_stats & ex_stats_disk) {
972 if ((binfo->rd_total_times != -1) && (binfo->wr_total_times != -1))
973 submit_derive2("disk_time", (derive_t)binfo->rd_total_times,
974 (derive_t)binfo->wr_total_times, dom, type_instance);
976 if (binfo->fl_req != -1)
977 submit(dom, "total_requests", flush_type_instance,
978 &(value_t){.derive = (derive_t)binfo->fl_req}, 1);
979 if (binfo->fl_total_times != -1) {
980 derive_t value = binfo->fl_total_times / 1000; // ns -> ms
981 submit(dom, "total_time_in_ms", flush_type_instance,
982 &(value_t){.derive = value}, 1);
989 static unsigned int parse_ex_stats_flags(char **exstats, int numexstats) {
990 unsigned int ex_stats_flags = ex_stats_none;
991 for (int i = 0; i < numexstats; i++) {
992 for (int j = 0; ex_stats_table[j].name != NULL; j++) {
993 if (strcasecmp(exstats[i], ex_stats_table[j].name) == 0) {
994 DEBUG(PLUGIN_NAME " plugin: enabling extra stats for '%s'",
995 ex_stats_table[j].name);
996 ex_stats_flags |= ex_stats_table[j].flag;
1000 if (ex_stats_table[j + 1].name == NULL) {
1001 ERROR(PLUGIN_NAME ": Unmatched ExtraStats option: %s", exstats[i]);
1005 return ex_stats_flags;
1008 static void domain_state_submit_notif(virDomainPtr dom, int state, int reason) {
1009 if ((state < 0) || (state >= STATIC_ARRAY_SIZE(domain_states))) {
1010 ERROR(PLUGIN_NAME ": Array index out of bounds: state=%d", state);
1014 char msg[DATA_MAX_NAME_LEN];
1015 const char *state_str = domain_states[state];
1016 #ifdef HAVE_DOM_REASON
1017 if ((reason < 0) || (reason >= STATIC_ARRAY_SIZE(domain_reasons[0]))) {
1018 ERROR(PLUGIN_NAME ": Array index out of bounds: reason=%d", reason);
1022 const char *reason_str = domain_reasons[state][reason];
1023 /* Array size for domain reasons is fixed, but different domain states can
1024 * have different number of reasons. We need to check if reason was
1025 * successfully parsed */
1027 ERROR(PLUGIN_NAME ": Invalid reason (%d) for domain state: %s", reason,
1032 const char *reason_str = "N/A";
1035 snprintf(msg, sizeof(msg), "Domain state: %s. Reason: %s", state_str,
1040 case VIR_DOMAIN_NOSTATE:
1041 case VIR_DOMAIN_RUNNING:
1042 case VIR_DOMAIN_SHUTDOWN:
1043 case VIR_DOMAIN_SHUTOFF:
1044 severity = NOTIF_OKAY;
1046 case VIR_DOMAIN_BLOCKED:
1047 case VIR_DOMAIN_PAUSED:
1048 #ifdef DOM_STATE_PMSUSPENDED
1049 case VIR_DOMAIN_PMSUSPENDED:
1051 severity = NOTIF_WARNING;
1053 case VIR_DOMAIN_CRASHED:
1054 severity = NOTIF_FAILURE;
1057 ERROR(PLUGIN_NAME ": Unrecognized domain state (%d)", state);
1060 submit_notif(dom, severity, msg, "domain_state", NULL);
1063 static int lv_config(const char *key, const char *value) {
1064 if (virInitialize() != 0)
1067 if (il_domains == NULL)
1068 il_domains = ignorelist_create(1);
1069 if (il_block_devices == NULL)
1070 il_block_devices = ignorelist_create(1);
1071 if (il_interface_devices == NULL)
1072 il_interface_devices = ignorelist_create(1);
1074 if (strcasecmp(key, "Connection") == 0) {
1075 char *tmp = strdup(value);
1077 ERROR(PLUGIN_NAME " plugin: Connection strdup failed.");
1085 if (strcasecmp(key, "RefreshInterval") == 0) {
1087 interval = strtol(value, &eptr, 10);
1088 if (eptr == NULL || *eptr != '\0')
1093 if (strcasecmp(key, "Domain") == 0) {
1094 if (ignorelist_add(il_domains, value))
1098 if (strcasecmp(key, "BlockDevice") == 0) {
1099 if (ignorelist_add(il_block_devices, value))
1104 if (strcasecmp(key, "BlockDeviceFormat") == 0) {
1105 if (strcasecmp(value, "target") == 0)
1106 blockdevice_format = target;
1107 else if (strcasecmp(value, "source") == 0)
1108 blockdevice_format = source;
1110 ERROR(PLUGIN_NAME " plugin: unknown BlockDeviceFormat: %s", value);
1115 if (strcasecmp(key, "BlockDeviceFormatBasename") == 0) {
1116 blockdevice_format_basename = IS_TRUE(value);
1119 if (strcasecmp(key, "InterfaceDevice") == 0) {
1120 if (ignorelist_add(il_interface_devices, value))
1125 if (strcasecmp(key, "IgnoreSelected") == 0) {
1126 if (IS_TRUE(value)) {
1127 ignorelist_set_invert(il_domains, 0);
1128 ignorelist_set_invert(il_block_devices, 0);
1129 ignorelist_set_invert(il_interface_devices, 0);
1131 ignorelist_set_invert(il_domains, 1);
1132 ignorelist_set_invert(il_block_devices, 1);
1133 ignorelist_set_invert(il_interface_devices, 1);
1138 if (strcasecmp(key, "HostnameFormat") == 0) {
1140 char *fields[HF_MAX_FIELDS];
1143 value_copy = strdup(value);
1144 if (value_copy == NULL) {
1145 ERROR(PLUGIN_NAME " plugin: strdup failed.");
1149 n = strsplit(value_copy, fields, HF_MAX_FIELDS);
1152 ERROR(PLUGIN_NAME " plugin: HostnameFormat: no fields");
1156 for (int i = 0; i < n; ++i) {
1157 if (strcasecmp(fields[i], "hostname") == 0)
1158 hostname_format[i] = hf_hostname;
1159 else if (strcasecmp(fields[i], "name") == 0)
1160 hostname_format[i] = hf_name;
1161 else if (strcasecmp(fields[i], "uuid") == 0)
1162 hostname_format[i] = hf_uuid;
1164 ERROR(PLUGIN_NAME " plugin: unknown HostnameFormat field: %s",
1172 for (int i = n; i < HF_MAX_FIELDS; ++i)
1173 hostname_format[i] = hf_none;
1178 if (strcasecmp(key, "PluginInstanceFormat") == 0) {
1180 char *fields[PLGINST_MAX_FIELDS];
1183 value_copy = strdup(value);
1184 if (value_copy == NULL) {
1185 ERROR(PLUGIN_NAME " plugin: strdup failed.");
1189 n = strsplit(value_copy, fields, PLGINST_MAX_FIELDS);
1192 ERROR(PLUGIN_NAME " plugin: PluginInstanceFormat: no fields");
1196 for (int i = 0; i < n; ++i) {
1197 if (strcasecmp(fields[i], "none") == 0) {
1198 plugin_instance_format[i] = plginst_none;
1200 } else if (strcasecmp(fields[i], "name") == 0)
1201 plugin_instance_format[i] = plginst_name;
1202 else if (strcasecmp(fields[i], "uuid") == 0)
1203 plugin_instance_format[i] = plginst_uuid;
1205 ERROR(PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s",
1213 for (int i = n; i < PLGINST_MAX_FIELDS; ++i)
1214 plugin_instance_format[i] = plginst_none;
1219 if (strcasecmp(key, "InterfaceFormat") == 0) {
1220 if (strcasecmp(value, "name") == 0)
1221 interface_format = if_name;
1222 else if (strcasecmp(value, "address") == 0)
1223 interface_format = if_address;
1224 else if (strcasecmp(value, "number") == 0)
1225 interface_format = if_number;
1227 ERROR(PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", value);
1233 if (strcasecmp(key, "Instances") == 0) {
1235 double val = strtod(value, &eptr);
1237 if (*eptr != '\0') {
1238 ERROR(PLUGIN_NAME " plugin: Invalid value for Instances = '%s'", value);
1242 ERROR(PLUGIN_NAME " plugin: Instances <= 0 makes no sense.");
1245 if (val > NR_INSTANCES_MAX) {
1246 ERROR(PLUGIN_NAME " plugin: Instances=%f > NR_INSTANCES_MAX=%i"
1247 " use a lower setting or recompile the plugin.",
1248 val, NR_INSTANCES_MAX);
1252 nr_instances = (int)val;
1253 DEBUG(PLUGIN_NAME " plugin: configured %i instances", nr_instances);
1257 if (strcasecmp(key, "ExtraStats") == 0) {
1258 char *localvalue = strdup(value);
1259 if (localvalue != NULL) {
1260 char *exstats[EX_STATS_MAX_FIELDS];
1262 strsplit(localvalue, exstats, STATIC_ARRAY_SIZE(exstats));
1263 extra_stats = parse_ex_stats_flags(exstats, numexstats);
1266 #ifdef HAVE_JOB_STATS
1267 if ((extra_stats & ex_stats_job_stats_completed) &&
1268 (extra_stats & ex_stats_job_stats_background)) {
1269 ERROR(PLUGIN_NAME " plugin: Invalid job stats configuration. Only one "
1270 "type of job statistics can be collected at the same "
1278 if (strcasecmp(key, "PersistentNotification") == 0) {
1279 persistent_notification = IS_TRUE(value);
1283 /* Unrecognised option. */
1287 static int lv_connect(void) {
1289 /* `conn_string == NULL' is acceptable */
1291 /* virDomainGetFSInfo requires full read-write access connection */
1292 if (extra_stats & ex_stats_fs_info)
1293 conn = virConnectOpen(conn_string);
1296 conn = virConnectOpenReadOnly(conn_string);
1298 c_complain(LOG_ERR, &conn_complain,
1299 PLUGIN_NAME " plugin: Unable to connect: "
1300 "virConnectOpen failed.");
1303 int status = virNodeGetInfo(conn, &nodeinfo);
1305 ERROR(PLUGIN_NAME ": virNodeGetInfo failed");
1309 c_release(LOG_NOTICE, &conn_complain,
1310 PLUGIN_NAME " plugin: Connection established.");
1314 static void lv_disconnect(void) {
1316 virConnectClose(conn);
1318 WARNING(PLUGIN_NAME " plugin: closed connection to libvirt");
1321 static int lv_domain_block_info(virDomainPtr dom, const char *path,
1322 struct lv_block_info *binfo) {
1323 #ifdef HAVE_BLOCK_STATS_FLAGS
1325 if (virDomainBlockStatsFlags(dom, path, NULL, &nparams, 0) < 0 ||
1327 VIRT_ERROR(conn, "getting the disk params count");
1331 virTypedParameterPtr params = calloc((size_t)nparams, sizeof(*params));
1332 if (params == NULL) {
1333 ERROR("virt plugin: alloc(%i) for block=%s parameters failed.", nparams,
1339 if (virDomainBlockStatsFlags(dom, path, params, &nparams, 0) < 0) {
1340 VIRT_ERROR(conn, "getting the disk params values");
1342 rc = get_block_info(binfo, params, nparams);
1345 virTypedParamsClear(params, nparams);
1349 return virDomainBlockStats(dom, path, &(binfo->bi), sizeof(binfo->bi));
1350 #endif /* HAVE_BLOCK_STATS_FLAGS */
1353 #ifdef HAVE_PERF_STATS
1354 static void perf_submit(virDomainStatsRecordPtr stats) {
1355 for (int i = 0; i < stats->nparams; ++i) {
1356 /* Replace '.' with '_' in event field to match other metrics' naming
1358 char *c = strchr(stats->params[i].field, '.');
1361 submit(stats->dom, "perf", stats->params[i].field,
1362 &(value_t){.derive = stats->params[i].value.ul}, 1);
1366 static int get_perf_events(virDomainPtr domain) {
1367 virDomainStatsRecordPtr *stats = NULL;
1368 /* virDomainListGetStats requires a NULL terminated list of domains */
1369 virDomainPtr domain_array[] = {domain, NULL};
1372 virDomainListGetStats(domain_array, VIR_DOMAIN_STATS_PERF, &stats, 0);
1374 ERROR("virt plugin: virDomainListGetStats failed with status %i.", status);
1378 for (int i = 0; i < status; ++i)
1379 perf_submit(stats[i]);
1381 virDomainStatsRecordListFree(stats);
1384 #endif /* HAVE_PERF_STATS */
1386 static void vcpu_pin_submit(virDomainPtr dom, int max_cpus, int vcpu,
1387 unsigned char *cpu_maps, int cpu_map_len) {
1388 for (int cpu = 0; cpu < max_cpus; ++cpu) {
1389 char type_instance[DATA_MAX_NAME_LEN];
1390 _Bool is_set = VIR_CPU_USABLE(cpu_maps, cpu_map_len, vcpu, cpu) ? 1 : 0;
1392 snprintf(type_instance, sizeof(type_instance), "vcpu_%d-cpu_%d", vcpu, cpu);
1393 submit(dom, "cpu_affinity", type_instance, &(value_t){.gauge = is_set}, 1);
1397 static int get_vcpu_stats(virDomainPtr domain, unsigned short nr_virt_cpu) {
1398 int max_cpus = VIR_NODEINFO_MAXCPUS(nodeinfo);
1399 int cpu_map_len = VIR_CPU_MAPLEN(max_cpus);
1401 virVcpuInfoPtr vinfo = calloc(nr_virt_cpu, sizeof(vinfo[0]));
1402 if (vinfo == NULL) {
1403 ERROR(PLUGIN_NAME " plugin: malloc failed.");
1407 unsigned char *cpumaps = calloc(nr_virt_cpu, cpu_map_len);
1408 if (cpumaps == NULL) {
1409 ERROR(PLUGIN_NAME " plugin: malloc failed.");
1415 virDomainGetVcpus(domain, vinfo, nr_virt_cpu, cpumaps, cpu_map_len);
1417 ERROR(PLUGIN_NAME " plugin: virDomainGetVcpus failed with status %i.",
1424 for (int i = 0; i < nr_virt_cpu; ++i) {
1425 vcpu_submit(vinfo[i].cpuTime, domain, vinfo[i].number, "virt_vcpu");
1426 if (extra_stats & ex_stats_vcpupin)
1427 vcpu_pin_submit(domain, max_cpus, i, cpumaps, cpu_map_len);
1435 #ifdef HAVE_DOM_REASON
1437 static void domain_state_submit(virDomainPtr dom, int state, int reason) {
1438 value_t values[] = {
1439 {.gauge = (gauge_t)state}, {.gauge = (gauge_t)reason},
1442 submit(dom, "domain_state", NULL, values, STATIC_ARRAY_SIZE(values));
1445 static int get_domain_state(virDomainPtr domain) {
1446 int domain_state = 0;
1447 int domain_reason = 0;
1449 int status = virDomainGetState(domain, &domain_state, &domain_reason, 0);
1451 ERROR(PLUGIN_NAME " plugin: virDomainGetState failed with status %i.",
1456 domain_state_submit(domain, domain_state, domain_reason);
1461 #ifdef HAVE_LIST_ALL_DOMAINS
1462 static int get_domain_state_notify(virDomainPtr domain) {
1463 int domain_state = 0;
1464 int domain_reason = 0;
1466 int status = virDomainGetState(domain, &domain_state, &domain_reason, 0);
1468 ERROR(PLUGIN_NAME " plugin: virDomainGetState failed with status %i.",
1473 if (persistent_notification)
1474 domain_state_submit_notif(domain, domain_state, domain_reason);
1478 #endif /* HAVE_LIST_ALL_DOMAINS */
1479 #endif /* HAVE_DOM_REASON */
1481 static int get_memory_stats(virDomainPtr domain) {
1482 virDomainMemoryStatPtr minfo =
1483 calloc(VIR_DOMAIN_MEMORY_STAT_NR, sizeof(virDomainMemoryStatStruct));
1484 if (minfo == NULL) {
1485 ERROR("virt plugin: malloc failed.");
1490 virDomainMemoryStats(domain, minfo, VIR_DOMAIN_MEMORY_STAT_NR, 0);
1491 if (mem_stats < 0) {
1492 ERROR("virt plugin: virDomainMemoryStats failed with mem_stats %i.",
1498 for (int i = 0; i < mem_stats; i++)
1499 memory_stats_submit((gauge_t)minfo[i].val * 1024, domain, minfo[i].tag);
1505 #ifdef HAVE_DISK_ERR
1506 static void disk_err_submit(virDomainPtr domain,
1507 virDomainDiskErrorPtr disk_err) {
1508 submit(domain, "disk_error", disk_err->disk,
1509 &(value_t){.gauge = disk_err->error}, 1);
1512 static int get_disk_err(virDomainPtr domain) {
1513 /* Get preferred size of disk errors array */
1514 int disk_err_count = virDomainGetDiskErrors(domain, NULL, 0, 0);
1515 if (disk_err_count == -1) {
1517 " plugin: failed to get preferred size of disk errors array");
1522 " plugin: preferred size of disk errors array: %d for domain %s",
1523 disk_err_count, virDomainGetName(domain));
1524 virDomainDiskError disk_err[disk_err_count];
1526 disk_err_count = virDomainGetDiskErrors(domain, disk_err, disk_err_count, 0);
1527 if (disk_err_count == -1) {
1528 ERROR(PLUGIN_NAME " plugin: virDomainGetDiskErrors failed with status %d",
1533 DEBUG(PLUGIN_NAME " plugin: detected %d disk errors in domain %s",
1534 disk_err_count, virDomainGetName(domain));
1536 for (int i = 0; i < disk_err_count; ++i) {
1537 disk_err_submit(domain, &disk_err[i]);
1538 sfree(disk_err[i].disk);
1543 #endif /* HAVE_DISK_ERR */
1545 static int get_block_stats(struct block_device *block_dev) {
1548 ERROR(PLUGIN_NAME " plugin: get_block_stats NULL pointer");
1552 struct lv_block_info binfo;
1553 init_block_info(&binfo);
1555 if (lv_domain_block_info(block_dev->dom, block_dev->path, &binfo) < 0) {
1556 ERROR(PLUGIN_NAME " plugin: lv_domain_block_info failed");
1560 disk_submit(&binfo, block_dev->dom, block_dev->path);
1566 #define NM_ADD_ITEM(_fun, _name, _val) \
1568 ret = _fun(¬if, _name, _val); \
1570 ERROR(PLUGIN_NAME " plugin: failed to add notification metadata"); \
1575 #define NM_ADD_STR_ITEMS(_items, _size) \
1577 for (int _i = 0; _i < _size; ++_i) { \
1579 " plugin: Adding notification metadata name=%s value=%s", \
1580 _items[_i].name, _items[_i].value); \
1581 NM_ADD_ITEM(plugin_notification_meta_add_string, _items[_i].name, \
1582 _items[_i].value); \
1586 static int fs_info_notify(virDomainPtr domain, virDomainFSInfoPtr fs_info) {
1587 notification_t notif;
1590 /* Local struct, just for the purpose of this function. */
1591 typedef struct nm_str_item_s {
1596 nm_str_item_t fs_dev_alias[fs_info->ndevAlias];
1597 nm_str_item_t fs_str_items[] = {
1598 {.name = "mountpoint", .value = fs_info->mountpoint},
1599 {.name = "name", .value = fs_info->name},
1600 {.name = "fstype", .value = fs_info->fstype}};
1602 for (int i = 0; i < fs_info->ndevAlias; ++i) {
1603 fs_dev_alias[i].name = "devAlias";
1604 fs_dev_alias[i].value = fs_info->devAlias[i];
1607 init_notif(¬if, domain, NOTIF_OKAY, "File system information",
1608 "file_system", NULL);
1609 NM_ADD_STR_ITEMS(fs_str_items, STATIC_ARRAY_SIZE(fs_str_items));
1610 NM_ADD_ITEM(plugin_notification_meta_add_unsigned_int, "ndevAlias",
1611 fs_info->ndevAlias);
1612 NM_ADD_STR_ITEMS(fs_dev_alias, fs_info->ndevAlias);
1614 plugin_dispatch_notification(¬if);
1618 plugin_notification_meta_free(notif.meta);
1622 #undef RETURN_ON_ERR
1623 #undef NM_ADD_STR_ITEMS
1625 static int get_fs_info(virDomainPtr domain) {
1626 virDomainFSInfoPtr *fs_info = NULL;
1629 int mount_points_cnt = virDomainGetFSInfo(domain, &fs_info, 0);
1630 if (mount_points_cnt == -1) {
1631 ERROR(PLUGIN_NAME " plugin: virDomainGetFSInfo failed: %d",
1633 return mount_points_cnt;
1636 for (int i = 0; i < mount_points_cnt; ++i) {
1637 if (fs_info_notify(domain, fs_info[i]) != 0) {
1638 ERROR(PLUGIN_NAME " plugin: failed to send file system notification "
1639 "for mount point %s",
1640 fs_info[i]->mountpoint);
1643 virDomainFSInfoFree(fs_info[i]);
1650 #endif /* HAVE_FS_INFO */
1652 #ifdef HAVE_JOB_STATS
1653 static void job_stats_submit(virDomainPtr domain, virTypedParameterPtr param) {
1656 if (param->type == VIR_TYPED_PARAM_INT)
1657 vl.derive = param->value.i;
1658 else if (param->type == VIR_TYPED_PARAM_UINT)
1659 vl.derive = param->value.ui;
1660 else if (param->type == VIR_TYPED_PARAM_LLONG)
1661 vl.derive = param->value.l;
1662 else if (param->type == VIR_TYPED_PARAM_ULLONG)
1663 vl.derive = param->value.ul;
1664 else if (param->type == VIR_TYPED_PARAM_DOUBLE)
1665 vl.derive = param->value.d;
1666 else if (param->type == VIR_TYPED_PARAM_BOOLEAN)
1667 vl.derive = param->value.b;
1668 else if (param->type == VIR_TYPED_PARAM_STRING) {
1669 submit_notif(domain, NOTIF_OKAY, param->value.s, "job_stats", param->field);
1672 ERROR(PLUGIN_NAME " plugin: unrecognized virTypedParameterType");
1676 submit(domain, "job_stats", param->field, &vl, 1);
1679 static int get_job_stats(virDomainPtr domain) {
1683 virTypedParameterPtr params = NULL;
1684 int flags = (extra_stats & ex_stats_job_stats_completed)
1685 ? VIR_DOMAIN_JOB_STATS_COMPLETED
1688 ret = virDomainGetJobStats(domain, &job_type, ¶ms, &nparams, flags);
1690 ERROR(PLUGIN_NAME " plugin: virDomainGetJobStats failed: %d", ret);
1694 DEBUG(PLUGIN_NAME " plugin: job_type=%d nparams=%d", job_type, nparams);
1696 for (int i = 0; i < nparams; ++i) {
1697 DEBUG(PLUGIN_NAME " plugin: param[%d] field=%s type=%d", i, params[i].field,
1699 job_stats_submit(domain, ¶ms[i]);
1702 virTypedParamsFree(params, nparams);
1705 #endif /* HAVE_JOB_STATS */
1707 static int get_domain_metrics(domain_t *domain) {
1708 struct lv_info info;
1710 if (!domain || !domain->ptr) {
1711 ERROR(PLUGIN_NAME ": get_domain_metrics: NULL pointer");
1715 init_lv_info(&info);
1716 int status = lv_domain_info(domain->ptr, &info);
1718 ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
1723 if (extra_stats & ex_stats_domain_state) {
1724 #ifdef HAVE_DOM_REASON
1725 /* At this point we already know domain's state from virDomainGetInfo call,
1726 * however it doesn't provide a reason for entering particular state.
1727 * We need to get it from virDomainGetState.
1729 GET_STATS(get_domain_state, "domain reason", domain->ptr);
1733 /* Gather remaining stats only for running domains */
1734 if (info.di.state != VIR_DOMAIN_RUNNING)
1737 pcpu_submit(domain->ptr, &info);
1738 cpu_submit(domain, info.di.cpuTime);
1740 memory_submit(domain->ptr, (gauge_t)info.di.memory * 1024);
1742 GET_STATS(get_vcpu_stats, "vcpu stats", domain->ptr, info.di.nrVirtCpu);
1743 GET_STATS(get_memory_stats, "memory stats", domain->ptr);
1745 #ifdef HAVE_PERF_STATS
1746 if (extra_stats & ex_stats_perf)
1747 GET_STATS(get_perf_events, "performance monitoring events", domain->ptr);
1751 if (extra_stats & ex_stats_fs_info)
1752 GET_STATS(get_fs_info, "file system info", domain->ptr);
1755 #ifdef HAVE_DISK_ERR
1756 if (extra_stats & ex_stats_disk_err)
1757 GET_STATS(get_disk_err, "disk errors", domain->ptr);
1760 #ifdef HAVE_JOB_STATS
1762 (ex_stats_job_stats_completed | ex_stats_job_stats_background))
1763 GET_STATS(get_job_stats, "job stats", domain->ptr);
1766 /* Update cached virDomainInfo. It has to be done after cpu_submit */
1767 memcpy(&domain->info, &info.di, sizeof(domain->info));
1772 static int get_if_dev_stats(struct interface_device *if_dev) {
1773 virDomainInterfaceStatsStruct stats = {0};
1774 char *display_name = NULL;
1777 ERROR(PLUGIN_NAME " plugin: get_if_dev_stats: NULL pointer");
1781 switch (interface_format) {
1783 display_name = if_dev->address;
1786 display_name = if_dev->number;
1790 display_name = if_dev->path;
1793 if (virDomainInterfaceStats(if_dev->dom, if_dev->path, &stats,
1794 sizeof(stats)) != 0) {
1795 ERROR(PLUGIN_NAME " plugin: virDomainInterfaceStats failed");
1799 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
1800 submit_derive2("if_octets", (derive_t)stats.rx_bytes,
1801 (derive_t)stats.tx_bytes, if_dev->dom, display_name);
1803 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
1804 submit_derive2("if_packets", (derive_t)stats.rx_packets,
1805 (derive_t)stats.tx_packets, if_dev->dom, display_name);
1807 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
1808 submit_derive2("if_errors", (derive_t)stats.rx_errs,
1809 (derive_t)stats.tx_errs, if_dev->dom, display_name);
1811 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
1812 submit_derive2("if_dropped", (derive_t)stats.rx_drop,
1813 (derive_t)stats.tx_drop, if_dev->dom, display_name);
1817 static int domain_lifecycle_event_cb(__attribute__((unused)) virConnectPtr conn,
1818 virDomainPtr dom, int event, int detail,
1819 __attribute__((unused)) void *opaque) {
1820 int domain_state = map_domain_event_to_state(event);
1821 int domain_reason = map_domain_event_detail_to_reason(event, detail);
1822 domain_state_submit_notif(dom, domain_state, domain_reason);
1827 static int register_event_impl(void) {
1828 if (virEventRegisterDefaultImpl() < 0) {
1829 virErrorPtr err = virGetLastError();
1831 " plugin: error while event implementation registering: %s",
1832 err && err->message ? err->message : "Unknown error");
1839 /* worker function running default event implementation */
1840 static void *event_loop_worker(__attribute__((unused)) void *arg) {
1842 if (virEventRunDefaultImpl() < 0) {
1843 virErrorPtr err = virGetLastError();
1844 ERROR(PLUGIN_NAME " plugin: failed to run event loop: %s\n",
1845 err && err->message ? err->message : "Unknown error");
1852 /* register domain event callback and start event loop thread */
1853 static int start_event_loop(void) {
1854 domain_event_cb_id = virConnectDomainEventRegisterAny(
1855 conn, NULL, VIR_DOMAIN_EVENT_ID_LIFECYCLE,
1856 VIR_DOMAIN_EVENT_CALLBACK(domain_lifecycle_event_cb), NULL, NULL);
1857 if (domain_event_cb_id == -1) {
1858 ERROR(PLUGIN_NAME " plugin: error while callback registering");
1862 if (pthread_create(&event_loop_tid, NULL, event_loop_worker, NULL)) {
1863 ERROR(PLUGIN_NAME " plugin: failed event loop thread creation");
1864 virConnectDomainEventDeregisterAny(conn, domain_event_cb_id);
1871 /* stop event loop thread and deregister callback */
1872 static void stop_event_loop(void) {
1873 if (pthread_cancel(event_loop_tid) != 0)
1874 ERROR(PLUGIN_NAME " plugin: cancelling thread %lu failed", event_loop_tid);
1876 if (pthread_join(event_loop_tid, NULL) != 0)
1877 ERROR(PLUGIN_NAME " plugin: stopping thread %lu failed", event_loop_tid);
1879 if (conn != NULL && domain_event_cb_id != -1)
1880 virConnectDomainEventDeregisterAny(conn, domain_event_cb_id);
1883 static int persistent_domains_state_notification(void) {
1886 #ifdef HAVE_LIST_ALL_DOMAINS
1887 virDomainPtr *domains;
1888 n = virConnectListAllDomains(conn, &domains,
1889 VIR_CONNECT_GET_ALL_DOMAINS_STATS_PERSISTENT);
1891 VIRT_ERROR(conn, "reading list of persistent domains");
1894 DEBUG(PLUGIN_NAME " plugin: getting state of %i persistent domains", n);
1895 /* Fetch each persistent domain's state and notify it */
1897 for (int i = 0; i < n; ++i) {
1898 status = get_domain_state_notify(domains[i]);
1901 ERROR(PLUGIN_NAME " plugin: could not notify state of domain %s",
1902 virDomainGetName(domains[i]));
1907 DEBUG(PLUGIN_NAME " plugin: notified state of %i persistent domains",
1911 n = virConnectNumOfDomains(conn);
1914 /* Get list of domains. */
1915 domids = malloc(sizeof(*domids) * n);
1916 if (domids == NULL) {
1917 ERROR(PLUGIN_NAME " plugin: malloc failed.");
1920 n = virConnectListDomains(conn, domids, n);
1922 VIRT_ERROR(conn, "reading list of domains");
1926 /* Fetch info of each active domain and notify it */
1927 for (int i = 0; i < n; ++i) {
1929 virDomainPtr dom = NULL;
1930 dom = virDomainLookupByID(conn, domids[i]);
1932 VIRT_ERROR(conn, "virDomainLookupByID");
1933 /* Could be that the domain went away -- ignore it anyway. */
1936 status = virDomainGetInfo(dom, &info);
1938 ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
1942 /* virDomainGetState is not available. Submit 0, which corresponds to
1943 * unknown reason. */
1944 domain_state_submit_notif(dom, info.state, 0);
1953 static int lv_read(user_data_t *ud) {
1955 struct lv_read_instance *inst = NULL;
1956 struct lv_read_state *state = NULL;
1958 if (ud->data == NULL) {
1959 ERROR(PLUGIN_NAME " plugin: NULL userdata");
1964 state = &inst->read_state;
1966 _Bool reconnect = conn == NULL ? 1 : 0;
1967 /* event implementation must be registered before connection is opened */
1968 if (inst->id == 0) {
1969 if (!persistent_notification && reconnect)
1970 if (register_event_impl() != 0)
1973 if (lv_connect() < 0)
1976 if (!persistent_notification && reconnect && conn != NULL)
1977 if (start_event_loop() != 0)
1983 /* Need to refresh domain or device lists? */
1984 if ((last_refresh == (time_t)0) ||
1985 ((interval > 0) && ((last_refresh + interval) <= t))) {
1986 if (inst->id == 0 && persistent_notification) {
1987 int status = persistent_domains_state_notification();
1989 DEBUG(PLUGIN_NAME " plugin: persistent_domains_state_notifications "
1990 "returned with status %i",
1993 if (refresh_lists(inst) != 0) {
1994 if (inst->id == 0) {
1995 if (!persistent_notification)
2005 for (int i = 0; i < state->nr_domains; ++i)
2006 DEBUG(PLUGIN_NAME " plugin: domain %s",
2007 virDomainGetName(state->domains[i].ptr));
2008 for (int i = 0; i < state->nr_block_devices; ++i)
2009 DEBUG(PLUGIN_NAME " plugin: block device %d %s:%s", i,
2010 virDomainGetName(state->block_devices[i].dom),
2011 state->block_devices[i].path);
2012 for (int i = 0; i < state->nr_interface_devices; ++i)
2013 DEBUG(PLUGIN_NAME " plugin: interface device %d %s:%s", i,
2014 virDomainGetName(state->interface_devices[i].dom),
2015 state->interface_devices[i].path);
2018 /* Get domains' metrics */
2019 for (int i = 0; i < state->nr_domains; ++i) {
2020 domain_t *dom = &state->domains[i];
2023 status = get_domain_metrics(dom);
2025 status = get_domain_state(dom->ptr);
2028 ERROR(PLUGIN_NAME " failed to get metrics for domain=%s",
2029 virDomainGetName(dom->ptr));
2032 /* Get block device stats for each domain. */
2033 for (int i = 0; i < state->nr_block_devices; ++i) {
2034 int status = get_block_stats(&state->block_devices[i]);
2037 " failed to get stats for block device (%s) in domain %s",
2038 state->block_devices[i].path,
2039 virDomainGetName(state->domains[i].ptr));
2042 /* Get interface stats for each domain. */
2043 for (int i = 0; i < state->nr_interface_devices; ++i) {
2044 int status = get_if_dev_stats(&state->interface_devices[i]);
2047 " failed to get interface stats for device (%s) in domain %s",
2048 state->interface_devices[i].path,
2049 virDomainGetName(state->interface_devices[i].dom));
2055 static int lv_init_instance(size_t i, plugin_read_cb callback) {
2056 struct lv_user_data *lv_ud = &(lv_read_user_data[i]);
2057 struct lv_read_instance *inst = &(lv_ud->inst);
2059 memset(lv_ud, 0, sizeof(*lv_ud));
2061 snprintf(inst->tag, sizeof(inst->tag), "%s-%" PRIsz, PLUGIN_NAME, i);
2064 user_data_t *ud = &(lv_ud->ud);
2066 ud->free_func = NULL;
2068 INFO(PLUGIN_NAME " plugin: reader %s initialized", inst->tag);
2070 return plugin_register_complex_read(NULL, inst->tag, callback, 0, ud);
2073 static void lv_clean_read_state(struct lv_read_state *state) {
2074 free_block_devices(state);
2075 free_interface_devices(state);
2076 free_domains(state);
2079 static void lv_fini_instance(size_t i) {
2080 struct lv_read_instance *inst = &(lv_read_user_data[i].inst);
2081 struct lv_read_state *state = &(inst->read_state);
2083 lv_clean_read_state(state);
2085 INFO(PLUGIN_NAME " plugin: reader %s finalized", inst->tag);
2088 static int lv_init(void) {
2089 if (virInitialize() != 0)
2092 /* event implementation must be registered before connection is opened */
2093 if (!persistent_notification)
2094 if (register_event_impl() != 0)
2097 if (lv_connect() != 0)
2100 DEBUG(PLUGIN_NAME " plugin: starting event loop");
2102 if (!persistent_notification)
2103 if (start_event_loop() != 0)
2106 DEBUG(PLUGIN_NAME " plugin: starting %i instances", nr_instances);
2108 for (int i = 0; i < nr_instances; ++i)
2109 if (lv_init_instance(i, lv_read) != 0)
2116 * returns 0 on success and <0 on error
2118 static int lv_domain_get_tag(xmlXPathContextPtr xpath_ctx, const char *dom_name,
2120 char xpath_str[BUFFER_MAX_LEN] = {'\0'};
2121 xmlXPathObjectPtr xpath_obj = NULL;
2122 xmlNodePtr xml_node = NULL;
2126 err = xmlXPathRegisterNs(xpath_ctx,
2127 (const xmlChar *)METADATA_VM_PARTITION_PREFIX,
2128 (const xmlChar *)METADATA_VM_PARTITION_URI);
2130 ERROR(PLUGIN_NAME " plugin: xmlXpathRegisterNs(%s, %s) failed on domain %s",
2131 METADATA_VM_PARTITION_PREFIX, METADATA_VM_PARTITION_URI, dom_name);
2135 snprintf(xpath_str, sizeof(xpath_str), "/domain/metadata/%s:%s/text()",
2136 METADATA_VM_PARTITION_PREFIX, METADATA_VM_PARTITION_ELEMENT);
2137 xpath_obj = xmlXPathEvalExpression((xmlChar *)xpath_str, xpath_ctx);
2138 if (xpath_obj == NULL) {
2139 ERROR(PLUGIN_NAME " plugin: xmlXPathEval(%s) failed on domain %s",
2140 xpath_str, dom_name);
2144 if (xpath_obj->type != XPATH_NODESET) {
2145 ERROR(PLUGIN_NAME " plugin: xmlXPathEval(%s) unexpected return type %d "
2146 "(wanted %d) on domain %s",
2147 xpath_str, xpath_obj->type, XPATH_NODESET, dom_name);
2152 * from now on there is no real error, it's ok if a domain
2153 * doesn't have the metadata partition tag.
2156 if (xpath_obj->nodesetval == NULL || xpath_obj->nodesetval->nodeNr != 1) {
2157 DEBUG(PLUGIN_NAME " plugin: xmlXPathEval(%s) return nodeset size=%i "
2158 "expected=1 on domain %s",
2160 (xpath_obj->nodesetval == NULL) ? 0 : xpath_obj->nodesetval->nodeNr,
2163 xml_node = xpath_obj->nodesetval->nodeTab[0];
2164 sstrncpy(dom_tag, (const char *)xml_node->content, PARTITION_TAG_MAX_LEN);
2168 /* deregister to clean up */
2169 err = xmlXPathRegisterNs(xpath_ctx,
2170 (const xmlChar *)METADATA_VM_PARTITION_PREFIX, NULL);
2172 /* we can't really recover here */
2174 " plugin: deregistration of namespace %s failed for domain %s",
2175 METADATA_VM_PARTITION_PREFIX, dom_name);
2178 xmlXPathFreeObject(xpath_obj);
2183 static int is_known_tag(const char *dom_tag) {
2184 for (int i = 0; i < nr_instances; ++i)
2185 if (!strcmp(dom_tag, lv_read_user_data[i].inst.tag))
2190 static int lv_instance_include_domain(struct lv_read_instance *inst,
2191 const char *dom_name,
2192 const char *dom_tag) {
2193 if ((dom_tag[0] != '\0') && (strcmp(dom_tag, inst->tag) == 0))
2196 /* instance#0 will always be there, so it is in charge of extra duties */
2197 if (inst->id == 0) {
2198 if (dom_tag[0] == '\0' || !is_known_tag(dom_tag)) {
2199 DEBUG(PLUGIN_NAME " plugin#%s: refreshing domain %s "
2200 "with unknown tag '%s'",
2201 inst->tag, dom_name, dom_tag);
2209 static int refresh_lists(struct lv_read_instance *inst) {
2210 struct lv_read_state *state = &inst->read_state;
2213 #ifndef HAVE_LIST_ALL_DOMAINS
2214 n = virConnectNumOfDomains(conn);
2216 VIRT_ERROR(conn, "reading number of domains");
2221 lv_clean_read_state(state);
2223 #ifndef HAVE_LIST_ALL_DOMAINS
2228 #ifdef HAVE_LIST_ALL_DOMAINS
2229 virDomainPtr *domains, *domains_inactive;
2230 int m = virConnectListAllDomains(conn, &domains_inactive,
2231 VIR_CONNECT_LIST_DOMAINS_INACTIVE);
2232 n = virConnectListAllDomains(conn, &domains, VIR_CONNECT_LIST_DOMAINS_ACTIVE);
2236 /* Get list of domains. */
2237 domids = malloc(sizeof(*domids) * n);
2238 if (domids == NULL) {
2239 ERROR(PLUGIN_NAME " plugin: malloc failed.");
2243 n = virConnectListDomains(conn, domids, n);
2247 VIRT_ERROR(conn, "reading list of domains");
2248 #ifndef HAVE_LIST_ALL_DOMAINS
2251 sfree(domains_inactive);
2256 #ifdef HAVE_LIST_ALL_DOMAINS
2257 for (int i = 0; i < m; ++i)
2258 if (add_domain(state, domains_inactive[i], 0) < 0) {
2259 ERROR(PLUGIN_NAME " plugin: malloc failed.");
2264 /* Fetch each domain and add it to the list, unless ignore. */
2265 for (int i = 0; i < n; ++i) {
2268 xmlDocPtr xml_doc = NULL;
2269 xmlXPathContextPtr xpath_ctx = NULL;
2270 xmlXPathObjectPtr xpath_obj = NULL;
2271 char tag[PARTITION_TAG_MAX_LEN] = {'\0'};
2275 #ifdef HAVE_LIST_ALL_DOMAINS
2276 virDomainPtr dom = domains[i];
2278 virDomainPtr dom = NULL;
2279 dom = virDomainLookupByID(conn, domids[i]);
2281 VIRT_ERROR(conn, "virDomainLookupByID");
2282 /* Could be that the domain went away -- ignore it anyway. */
2287 name = virDomainGetName(dom);
2289 VIRT_ERROR(conn, "virDomainGetName");
2293 status = virDomainGetInfo(dom, &info);
2295 ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
2300 if (info.state != VIR_DOMAIN_RUNNING) {
2301 DEBUG(PLUGIN_NAME " plugin: skipping inactive domain %s", name);
2305 if (il_domains && ignorelist_match(il_domains, name) != 0)
2308 /* Get a list of devices for this domain. */
2309 xml = virDomainGetXMLDesc(dom, 0);
2311 VIRT_ERROR(conn, "virDomainGetXMLDesc");
2315 /* Yuck, XML. Parse out the devices. */
2316 xml_doc = xmlReadDoc((xmlChar *)xml, NULL, NULL, XML_PARSE_NONET);
2317 if (xml_doc == NULL) {
2318 VIRT_ERROR(conn, "xmlReadDoc");
2322 xpath_ctx = xmlXPathNewContext(xml_doc);
2324 if (lv_domain_get_tag(xpath_ctx, name, tag) < 0) {
2325 ERROR(PLUGIN_NAME " plugin: lv_domain_get_tag failed.");
2329 if (!lv_instance_include_domain(inst, name, tag))
2332 if (add_domain(state, dom, 1) < 0) {
2333 ERROR(PLUGIN_NAME " plugin: malloc failed.");
2337 /* Block devices. */
2338 const char *bd_xmlpath = "/domain/devices/disk/target[@dev]";
2339 if (blockdevice_format == source)
2340 bd_xmlpath = "/domain/devices/disk/source[@dev]";
2341 xpath_obj = xmlXPathEval((const xmlChar *)bd_xmlpath, xpath_ctx);
2343 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
2344 xpath_obj->nodesetval == NULL)
2347 for (int j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
2351 node = xpath_obj->nodesetval->nodeTab[j];
2354 path = (char *)xmlGetProp(node, (xmlChar *)"dev");
2358 if (il_block_devices &&
2359 ignore_device_match(il_block_devices, name, path) != 0)
2362 add_block_device(state, dom, path);
2367 xmlXPathFreeObject(xpath_obj);
2369 /* Network interfaces. */
2370 xpath_obj = xmlXPathEval(
2371 (xmlChar *)"/domain/devices/interface[target[@dev]]", xpath_ctx);
2372 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
2373 xpath_obj->nodesetval == NULL)
2376 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
2378 for (int j = 0; j < xml_interfaces->nodeNr; ++j) {
2380 char *address = NULL;
2381 xmlNodePtr xml_interface;
2383 xml_interface = xml_interfaces->nodeTab[j];
2387 for (xmlNodePtr child = xml_interface->children; child;
2388 child = child->next) {
2389 if (child->type != XML_ELEMENT_NODE)
2392 if (xmlStrEqual(child->name, (const xmlChar *)"target")) {
2393 path = (char *)xmlGetProp(child, (const xmlChar *)"dev");
2396 } else if (xmlStrEqual(child->name, (const xmlChar *)"mac")) {
2397 address = (char *)xmlGetProp(child, (const xmlChar *)"address");
2403 if (il_interface_devices &&
2404 (ignore_device_match(il_interface_devices, name, path) != 0 ||
2405 ignore_device_match(il_interface_devices, name, address) != 0))
2408 add_interface_device(state, dom, path, address, j + 1);
2418 xmlXPathFreeObject(xpath_obj);
2420 xmlXPathFreeContext(xpath_ctx);
2422 xmlFreeDoc(xml_doc);
2426 #ifdef HAVE_LIST_ALL_DOMAINS
2428 sfree(domains_inactive);
2435 DEBUG(PLUGIN_NAME " plugin#%s: refreshing"
2436 " domains=%i block_devices=%i iface_devices=%i",
2437 inst->tag, state->nr_domains, state->nr_block_devices,
2438 state->nr_interface_devices);
2443 static void free_domains(struct lv_read_state *state) {
2444 if (state->domains) {
2445 for (int i = 0; i < state->nr_domains; ++i)
2446 virDomainFree(state->domains[i].ptr);
2447 sfree(state->domains);
2449 state->domains = NULL;
2450 state->nr_domains = 0;
2453 static int add_domain(struct lv_read_state *state, virDomainPtr dom,
2456 int new_size = sizeof(state->domains[0]) * (state->nr_domains + 1);
2459 new_ptr = realloc(state->domains, new_size);
2461 new_ptr = malloc(new_size);
2463 if (new_ptr == NULL)
2466 state->domains = new_ptr;
2467 state->domains[state->nr_domains].ptr = dom;
2468 state->domains[state->nr_domains].active = active;
2469 memset(&state->domains[state->nr_domains].info, 0,
2470 sizeof(state->domains[state->nr_domains].info));
2472 return state->nr_domains++;
2475 static void free_block_devices(struct lv_read_state *state) {
2476 if (state->block_devices) {
2477 for (int i = 0; i < state->nr_block_devices; ++i)
2478 sfree(state->block_devices[i].path);
2479 sfree(state->block_devices);
2481 state->block_devices = NULL;
2482 state->nr_block_devices = 0;
2485 static int add_block_device(struct lv_read_state *state, virDomainPtr dom,
2487 struct block_device *new_ptr;
2489 sizeof(state->block_devices[0]) * (state->nr_block_devices + 1);
2492 path_copy = strdup(path);
2496 if (state->block_devices)
2497 new_ptr = realloc(state->block_devices, new_size);
2499 new_ptr = malloc(new_size);
2501 if (new_ptr == NULL) {
2505 state->block_devices = new_ptr;
2506 state->block_devices[state->nr_block_devices].dom = dom;
2507 state->block_devices[state->nr_block_devices].path = path_copy;
2508 return state->nr_block_devices++;
2511 static void free_interface_devices(struct lv_read_state *state) {
2512 if (state->interface_devices) {
2513 for (int i = 0; i < state->nr_interface_devices; ++i) {
2514 sfree(state->interface_devices[i].path);
2515 sfree(state->interface_devices[i].address);
2516 sfree(state->interface_devices[i].number);
2518 sfree(state->interface_devices);
2520 state->interface_devices = NULL;
2521 state->nr_interface_devices = 0;
2524 static int add_interface_device(struct lv_read_state *state, virDomainPtr dom,
2525 const char *path, const char *address,
2526 unsigned int number) {
2527 struct interface_device *new_ptr;
2529 sizeof(state->interface_devices[0]) * (state->nr_interface_devices + 1);
2530 char *path_copy, *address_copy, number_string[15];
2532 if ((path == NULL) || (address == NULL))
2535 path_copy = strdup(path);
2539 address_copy = strdup(address);
2540 if (!address_copy) {
2545 snprintf(number_string, sizeof(number_string), "interface-%u", number);
2547 if (state->interface_devices)
2548 new_ptr = realloc(state->interface_devices, new_size);
2550 new_ptr = malloc(new_size);
2552 if (new_ptr == NULL) {
2554 sfree(address_copy);
2557 state->interface_devices = new_ptr;
2558 state->interface_devices[state->nr_interface_devices].dom = dom;
2559 state->interface_devices[state->nr_interface_devices].path = path_copy;
2560 state->interface_devices[state->nr_interface_devices].address = address_copy;
2561 state->interface_devices[state->nr_interface_devices].number =
2562 strdup(number_string);
2563 return state->nr_interface_devices++;
2566 static int ignore_device_match(ignorelist_t *il, const char *domname,
2567 const char *devpath) {
2571 if ((domname == NULL) || (devpath == NULL))
2574 n = strlen(domname) + strlen(devpath) + 2;
2577 ERROR(PLUGIN_NAME " plugin: malloc failed.");
2580 snprintf(name, n, "%s:%s", domname, devpath);
2581 r = ignorelist_match(il, name);
2586 static int lv_shutdown(void) {
2587 for (int i = 0; i < nr_instances; ++i) {
2588 lv_fini_instance(i);
2591 DEBUG(PLUGIN_NAME " plugin: stopping event loop");
2593 if (!persistent_notification)
2598 ignorelist_free(il_domains);
2600 ignorelist_free(il_block_devices);
2601 il_block_devices = NULL;
2602 ignorelist_free(il_interface_devices);
2603 il_interface_devices = NULL;
2608 void module_register(void) {
2609 plugin_register_config(PLUGIN_NAME, lv_config, config_keys, NR_CONFIG_KEYS);
2610 plugin_register_init(PLUGIN_NAME, lv_init);
2611 plugin_register_shutdown(PLUGIN_NAME, lv_shutdown);