X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fvirt.c;h=8a44c268ffc7667fc178138f6a0a239f4c26b81a;hp=111311390aea4e1fa779ac6080e10a9599a69209;hb=da11ce02eb202b3e01d3e2d1b40f248a84430973;hpb=9d534e1f24505fa5a5042a1db2d5dbd3030e5263 diff --git a/src/virt.c b/src/virt.c index 11131139..8a44c268 100644 --- a/src/virt.c +++ b/src/virt.c @@ -410,12 +410,6 @@ static time_t last_refresh = (time_t)0; static int refresh_lists(struct lv_read_instance *inst); -struct lv_info { - virDomainInfo di; - unsigned long long total_user_cpu_time; - unsigned long long total_syst_cpu_time; -}; - struct lv_block_info { virDomainBlockStatsStruct bi; @@ -444,12 +438,10 @@ static void init_block_info(struct lv_block_info *binfo) { #ifdef HAVE_BLOCK_STATS_FLAGS #define GET_BLOCK_INFO_VALUE(NAME, FIELD) \ - do { \ - if (!strcmp(param[i].field, NAME)) { \ - binfo->FIELD = param[i].value.l; \ - continue; \ - } \ - } while (0) + if (!strcmp(param[i].field, NAME)) { \ + binfo->FIELD = param[i].value.l; \ + continue; \ + } static int get_block_info(struct lv_block_info *binfo, virTypedParameterPtr param, int nparams) { @@ -481,57 +473,9 @@ static int get_block_info(struct lv_block_info *binfo, virErrorPtr err; \ err = (conn) ? virConnGetLastError((conn)) : virGetLastError(); \ if (err) \ - ERROR("%s: %s", (s), err->message); \ + ERROR(PLUGIN_NAME " plugin: %s failed: %s", (s), err->message); \ } while (0) -static void init_lv_info(struct lv_info *info) { - if (info != NULL) - memset(info, 0, sizeof(*info)); -} - -static int lv_domain_info(virDomainPtr dom, struct lv_info *info) { -#ifdef HAVE_CPU_STATS - virTypedParameterPtr param = NULL; - int nparams = 0; -#endif /* HAVE_CPU_STATS */ - int ret = virDomainGetInfo(dom, &(info->di)); - if (ret != 0) { - return ret; - } - -#ifdef HAVE_CPU_STATS - nparams = virDomainGetCPUStats(dom, NULL, 0, -1, 1, 0); - if (nparams < 0) { - VIRT_ERROR(conn, "getting the CPU params count"); - return -1; - } - - param = calloc(nparams, sizeof(virTypedParameter)); - if (param == NULL) { - ERROR("virt plugin: alloc(%i) for cpu parameters failed.", nparams); - return -1; - } - - ret = virDomainGetCPUStats(dom, param, nparams, -1, 1, 0); // total stats. - if (ret < 0) { - virTypedParamsFree(param, nparams); - VIRT_ERROR(conn, "getting the disk params values"); - return -1; - } - - for (int i = 0; i < nparams; ++i) { - if (!strcmp(param[i].field, "user_time")) - info->total_user_cpu_time = param[i].value.ul; - else if (!strcmp(param[i].field, "system_time")) - info->total_syst_cpu_time = param[i].value.ul; - } - - virTypedParamsFree(param, nparams); -#endif /* HAVE_CPU_STATS */ - - return 0; -} - static void init_value_list(value_list_t *vl, virDomainPtr dom) { int n; const char *name; @@ -676,14 +620,6 @@ static void submit_derive2(const char *type, derive_t v0, derive_t v1, submit(dom, type, devname, values, STATIC_ARRAY_SIZE(values)); } /* void submit_derive2 */ -static void pcpu_submit(virDomainPtr dom, struct lv_info *info) { -#ifdef HAVE_CPU_STATS - if (extra_stats & ex_stats_pcpu) - submit_derive2("ps_cputime", info->total_user_cpu_time, - info->total_syst_cpu_time, dom, NULL); -#endif /* HAVE_CPU_STATS */ -} - static double cpu_ns_to_percent(unsigned int node_cpus, unsigned long long cpu_time_old, unsigned long long cpu_time_new) { @@ -729,7 +665,7 @@ static void vcpu_submit(derive_t value, virDomainPtr dom, int vcpu_nr, const char *type) { char type_instance[DATA_MAX_NAME_LEN]; - ssnprintf(type_instance, sizeof(type_instance), "%d", vcpu_nr); + snprintf(type_instance, sizeof(type_instance), "%d", vcpu_nr); submit(dom, type, type_instance, &(value_t){.derive = value}, 1); } @@ -750,8 +686,8 @@ static void disk_submit(struct lv_block_info *binfo, virDomainPtr dom, } char flush_type_instance[DATA_MAX_NAME_LEN]; - ssnprintf(flush_type_instance, sizeof(flush_type_instance), "flush-%s", - type_instance); + snprintf(flush_type_instance, sizeof(flush_type_instance), "flush-%s", + type_instance); if ((binfo->bi.rd_req != -1) && (binfo->bi.wr_req != -1)) submit_derive2("disk_ops", (derive_t)binfo->bi.rd_req, @@ -826,8 +762,8 @@ static void domain_state_submit(virDomainPtr dom, int state, int reason) { const char *reason_str = "N/A"; #endif - ssnprintf(msg, sizeof(msg), "Domain state: %s. Reason: %s", state_str, - reason_str); + snprintf(msg, sizeof(msg), "Domain state: %s. Reason: %s", state_str, + reason_str); int severity; switch (state) { @@ -1110,31 +1046,29 @@ static void lv_disconnect(void) { static int lv_domain_block_info(virDomainPtr dom, const char *path, struct lv_block_info *binfo) { #ifdef HAVE_BLOCK_STATS_FLAGS - virTypedParameterPtr params = NULL; int nparams = 0; - int rc = -1; - int ret = virDomainBlockStatsFlags(dom, path, NULL, &nparams, 0); - if (ret < 0 || nparams == 0) { + if (virDomainBlockStatsFlags(dom, path, NULL, &nparams, 0) < 0 || + nparams <= 0) { VIRT_ERROR(conn, "getting the disk params count"); return -1; } - params = calloc(nparams, sizeof(virTypedParameter)); + 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; } - ret = virDomainBlockStatsFlags(dom, path, params, &nparams, 0); - if (ret < 0) { + + int rc = -1; + if (virDomainBlockStatsFlags(dom, path, params, &nparams, 0) < 0) { VIRT_ERROR(conn, "getting the disk params values"); - goto done; + } else { + rc = get_block_info(binfo, params, nparams); } - rc = get_block_info(binfo, params, nparams); - -done: - virTypedParamsFree(params, nparams); + virTypedParamsClear(params, nparams); + sfree(params); return rc; #else return virDomainBlockStats(dom, path, &(binfo->bi), sizeof(binfo->bi)); @@ -1180,8 +1114,7 @@ static void vcpu_pin_submit(virDomainPtr dom, int max_cpus, int vcpu, 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); + snprintf(type_instance, sizeof(type_instance), "vcpu_%d-cpu_%d", vcpu, cpu); submit(dom, "cpu_affinity", type_instance, &(value_t){.gauge = is_set}, 1); } } @@ -1192,13 +1125,13 @@ static int get_vcpu_stats(virDomainPtr domain, unsigned short nr_virt_cpu) { virVcpuInfoPtr vinfo = calloc(nr_virt_cpu, sizeof(vinfo[0])); if (vinfo == NULL) { - ERROR(PLUGIN_NAME " plugin: malloc failed."); + 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: malloc failed."); + ERROR(PLUGIN_NAME " plugin: calloc failed."); sfree(vinfo); return -1; } @@ -1224,6 +1157,49 @@ static int get_vcpu_stats(virDomainPtr domain, unsigned short nr_virt_cpu) { 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 int get_domain_state(virDomainPtr domain) { int domain_state = 0; @@ -1468,15 +1444,13 @@ static int get_job_stats(virDomainPtr domain) { #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"); + ERROR(PLUGIN_NAME "plugin: get_domain_metrics: NULL pointer"); return -1; } - init_lv_info(&info); - int status = lv_domain_info(domain->ptr, &info); + virDomainInfo info; + int status = virDomainGetInfo(domain->ptr, &info); if (status != 0) { ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.", status); @@ -1493,20 +1467,24 @@ static int get_domain_metrics(domain_t *domain) { #else /* virDomainGetState is not available. Submit 0, which corresponds to * unknown reason. */ - domain_state_submit(domain->ptr, info.di.state, 0); + domain_state_submit(domain->ptr, info.state, 0); #endif } /* Gather remaining stats only for running domains */ - if (info.di.state != VIR_DOMAIN_RUNNING) + if (info.state != VIR_DOMAIN_RUNNING) return 0; - pcpu_submit(domain->ptr, &info); - cpu_submit(domain, info.di.cpuTime); +#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.di.memory * 1024); + memory_submit(domain->ptr, (gauge_t)info.memory * 1024); - GET_STATS(get_vcpu_stats, "vcpu stats", domain->ptr, info.di.nrVirtCpu); + GET_STATS(get_vcpu_stats, "vcpu stats", domain->ptr, info.nrVirtCpu); GET_STATS(get_memory_stats, "memory stats", domain->ptr); #ifdef HAVE_PERF_STATS @@ -1531,7 +1509,7 @@ static int get_domain_metrics(domain_t *domain) { #endif /* Update cached virDomainInfo. It has to be done after cpu_submit */ - memcpy(&domain->info, &info.di, sizeof(domain->info)); + memcpy(&domain->info, &info, sizeof(domain->info)); return 0; } @@ -1639,7 +1617,7 @@ static int lv_read(user_data_t *ud) { ERROR(PLUGIN_NAME " failed to get stats for block device (%s) in domain %s", state->block_devices[i].path, - virDomainGetName(state->domains[i].ptr)); + virDomainGetName(state->block_devices[i].dom)); } /* Get interface stats for each domain. */ @@ -1661,7 +1639,7 @@ static int lv_init_instance(size_t i, plugin_read_cb callback) { memset(lv_ud, 0, sizeof(*lv_ud)); - ssnprintf(inst->tag, sizeof(inst->tag), "%s-%zu", PLUGIN_NAME, i); + snprintf(inst->tag, sizeof(inst->tag), "%s-%zu", PLUGIN_NAME, i); inst->id = i; user_data_t *ud = &(lv_ud->ud); @@ -1721,8 +1699,8 @@ static int lv_domain_get_tag(xmlXPathContextPtr xpath_ctx, const char *dom_name, goto done; } - ssnprintf(xpath_str, sizeof(xpath_str), "/domain/metadata/%s:%s/text()", - METADATA_VM_PARTITION_PREFIX, METADATA_VM_PARTITION_ELEMENT); + snprintf(xpath_str, sizeof(xpath_str), "/domain/metadata/%s:%s/text()", + METADATA_VM_PARTITION_PREFIX, METADATA_VM_PARTITION_ELEMENT); xpath_obj = xmlXPathEvalExpression((xmlChar *)xpath_str, xpath_ctx); if (xpath_obj == NULL) { ERROR(PLUGIN_NAME " plugin: xmlXPathEval(%s) failed on domain %s", @@ -1795,6 +1773,18 @@ static int lv_instance_include_domain(struct lv_read_instance *inst, return 0; } +/* + virConnectListAllDomains() appeared in 0.10.2 + Note that LIBVIR_CHECK_VERSION appeared a year later, so + in some systems which actually have virConnectListAllDomains() + we can't detect this. + */ +#ifdef LIBVIR_CHECK_VERSION +#if LIBVIR_CHECK_VERSION(0, 10, 2) +#define HAVE_LIST_ALL_DOMAINS 1 +#endif +#endif + static int refresh_lists(struct lv_read_instance *inst) { struct lv_read_state *state = &inst->read_state; int n; @@ -1808,6 +1798,11 @@ static int refresh_lists(struct lv_read_instance *inst) { lv_clean_read_state(state); if (n > 0) { +#ifdef HAVE_LIST_ALL_DOMAINS + virDomainPtr *domains; + n = virConnectListAllDomains(conn, &domains, + VIR_CONNECT_LIST_DOMAINS_ACTIVE); +#else int *domids; /* Get list of domains. */ @@ -1818,15 +1813,18 @@ static int refresh_lists(struct lv_read_instance *inst) { } n = virConnectListDomains(conn, domids, n); +#endif + if (n < 0) { VIRT_ERROR(conn, "reading list of domains"); +#ifndef HAVE_LIST_ALL_DOMAINS sfree(domids); +#endif return -1; } /* Fetch each domain and add it to the list, unless ignore. */ for (int i = 0; i < n; ++i) { - virDomainPtr dom = NULL; const char *name; char *xml = NULL; xmlDocPtr xml_doc = NULL; @@ -1836,12 +1834,17 @@ static int refresh_lists(struct lv_read_instance *inst) { virDomainInfo info; int status; +#ifdef HAVE_LIST_ALL_DOMAINS + virDomainPtr dom = domains[i]; +#else + virDomainPtr dom = NULL; dom = virDomainLookupByID(conn, domids[i]); if (dom == NULL) { VIRT_ERROR(conn, "virDomainLookupByID"); /* Could be that the domain went away -- ignore it anyway. */ continue; } +#endif name = virDomainGetName(dom); if (name == NULL) { @@ -1982,7 +1985,11 @@ static int refresh_lists(struct lv_read_instance *inst) { sfree(xml); } +#ifdef HAVE_LIST_ALL_DOMAINS + sfree(domains); +#else sfree(domids); +#endif } DEBUG(PLUGIN_NAME " plugin#%s: refreshing" @@ -2122,13 +2129,13 @@ static int ignore_device_match(ignorelist_t *il, const char *domname, if ((domname == NULL) || (devpath == NULL)) return 0; - n = sizeof(char) * (strlen(domname) + strlen(devpath) + 2); + n = strlen(domname) + strlen(devpath) + 2; name = malloc(n); if (name == NULL) { ERROR(PLUGIN_NAME " plugin: malloc failed."); return 0; } - ssnprintf(name, n, "%s:%s", domname, devpath); + snprintf(name, n, "%s:%s", domname, devpath); r = ignorelist_match(il, name); sfree(name); return r;