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>
40 #define PLUGIN_NAME "virt"
42 /* Secure strcat macro assuring null termination. Parameter (n) is the size of
43 buffer (d), allowing this macro to be safe for static and dynamic buffers */
44 #define SSTRNCAT(d, s, n) \
46 size_t _l = strlen(d); \
47 sstrncpy((d) + _l, (s), (n)-_l); \
50 #ifdef LIBVIR_CHECK_VERSION
52 #if LIBVIR_CHECK_VERSION(0, 9, 2)
53 #define HAVE_DOM_REASON 1
56 #if LIBVIR_CHECK_VERSION(0, 9, 5)
57 #define HAVE_BLOCK_STATS_FLAGS 1
58 #define HAVE_DOM_REASON_PAUSED_SHUTTING_DOWN 1
61 #if LIBVIR_CHECK_VERSION(0, 9, 10)
62 #define HAVE_DISK_ERR 1
65 #if LIBVIR_CHECK_VERSION(0, 9, 11)
66 #define HAVE_CPU_STATS 1
67 #define HAVE_DOM_STATE_PMSUSPENDED 1
68 #define HAVE_DOM_REASON_RUNNING_WAKEUP 1
72 virConnectListAllDomains() appeared in 0.10.2
73 Note that LIBVIR_CHECK_VERSION appeared a year later, so
74 in some systems which actually have virConnectListAllDomains()
77 #if LIBVIR_CHECK_VERSION(0, 10, 2)
78 #define HAVE_LIST_ALL_DOMAINS 1
81 #if LIBVIR_CHECK_VERSION(1, 0, 1)
82 #define HAVE_DOM_REASON_PAUSED_SNAPSHOT 1
85 #if LIBVIR_CHECK_VERSION(1, 1, 1)
86 #define HAVE_DOM_REASON_PAUSED_CRASHED 1
89 #if LIBVIR_CHECK_VERSION(1, 2, 9)
90 #define HAVE_JOB_STATS 1
93 #if LIBVIR_CHECK_VERSION(1, 2, 10)
94 #define HAVE_DOM_REASON_CRASHED 1
97 #if LIBVIR_CHECK_VERSION(1, 2, 11)
98 #define HAVE_FS_INFO 1
101 #if LIBVIR_CHECK_VERSION(1, 2, 15)
102 #define HAVE_DOM_REASON_PAUSED_STARTING_UP 1
105 #if LIBVIR_CHECK_VERSION(1, 3, 3)
106 #define HAVE_PERF_STATS 1
107 #define HAVE_DOM_REASON_POSTCOPY 1
110 #endif /* LIBVIR_CHECK_VERSION */
112 /* structure used for aggregating notification-thread data*/
113 typedef struct virt_notif_thread_s {
114 pthread_t event_loop_tid;
115 int domain_event_cb_id;
116 pthread_mutex_t active_mutex; /* protects 'is_active' member access*/
118 } virt_notif_thread_t;
120 static const char *config_keys[] = {"Connection",
127 "BlockDeviceFormatBasename",
134 "PluginInstanceFormat",
138 "PersistentNotification",
141 /* PersistentNotification is false by default */
142 static bool persistent_notification = false;
144 /* Thread used for handling libvirt notifications events */
145 static virt_notif_thread_t notif_thread;
147 const char *domain_states[] = {
148 [VIR_DOMAIN_NOSTATE] = "no state",
149 [VIR_DOMAIN_RUNNING] = "the domain is running",
150 [VIR_DOMAIN_BLOCKED] = "the domain is blocked on resource",
151 [VIR_DOMAIN_PAUSED] = "the domain is paused by user",
152 [VIR_DOMAIN_SHUTDOWN] = "the domain is being shut down",
153 [VIR_DOMAIN_SHUTOFF] = "the domain is shut off",
154 [VIR_DOMAIN_CRASHED] = "the domain is crashed",
155 #ifdef HAVE_DOM_STATE_PMSUSPENDED
156 [VIR_DOMAIN_PMSUSPENDED] =
157 "the domain is suspended by guest power management",
161 static int map_domain_event_to_state(int event) {
164 case VIR_DOMAIN_EVENT_STARTED:
165 ret = VIR_DOMAIN_RUNNING;
167 case VIR_DOMAIN_EVENT_SUSPENDED:
168 ret = VIR_DOMAIN_PAUSED;
170 case VIR_DOMAIN_EVENT_RESUMED:
171 ret = VIR_DOMAIN_RUNNING;
173 case VIR_DOMAIN_EVENT_STOPPED:
174 ret = VIR_DOMAIN_SHUTOFF;
176 case VIR_DOMAIN_EVENT_SHUTDOWN:
177 ret = VIR_DOMAIN_SHUTDOWN;
179 #ifdef HAVE_DOM_STATE_PMSUSPENDED
180 case VIR_DOMAIN_EVENT_PMSUSPENDED:
181 ret = VIR_DOMAIN_PMSUSPENDED;
184 #ifdef HAVE_DOM_REASON_CRASHED
185 case VIR_DOMAIN_EVENT_CRASHED:
186 ret = VIR_DOMAIN_CRASHED;
190 ret = VIR_DOMAIN_NOSTATE;
195 #ifdef HAVE_DOM_REASON
196 static int map_domain_event_detail_to_reason(int event, int detail) {
199 case VIR_DOMAIN_EVENT_STARTED:
201 case VIR_DOMAIN_EVENT_STARTED_BOOTED: /* Normal startup from boot */
202 ret = VIR_DOMAIN_RUNNING_BOOTED;
204 case VIR_DOMAIN_EVENT_STARTED_MIGRATED: /* Incoming migration from another
206 ret = VIR_DOMAIN_RUNNING_MIGRATED;
208 case VIR_DOMAIN_EVENT_STARTED_RESTORED: /* Restored from a state file */
209 ret = VIR_DOMAIN_RUNNING_RESTORED;
211 case VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT: /* Restored from snapshot */
212 ret = VIR_DOMAIN_RUNNING_FROM_SNAPSHOT;
214 #ifdef HAVE_DOM_REASON_RUNNING_WAKEUP
215 case VIR_DOMAIN_EVENT_STARTED_WAKEUP: /* Started due to wakeup event */
216 ret = VIR_DOMAIN_RUNNING_WAKEUP;
220 ret = VIR_DOMAIN_RUNNING_UNKNOWN;
223 case VIR_DOMAIN_EVENT_SUSPENDED:
225 case VIR_DOMAIN_EVENT_SUSPENDED_PAUSED: /* Normal suspend due to admin
227 ret = VIR_DOMAIN_PAUSED_USER;
229 case VIR_DOMAIN_EVENT_SUSPENDED_MIGRATED: /* Suspended for offline
231 ret = VIR_DOMAIN_PAUSED_MIGRATION;
233 case VIR_DOMAIN_EVENT_SUSPENDED_IOERROR: /* Suspended due to a disk I/O
235 ret = VIR_DOMAIN_PAUSED_IOERROR;
237 case VIR_DOMAIN_EVENT_SUSPENDED_WATCHDOG: /* Suspended due to a watchdog
239 ret = VIR_DOMAIN_PAUSED_WATCHDOG;
241 case VIR_DOMAIN_EVENT_SUSPENDED_RESTORED: /* Restored from paused state
243 ret = VIR_DOMAIN_PAUSED_UNKNOWN;
245 case VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT: /* Restored from paused
247 ret = VIR_DOMAIN_PAUSED_FROM_SNAPSHOT;
249 case VIR_DOMAIN_EVENT_SUSPENDED_API_ERROR: /* Suspended after failure during
251 ret = VIR_DOMAIN_PAUSED_UNKNOWN;
253 #ifdef HAVE_DOM_REASON_POSTCOPY
254 case VIR_DOMAIN_EVENT_SUSPENDED_POSTCOPY: /* Suspended for post-copy
256 ret = VIR_DOMAIN_PAUSED_POSTCOPY;
258 case VIR_DOMAIN_EVENT_SUSPENDED_POSTCOPY_FAILED: /* Suspended after failed
260 ret = VIR_DOMAIN_PAUSED_POSTCOPY_FAILED;
264 ret = VIR_DOMAIN_PAUSED_UNKNOWN;
267 case VIR_DOMAIN_EVENT_RESUMED:
269 case VIR_DOMAIN_EVENT_RESUMED_UNPAUSED: /* Normal resume due to admin
271 ret = VIR_DOMAIN_RUNNING_UNPAUSED;
273 case VIR_DOMAIN_EVENT_RESUMED_MIGRATED: /* Resumed for completion of
275 ret = VIR_DOMAIN_RUNNING_MIGRATED;
277 case VIR_DOMAIN_EVENT_RESUMED_FROM_SNAPSHOT: /* Resumed from snapshot */
278 ret = VIR_DOMAIN_RUNNING_FROM_SNAPSHOT;
280 #ifdef HAVE_DOM_REASON_POSTCOPY
281 case VIR_DOMAIN_EVENT_RESUMED_POSTCOPY: /* Resumed, but migration is still
282 running in post-copy mode */
283 ret = VIR_DOMAIN_RUNNING_POSTCOPY;
287 ret = VIR_DOMAIN_RUNNING_UNKNOWN;
290 case VIR_DOMAIN_EVENT_STOPPED:
292 case VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN: /* Normal shutdown */
293 ret = VIR_DOMAIN_SHUTOFF_SHUTDOWN;
295 case VIR_DOMAIN_EVENT_STOPPED_DESTROYED: /* Forced poweroff from host */
296 ret = VIR_DOMAIN_SHUTOFF_DESTROYED;
298 case VIR_DOMAIN_EVENT_STOPPED_CRASHED: /* Guest crashed */
299 ret = VIR_DOMAIN_SHUTOFF_CRASHED;
301 case VIR_DOMAIN_EVENT_STOPPED_MIGRATED: /* Migrated off to another host */
302 ret = VIR_DOMAIN_SHUTOFF_MIGRATED;
304 case VIR_DOMAIN_EVENT_STOPPED_SAVED: /* Saved to a state file */
305 ret = VIR_DOMAIN_SHUTOFF_SAVED;
307 case VIR_DOMAIN_EVENT_STOPPED_FAILED: /* Host emulator/mgmt failed */
308 ret = VIR_DOMAIN_SHUTOFF_FAILED;
310 case VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT: /* Offline snapshot loaded */
311 ret = VIR_DOMAIN_SHUTOFF_FROM_SNAPSHOT;
314 ret = VIR_DOMAIN_SHUTOFF_UNKNOWN;
317 case VIR_DOMAIN_EVENT_SHUTDOWN:
319 case VIR_DOMAIN_EVENT_SHUTDOWN_FINISHED: /* Guest finished shutdown
321 ret = VIR_DOMAIN_SHUTDOWN_USER;
324 ret = VIR_DOMAIN_SHUTDOWN_UNKNOWN;
327 #ifdef HAVE_DOM_STATE_PMSUSPENDED
328 case VIR_DOMAIN_EVENT_PMSUSPENDED:
330 case VIR_DOMAIN_EVENT_PMSUSPENDED_MEMORY: /* Guest was PM suspended to
332 ret = VIR_DOMAIN_PMSUSPENDED_UNKNOWN;
334 case VIR_DOMAIN_EVENT_PMSUSPENDED_DISK: /* Guest was PM suspended to disk */
335 ret = VIR_DOMAIN_PMSUSPENDED_DISK_UNKNOWN;
338 ret = VIR_DOMAIN_PMSUSPENDED_UNKNOWN;
342 case VIR_DOMAIN_EVENT_CRASHED:
344 case VIR_DOMAIN_EVENT_CRASHED_PANICKED: /* Guest was panicked */
345 ret = VIR_DOMAIN_CRASHED_PANICKED;
348 ret = VIR_DOMAIN_CRASHED_UNKNOWN;
352 ret = VIR_DOMAIN_NOSTATE_UNKNOWN;
357 #define DOMAIN_STATE_REASON_MAX_SIZE 20
358 const char *domain_reasons[][DOMAIN_STATE_REASON_MAX_SIZE] = {
359 [VIR_DOMAIN_NOSTATE][VIR_DOMAIN_NOSTATE_UNKNOWN] =
360 "the reason is unknown",
362 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_UNKNOWN] =
363 "the reason is unknown",
364 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_BOOTED] =
365 "normal startup from boot",
366 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_MIGRATED] =
367 "migrated from another host",
368 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_RESTORED] =
369 "restored from a state file",
370 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_FROM_SNAPSHOT] =
371 "restored from snapshot",
372 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_UNPAUSED] =
373 "returned from paused state",
374 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_MIGRATION_CANCELED] =
375 "returned from migration",
376 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_SAVE_CANCELED] =
377 "returned from failed save process",
378 #ifdef HAVE_DOM_REASON_RUNNING_WAKEUP
379 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_WAKEUP] =
380 "returned from pmsuspended due to wakeup event",
382 #ifdef HAVE_DOM_REASON_CRASHED
383 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_CRASHED] =
384 "resumed from crashed",
386 #ifdef HAVE_DOM_REASON_POSTCOPY
387 [VIR_DOMAIN_RUNNING][VIR_DOMAIN_RUNNING_POSTCOPY] =
388 "running in post-copy migration mode",
390 [VIR_DOMAIN_BLOCKED][VIR_DOMAIN_BLOCKED_UNKNOWN] =
391 "the reason is unknown",
393 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_UNKNOWN] =
394 "the reason is unknown",
395 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_USER] = "paused on user request",
396 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_MIGRATION] =
397 "paused for offline migration",
398 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_SAVE] = "paused for save",
399 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_DUMP] =
400 "paused for offline core dump",
401 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_IOERROR] =
402 "paused due to a disk I/O error",
403 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_WATCHDOG] =
404 "paused due to a watchdog event",
405 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_FROM_SNAPSHOT] =
406 "paused after restoring from snapshot",
407 #ifdef HAVE_DOM_REASON_PAUSED_SHUTTING_DOWN
408 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_SHUTTING_DOWN] =
409 "paused during shutdown process",
411 #ifdef HAVE_DOM_REASON_PAUSED_SNAPSHOT
412 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_SNAPSHOT] =
413 "paused while creating a snapshot",
415 #ifdef HAVE_DOM_REASON_PAUSED_CRASHED
416 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_CRASHED] =
417 "paused due to a guest crash",
419 #ifdef HAVE_DOM_REASON_PAUSED_STARTING_UP
420 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_STARTING_UP] =
421 "the domain is being started",
423 #ifdef HAVE_DOM_REASON_POSTCOPY
424 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_POSTCOPY] =
425 "paused for post-copy migration",
426 [VIR_DOMAIN_PAUSED][VIR_DOMAIN_PAUSED_POSTCOPY_FAILED] =
427 "paused after failed post-copy",
429 [VIR_DOMAIN_SHUTDOWN][VIR_DOMAIN_SHUTDOWN_UNKNOWN] =
430 "the reason is unknown",
431 [VIR_DOMAIN_SHUTDOWN][VIR_DOMAIN_SHUTDOWN_USER] =
432 "shutting down on user request",
434 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_UNKNOWN] =
435 "the reason is unknown",
436 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_SHUTDOWN] = "normal shutdown",
437 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_DESTROYED] = "forced poweroff",
438 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_CRASHED] = "domain crashed",
439 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_MIGRATED] =
440 "migrated to another host",
441 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_SAVED] = "saved to a file",
442 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_FAILED] =
443 "domain failed to start",
444 [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_FROM_SNAPSHOT] =
445 "restored from a snapshot which was taken while domain was shutoff",
447 [VIR_DOMAIN_CRASHED][VIR_DOMAIN_CRASHED_UNKNOWN] =
448 "the reason is unknown",
449 #ifdef VIR_DOMAIN_CRASHED_PANICKED
450 [VIR_DOMAIN_CRASHED][VIR_DOMAIN_CRASHED_PANICKED] = "domain panicked",
453 #ifdef HAVE_DOM_STATE_PMSUSPENDED
454 [VIR_DOMAIN_PMSUSPENDED][VIR_DOMAIN_PMSUSPENDED_UNKNOWN] =
455 "the reason is unknown",
458 #endif /* HAVE_DOM_REASON */
460 #define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
461 #define NANOSEC_IN_SEC 1e9
463 #define GET_STATS(_f, _name, ...) \
465 status = _f(__VA_ARGS__); \
467 ERROR(PLUGIN_NAME ": Failed to get " _name); \
471 static virConnectPtr conn;
472 static char *conn_string;
473 static c_complain_t conn_complain = C_COMPLAIN_INIT_STATIC;
475 /* Node information required for %CPU */
476 static virNodeInfo nodeinfo;
478 /* Seconds between list refreshes, 0 disables completely. */
479 static int interval = 60;
481 /* List of domains, if specified. */
482 static ignorelist_t *il_domains;
483 /* List of block devices, if specified. */
484 static ignorelist_t *il_block_devices;
485 /* List of network interface devices, if specified. */
486 static ignorelist_t *il_interface_devices;
488 static int ignore_device_match(ignorelist_t *, const char *domname,
489 const char *devpath);
491 /* Actual list of block devices found on last refresh. */
492 struct block_device {
493 virDomainPtr dom; /* domain */
494 char *path; /* name of block device */
497 /* Actual list of network interfaces found on last refresh. */
498 struct interface_device {
499 virDomainPtr dom; /* domain */
500 char *path; /* name of interface device */
501 char *address; /* mac address of interface device */
502 char *number; /* interface device number */
505 typedef struct domain_s {
511 struct lv_read_state {
512 /* Actual list of domains found on last refresh. */
516 struct block_device *block_devices;
517 int nr_block_devices;
519 struct interface_device *interface_devices;
520 int nr_interface_devices;
523 static void free_domains(struct lv_read_state *state);
524 static int add_domain(struct lv_read_state *state, virDomainPtr dom,
527 static void free_block_devices(struct lv_read_state *state);
528 static int add_block_device(struct lv_read_state *state, virDomainPtr dom,
531 static void free_interface_devices(struct lv_read_state *state);
532 static int add_interface_device(struct lv_read_state *state, virDomainPtr dom,
533 const char *path, const char *address,
534 unsigned int number);
536 #define METADATA_VM_PARTITION_URI "http://ovirt.org/ovirtmap/tag/1.0"
537 #define METADATA_VM_PARTITION_ELEMENT "tag"
538 #define METADATA_VM_PARTITION_PREFIX "ovirtmap"
540 #define BUFFER_MAX_LEN 256
541 #define PARTITION_TAG_MAX_LEN 32
543 struct lv_read_instance {
544 struct lv_read_state read_state;
545 char tag[PARTITION_TAG_MAX_LEN];
549 struct lv_user_data {
550 struct lv_read_instance inst;
554 #define NR_INSTANCES_DEFAULT 1
555 #define NR_INSTANCES_MAX 128
556 static int nr_instances = NR_INSTANCES_DEFAULT;
557 static struct lv_user_data lv_read_user_data[NR_INSTANCES_MAX];
559 /* HostnameFormat. */
560 #define HF_MAX_FIELDS 3
562 enum hf_field { hf_none = 0, hf_hostname, hf_name, hf_uuid };
564 static enum hf_field hostname_format[HF_MAX_FIELDS] = {hf_name};
566 /* PluginInstanceFormat */
567 #define PLGINST_MAX_FIELDS 2
569 enum plginst_field { plginst_none = 0, plginst_name, plginst_uuid };
571 static enum plginst_field plugin_instance_format[PLGINST_MAX_FIELDS] = {
574 /* BlockDeviceFormat */
575 enum bd_field { target, source };
577 /* InterfaceFormat. */
578 enum if_field { if_address, if_name, if_number };
581 #define EX_STATS_MAX_FIELDS 15
584 ex_stats_disk = 1 << 0,
585 ex_stats_pcpu = 1 << 1,
586 ex_stats_cpu_util = 1 << 2,
587 ex_stats_domain_state = 1 << 3,
588 #ifdef HAVE_PERF_STATS
589 ex_stats_perf = 1 << 4,
591 ex_stats_vcpupin = 1 << 5,
593 ex_stats_disk_err = 1 << 6,
596 ex_stats_fs_info = 1 << 7,
598 #ifdef HAVE_JOB_STATS
599 ex_stats_job_stats_completed = 1 << 8,
600 ex_stats_job_stats_background = 1 << 9,
604 static unsigned int extra_stats = ex_stats_none;
606 struct ex_stats_item {
610 static const struct ex_stats_item ex_stats_table[] = {
611 {"disk", ex_stats_disk},
612 {"pcpu", ex_stats_pcpu},
613 {"cpu_util", ex_stats_cpu_util},
614 {"domain_state", ex_stats_domain_state},
615 #ifdef HAVE_PERF_STATS
616 {"perf", ex_stats_perf},
618 {"vcpupin", ex_stats_vcpupin},
620 {"disk_err", ex_stats_disk_err},
623 {"fs_info", ex_stats_fs_info},
625 #ifdef HAVE_JOB_STATS
626 {"job_stats_completed", ex_stats_job_stats_completed},
627 {"job_stats_background", ex_stats_job_stats_background},
629 {NULL, ex_stats_none},
632 /* BlockDeviceFormatBasename */
633 static bool blockdevice_format_basename;
634 static enum bd_field blockdevice_format = target;
635 static enum if_field interface_format = if_name;
637 /* Time that we last refreshed. */
638 static time_t last_refresh = (time_t)0;
640 static int refresh_lists(struct lv_read_instance *inst);
642 struct lv_block_info {
643 virDomainBlockStatsStruct bi;
645 long long rd_total_times;
646 long long wr_total_times;
649 long long fl_total_times;
652 static void init_block_info(struct lv_block_info *binfo) {
656 binfo->bi.rd_req = -1;
657 binfo->bi.wr_req = -1;
658 binfo->bi.rd_bytes = -1;
659 binfo->bi.wr_bytes = -1;
661 binfo->rd_total_times = -1;
662 binfo->wr_total_times = -1;
664 binfo->fl_total_times = -1;
667 #ifdef HAVE_BLOCK_STATS_FLAGS
669 #define GET_BLOCK_INFO_VALUE(NAME, FIELD) \
670 if (!strcmp(param[i].field, NAME)) { \
671 binfo->FIELD = param[i].value.l; \
675 static int get_block_info(struct lv_block_info *binfo,
676 virTypedParameterPtr param, int nparams) {
677 if (binfo == NULL || param == NULL)
680 for (int i = 0; i < nparams; ++i) {
681 /* ignore type. Everything must be LLONG anyway. */
682 GET_BLOCK_INFO_VALUE("rd_operations", bi.rd_req);
683 GET_BLOCK_INFO_VALUE("wr_operations", bi.wr_req);
684 GET_BLOCK_INFO_VALUE("rd_bytes", bi.rd_bytes);
685 GET_BLOCK_INFO_VALUE("wr_bytes", bi.wr_bytes);
686 GET_BLOCK_INFO_VALUE("rd_total_times", rd_total_times);
687 GET_BLOCK_INFO_VALUE("wr_total_times", wr_total_times);
688 GET_BLOCK_INFO_VALUE("flush_operations", fl_req);
689 GET_BLOCK_INFO_VALUE("flush_total_times", fl_total_times);
695 #undef GET_BLOCK_INFO_VALUE
697 #endif /* HAVE_BLOCK_STATS_FLAGS */
699 /* ERROR(...) macro for virterrors. */
700 #define VIRT_ERROR(conn, s) \
703 err = (conn) ? virConnGetLastError((conn)) : virGetLastError(); \
705 ERROR(PLUGIN_NAME " plugin: %s failed: %s", (s), err->message); \
708 static void init_value_list(value_list_t *vl, virDomainPtr dom) {
710 char uuid[VIR_UUID_STRING_BUFLEN];
712 sstrncpy(vl->plugin, PLUGIN_NAME, sizeof(vl->plugin));
716 /* Construct the hostname field according to HostnameFormat. */
717 for (int i = 0; i < HF_MAX_FIELDS; ++i) {
718 if (hostname_format[i] == hf_none)
722 SSTRNCAT(vl->host, ":", sizeof(vl->host));
724 switch (hostname_format[i]) {
728 SSTRNCAT(vl->host, hostname_g, sizeof(vl->host));
731 name = virDomainGetName(dom);
733 SSTRNCAT(vl->host, name, sizeof(vl->host));
736 if (virDomainGetUUIDString(dom, uuid) == 0)
737 SSTRNCAT(vl->host, uuid, sizeof(vl->host));
742 /* Construct the plugin instance field according to PluginInstanceFormat. */
743 for (int i = 0; i < PLGINST_MAX_FIELDS; ++i) {
744 if (plugin_instance_format[i] == plginst_none)
748 SSTRNCAT(vl->plugin_instance, ":", sizeof(vl->plugin_instance));
750 switch (plugin_instance_format[i]) {
754 name = virDomainGetName(dom);
756 SSTRNCAT(vl->plugin_instance, name, sizeof(vl->plugin_instance));
759 if (virDomainGetUUIDString(dom, uuid) == 0)
760 SSTRNCAT(vl->plugin_instance, uuid, sizeof(vl->plugin_instance));
765 } /* void init_value_list */
767 static int init_notif(notification_t *notif, const virDomainPtr domain,
768 int severity, const char *msg, const char *type,
769 const char *type_instance) {
770 value_list_t vl = VALUE_LIST_INIT;
773 ERROR(PLUGIN_NAME ": init_notif: NULL pointer");
777 init_value_list(&vl, domain);
778 notification_init(notif, severity, msg, vl.host, vl.plugin,
779 vl.plugin_instance, type, type_instance);
780 notif->time = cdtime();
784 static void submit_notif(const virDomainPtr domain, int severity,
785 const char *msg, const char *type,
786 const char *type_instance) {
787 notification_t notif;
789 init_notif(¬if, domain, severity, msg, type, type_instance);
790 plugin_dispatch_notification(¬if);
792 plugin_notification_meta_free(notif.meta);
795 static void submit(virDomainPtr dom, char const *type,
796 char const *type_instance, value_t *values,
798 value_list_t vl = VALUE_LIST_INIT;
799 init_value_list(&vl, dom);
802 vl.values_len = values_len;
804 sstrncpy(vl.type, type, sizeof(vl.type));
805 if (type_instance != NULL)
806 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
808 plugin_dispatch_values(&vl);
811 static void memory_submit(virDomainPtr dom, gauge_t value) {
812 submit(dom, "memory", "total", &(value_t){.gauge = value}, 1);
815 static void memory_stats_submit(gauge_t value, virDomainPtr dom,
817 static const char *tags[] = {"swap_in", "swap_out", "major_fault",
818 "minor_fault", "unused", "available",
819 "actual_balloon", "rss", "usable",
822 if ((tag_index < 0) || (tag_index >= (int)STATIC_ARRAY_SIZE(tags))) {
823 ERROR("virt plugin: Array index out of bounds: tag_index = %d", tag_index);
827 submit(dom, "memory", tags[tag_index], &(value_t){.gauge = value}, 1);
830 static void submit_derive2(const char *type, derive_t v0, derive_t v1,
831 virDomainPtr dom, const char *devname) {
833 {.derive = v0}, {.derive = v1},
836 submit(dom, type, devname, values, STATIC_ARRAY_SIZE(values));
837 } /* void submit_derive2 */
839 static double cpu_ns_to_percent(unsigned int node_cpus,
840 unsigned long long cpu_time_old,
841 unsigned long long cpu_time_new) {
842 double percent = 0.0;
843 unsigned long long cpu_time_diff = 0;
844 double time_diff_sec = CDTIME_T_TO_DOUBLE(plugin_get_interval());
846 if (node_cpus != 0 && time_diff_sec != 0 && cpu_time_old != 0) {
847 cpu_time_diff = cpu_time_new - cpu_time_old;
848 percent = ((double)(100 * cpu_time_diff)) /
849 (time_diff_sec * node_cpus * NANOSEC_IN_SEC);
852 DEBUG(PLUGIN_NAME ": node_cpus=%u cpu_time_old=%" PRIu64
853 " cpu_time_new=%" PRIu64 "cpu_time_diff=%" PRIu64
854 " time_diff_sec=%f percent=%f",
855 node_cpus, (uint64_t)cpu_time_old, (uint64_t)cpu_time_new,
856 (uint64_t)cpu_time_diff, time_diff_sec, percent);
861 static void cpu_submit(const domain_t *dom, unsigned long long cpuTime_new) {
866 if (extra_stats & ex_stats_cpu_util) {
867 /* Computing %CPU requires 2 samples of cpuTime */
868 if (dom->info.cpuTime != 0 && cpuTime_new != 0) {
870 submit(dom->ptr, "percent", "virt_cpu_total",
871 &(value_t){.gauge = cpu_ns_to_percent(
872 nodeinfo.cpus, dom->info.cpuTime, cpuTime_new)},
877 submit(dom->ptr, "virt_cpu_total", NULL, &(value_t){.derive = cpuTime_new},
881 static void vcpu_submit(derive_t value, virDomainPtr dom, int vcpu_nr,
883 char type_instance[DATA_MAX_NAME_LEN];
885 snprintf(type_instance, sizeof(type_instance), "%d", vcpu_nr);
886 submit(dom, type, type_instance, &(value_t){.derive = value}, 1);
889 static void disk_submit(struct lv_block_info *binfo, virDomainPtr dom,
891 char *dev_copy = strdup(dev);
892 const char *type_instance = dev_copy;
897 if (blockdevice_format_basename && blockdevice_format == source)
898 type_instance = basename(dev_copy);
900 if (!type_instance) {
905 char flush_type_instance[DATA_MAX_NAME_LEN];
906 snprintf(flush_type_instance, sizeof(flush_type_instance), "flush-%s",
909 if ((binfo->bi.rd_req != -1) && (binfo->bi.wr_req != -1))
910 submit_derive2("disk_ops", (derive_t)binfo->bi.rd_req,
911 (derive_t)binfo->bi.wr_req, dom, type_instance);
913 if ((binfo->bi.rd_bytes != -1) && (binfo->bi.wr_bytes != -1))
914 submit_derive2("disk_octets", (derive_t)binfo->bi.rd_bytes,
915 (derive_t)binfo->bi.wr_bytes, dom, type_instance);
917 if (extra_stats & ex_stats_disk) {
918 if ((binfo->rd_total_times != -1) && (binfo->wr_total_times != -1))
919 submit_derive2("disk_time", (derive_t)binfo->rd_total_times,
920 (derive_t)binfo->wr_total_times, dom, type_instance);
922 if (binfo->fl_req != -1)
923 submit(dom, "total_requests", flush_type_instance,
924 &(value_t){.derive = (derive_t)binfo->fl_req}, 1);
925 if (binfo->fl_total_times != -1) {
926 derive_t value = binfo->fl_total_times / 1000; // ns -> ms
927 submit(dom, "total_time_in_ms", flush_type_instance,
928 &(value_t){.derive = value}, 1);
935 static unsigned int parse_ex_stats_flags(char **exstats, int numexstats) {
936 unsigned int ex_stats_flags = ex_stats_none;
937 for (int i = 0; i < numexstats; i++) {
938 for (int j = 0; ex_stats_table[j].name != NULL; j++) {
939 if (strcasecmp(exstats[i], ex_stats_table[j].name) == 0) {
940 DEBUG(PLUGIN_NAME " plugin: enabling extra stats for '%s'",
941 ex_stats_table[j].name);
942 ex_stats_flags |= ex_stats_table[j].flag;
946 if (ex_stats_table[j + 1].name == NULL) {
947 ERROR(PLUGIN_NAME ": Unmatched ExtraStats option: %s", exstats[i]);
951 return ex_stats_flags;
954 static void domain_state_submit_notif(virDomainPtr dom, int state, int reason) {
955 if ((state < 0) || ((size_t)state >= STATIC_ARRAY_SIZE(domain_states))) {
956 ERROR(PLUGIN_NAME ": Array index out of bounds: state=%d", state);
960 char msg[DATA_MAX_NAME_LEN];
961 const char *state_str = domain_states[state];
962 #ifdef HAVE_DOM_REASON
964 ((size_t)reason >= STATIC_ARRAY_SIZE(domain_reasons[0]))) {
965 ERROR(PLUGIN_NAME ": Array index out of bounds: reason=%d", reason);
969 const char *reason_str = domain_reasons[state][reason];
970 /* Array size for domain reasons is fixed, but different domain states can
971 * have different number of reasons. We need to check if reason was
972 * successfully parsed */
974 ERROR(PLUGIN_NAME ": Invalid reason (%d) for domain state: %s", reason,
979 const char *reason_str = "N/A";
982 snprintf(msg, sizeof(msg), "Domain state: %s. Reason: %s", state_str,
987 case VIR_DOMAIN_NOSTATE:
988 case VIR_DOMAIN_RUNNING:
989 case VIR_DOMAIN_SHUTDOWN:
990 case VIR_DOMAIN_SHUTOFF:
991 severity = NOTIF_OKAY;
993 case VIR_DOMAIN_BLOCKED:
994 case VIR_DOMAIN_PAUSED:
995 #ifdef DOM_STATE_PMSUSPENDED
996 case VIR_DOMAIN_PMSUSPENDED:
998 severity = NOTIF_WARNING;
1000 case VIR_DOMAIN_CRASHED:
1001 severity = NOTIF_FAILURE;
1004 ERROR(PLUGIN_NAME ": Unrecognized domain state (%d)", state);
1007 submit_notif(dom, severity, msg, "domain_state", NULL);
1010 static int lv_config(const char *key, const char *value) {
1011 if (virInitialize() != 0)
1014 if (il_domains == NULL)
1015 il_domains = ignorelist_create(1);
1016 if (il_block_devices == NULL)
1017 il_block_devices = ignorelist_create(1);
1018 if (il_interface_devices == NULL)
1019 il_interface_devices = ignorelist_create(1);
1021 if (strcasecmp(key, "Connection") == 0) {
1022 char *tmp = strdup(value);
1024 ERROR(PLUGIN_NAME " plugin: Connection strdup failed.");
1032 if (strcasecmp(key, "RefreshInterval") == 0) {
1034 interval = strtol(value, &eptr, 10);
1035 if (eptr == NULL || *eptr != '\0')
1040 if (strcasecmp(key, "Domain") == 0) {
1041 if (ignorelist_add(il_domains, value))
1045 if (strcasecmp(key, "BlockDevice") == 0) {
1046 if (ignorelist_add(il_block_devices, value))
1051 if (strcasecmp(key, "BlockDeviceFormat") == 0) {
1052 if (strcasecmp(value, "target") == 0)
1053 blockdevice_format = target;
1054 else if (strcasecmp(value, "source") == 0)
1055 blockdevice_format = source;
1057 ERROR(PLUGIN_NAME " plugin: unknown BlockDeviceFormat: %s", value);
1062 if (strcasecmp(key, "BlockDeviceFormatBasename") == 0) {
1063 blockdevice_format_basename = IS_TRUE(value) ? true : false;
1066 if (strcasecmp(key, "InterfaceDevice") == 0) {
1067 if (ignorelist_add(il_interface_devices, value))
1072 if (strcasecmp(key, "IgnoreSelected") == 0) {
1073 if (IS_TRUE(value)) {
1074 ignorelist_set_invert(il_domains, 0);
1075 ignorelist_set_invert(il_block_devices, 0);
1076 ignorelist_set_invert(il_interface_devices, 0);
1078 ignorelist_set_invert(il_domains, 1);
1079 ignorelist_set_invert(il_block_devices, 1);
1080 ignorelist_set_invert(il_interface_devices, 1);
1085 if (strcasecmp(key, "HostnameFormat") == 0) {
1087 char *fields[HF_MAX_FIELDS];
1090 value_copy = strdup(value);
1091 if (value_copy == NULL) {
1092 ERROR(PLUGIN_NAME " plugin: strdup failed.");
1096 n = strsplit(value_copy, fields, HF_MAX_FIELDS);
1099 ERROR(PLUGIN_NAME " plugin: HostnameFormat: no fields");
1103 for (int i = 0; i < n; ++i) {
1104 if (strcasecmp(fields[i], "hostname") == 0)
1105 hostname_format[i] = hf_hostname;
1106 else if (strcasecmp(fields[i], "name") == 0)
1107 hostname_format[i] = hf_name;
1108 else if (strcasecmp(fields[i], "uuid") == 0)
1109 hostname_format[i] = hf_uuid;
1111 ERROR(PLUGIN_NAME " plugin: unknown HostnameFormat field: %s",
1119 for (int i = n; i < HF_MAX_FIELDS; ++i)
1120 hostname_format[i] = hf_none;
1125 if (strcasecmp(key, "PluginInstanceFormat") == 0) {
1127 char *fields[PLGINST_MAX_FIELDS];
1130 value_copy = strdup(value);
1131 if (value_copy == NULL) {
1132 ERROR(PLUGIN_NAME " plugin: strdup failed.");
1136 n = strsplit(value_copy, fields, PLGINST_MAX_FIELDS);
1139 ERROR(PLUGIN_NAME " plugin: PluginInstanceFormat: no fields");
1143 for (int i = 0; i < n; ++i) {
1144 if (strcasecmp(fields[i], "none") == 0) {
1145 plugin_instance_format[i] = plginst_none;
1147 } else if (strcasecmp(fields[i], "name") == 0)
1148 plugin_instance_format[i] = plginst_name;
1149 else if (strcasecmp(fields[i], "uuid") == 0)
1150 plugin_instance_format[i] = plginst_uuid;
1152 ERROR(PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s",
1160 for (int i = n; i < PLGINST_MAX_FIELDS; ++i)
1161 plugin_instance_format[i] = plginst_none;
1166 if (strcasecmp(key, "InterfaceFormat") == 0) {
1167 if (strcasecmp(value, "name") == 0)
1168 interface_format = if_name;
1169 else if (strcasecmp(value, "address") == 0)
1170 interface_format = if_address;
1171 else if (strcasecmp(value, "number") == 0)
1172 interface_format = if_number;
1174 ERROR(PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", value);
1180 if (strcasecmp(key, "Instances") == 0) {
1182 double val = strtod(value, &eptr);
1184 if (*eptr != '\0') {
1185 ERROR(PLUGIN_NAME " plugin: Invalid value for Instances = '%s'", value);
1189 ERROR(PLUGIN_NAME " plugin: Instances <= 0 makes no sense.");
1192 if (val > NR_INSTANCES_MAX) {
1193 ERROR(PLUGIN_NAME " plugin: Instances=%f > NR_INSTANCES_MAX=%i"
1194 " use a lower setting or recompile the plugin.",
1195 val, NR_INSTANCES_MAX);
1199 nr_instances = (int)val;
1200 DEBUG(PLUGIN_NAME " plugin: configured %i instances", nr_instances);
1204 if (strcasecmp(key, "ExtraStats") == 0) {
1205 char *localvalue = strdup(value);
1206 if (localvalue != NULL) {
1207 char *exstats[EX_STATS_MAX_FIELDS];
1209 strsplit(localvalue, exstats, STATIC_ARRAY_SIZE(exstats));
1210 extra_stats = parse_ex_stats_flags(exstats, numexstats);
1213 #ifdef HAVE_JOB_STATS
1214 if ((extra_stats & ex_stats_job_stats_completed) &&
1215 (extra_stats & ex_stats_job_stats_background)) {
1216 ERROR(PLUGIN_NAME " plugin: Invalid job stats configuration. Only one "
1217 "type of job statistics can be collected at the same "
1225 if (strcasecmp(key, "PersistentNotification") == 0) {
1226 persistent_notification = IS_TRUE(value);
1230 /* Unrecognised option. */
1234 static int lv_connect(void) {
1236 /* `conn_string == NULL' is acceptable */
1238 /* virDomainGetFSInfo requires full read-write access connection */
1239 if (extra_stats & ex_stats_fs_info)
1240 conn = virConnectOpen(conn_string);
1243 conn = virConnectOpenReadOnly(conn_string);
1245 c_complain(LOG_ERR, &conn_complain,
1246 PLUGIN_NAME " plugin: Unable to connect: "
1247 "virConnectOpen failed.");
1250 int status = virNodeGetInfo(conn, &nodeinfo);
1252 ERROR(PLUGIN_NAME ": virNodeGetInfo failed");
1256 c_release(LOG_NOTICE, &conn_complain,
1257 PLUGIN_NAME " plugin: Connection established.");
1261 static void lv_disconnect(void) {
1263 virConnectClose(conn);
1265 WARNING(PLUGIN_NAME " plugin: closed connection to libvirt");
1268 static int lv_domain_block_info(virDomainPtr dom, const char *path,
1269 struct lv_block_info *binfo) {
1270 #ifdef HAVE_BLOCK_STATS_FLAGS
1272 if (virDomainBlockStatsFlags(dom, path, NULL, &nparams, 0) < 0 ||
1274 VIRT_ERROR(conn, "getting the disk params count");
1278 virTypedParameterPtr params = calloc((size_t)nparams, sizeof(*params));
1279 if (params == NULL) {
1280 ERROR("virt plugin: alloc(%i) for block=%s parameters failed.", nparams,
1286 if (virDomainBlockStatsFlags(dom, path, params, &nparams, 0) < 0) {
1287 VIRT_ERROR(conn, "getting the disk params values");
1289 rc = get_block_info(binfo, params, nparams);
1292 virTypedParamsClear(params, nparams);
1296 return virDomainBlockStats(dom, path, &(binfo->bi), sizeof(binfo->bi));
1297 #endif /* HAVE_BLOCK_STATS_FLAGS */
1300 #ifdef HAVE_PERF_STATS
1301 static void perf_submit(virDomainStatsRecordPtr stats) {
1302 for (int i = 0; i < stats->nparams; ++i) {
1303 /* Replace '.' with '_' in event field to match other metrics' naming
1305 char *c = strchr(stats->params[i].field, '.');
1308 submit(stats->dom, "perf", stats->params[i].field,
1309 &(value_t){.derive = stats->params[i].value.ul}, 1);
1313 static int get_perf_events(virDomainPtr domain) {
1314 virDomainStatsRecordPtr *stats = NULL;
1315 /* virDomainListGetStats requires a NULL terminated list of domains */
1316 virDomainPtr domain_array[] = {domain, NULL};
1319 virDomainListGetStats(domain_array, VIR_DOMAIN_STATS_PERF, &stats, 0);
1321 ERROR("virt plugin: virDomainListGetStats failed with status %i.", status);
1325 for (int i = 0; i < status; ++i)
1326 perf_submit(stats[i]);
1328 virDomainStatsRecordListFree(stats);
1331 #endif /* HAVE_PERF_STATS */
1333 static void vcpu_pin_submit(virDomainPtr dom, int max_cpus, int vcpu,
1334 unsigned char *cpu_maps, int cpu_map_len) {
1335 for (int cpu = 0; cpu < max_cpus; ++cpu) {
1336 char type_instance[DATA_MAX_NAME_LEN];
1337 bool is_set = VIR_CPU_USABLE(cpu_maps, cpu_map_len, vcpu, cpu);
1339 snprintf(type_instance, sizeof(type_instance), "vcpu_%d-cpu_%d", vcpu, cpu);
1340 submit(dom, "cpu_affinity", type_instance, &(value_t){.gauge = is_set}, 1);
1344 static int get_vcpu_stats(virDomainPtr domain, unsigned short nr_virt_cpu) {
1345 int max_cpus = VIR_NODEINFO_MAXCPUS(nodeinfo);
1346 int cpu_map_len = VIR_CPU_MAPLEN(max_cpus);
1348 virVcpuInfoPtr vinfo = calloc(nr_virt_cpu, sizeof(vinfo[0]));
1349 if (vinfo == NULL) {
1350 ERROR(PLUGIN_NAME " plugin: calloc failed.");
1354 unsigned char *cpumaps = calloc(nr_virt_cpu, cpu_map_len);
1355 if (cpumaps == NULL) {
1356 ERROR(PLUGIN_NAME " plugin: calloc failed.");
1362 virDomainGetVcpus(domain, vinfo, nr_virt_cpu, cpumaps, cpu_map_len);
1364 ERROR(PLUGIN_NAME " plugin: virDomainGetVcpus failed with status %i.",
1371 for (int i = 0; i < nr_virt_cpu; ++i) {
1372 vcpu_submit(vinfo[i].cpuTime, domain, vinfo[i].number, "virt_vcpu");
1373 if (extra_stats & ex_stats_vcpupin)
1374 vcpu_pin_submit(domain, max_cpus, i, cpumaps, cpu_map_len);
1382 #ifdef HAVE_CPU_STATS
1383 static int get_pcpu_stats(virDomainPtr dom) {
1384 int nparams = virDomainGetCPUStats(dom, NULL, 0, -1, 1, 0);
1386 VIRT_ERROR(conn, "getting the CPU params count");
1390 virTypedParameterPtr param = calloc(nparams, sizeof(virTypedParameter));
1391 if (param == NULL) {
1392 ERROR(PLUGIN_NAME " plugin: alloc(%i) for cpu parameters failed.", nparams);
1396 int ret = virDomainGetCPUStats(dom, param, nparams, -1, 1, 0); // total stats.
1398 virTypedParamsClear(param, nparams);
1400 VIRT_ERROR(conn, "getting the CPU params values");
1404 unsigned long long total_user_cpu_time = 0;
1405 unsigned long long total_syst_cpu_time = 0;
1407 for (int i = 0; i < nparams; ++i) {
1408 if (!strcmp(param[i].field, "user_time"))
1409 total_user_cpu_time = param[i].value.ul;
1410 else if (!strcmp(param[i].field, "system_time"))
1411 total_syst_cpu_time = param[i].value.ul;
1414 if (total_user_cpu_time > 0 || total_syst_cpu_time > 0)
1415 submit_derive2("ps_cputime", total_user_cpu_time, total_syst_cpu_time, dom,
1418 virTypedParamsClear(param, nparams);
1423 #endif /* HAVE_CPU_STATS */
1425 #ifdef HAVE_DOM_REASON
1427 static void domain_state_submit(virDomainPtr dom, int state, int reason) {
1428 value_t values[] = {
1429 {.gauge = (gauge_t)state}, {.gauge = (gauge_t)reason},
1432 submit(dom, "domain_state", NULL, values, STATIC_ARRAY_SIZE(values));
1435 static int get_domain_state(virDomainPtr domain) {
1436 int domain_state = 0;
1437 int domain_reason = 0;
1439 int status = virDomainGetState(domain, &domain_state, &domain_reason, 0);
1441 ERROR(PLUGIN_NAME " plugin: virDomainGetState failed with status %i.",
1446 domain_state_submit(domain, domain_state, domain_reason);
1451 #ifdef HAVE_LIST_ALL_DOMAINS
1452 static int get_domain_state_notify(virDomainPtr domain) {
1453 int domain_state = 0;
1454 int domain_reason = 0;
1456 int status = virDomainGetState(domain, &domain_state, &domain_reason, 0);
1458 ERROR(PLUGIN_NAME " plugin: virDomainGetState failed with status %i.",
1463 if (persistent_notification)
1464 domain_state_submit_notif(domain, domain_state, domain_reason);
1468 #endif /* HAVE_LIST_ALL_DOMAINS */
1469 #endif /* HAVE_DOM_REASON */
1471 static int get_memory_stats(virDomainPtr domain) {
1472 virDomainMemoryStatPtr minfo =
1473 calloc(VIR_DOMAIN_MEMORY_STAT_NR, sizeof(virDomainMemoryStatStruct));
1474 if (minfo == NULL) {
1475 ERROR("virt plugin: malloc failed.");
1480 virDomainMemoryStats(domain, minfo, VIR_DOMAIN_MEMORY_STAT_NR, 0);
1481 if (mem_stats < 0) {
1482 ERROR("virt plugin: virDomainMemoryStats failed with mem_stats %i.",
1488 for (int i = 0; i < mem_stats; i++)
1489 memory_stats_submit((gauge_t)minfo[i].val * 1024, domain, minfo[i].tag);
1495 #ifdef HAVE_DISK_ERR
1496 static void disk_err_submit(virDomainPtr domain,
1497 virDomainDiskErrorPtr disk_err) {
1498 submit(domain, "disk_error", disk_err->disk,
1499 &(value_t){.gauge = disk_err->error}, 1);
1502 static int get_disk_err(virDomainPtr domain) {
1503 /* Get preferred size of disk errors array */
1504 int disk_err_count = virDomainGetDiskErrors(domain, NULL, 0, 0);
1505 if (disk_err_count == -1) {
1507 " plugin: failed to get preferred size of disk errors array");
1512 " plugin: preferred size of disk errors array: %d for domain %s",
1513 disk_err_count, virDomainGetName(domain));
1514 virDomainDiskError disk_err[disk_err_count];
1516 disk_err_count = virDomainGetDiskErrors(domain, disk_err, disk_err_count, 0);
1517 if (disk_err_count == -1) {
1518 ERROR(PLUGIN_NAME " plugin: virDomainGetDiskErrors failed with status %d",
1523 DEBUG(PLUGIN_NAME " plugin: detected %d disk errors in domain %s",
1524 disk_err_count, virDomainGetName(domain));
1526 for (int i = 0; i < disk_err_count; ++i) {
1527 disk_err_submit(domain, &disk_err[i]);
1528 sfree(disk_err[i].disk);
1533 #endif /* HAVE_DISK_ERR */
1535 static int get_block_stats(struct block_device *block_dev) {
1538 ERROR(PLUGIN_NAME " plugin: get_block_stats NULL pointer");
1542 struct lv_block_info binfo;
1543 init_block_info(&binfo);
1545 if (lv_domain_block_info(block_dev->dom, block_dev->path, &binfo) < 0) {
1546 ERROR(PLUGIN_NAME " plugin: lv_domain_block_info failed");
1550 disk_submit(&binfo, block_dev->dom, block_dev->path);
1556 #define NM_ADD_ITEM(_fun, _name, _val) \
1558 ret = _fun(¬if, _name, _val); \
1560 ERROR(PLUGIN_NAME " plugin: failed to add notification metadata"); \
1565 #define NM_ADD_STR_ITEMS(_items, _size) \
1567 for (size_t _i = 0; _i < _size; ++_i) { \
1569 " plugin: Adding notification metadata name=%s value=%s", \
1570 _items[_i].name, _items[_i].value); \
1571 NM_ADD_ITEM(plugin_notification_meta_add_string, _items[_i].name, \
1572 _items[_i].value); \
1576 static int fs_info_notify(virDomainPtr domain, virDomainFSInfoPtr fs_info) {
1577 notification_t notif;
1580 /* Local struct, just for the purpose of this function. */
1581 typedef struct nm_str_item_s {
1586 nm_str_item_t fs_dev_alias[fs_info->ndevAlias];
1587 nm_str_item_t fs_str_items[] = {
1588 {.name = "mountpoint", .value = fs_info->mountpoint},
1589 {.name = "name", .value = fs_info->name},
1590 {.name = "fstype", .value = fs_info->fstype}};
1592 for (size_t i = 0; i < fs_info->ndevAlias; ++i) {
1593 fs_dev_alias[i].name = "devAlias";
1594 fs_dev_alias[i].value = fs_info->devAlias[i];
1597 init_notif(¬if, domain, NOTIF_OKAY, "File system information",
1598 "file_system", NULL);
1599 NM_ADD_STR_ITEMS(fs_str_items, STATIC_ARRAY_SIZE(fs_str_items));
1600 NM_ADD_ITEM(plugin_notification_meta_add_unsigned_int, "ndevAlias",
1601 fs_info->ndevAlias);
1602 NM_ADD_STR_ITEMS(fs_dev_alias, fs_info->ndevAlias);
1604 plugin_dispatch_notification(¬if);
1608 plugin_notification_meta_free(notif.meta);
1612 #undef RETURN_ON_ERR
1613 #undef NM_ADD_STR_ITEMS
1615 static int get_fs_info(virDomainPtr domain) {
1616 virDomainFSInfoPtr *fs_info = NULL;
1619 int mount_points_cnt = virDomainGetFSInfo(domain, &fs_info, 0);
1620 if (mount_points_cnt == -1) {
1621 ERROR(PLUGIN_NAME " plugin: virDomainGetFSInfo failed: %d",
1623 return mount_points_cnt;
1626 for (int i = 0; i < mount_points_cnt; ++i) {
1627 if (fs_info_notify(domain, fs_info[i]) != 0) {
1628 ERROR(PLUGIN_NAME " plugin: failed to send file system notification "
1629 "for mount point %s",
1630 fs_info[i]->mountpoint);
1633 virDomainFSInfoFree(fs_info[i]);
1640 #endif /* HAVE_FS_INFO */
1642 #ifdef HAVE_JOB_STATS
1643 static void job_stats_submit(virDomainPtr domain, virTypedParameterPtr param) {
1646 if (param->type == VIR_TYPED_PARAM_INT)
1647 vl.derive = param->value.i;
1648 else if (param->type == VIR_TYPED_PARAM_UINT)
1649 vl.derive = param->value.ui;
1650 else if (param->type == VIR_TYPED_PARAM_LLONG)
1651 vl.derive = param->value.l;
1652 else if (param->type == VIR_TYPED_PARAM_ULLONG)
1653 vl.derive = param->value.ul;
1654 else if (param->type == VIR_TYPED_PARAM_DOUBLE)
1655 vl.derive = param->value.d;
1656 else if (param->type == VIR_TYPED_PARAM_BOOLEAN)
1657 vl.derive = param->value.b;
1658 else if (param->type == VIR_TYPED_PARAM_STRING) {
1659 submit_notif(domain, NOTIF_OKAY, param->value.s, "job_stats", param->field);
1662 ERROR(PLUGIN_NAME " plugin: unrecognized virTypedParameterType");
1666 submit(domain, "job_stats", param->field, &vl, 1);
1669 static int get_job_stats(virDomainPtr domain) {
1673 virTypedParameterPtr params = NULL;
1674 int flags = (extra_stats & ex_stats_job_stats_completed)
1675 ? VIR_DOMAIN_JOB_STATS_COMPLETED
1678 ret = virDomainGetJobStats(domain, &job_type, ¶ms, &nparams, flags);
1680 ERROR(PLUGIN_NAME " plugin: virDomainGetJobStats failed: %d", ret);
1684 DEBUG(PLUGIN_NAME " plugin: job_type=%d nparams=%d", job_type, nparams);
1686 for (int i = 0; i < nparams; ++i) {
1687 DEBUG(PLUGIN_NAME " plugin: param[%d] field=%s type=%d", i, params[i].field,
1689 job_stats_submit(domain, ¶ms[i]);
1692 virTypedParamsFree(params, nparams);
1695 #endif /* HAVE_JOB_STATS */
1697 static int get_domain_metrics(domain_t *domain) {
1698 if (!domain || !domain->ptr) {
1699 ERROR(PLUGIN_NAME "plugin: get_domain_metrics: NULL pointer");
1704 int status = virDomainGetInfo(domain->ptr, &info);
1706 ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
1711 if (extra_stats & ex_stats_domain_state) {
1712 #ifdef HAVE_DOM_REASON
1713 /* At this point we already know domain's state from virDomainGetInfo call,
1714 * however it doesn't provide a reason for entering particular state.
1715 * We need to get it from virDomainGetState.
1717 GET_STATS(get_domain_state, "domain reason", domain->ptr);
1721 /* Gather remaining stats only for running domains */
1722 if (info.state != VIR_DOMAIN_RUNNING)
1725 #ifdef HAVE_CPU_STATS
1726 if (extra_stats & ex_stats_pcpu)
1727 get_pcpu_stats(domain->ptr);
1730 cpu_submit(domain, info.cpuTime);
1732 memory_submit(domain->ptr, (gauge_t)info.memory * 1024);
1734 GET_STATS(get_vcpu_stats, "vcpu stats", domain->ptr, info.nrVirtCpu);
1735 GET_STATS(get_memory_stats, "memory stats", domain->ptr);
1737 #ifdef HAVE_PERF_STATS
1738 if (extra_stats & ex_stats_perf)
1739 GET_STATS(get_perf_events, "performance monitoring events", domain->ptr);
1743 if (extra_stats & ex_stats_fs_info)
1744 GET_STATS(get_fs_info, "file system info", domain->ptr);
1747 #ifdef HAVE_DISK_ERR
1748 if (extra_stats & ex_stats_disk_err)
1749 GET_STATS(get_disk_err, "disk errors", domain->ptr);
1752 #ifdef HAVE_JOB_STATS
1754 (ex_stats_job_stats_completed | ex_stats_job_stats_background))
1755 GET_STATS(get_job_stats, "job stats", domain->ptr);
1758 /* Update cached virDomainInfo. It has to be done after cpu_submit */
1759 memcpy(&domain->info, &info, sizeof(domain->info));
1764 static int get_if_dev_stats(struct interface_device *if_dev) {
1765 virDomainInterfaceStatsStruct stats = {0};
1766 char *display_name = NULL;
1769 ERROR(PLUGIN_NAME " plugin: get_if_dev_stats: NULL pointer");
1773 switch (interface_format) {
1775 display_name = if_dev->address;
1778 display_name = if_dev->number;
1782 display_name = if_dev->path;
1785 if (virDomainInterfaceStats(if_dev->dom, if_dev->path, &stats,
1786 sizeof(stats)) != 0) {
1787 ERROR(PLUGIN_NAME " plugin: virDomainInterfaceStats failed");
1791 if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
1792 submit_derive2("if_octets", (derive_t)stats.rx_bytes,
1793 (derive_t)stats.tx_bytes, if_dev->dom, display_name);
1795 if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
1796 submit_derive2("if_packets", (derive_t)stats.rx_packets,
1797 (derive_t)stats.tx_packets, if_dev->dom, display_name);
1799 if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
1800 submit_derive2("if_errors", (derive_t)stats.rx_errs,
1801 (derive_t)stats.tx_errs, if_dev->dom, display_name);
1803 if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
1804 submit_derive2("if_dropped", (derive_t)stats.rx_drop,
1805 (derive_t)stats.tx_drop, if_dev->dom, display_name);
1809 static int domain_lifecycle_event_cb(__attribute__((unused)) virConnectPtr con_,
1810 virDomainPtr dom, int event, int detail,
1811 __attribute__((unused)) void *opaque) {
1812 int domain_state = map_domain_event_to_state(event);
1813 int domain_reason = 0; /* 0 means UNKNOWN reason for any state */
1814 #ifdef HAVE_DOM_REASON
1815 domain_reason = map_domain_event_detail_to_reason(event, detail);
1817 domain_state_submit_notif(dom, domain_state, domain_reason);
1822 static int register_event_impl(void) {
1823 if (virEventRegisterDefaultImpl() < 0) {
1824 virErrorPtr err = virGetLastError();
1826 " plugin: error while event implementation registering: %s",
1827 err && err->message ? err->message : "Unknown error");
1834 static void virt_notif_thread_set_active(virt_notif_thread_t *thread_data,
1835 const bool active) {
1836 assert(thread_data != NULL);
1837 pthread_mutex_lock(&thread_data->active_mutex);
1838 thread_data->is_active = active;
1839 pthread_mutex_unlock(&thread_data->active_mutex);
1842 static bool virt_notif_thread_is_active(virt_notif_thread_t *thread_data) {
1843 bool active = false;
1845 assert(thread_data != NULL);
1846 pthread_mutex_lock(&thread_data->active_mutex);
1847 active = thread_data->is_active;
1848 pthread_mutex_unlock(&thread_data->active_mutex);
1853 /* worker function running default event implementation */
1854 static void *event_loop_worker(void *arg) {
1855 virt_notif_thread_t *thread_data = (virt_notif_thread_t *)arg;
1857 while (virt_notif_thread_is_active(thread_data)) {
1858 if (virEventRunDefaultImpl() < 0) {
1859 virErrorPtr err = virGetLastError();
1860 ERROR(PLUGIN_NAME " plugin: failed to run event loop: %s\n",
1861 err && err->message ? err->message : "Unknown error");
1868 static int virt_notif_thread_init(virt_notif_thread_t *thread_data) {
1871 assert(thread_data != NULL);
1872 ret = pthread_mutex_init(&thread_data->active_mutex, NULL);
1874 ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
1879 * '0' and positive integers are meaningful ID's, therefore setting
1880 * domain_event_cb_id to '-1'
1882 thread_data->domain_event_cb_id = -1;
1883 pthread_mutex_lock(&thread_data->active_mutex);
1884 thread_data->is_active = false;
1885 pthread_mutex_unlock(&thread_data->active_mutex);
1890 /* register domain event callback and start event loop thread */
1891 static int start_event_loop(virt_notif_thread_t *thread_data) {
1892 assert(thread_data != NULL);
1893 thread_data->domain_event_cb_id = virConnectDomainEventRegisterAny(
1894 conn, NULL, VIR_DOMAIN_EVENT_ID_LIFECYCLE,
1895 VIR_DOMAIN_EVENT_CALLBACK(domain_lifecycle_event_cb), NULL, NULL);
1896 if (thread_data->domain_event_cb_id == -1) {
1897 ERROR(PLUGIN_NAME " plugin: error while callback registering");
1901 virt_notif_thread_set_active(thread_data, 1);
1902 if (pthread_create(&thread_data->event_loop_tid, NULL, event_loop_worker,
1904 ERROR(PLUGIN_NAME " plugin: failed event loop thread creation");
1905 virConnectDomainEventDeregisterAny(conn, thread_data->domain_event_cb_id);
1912 /* stop event loop thread and deregister callback */
1913 static void stop_event_loop(virt_notif_thread_t *thread_data) {
1914 /* stopping loop and de-registering event handler*/
1915 virt_notif_thread_set_active(thread_data, 0);
1916 if (conn != NULL && thread_data->domain_event_cb_id != -1)
1917 virConnectDomainEventDeregisterAny(conn, thread_data->domain_event_cb_id);
1919 if (pthread_join(notif_thread.event_loop_tid, NULL) != 0)
1920 ERROR(PLUGIN_NAME " plugin: stopping notification thread failed");
1923 static int persistent_domains_state_notification(void) {
1926 #ifdef HAVE_LIST_ALL_DOMAINS
1927 virDomainPtr *domains = NULL;
1928 n = virConnectListAllDomains(conn, &domains,
1929 VIR_CONNECT_LIST_DOMAINS_PERSISTENT);
1931 VIRT_ERROR(conn, "reading list of persistent domains");
1934 DEBUG(PLUGIN_NAME " plugin: getting state of %i persistent domains", n);
1935 /* Fetch each persistent domain's state and notify it */
1937 for (int i = 0; i < n; ++i) {
1938 status = get_domain_state_notify(domains[i]);
1941 ERROR(PLUGIN_NAME " plugin: could not notify state of domain %s",
1942 virDomainGetName(domains[i]));
1944 virDomainFree(domains[i]);
1948 DEBUG(PLUGIN_NAME " plugin: notified state of %i persistent domains",
1952 n = virConnectNumOfDomains(conn);
1955 /* Get list of domains. */
1956 domids = calloc(n, sizeof(*domids));
1957 if (domids == NULL) {
1958 ERROR(PLUGIN_NAME " plugin: calloc failed.");
1961 n = virConnectListDomains(conn, domids, n);
1963 VIRT_ERROR(conn, "reading list of domains");
1967 /* Fetch info of each active domain and notify it */
1968 for (int i = 0; i < n; ++i) {
1970 virDomainPtr dom = NULL;
1971 dom = virDomainLookupByID(conn, domids[i]);
1973 VIRT_ERROR(conn, "virDomainLookupByID");
1974 /* Could be that the domain went away -- ignore it anyway. */
1977 status = virDomainGetInfo(dom, &info);
1979 /* virDomainGetState is not available. Submit 0, which corresponds to
1980 * unknown reason. */
1981 domain_state_submit_notif(dom, info.state, 0);
1983 ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
1995 static int lv_read(user_data_t *ud) {
1997 struct lv_read_instance *inst = NULL;
1998 struct lv_read_state *state = NULL;
2000 if (ud->data == NULL) {
2001 ERROR(PLUGIN_NAME " plugin: NULL userdata");
2006 state = &inst->read_state;
2008 bool reconnect = conn == NULL ? true : false;
2009 /* event implementation must be registered before connection is opened */
2010 if (inst->id == 0) {
2011 if (!persistent_notification && reconnect)
2012 if (register_event_impl() != 0)
2015 if (lv_connect() < 0)
2018 if (!persistent_notification && reconnect && conn != NULL)
2019 if (start_event_loop(¬if_thread) != 0)
2025 /* Need to refresh domain or device lists? */
2026 if ((last_refresh == (time_t)0) ||
2027 ((interval > 0) && ((last_refresh + interval) <= t))) {
2028 if (refresh_lists(inst) != 0) {
2029 if (inst->id == 0) {
2030 if (!persistent_notification)
2031 stop_event_loop(¬if_thread);
2039 /* persistent domains state notifications are handled by instance 0 */
2040 if (inst->id == 0 && persistent_notification) {
2041 int status = persistent_domains_state_notification();
2043 DEBUG(PLUGIN_NAME " plugin: persistent_domains_state_notifications "
2044 "returned with status %i",
2049 for (int i = 0; i < state->nr_domains; ++i)
2050 DEBUG(PLUGIN_NAME " plugin: domain %s",
2051 virDomainGetName(state->domains[i].ptr));
2052 for (int i = 0; i < state->nr_block_devices; ++i)
2053 DEBUG(PLUGIN_NAME " plugin: block device %d %s:%s", i,
2054 virDomainGetName(state->block_devices[i].dom),
2055 state->block_devices[i].path);
2056 for (int i = 0; i < state->nr_interface_devices; ++i)
2057 DEBUG(PLUGIN_NAME " plugin: interface device %d %s:%s", i,
2058 virDomainGetName(state->interface_devices[i].dom),
2059 state->interface_devices[i].path);
2062 /* Get domains' metrics */
2063 for (int i = 0; i < state->nr_domains; ++i) {
2064 domain_t *dom = &state->domains[i];
2067 status = get_domain_metrics(dom);
2068 #ifdef HAVE_DOM_REASON
2070 status = get_domain_state(dom->ptr);
2074 ERROR(PLUGIN_NAME " failed to get metrics for domain=%s",
2075 virDomainGetName(dom->ptr));
2078 /* Get block device stats for each domain. */
2079 for (int i = 0; i < state->nr_block_devices; ++i) {
2080 int status = get_block_stats(&state->block_devices[i]);
2083 " failed to get stats for block device (%s) in domain %s",
2084 state->block_devices[i].path,
2085 virDomainGetName(state->block_devices[i].dom));
2088 /* Get interface stats for each domain. */
2089 for (int i = 0; i < state->nr_interface_devices; ++i) {
2090 int status = get_if_dev_stats(&state->interface_devices[i]);
2093 " failed to get interface stats for device (%s) in domain %s",
2094 state->interface_devices[i].path,
2095 virDomainGetName(state->interface_devices[i].dom));
2101 static int lv_init_instance(size_t i, plugin_read_cb callback) {
2102 struct lv_user_data *lv_ud = &(lv_read_user_data[i]);
2103 struct lv_read_instance *inst = &(lv_ud->inst);
2105 memset(lv_ud, 0, sizeof(*lv_ud));
2107 snprintf(inst->tag, sizeof(inst->tag), "%s-%" PRIsz, PLUGIN_NAME, i);
2110 user_data_t *ud = &(lv_ud->ud);
2112 ud->free_func = NULL;
2114 INFO(PLUGIN_NAME " plugin: reader %s initialized", inst->tag);
2116 return plugin_register_complex_read(NULL, inst->tag, callback, 0, ud);
2119 static void lv_clean_read_state(struct lv_read_state *state) {
2120 free_block_devices(state);
2121 free_interface_devices(state);
2122 free_domains(state);
2125 static void lv_fini_instance(size_t i) {
2126 struct lv_read_instance *inst = &(lv_read_user_data[i].inst);
2127 struct lv_read_state *state = &(inst->read_state);
2129 lv_clean_read_state(state);
2131 INFO(PLUGIN_NAME " plugin: reader %s finalized", inst->tag);
2134 static int lv_init(void) {
2135 if (virInitialize() != 0)
2138 /* event implementation must be registered before connection is opened */
2139 if (!persistent_notification)
2140 if (register_event_impl() != 0)
2143 if (lv_connect() != 0)
2146 DEBUG(PLUGIN_NAME " plugin: starting event loop");
2148 if (!persistent_notification) {
2149 virt_notif_thread_init(¬if_thread);
2150 if (start_event_loop(¬if_thread) != 0)
2154 DEBUG(PLUGIN_NAME " plugin: starting %i instances", nr_instances);
2156 for (int i = 0; i < nr_instances; ++i)
2157 if (lv_init_instance(i, lv_read) != 0)
2164 * returns 0 on success and <0 on error
2166 static int lv_domain_get_tag(xmlXPathContextPtr xpath_ctx, const char *dom_name,
2168 char xpath_str[BUFFER_MAX_LEN] = {'\0'};
2169 xmlXPathObjectPtr xpath_obj = NULL;
2170 xmlNodePtr xml_node = NULL;
2174 err = xmlXPathRegisterNs(xpath_ctx,
2175 (const xmlChar *)METADATA_VM_PARTITION_PREFIX,
2176 (const xmlChar *)METADATA_VM_PARTITION_URI);
2178 ERROR(PLUGIN_NAME " plugin: xmlXpathRegisterNs(%s, %s) failed on domain %s",
2179 METADATA_VM_PARTITION_PREFIX, METADATA_VM_PARTITION_URI, dom_name);
2183 snprintf(xpath_str, sizeof(xpath_str), "/domain/metadata/%s:%s/text()",
2184 METADATA_VM_PARTITION_PREFIX, METADATA_VM_PARTITION_ELEMENT);
2185 xpath_obj = xmlXPathEvalExpression((xmlChar *)xpath_str, xpath_ctx);
2186 if (xpath_obj == NULL) {
2187 ERROR(PLUGIN_NAME " plugin: xmlXPathEval(%s) failed on domain %s",
2188 xpath_str, dom_name);
2192 if (xpath_obj->type != XPATH_NODESET) {
2193 ERROR(PLUGIN_NAME " plugin: xmlXPathEval(%s) unexpected return type %d "
2194 "(wanted %d) on domain %s",
2195 xpath_str, xpath_obj->type, XPATH_NODESET, dom_name);
2200 * from now on there is no real error, it's ok if a domain
2201 * doesn't have the metadata partition tag.
2204 if (xpath_obj->nodesetval == NULL || xpath_obj->nodesetval->nodeNr != 1) {
2205 DEBUG(PLUGIN_NAME " plugin: xmlXPathEval(%s) return nodeset size=%i "
2206 "expected=1 on domain %s",
2208 (xpath_obj->nodesetval == NULL) ? 0 : xpath_obj->nodesetval->nodeNr,
2211 xml_node = xpath_obj->nodesetval->nodeTab[0];
2212 sstrncpy(dom_tag, (const char *)xml_node->content, PARTITION_TAG_MAX_LEN);
2216 /* deregister to clean up */
2217 err = xmlXPathRegisterNs(xpath_ctx,
2218 (const xmlChar *)METADATA_VM_PARTITION_PREFIX, NULL);
2220 /* we can't really recover here */
2222 " plugin: deregistration of namespace %s failed for domain %s",
2223 METADATA_VM_PARTITION_PREFIX, dom_name);
2226 xmlXPathFreeObject(xpath_obj);
2231 static int is_known_tag(const char *dom_tag) {
2232 for (int i = 0; i < nr_instances; ++i)
2233 if (!strcmp(dom_tag, lv_read_user_data[i].inst.tag))
2238 static int lv_instance_include_domain(struct lv_read_instance *inst,
2239 const char *dom_name,
2240 const char *dom_tag) {
2241 if ((dom_tag[0] != '\0') && (strcmp(dom_tag, inst->tag) == 0))
2244 /* instance#0 will always be there, so it is in charge of extra duties */
2245 if (inst->id == 0) {
2246 if (dom_tag[0] == '\0' || !is_known_tag(dom_tag)) {
2247 DEBUG(PLUGIN_NAME " plugin#%s: refreshing domain %s "
2248 "with unknown tag '%s'",
2249 inst->tag, dom_name, dom_tag);
2257 static int refresh_lists(struct lv_read_instance *inst) {
2258 struct lv_read_state *state = &inst->read_state;
2261 #ifndef HAVE_LIST_ALL_DOMAINS
2262 n = virConnectNumOfDomains(conn);
2264 VIRT_ERROR(conn, "reading number of domains");
2269 lv_clean_read_state(state);
2271 #ifndef HAVE_LIST_ALL_DOMAINS
2276 #ifdef HAVE_LIST_ALL_DOMAINS
2277 virDomainPtr *domains, *domains_inactive;
2278 int m = virConnectListAllDomains(conn, &domains_inactive,
2279 VIR_CONNECT_LIST_DOMAINS_INACTIVE);
2280 n = virConnectListAllDomains(conn, &domains, VIR_CONNECT_LIST_DOMAINS_ACTIVE);
2284 /* Get list of domains. */
2285 domids = calloc(n, sizeof(*domids));
2286 if (domids == NULL) {
2287 ERROR(PLUGIN_NAME " plugin: calloc failed.");
2291 n = virConnectListDomains(conn, domids, n);
2295 VIRT_ERROR(conn, "reading list of domains");
2296 #ifndef HAVE_LIST_ALL_DOMAINS
2299 for (int i = 0; i < m; ++i)
2300 virDomainFree(domains_inactive[i]);
2301 sfree(domains_inactive);
2306 #ifdef HAVE_LIST_ALL_DOMAINS
2307 for (int i = 0; i < m; ++i)
2308 if (add_domain(state, domains_inactive[i], 0) < 0) {
2309 ERROR(PLUGIN_NAME " plugin: malloc failed.");
2310 virDomainFree(domains_inactive[i]);
2311 domains_inactive[i] = NULL;
2316 /* Fetch each domain and add it to the list, unless ignore. */
2317 for (int i = 0; i < n; ++i) {
2320 xmlDocPtr xml_doc = NULL;
2321 xmlXPathContextPtr xpath_ctx = NULL;
2322 xmlXPathObjectPtr xpath_obj = NULL;
2323 char tag[PARTITION_TAG_MAX_LEN] = {'\0'};
2327 #ifdef HAVE_LIST_ALL_DOMAINS
2328 virDomainPtr dom = domains[i];
2330 virDomainPtr dom = NULL;
2331 dom = virDomainLookupByID(conn, domids[i]);
2333 VIRT_ERROR(conn, "virDomainLookupByID");
2334 /* Could be that the domain went away -- ignore it anyway. */
2339 if (add_domain(state, dom, 1) < 0) {
2341 * When domain is already tracked, then there is
2342 * no problem with memory handling (will be freed
2343 * with the rest of domains cached data)
2344 * But in case of error like this (error occurred
2345 * before adding domain to track) we have to take
2346 * care it ourselves and call virDomainFree
2348 ERROR(PLUGIN_NAME " plugin: malloc failed.");
2353 name = virDomainGetName(dom);
2355 VIRT_ERROR(conn, "virDomainGetName");
2359 status = virDomainGetInfo(dom, &info);
2361 ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
2366 if (info.state != VIR_DOMAIN_RUNNING) {
2367 DEBUG(PLUGIN_NAME " plugin: skipping inactive domain %s", name);
2371 if (il_domains && ignorelist_match(il_domains, name) != 0)
2374 /* Get a list of devices for this domain. */
2375 xml = virDomainGetXMLDesc(dom, 0);
2377 VIRT_ERROR(conn, "virDomainGetXMLDesc");
2381 /* Yuck, XML. Parse out the devices. */
2382 xml_doc = xmlReadDoc((xmlChar *)xml, NULL, NULL, XML_PARSE_NONET);
2383 if (xml_doc == NULL) {
2384 VIRT_ERROR(conn, "xmlReadDoc");
2388 xpath_ctx = xmlXPathNewContext(xml_doc);
2390 if (lv_domain_get_tag(xpath_ctx, name, tag) < 0) {
2391 ERROR(PLUGIN_NAME " plugin: lv_domain_get_tag failed.");
2395 if (!lv_instance_include_domain(inst, name, tag))
2398 /* Block devices. */
2399 const char *bd_xmlpath = "/domain/devices/disk/target[@dev]";
2400 if (blockdevice_format == source)
2401 bd_xmlpath = "/domain/devices/disk/source[@dev]";
2402 xpath_obj = xmlXPathEval((const xmlChar *)bd_xmlpath, xpath_ctx);
2404 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
2405 xpath_obj->nodesetval == NULL)
2408 for (int j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) {
2412 node = xpath_obj->nodesetval->nodeTab[j];
2415 path = (char *)xmlGetProp(node, (xmlChar *)"dev");
2419 if (il_block_devices &&
2420 ignore_device_match(il_block_devices, name, path) != 0)
2423 add_block_device(state, dom, path);
2428 xmlXPathFreeObject(xpath_obj);
2430 /* Network interfaces. */
2431 xpath_obj = xmlXPathEval(
2432 (xmlChar *)"/domain/devices/interface[target[@dev]]", xpath_ctx);
2433 if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET ||
2434 xpath_obj->nodesetval == NULL)
2437 xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval;
2439 for (int j = 0; j < xml_interfaces->nodeNr; ++j) {
2441 char *address = NULL;
2442 xmlNodePtr xml_interface;
2444 xml_interface = xml_interfaces->nodeTab[j];
2448 for (xmlNodePtr child = xml_interface->children; child;
2449 child = child->next) {
2450 if (child->type != XML_ELEMENT_NODE)
2453 if (xmlStrEqual(child->name, (const xmlChar *)"target")) {
2454 path = (char *)xmlGetProp(child, (const xmlChar *)"dev");
2457 } else if (xmlStrEqual(child->name, (const xmlChar *)"mac")) {
2458 address = (char *)xmlGetProp(child, (const xmlChar *)"address");
2464 if (il_interface_devices &&
2465 (ignore_device_match(il_interface_devices, name, path) != 0 ||
2466 ignore_device_match(il_interface_devices, name, address) != 0))
2469 add_interface_device(state, dom, path, address, j + 1);
2479 xmlXPathFreeObject(xpath_obj);
2481 xmlXPathFreeContext(xpath_ctx);
2483 xmlFreeDoc(xml_doc);
2487 #ifdef HAVE_LIST_ALL_DOMAINS
2488 /* NOTE: domains_active and domains_inactive data will be cleared during
2489 refresh of all domains (inside lv_clean_read_state function) so we need
2490 to free here only allocated arrays */
2492 sfree(domains_inactive);
2499 DEBUG(PLUGIN_NAME " plugin#%s: refreshing"
2500 " domains=%i block_devices=%i iface_devices=%i",
2501 inst->tag, state->nr_domains, state->nr_block_devices,
2502 state->nr_interface_devices);
2507 static void free_domains(struct lv_read_state *state) {
2508 if (state->domains) {
2509 for (int i = 0; i < state->nr_domains; ++i)
2510 virDomainFree(state->domains[i].ptr);
2511 sfree(state->domains);
2513 state->domains = NULL;
2514 state->nr_domains = 0;
2517 static int add_domain(struct lv_read_state *state, virDomainPtr dom,
2520 int new_size = sizeof(state->domains[0]) * (state->nr_domains + 1);
2523 new_ptr = realloc(state->domains, new_size);
2525 new_ptr = malloc(new_size);
2527 if (new_ptr == NULL)
2530 state->domains = new_ptr;
2531 state->domains[state->nr_domains].ptr = dom;
2532 state->domains[state->nr_domains].active = active;
2533 memset(&state->domains[state->nr_domains].info, 0,
2534 sizeof(state->domains[state->nr_domains].info));
2536 return state->nr_domains++;
2539 static void free_block_devices(struct lv_read_state *state) {
2540 if (state->block_devices) {
2541 for (int i = 0; i < state->nr_block_devices; ++i)
2542 sfree(state->block_devices[i].path);
2543 sfree(state->block_devices);
2545 state->block_devices = NULL;
2546 state->nr_block_devices = 0;
2549 static int add_block_device(struct lv_read_state *state, virDomainPtr dom,
2551 struct block_device *new_ptr;
2553 sizeof(state->block_devices[0]) * (state->nr_block_devices + 1);
2556 path_copy = strdup(path);
2560 if (state->block_devices)
2561 new_ptr = realloc(state->block_devices, new_size);
2563 new_ptr = malloc(new_size);
2565 if (new_ptr == NULL) {
2569 state->block_devices = new_ptr;
2570 state->block_devices[state->nr_block_devices].dom = dom;
2571 state->block_devices[state->nr_block_devices].path = path_copy;
2572 return state->nr_block_devices++;
2575 static void free_interface_devices(struct lv_read_state *state) {
2576 if (state->interface_devices) {
2577 for (int i = 0; i < state->nr_interface_devices; ++i) {
2578 sfree(state->interface_devices[i].path);
2579 sfree(state->interface_devices[i].address);
2580 sfree(state->interface_devices[i].number);
2582 sfree(state->interface_devices);
2584 state->interface_devices = NULL;
2585 state->nr_interface_devices = 0;
2588 static int add_interface_device(struct lv_read_state *state, virDomainPtr dom,
2589 const char *path, const char *address,
2590 unsigned int number) {
2591 struct interface_device *new_ptr;
2593 sizeof(state->interface_devices[0]) * (state->nr_interface_devices + 1);
2594 char *path_copy, *address_copy, number_string[21];
2596 if ((path == NULL) || (address == NULL))
2599 path_copy = strdup(path);
2603 address_copy = strdup(address);
2604 if (!address_copy) {
2609 snprintf(number_string, sizeof(number_string), "interface-%u", number);
2611 if (state->interface_devices)
2612 new_ptr = realloc(state->interface_devices, new_size);
2614 new_ptr = malloc(new_size);
2616 if (new_ptr == NULL) {
2618 sfree(address_copy);
2621 state->interface_devices = new_ptr;
2622 state->interface_devices[state->nr_interface_devices].dom = dom;
2623 state->interface_devices[state->nr_interface_devices].path = path_copy;
2624 state->interface_devices[state->nr_interface_devices].address = address_copy;
2625 state->interface_devices[state->nr_interface_devices].number =
2626 strdup(number_string);
2627 return state->nr_interface_devices++;
2630 static int ignore_device_match(ignorelist_t *il, const char *domname,
2631 const char *devpath) {
2635 if ((domname == NULL) || (devpath == NULL))
2638 size_t n = strlen(domname) + strlen(devpath) + 2;
2641 ERROR(PLUGIN_NAME " plugin: malloc failed.");
2644 snprintf(name, n, "%s:%s", domname, devpath);
2645 r = ignorelist_match(il, name);
2650 static int lv_shutdown(void) {
2651 for (int i = 0; i < nr_instances; ++i) {
2652 lv_fini_instance(i);
2655 DEBUG(PLUGIN_NAME " plugin: stopping event loop");
2657 if (!persistent_notification)
2658 stop_event_loop(¬if_thread);
2662 ignorelist_free(il_domains);
2664 ignorelist_free(il_block_devices);
2665 il_block_devices = NULL;
2666 ignorelist_free(il_interface_devices);
2667 il_interface_devices = NULL;
2672 void module_register(void) {
2673 plugin_register_config(PLUGIN_NAME, lv_config, config_keys, NR_CONFIG_KEYS);
2674 plugin_register_init(PLUGIN_NAME, lv_init);
2675 plugin_register_shutdown(PLUGIN_NAME, lv_shutdown);