+ int n = strsplit(value_copy, fields, PLGINST_MAX_FIELDS);
+ if (n < 1) {
+ sfree(value_copy);
+ ERROR(PLUGIN_NAME " plugin: PluginInstanceFormat: no fields");
+ return -1;
+ }
+
+ for (int i = 0; i < n; ++i) {
+ if (strcasecmp(fields[i], "none") == 0) {
+ plugin_instance_format[i] = plginst_none;
+ break;
+ } else if (strcasecmp(fields[i], "name") == 0)
+ plugin_instance_format[i] = plginst_name;
+ else if (strcasecmp(fields[i], "uuid") == 0)
+ plugin_instance_format[i] = plginst_uuid;
+ else if (strcasecmp(fields[i], "metadata") == 0)
+ plugin_instance_format[i] = plginst_metadata;
+ else {
+ ERROR(PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s",
+ fields[i]);
+ sfree(value_copy);
+ return -1;
+ }
+ }
+ sfree(value_copy);
+
+ for (int i = n; i < PLGINST_MAX_FIELDS; ++i)
+ plugin_instance_format[i] = plginst_none;
+
+ return 0;
+ }
+
+ if (strcasecmp(key, "InterfaceFormat") == 0) {
+ if (strcasecmp(value, "name") == 0)
+ interface_format = if_name;
+ else if (strcasecmp(value, "address") == 0)
+ interface_format = if_address;
+ else if (strcasecmp(value, "number") == 0)
+ interface_format = if_number;
+ else {
+ ERROR(PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", value);
+ return -1;
+ }
+ return 0;
+ }
+
+ if (strcasecmp(key, "Instances") == 0) {
+ char *eptr = NULL;
+ double val = strtod(value, &eptr);
+
+ if (*eptr != '\0') {
+ ERROR(PLUGIN_NAME " plugin: Invalid value for Instances = '%s'", value);
+ return 1;
+ }
+ if (val <= 0) {
+ ERROR(PLUGIN_NAME " plugin: Instances <= 0 makes no sense.");
+ return 1;
+ }
+ if (val > NR_INSTANCES_MAX) {
+ ERROR(PLUGIN_NAME " plugin: Instances=%f > NR_INSTANCES_MAX=%i"
+ " use a lower setting or recompile the plugin.",
+ val, NR_INSTANCES_MAX);
+ return 1;
+ }
+
+ nr_instances = (int)val;
+ DEBUG(PLUGIN_NAME " plugin: configured %i instances", nr_instances);
+ return 0;
+ }
+
+ if (strcasecmp(key, "ExtraStats") == 0) {
+ char *localvalue = strdup(value);
+ if (localvalue != NULL) {
+ char *exstats[EX_STATS_MAX_FIELDS];
+ int numexstats =
+ strsplit(localvalue, exstats, STATIC_ARRAY_SIZE(exstats));
+ extra_stats = parse_ex_stats_flags(exstats, numexstats);
+ sfree(localvalue);
+
+#ifdef HAVE_JOB_STATS
+ if ((extra_stats & ex_stats_job_stats_completed) &&
+ (extra_stats & ex_stats_job_stats_background)) {
+ ERROR(PLUGIN_NAME " plugin: Invalid job stats configuration. Only one "
+ "type of job statistics can be collected at the same "
+ "time");
+ return 1;
+ }
+#endif
+ }
+ }
+
+ if (strcasecmp(key, "PersistentNotification") == 0) {
+ persistent_notification = IS_TRUE(value);
+ return 0;
+ }
+
+ /* Unrecognised option. */
+ return -1;
+}
+
+static int lv_connect(void) {
+ if (conn == NULL) {
+/* `conn_string == NULL' is acceptable */
+#ifdef HAVE_FS_INFO
+ /* virDomainGetFSInfo requires full read-write access connection */
+ if (extra_stats & ex_stats_fs_info)
+ conn = virConnectOpen(conn_string);
+ else
+#endif
+ conn = virConnectOpenReadOnly(conn_string);
+ if (conn == NULL) {
+ c_complain(LOG_ERR, &conn_complain,
+ PLUGIN_NAME " plugin: Unable to connect: "
+ "virConnectOpen failed.");
+ return -1;
+ }
+ int status = virNodeGetInfo(conn, &nodeinfo);
+ if (status != 0) {
+ ERROR(PLUGIN_NAME " plugin: virNodeGetInfo failed");
+ return -1;
+ }
+ }
+ c_release(LOG_NOTICE, &conn_complain,
+ PLUGIN_NAME " plugin: Connection established.");
+ return 0;
+}
+
+static void lv_disconnect(void) {
+ if (conn != NULL)
+ virConnectClose(conn);
+ conn = NULL;
+ WARNING(PLUGIN_NAME " plugin: closed connection to libvirt");
+}
+
+static int lv_domain_block_info(virDomainPtr dom, const char *path,
+ struct lv_block_info *binfo) {
+#ifdef HAVE_BLOCK_STATS_FLAGS
+ int nparams = 0;
+ if (virDomainBlockStatsFlags(dom, path, NULL, &nparams, 0) < 0 ||
+ nparams <= 0) {
+ VIRT_ERROR(conn, "getting the disk params count");
+ return -1;
+ }
+
+ virTypedParameterPtr params = calloc((size_t)nparams, sizeof(*params));
+ if (params == NULL) {
+ ERROR("virt plugin: alloc(%i) for block=%s parameters failed.", nparams,
+ path);
+ return -1;
+ }
+
+ int rc = -1;
+ if (virDomainBlockStatsFlags(dom, path, params, &nparams, 0) < 0) {
+ VIRT_ERROR(conn, "getting the disk params values");
+ } else {
+ rc = get_block_info(binfo, params, nparams);
+ }
+
+ virTypedParamsClear(params, nparams);
+ sfree(params);
+ return rc;
+#else
+ return virDomainBlockStats(dom, path, &(binfo->bi), sizeof(binfo->bi));
+#endif /* HAVE_BLOCK_STATS_FLAGS */
+}
+
+#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);
+
+ 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: calloc failed.");
+ return -1;
+ }
+
+ unsigned char *cpumaps = calloc(nr_virt_cpu, cpu_map_len);
+ if (cpumaps == NULL) {
+ ERROR(PLUGIN_NAME " plugin: calloc 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_CPU_STATS
+static int get_pcpu_stats(virDomainPtr dom) {
+ int nparams = virDomainGetCPUStats(dom, NULL, 0, -1, 1, 0);
+ if (nparams < 0) {
+ VIRT_ERROR(conn, "getting the CPU params count");
+ return -1;
+ }
+
+ virTypedParameterPtr param = calloc(nparams, sizeof(virTypedParameter));
+ if (param == NULL) {
+ ERROR(PLUGIN_NAME " plugin: alloc(%i) for cpu parameters failed.", nparams);
+ return -1;
+ }
+
+ int ret = virDomainGetCPUStats(dom, param, nparams, -1, 1, 0); // total stats.
+ if (ret < 0) {
+ virTypedParamsClear(param, nparams);
+ sfree(param);
+ VIRT_ERROR(conn, "getting the CPU params values");
+ return -1;
+ }
+
+ unsigned long long total_user_cpu_time = 0;
+ unsigned long long total_syst_cpu_time = 0;
+
+ for (int i = 0; i < nparams; ++i) {
+ if (!strcmp(param[i].field, "user_time"))
+ total_user_cpu_time = param[i].value.ul;
+ else if (!strcmp(param[i].field, "system_time"))
+ total_syst_cpu_time = param[i].value.ul;
+ }
+
+ if (total_user_cpu_time > 0 || total_syst_cpu_time > 0)
+ submit_derive2("ps_cputime", total_user_cpu_time, total_syst_cpu_time, dom,
+ NULL);
+
+ virTypedParamsClear(param, nparams);
+ sfree(param);
+
+ return 0;
+}
+#endif /* HAVE_CPU_STATS */
+
+#ifdef HAVE_DOM_REASON
+
+static void domain_state_submit(virDomainPtr dom, int state, int reason) {
+ value_t values[] = {
+ {.gauge = (gauge_t)state}, {.gauge = (gauge_t)reason},
+ };
+
+ submit(dom, "domain_state", NULL, values, STATIC_ARRAY_SIZE(values));
+}
+
+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;
+}
+
+#ifdef HAVE_LIST_ALL_DOMAINS
+static int get_domain_state_notify(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;
+ }
+
+ if (persistent_notification)
+ domain_state_submit_notif(domain, domain_state, domain_reason);
+
+ return status;
+}
+#endif /* HAVE_LIST_ALL_DOMAINS */
+#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 (size_t _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 (size_t 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) {
+ if (!domain || !domain->ptr) {
+ ERROR(PLUGIN_NAME " plugin: get_domain_metrics: NULL pointer");
+ return -1;
+ }
+
+ virDomainInfo info;
+ int status = virDomainGetInfo(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);
+#endif
+ }
+
+ /* Gather remaining stats only for running domains */
+ if (info.state != VIR_DOMAIN_RUNNING)
+ return 0;
+
+#ifdef HAVE_CPU_STATS
+ if (extra_stats & ex_stats_pcpu)
+ get_pcpu_stats(domain->ptr);
+#endif
+
+ cpu_submit(domain, info.cpuTime);
+
+ memory_submit(domain->ptr, (gauge_t)info.memory * 1024);
+
+ GET_STATS(get_vcpu_stats, "vcpu stats", domain->ptr, info.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, 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 domain_lifecycle_event_cb(__attribute__((unused)) virConnectPtr con_,
+ virDomainPtr dom, int event, int detail,
+ __attribute__((unused)) void *opaque) {
+ int domain_state = map_domain_event_to_state(event);
+ int domain_reason = 0; /* 0 means UNKNOWN reason for any state */
+#ifdef HAVE_DOM_REASON
+ domain_reason = map_domain_event_detail_to_reason(event, detail);
+#endif
+ domain_state_submit_notif(dom, domain_state, domain_reason);
+
+ return 0;
+}
+
+static int register_event_impl(void) {
+ if (virEventRegisterDefaultImpl() < 0) {
+ virErrorPtr err = virGetLastError();
+ ERROR(PLUGIN_NAME
+ " plugin: error while event implementation registering: %s",
+ err && err->message ? err->message : "Unknown error");
+ return -1;
+ }
+
+ return 0;
+}
+
+static void virt_notif_thread_set_active(virt_notif_thread_t *thread_data,
+ const bool active) {
+ assert(thread_data != NULL);
+ pthread_mutex_lock(&thread_data->active_mutex);
+ thread_data->is_active = active;
+ pthread_mutex_unlock(&thread_data->active_mutex);
+}
+
+static bool virt_notif_thread_is_active(virt_notif_thread_t *thread_data) {
+ bool active = false;
+
+ assert(thread_data != NULL);
+ pthread_mutex_lock(&thread_data->active_mutex);
+ active = thread_data->is_active;
+ pthread_mutex_unlock(&thread_data->active_mutex);
+
+ return active;
+}
+
+/* worker function running default event implementation */
+static void *event_loop_worker(void *arg) {
+ virt_notif_thread_t *thread_data = (virt_notif_thread_t *)arg;
+
+ while (virt_notif_thread_is_active(thread_data)) {
+ if (virEventRunDefaultImpl() < 0) {
+ virErrorPtr err = virGetLastError();
+ ERROR(PLUGIN_NAME " plugin: failed to run event loop: %s\n",
+ err && err->message ? err->message : "Unknown error");
+ }
+ }
+
+ return NULL;
+}
+
+static int virt_notif_thread_init(virt_notif_thread_t *thread_data) {
+ int ret;
+
+ assert(thread_data != NULL);
+ ret = pthread_mutex_init(&thread_data->active_mutex, NULL);
+ if (ret != 0) {
+ ERROR(PLUGIN_NAME " plugin: Failed to initialize mutex, err %u", ret);
+ return ret;
+ }
+
+ /**
+ * '0' and positive integers are meaningful ID's, therefore setting
+ * domain_event_cb_id to '-1'
+ */
+ thread_data->domain_event_cb_id = -1;
+ pthread_mutex_lock(&thread_data->active_mutex);
+ thread_data->is_active = false;
+ pthread_mutex_unlock(&thread_data->active_mutex);
+
+ return 0;
+}
+
+/* register domain event callback and start event loop thread */
+static int start_event_loop(virt_notif_thread_t *thread_data) {
+ assert(thread_data != NULL);
+ thread_data->domain_event_cb_id = virConnectDomainEventRegisterAny(
+ conn, NULL, VIR_DOMAIN_EVENT_ID_LIFECYCLE,
+ VIR_DOMAIN_EVENT_CALLBACK(domain_lifecycle_event_cb), NULL, NULL);
+ if (thread_data->domain_event_cb_id == -1) {
+ ERROR(PLUGIN_NAME " plugin: error while callback registering");
+ return -1;
+ }
+
+ virt_notif_thread_set_active(thread_data, 1);
+ if (pthread_create(&thread_data->event_loop_tid, NULL, event_loop_worker,
+ thread_data)) {
+ ERROR(PLUGIN_NAME " plugin: failed event loop thread creation");
+ virConnectDomainEventDeregisterAny(conn, thread_data->domain_event_cb_id);
+ return -1;
+ }
+
+ return 0;
+}
+
+/* stop event loop thread and deregister callback */
+static void stop_event_loop(virt_notif_thread_t *thread_data) {
+ /* stopping loop and de-registering event handler*/
+ virt_notif_thread_set_active(thread_data, 0);
+ if (conn != NULL && thread_data->domain_event_cb_id != -1)
+ virConnectDomainEventDeregisterAny(conn, thread_data->domain_event_cb_id);
+
+ if (pthread_join(notif_thread.event_loop_tid, NULL) != 0)
+ ERROR(PLUGIN_NAME " plugin: stopping notification thread failed");
+}
+
+static int persistent_domains_state_notification(void) {
+ int status = 0;
+ int n;
+#ifdef HAVE_LIST_ALL_DOMAINS
+ virDomainPtr *domains = NULL;
+ n = virConnectListAllDomains(conn, &domains,
+ VIR_CONNECT_LIST_DOMAINS_PERSISTENT);
+ if (n < 0) {
+ VIRT_ERROR(conn, "reading list of persistent domains");
+ status = -1;
+ } else {
+ DEBUG(PLUGIN_NAME " plugin: getting state of %i persistent domains", n);
+ /* Fetch each persistent domain's state and notify it */
+ int n_notified = n;
+ for (int i = 0; i < n; ++i) {
+ status = get_domain_state_notify(domains[i]);
+ if (status != 0) {
+ n_notified--;
+ ERROR(PLUGIN_NAME " plugin: could not notify state of domain %s",
+ virDomainGetName(domains[i]));
+ }
+ virDomainFree(domains[i]);
+ }