+#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;
+}
+
+static int lv_read(user_data_t *ud) {
+ time_t t;
+ struct lv_read_instance *inst = NULL;
+ struct lv_read_state *state = NULL;
+
+ if (ud->data == NULL) {
+ ERROR(PLUGIN_NAME " plugin: NULL userdata");
+ return -1;
+ }
+
+ inst = ud->data;
+ state = &inst->read_state;
+
+ if (inst->id == 0) {
+ if (lv_connect() < 0)
+ return -1;
+ }
+
+ time(&t);
+
+ /* Need to refresh domain or device lists? */
+ if ((last_refresh == (time_t)0) ||
+ ((interval > 0) && ((last_refresh + interval) <= t))) {
+ if (refresh_lists(inst) != 0) {
+ if (inst->id == 0)
+ lv_disconnect();
+ return -1;