+#ifdef HAVE_PERF_STATS
+static void perf_submit(virDomainStatsRecordPtr stats) {
+ for (int i = 0; i < stats->nparams; ++i) {
+ /* Replace '.' with '_' in event field to match other metrics' naming
+ * convention */
+ char *c = strchr(stats->params[i].field, '.');
+ if (c)
+ *c = '_';
+ submit(stats->dom, "perf", stats->params[i].field,
+ &(value_t){.derive = stats->params[i].value.ul}, 1);
+ }
+}
+
+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;
+
+ ssnprintf(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;
+ }
+ virDomainFSInfoFree(fs_info[i]);
+ }
+
+ sfree(fs_info);
+ return ret;
+}
+
+#endif /* HAVE_FS_INFO */
+
+#ifdef HAVE_JOB_STATS
+static void job_stats_submit(virDomainPtr domain, virTypedParameterPtr param) {
+ value_t vl = {0};
+
+ if (param->type == VIR_TYPED_PARAM_INT)
+ vl.derive = param->value.i;
+ else if (param->type == VIR_TYPED_PARAM_UINT)
+ vl.derive = param->value.ui;
+ else if (param->type == VIR_TYPED_PARAM_LLONG)
+ vl.derive = param->value.l;
+ else if (param->type == VIR_TYPED_PARAM_ULLONG)
+ vl.derive = param->value.ul;
+ else if (param->type == VIR_TYPED_PARAM_DOUBLE)
+ vl.derive = param->value.d;
+ else if (param->type == VIR_TYPED_PARAM_BOOLEAN)
+ vl.derive = param->value.b;
+ else if (param->type == VIR_TYPED_PARAM_STRING) {
+ submit_notif(domain, NOTIF_OKAY, param->value.s, "job_stats", param->field);
+ return;
+ } else {
+ ERROR(PLUGIN_NAME " plugin: unrecognized virTypedParameterType");
+ return;
+ }
+
+ submit(domain, "job_stats", param->field, &vl, 1);
+}
+
+static int get_job_stats(virDomainPtr domain) {
+ int ret = 0;
+ int job_type = 0;
+ int nparams = 0;
+ virTypedParameterPtr params = NULL;
+ int flags = (extra_stats & ex_stats_job_stats_completed)
+ ? VIR_DOMAIN_JOB_STATS_COMPLETED
+ : 0;
+
+ ret = virDomainGetJobStats(domain, &job_type, ¶ms, &nparams, flags);
+ if (ret != 0) {
+ ERROR(PLUGIN_NAME " plugin: virDomainGetJobStats failed: %d", ret);
+ return ret;
+ }
+
+ DEBUG(PLUGIN_NAME " plugin: job_type=%d nparams=%d", job_type, nparams);
+
+ for (int i = 0; i < nparams; ++i) {
+ DEBUG(PLUGIN_NAME " plugin: param[%d] field=%s type=%d", i, params[i].field,
+ params[i].type);
+ job_stats_submit(domain, ¶ms[i]);
+ }
+
+ virTypedParamsFree(params, nparams);
+ return ret;
+}
+#endif /* HAVE_JOB_STATS */
+
+static int get_domain_metrics(domain_t *domain) {
+ struct lv_info info;
+
+ if (!domain || !domain->ptr) {
+ ERROR(PLUGIN_NAME ": get_domain_metrics: NULL pointer");
+ return -1;
+ }
+
+ init_lv_info(&info);
+ int status = lv_domain_info(domain->ptr, &info);
+ if (status != 0) {
+ ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
+ status);
+ return -1;
+ }
+
+ if (extra_stats & ex_stats_domain_state) {
+#ifdef HAVE_DOM_REASON
+ /* At this point we already know domain's state from virDomainGetInfo call,
+ * however it doesn't provide a reason for entering particular state.
+ * We need to get it from virDomainGetState.
+ */
+ GET_STATS(get_domain_state, "domain reason", domain->ptr);
+#else
+ /* virDomainGetState is not available. Submit 0, which corresponds to
+ * unknown reason. */
+ domain_state_submit(domain->ptr, info.di.state, 0);
+#endif
+ }
+
+ /* Gather remaining stats only for running domains */
+ if (info.di.state != VIR_DOMAIN_RUNNING)
+ return 0;
+
+ pcpu_submit(domain->ptr, &info);
+ cpu_submit(domain, info.di.cpuTime);
+
+ memory_submit(domain->ptr, (gauge_t)info.di.memory * 1024);
+
+ GET_STATS(get_vcpu_stats, "vcpu stats", domain->ptr, info.di.nrVirtCpu);
+ GET_STATS(get_memory_stats, "memory stats", domain->ptr);
+
+#ifdef HAVE_PERF_STATS
+ if (extra_stats & ex_stats_perf)
+ GET_STATS(get_perf_events, "performance monitoring events", domain->ptr);
+#endif
+
+#ifdef HAVE_FS_INFO
+ if (extra_stats & ex_stats_fs_info)
+ GET_STATS(get_fs_info, "file system info", domain->ptr);
+#endif
+
+#ifdef HAVE_DISK_ERR
+ if (extra_stats & ex_stats_disk_err)
+ GET_STATS(get_disk_err, "disk errors", domain->ptr);
+#endif
+
+#ifdef HAVE_JOB_STATS
+ if (extra_stats &
+ (ex_stats_job_stats_completed | ex_stats_job_stats_background))
+ GET_STATS(get_job_stats, "job stats", domain->ptr);
+#endif
+
+ /* Update cached virDomainInfo. It has to be done after cpu_submit */
+ memcpy(&domain->info, &info.di, sizeof(domain->info));
+ return 0;
+}
+
+static int get_if_dev_stats(struct interface_device *if_dev) {
+ virDomainInterfaceStatsStruct stats = {0};
+ char *display_name = NULL;
+
+ if (!if_dev) {
+ ERROR(PLUGIN_NAME " plugin: get_if_dev_stats: NULL pointer");
+ return -1;
+ }
+
+ switch (interface_format) {
+ case if_address:
+ display_name = if_dev->address;
+ break;
+ case if_number:
+ display_name = if_dev->number;
+ break;
+ case if_name:
+ default:
+ display_name = if_dev->path;
+ }
+
+ if (virDomainInterfaceStats(if_dev->dom, if_dev->path, &stats,
+ sizeof(stats)) != 0) {
+ ERROR(PLUGIN_NAME " plugin: virDomainInterfaceStats failed");
+ return -1;
+ }
+
+ if ((stats.rx_bytes != -1) && (stats.tx_bytes != -1))
+ submit_derive2("if_octets", (derive_t)stats.rx_bytes,
+ (derive_t)stats.tx_bytes, if_dev->dom, display_name);
+
+ if ((stats.rx_packets != -1) && (stats.tx_packets != -1))
+ submit_derive2("if_packets", (derive_t)stats.rx_packets,
+ (derive_t)stats.tx_packets, if_dev->dom, display_name);
+
+ if ((stats.rx_errs != -1) && (stats.tx_errs != -1))
+ submit_derive2("if_errors", (derive_t)stats.rx_errs,
+ (derive_t)stats.tx_errs, if_dev->dom, display_name);
+
+ if ((stats.rx_drop != -1) && (stats.tx_drop != -1))
+ submit_derive2("if_dropped", (derive_t)stats.rx_drop,
+ (derive_t)stats.tx_drop, if_dev->dom, display_name);
+ return 0;
+}
+