+static int get_perf_events(virDomainPtr domain) {
+ virDomainStatsRecordPtr *stats = NULL;
+ /* virDomainListGetStats requires a NULL terminated list of domains */
+ virDomainPtr domain_array[] = {domain, NULL};
+
+ int status =
+ virDomainListGetStats(domain_array, VIR_DOMAIN_STATS_PERF, &stats, 0);
+ if (status == -1) {
+ ERROR("virt plugin: virDomainListGetStats failed with status %i.", status);
+ return status;
+ }
+
+ for (int i = 0; i < status; ++i)
+ perf_submit(stats[i]);
+
+ virDomainStatsRecordListFree(stats);
+ return 0;
+}
+#endif /* HAVE_PERF_STATS */
+
+static void vcpu_pin_submit(virDomainPtr dom, int max_cpus, int vcpu,
+ unsigned char *cpu_maps, int cpu_map_len) {
+ for (int cpu = 0; cpu < max_cpus; ++cpu) {
+ char type_instance[DATA_MAX_NAME_LEN];
+ _Bool is_set = VIR_CPU_USABLE(cpu_maps, cpu_map_len, vcpu, cpu) ? 1 : 0;
+
+ snprintf(type_instance, sizeof(type_instance), "vcpu_%d-cpu_%d", vcpu, cpu);
+ submit(dom, "cpu_affinity", type_instance, &(value_t){.gauge = is_set}, 1);
+ }
+}
+
+static int get_vcpu_stats(virDomainPtr domain, unsigned short nr_virt_cpu) {
+ int max_cpus = VIR_NODEINFO_MAXCPUS(nodeinfo);
+ int cpu_map_len = VIR_CPU_MAPLEN(max_cpus);
+
+ virVcpuInfoPtr vinfo = calloc(nr_virt_cpu, sizeof(vinfo[0]));
+ if (vinfo == NULL) {
+ ERROR(PLUGIN_NAME " plugin: malloc failed.");
+ return -1;
+ }
+
+ unsigned char *cpumaps = calloc(nr_virt_cpu, cpu_map_len);
+ if (cpumaps == NULL) {
+ ERROR(PLUGIN_NAME " plugin: malloc failed.");
+ sfree(vinfo);
+ return -1;
+ }
+
+ int status =
+ virDomainGetVcpus(domain, vinfo, nr_virt_cpu, cpumaps, cpu_map_len);
+ if (status < 0) {
+ ERROR(PLUGIN_NAME " plugin: virDomainGetVcpus failed with status %i.",
+ status);
+ sfree(cpumaps);
+ sfree(vinfo);
+ return status;
+ }
+
+ for (int i = 0; i < nr_virt_cpu; ++i) {
+ vcpu_submit(vinfo[i].cpuTime, domain, vinfo[i].number, "virt_vcpu");
+ if (extra_stats & ex_stats_vcpupin)
+ vcpu_pin_submit(domain, max_cpus, i, cpumaps, cpu_map_len);
+ }
+
+ sfree(cpumaps);
+ sfree(vinfo);
+ return 0;
+}
+
+#ifdef HAVE_DOM_REASON
+static int get_domain_state(virDomainPtr domain) {
+ int domain_state = 0;
+ int domain_reason = 0;
+
+ int status = virDomainGetState(domain, &domain_state, &domain_reason, 0);
+ if (status != 0) {
+ ERROR(PLUGIN_NAME " plugin: virDomainGetState failed with status %i.",
+ status);
+ return status;
+ }
+
+ domain_state_submit(domain, domain_state, domain_reason);
+ return status;
+}
+#endif /* HAVE_DOM_REASON */
+
+static int get_memory_stats(virDomainPtr domain) {
+ virDomainMemoryStatPtr minfo =
+ calloc(VIR_DOMAIN_MEMORY_STAT_NR, sizeof(virDomainMemoryStatStruct));
+ if (minfo == NULL) {
+ ERROR("virt plugin: malloc failed.");
+ return -1;
+ }
+
+ int mem_stats =
+ virDomainMemoryStats(domain, minfo, VIR_DOMAIN_MEMORY_STAT_NR, 0);
+ if (mem_stats < 0) {
+ ERROR("virt plugin: virDomainMemoryStats failed with mem_stats %i.",
+ mem_stats);
+ sfree(minfo);
+ return mem_stats;
+ }
+
+ for (int i = 0; i < mem_stats; i++)
+ memory_stats_submit((gauge_t)minfo[i].val * 1024, domain, minfo[i].tag);
+
+ sfree(minfo);
+ return 0;
+}
+
+#ifdef HAVE_DISK_ERR
+static void disk_err_submit(virDomainPtr domain,
+ virDomainDiskErrorPtr disk_err) {
+ submit(domain, "disk_error", disk_err->disk,
+ &(value_t){.gauge = disk_err->error}, 1);
+}
+
+static int get_disk_err(virDomainPtr domain) {
+ /* Get preferred size of disk errors array */
+ int disk_err_count = virDomainGetDiskErrors(domain, NULL, 0, 0);
+ if (disk_err_count == -1) {
+ ERROR(PLUGIN_NAME
+ " plugin: failed to get preferred size of disk errors array");
+ return -1;
+ }
+
+ DEBUG(PLUGIN_NAME
+ " plugin: preferred size of disk errors array: %d for domain %s",
+ disk_err_count, virDomainGetName(domain));
+ virDomainDiskError disk_err[disk_err_count];
+
+ disk_err_count = virDomainGetDiskErrors(domain, disk_err, disk_err_count, 0);
+ if (disk_err_count == -1) {
+ ERROR(PLUGIN_NAME " plugin: virDomainGetDiskErrors failed with status %d",
+ disk_err_count);
+ return -1;
+ }
+
+ DEBUG(PLUGIN_NAME " plugin: detected %d disk errors in domain %s",
+ disk_err_count, virDomainGetName(domain));
+
+ for (int i = 0; i < disk_err_count; ++i) {
+ disk_err_submit(domain, &disk_err[i]);
+ sfree(disk_err[i].disk);
+ }
+
+ return 0;
+}
+#endif /* HAVE_DISK_ERR */
+
+static int get_block_stats(struct block_device *block_dev) {
+
+ if (!block_dev) {
+ ERROR(PLUGIN_NAME " plugin: get_block_stats NULL pointer");
+ return -1;
+ }
+
+ struct lv_block_info binfo;
+ init_block_info(&binfo);
+
+ if (lv_domain_block_info(block_dev->dom, block_dev->path, &binfo) < 0) {
+ ERROR(PLUGIN_NAME " plugin: lv_domain_block_info failed");
+ return -1;
+ }
+
+ disk_submit(&binfo, block_dev->dom, block_dev->path);
+ return 0;
+}
+
+#ifdef HAVE_FS_INFO
+
+#define NM_ADD_ITEM(_fun, _name, _val) \
+ do { \
+ ret = _fun(¬if, _name, _val); \
+ if (ret != 0) { \
+ ERROR(PLUGIN_NAME " plugin: failed to add notification metadata"); \
+ goto cleanup; \
+ } \
+ } while (0)
+
+#define NM_ADD_STR_ITEMS(_items, _size) \
+ do { \
+ for (int _i = 0; _i < _size; ++_i) { \
+ DEBUG(PLUGIN_NAME \
+ " plugin: Adding notification metadata name=%s value=%s", \
+ _items[_i].name, _items[_i].value); \
+ NM_ADD_ITEM(plugin_notification_meta_add_string, _items[_i].name, \
+ _items[_i].value); \
+ } \
+ } while (0)
+
+static int fs_info_notify(virDomainPtr domain, virDomainFSInfoPtr fs_info) {
+ notification_t notif;
+ int ret = 0;
+
+ /* Local struct, just for the purpose of this function. */
+ typedef struct nm_str_item_s {
+ const char *name;
+ const char *value;
+ } nm_str_item_t;
+
+ nm_str_item_t fs_dev_alias[fs_info->ndevAlias];
+ nm_str_item_t fs_str_items[] = {
+ {.name = "mountpoint", .value = fs_info->mountpoint},
+ {.name = "name", .value = fs_info->name},
+ {.name = "fstype", .value = fs_info->fstype}};
+
+ for (int i = 0; i < fs_info->ndevAlias; ++i) {
+ fs_dev_alias[i].name = "devAlias";
+ fs_dev_alias[i].value = fs_info->devAlias[i];
+ }
+
+ init_notif(¬if, domain, NOTIF_OKAY, "File system information",
+ "file_system", NULL);
+ NM_ADD_STR_ITEMS(fs_str_items, STATIC_ARRAY_SIZE(fs_str_items));
+ NM_ADD_ITEM(plugin_notification_meta_add_unsigned_int, "ndevAlias",
+ fs_info->ndevAlias);
+ NM_ADD_STR_ITEMS(fs_dev_alias, fs_info->ndevAlias);
+
+ plugin_dispatch_notification(¬if);
+
+cleanup:
+ if (notif.meta)
+ plugin_notification_meta_free(notif.meta);
+ return ret;
+}
+
+#undef RETURN_ON_ERR
+#undef NM_ADD_STR_ITEMS
+
+static int get_fs_info(virDomainPtr domain) {
+ virDomainFSInfoPtr *fs_info = NULL;
+ int ret = 0;
+
+ int mount_points_cnt = virDomainGetFSInfo(domain, &fs_info, 0);
+ if (mount_points_cnt == -1) {
+ ERROR(PLUGIN_NAME " plugin: virDomainGetFSInfo failed: %d",
+ mount_points_cnt);
+ return mount_points_cnt;
+ }
+
+ for (int i = 0; i < mount_points_cnt; ++i) {
+ if (fs_info_notify(domain, fs_info[i]) != 0) {
+ ERROR(PLUGIN_NAME " plugin: failed to send file system notification "
+ "for mount point %s",
+ fs_info[i]->mountpoint);
+ ret = -1;