X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fvirt.c;h=d3d8c6a72823d1c5cf023a9d484ee00cbb7bd54f;hb=abaa1c8a24e8eff5632dd6052b1da5f6535caf19;hp=443e5b434eb691944ceec74fdff4a2e9fd42017f;hpb=c3a4930c24272257db478b22710471bd5f4972f7;p=collectd.git diff --git a/src/virt.c b/src/virt.c index 443e5b43..d3d8c6a7 100644 --- a/src/virt.c +++ b/src/virt.c @@ -69,9 +69,10 @@ #endif /* - virConnectListAllDomains() appeared in 0.10.2 - Note that LIBVIR_CHECK_VERSION appeared a year later, so - in some systems which actually have virConnectListAllDomains() + virConnectListAllDomains() appeared in 0.10.2 (Sep 2012) + Note that LIBVIR_CHECK_VERSION appeared a year later (Dec 2013, + libvirt-1.2.0), + so in some systems which actually have virConnectListAllDomains() we can't detect this. */ #if LIBVIR_CHECK_VERSION(0, 10, 2) @@ -107,6 +108,9 @@ #define HAVE_DOM_REASON_POSTCOPY 1 #endif +#if LIBVIR_CHECK_VERSION(4, 10, 0) +#define HAVE_DOM_REASON_SHUTOFF_DAEMON 1 +#endif #endif /* LIBVIR_CHECK_VERSION */ /* structure used for aggregating notification-thread data*/ @@ -300,6 +304,16 @@ static int map_domain_event_detail_to_reason(int event, int detail) { switch (detail) { case VIR_DOMAIN_EVENT_SHUTDOWN_FINISHED: /* Guest finished shutdown sequence */ +#ifdef LIBVIR_CHECK_VERSION +#if LIBVIR_CHECK_VERSION(3, 4, 0) + case VIR_DOMAIN_EVENT_SHUTDOWN_GUEST: /* Domain finished shutting down after + request from the guest itself (e.g. + hardware-specific action) */ + case VIR_DOMAIN_EVENT_SHUTDOWN_HOST: /* Domain finished shutting down after + request from the host (e.g. killed + by a signal) */ +#endif +#endif ret = VIR_DOMAIN_SHUTDOWN_USER; break; default: @@ -425,6 +439,10 @@ const char *domain_reasons[][DOMAIN_STATE_REASON_MAX_SIZE] = { "domain failed to start", [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_FROM_SNAPSHOT] = "restored from a snapshot which was taken while domain was shutoff", +#ifdef HAVE_DOM_REASON_SHUTOFF_DAEMON + [VIR_DOMAIN_SHUTOFF][VIR_DOMAIN_SHUTOFF_DAEMON] = + "daemon decides to kill domain during reconnection processing", +#endif [VIR_DOMAIN_CRASHED][VIR_DOMAIN_CRASHED_UNKNOWN] = "the reason is unknown", @@ -635,6 +653,8 @@ static enum if_field interface_format = if_name; static time_t last_refresh = (time_t)0; static int refresh_lists(struct lv_read_instance *inst); +static int register_event_impl(void); +static int start_event_loop(virt_notif_thread_t *thread_data); struct lv_block_stats { virDomainBlockStatsStruct bi; @@ -916,10 +936,10 @@ static void memory_submit(virDomainPtr dom, gauge_t value) { static void memory_stats_submit(gauge_t value, virDomainPtr dom, int tag_index) { - static const char *tags[] = {"swap_in", "swap_out", "major_fault", - "minor_fault", "unused", "available", - "actual_balloon", "rss", "usable", - "last_update"}; + static const char *tags[] = {"swap_in", "swap_out", "major_fault", + "minor_fault", "unused", "available", + "actual_balloon", "rss", "usable", + "last_update", "disk_caches"}; if ((tag_index < 0) || (tag_index >= (int)STATIC_ARRAY_SIZE(tags))) { ERROR("virt plugin: Array index out of bounds: tag_index = %d", tag_index); @@ -984,7 +1004,7 @@ static void vcpu_submit(derive_t value, virDomainPtr dom, int vcpu_nr, const char *type) { char type_instance[DATA_MAX_NAME_LEN]; - snprintf(type_instance, sizeof(type_instance), "%d", vcpu_nr); + ssnprintf(type_instance, sizeof(type_instance), "%d", vcpu_nr); submit(dom, type, type_instance, &(value_t){.derive = value}, 1); } @@ -1006,7 +1026,7 @@ static void disk_block_stats_submit(struct lv_block_stats *bstats, } char flush_type_instance[DATA_MAX_NAME_LEN]; - snprintf(flush_type_instance, sizeof(flush_type_instance), "flush-%s", + ssnprintf(flush_type_instance, sizeof(flush_type_instance), "flush-%s", type_instance); if ((bstats->bi.rd_req != -1) && (bstats->bi.wr_req != -1)) @@ -1110,7 +1130,7 @@ static void domain_state_submit_notif(virDomainPtr dom, int state, int reason) { const char *reason_str = "N/A"; #endif - snprintf(msg, sizeof(msg), "Domain state: %s. Reason: %s", state_str, + ssnprintf(msg, sizeof(msg), "Domain state: %s. Reason: %s", state_str, reason_str); int severity; @@ -1155,6 +1175,11 @@ static int lv_init_ignorelists() { /* Validates config option that may take multiple strings arguments. * Returns 0 on success, -1 otherwise */ static int check_config_multiple_string_entry(const oconfig_item_t *ci) { + if (ci == NULL) { + ERROR(PLUGIN_NAME " plugin: ci oconfig_item can't be NULL"); + return -1; + } + if (ci->values_num < 1) { ERROR(PLUGIN_NAME " plugin: the '%s' option requires at least one string argument", @@ -1184,25 +1209,19 @@ static int lv_config(oconfig_item_t *ci) { oconfig_item_t *c = ci->children + i; if (strcasecmp(c->key, "Connection") == 0) { - if (cf_util_get_string(c, &conn_string) != 0 || conn_string == NULL) { - ERROR(PLUGIN_NAME " plugin: Could not get 'Connection' parameter"); + if (cf_util_get_string(c, &conn_string) != 0 || conn_string == NULL) return -1; - } continue; } else if (strcasecmp(c->key, "RefreshInterval") == 0) { - if (cf_util_get_int(c, &interval) != 0) { - ERROR(PLUGIN_NAME " plugin: Could not get 'RefreshInterval' parameter"); + if (cf_util_get_int(c, &interval) != 0) return -1; - } continue; } else if (strcasecmp(c->key, "Domain") == 0) { char *domain_name = NULL; - if (cf_util_get_string(c, &domain_name) != 0 || domain_name == NULL) { - ERROR(PLUGIN_NAME " plugin: Could not get 'Domain' parameter"); + if (cf_util_get_string(c, &domain_name) != 0) return -1; - } if (ignorelist_add(il_domains, domain_name)) { ERROR(PLUGIN_NAME " plugin: Adding '%s' to domain-ignorelist failed", @@ -1215,10 +1234,8 @@ static int lv_config(oconfig_item_t *ci) { continue; } else if (strcasecmp(c->key, "BlockDevice") == 0) { char *device_name = NULL; - if (cf_util_get_string(c, &device_name) != 0 || device_name == NULL) { - ERROR(PLUGIN_NAME " plugin: Could not get 'BlockDevice' parameter"); + if (cf_util_get_string(c, &device_name) != 0) return -1; - } if (ignorelist_add(il_block_devices, device_name) != 0) { ERROR(PLUGIN_NAME @@ -1232,11 +1249,8 @@ static int lv_config(oconfig_item_t *ci) { continue; } else if (strcasecmp(c->key, "BlockDeviceFormat") == 0) { char *device_format = NULL; - if (cf_util_get_string(c, &device_format) != 0 || device_format == NULL) { - ERROR(PLUGIN_NAME - " plugin: Could not get 'BlockDeviceFormat' parameter"); + if (cf_util_get_string(c, &device_format) != 0) return -1; - } if (strcasecmp(device_format, "target") == 0) blockdevice_format = target; @@ -1252,20 +1266,14 @@ static int lv_config(oconfig_item_t *ci) { sfree(device_format); continue; } else if (strcasecmp(c->key, "BlockDeviceFormatBasename") == 0) { - if (cf_util_get_boolean(c, &blockdevice_format_basename) != 0) { - ERROR(PLUGIN_NAME - " plugin: Could not get 'BlockDeviceFormatBasename' parameter"); + if (cf_util_get_boolean(c, &blockdevice_format_basename) != 0) return -1; - } continue; } else if (strcasecmp(c->key, "InterfaceDevice") == 0) { char *interface_name = NULL; - if (cf_util_get_string(c, &interface_name) != 0 || - interface_name == NULL) { - ERROR(PLUGIN_NAME " plugin: Could not get 'InterfaceDevice' parameter"); + if (cf_util_get_string(c, &interface_name) != 0) return -1; - } if (ignorelist_add(il_interface_devices, interface_name)) { ERROR(PLUGIN_NAME " plugin: Adding '%s' to interface-ignorelist failed", @@ -1278,10 +1286,8 @@ static int lv_config(oconfig_item_t *ci) { continue; } else if (strcasecmp(c->key, "IgnoreSelected") == 0) { bool ignore_selected = false; - if (cf_util_get_boolean(c, &ignore_selected) != 0) { - ERROR(PLUGIN_NAME " plugin: Could not get 'IgnoreSelected' parameter"); + if (cf_util_get_boolean(c, &ignore_selected) != 0) return -1; - } if (ignore_selected) { ignorelist_set_invert(il_domains, 0); @@ -1295,25 +1301,21 @@ static int lv_config(oconfig_item_t *ci) { continue; } else if (strcasecmp(c->key, "HostnameMetadataNS") == 0) { - if (cf_util_get_string(c, &hm_ns) != 0) { - ERROR(PLUGIN_NAME - " plugin: Could not get 'HostnameMetadataNS' parameter"); + if (cf_util_get_string(c, &hm_ns) != 0) return -1; - } continue; } else if (strcasecmp(c->key, "HostnameMetadataXPath") == 0) { - if (cf_util_get_string(c, &hm_xpath) != 0) { - ERROR(PLUGIN_NAME - " plugin: Could not get 'HostnameMetadataXPath' parameter"); + if (cf_util_get_string(c, &hm_xpath) != 0) return -1; - } continue; } else if (strcasecmp(c->key, "HostnameFormat") == 0) { /* this option can take multiple strings arguments in one config line*/ - if (check_config_multiple_string_entry(c) != 0) + if (check_config_multiple_string_entry(c) != 0) { + ERROR(PLUGIN_NAME " plugin: Could not get 'HostnameFormat' parameter"); return -1; + } const int params_num = c->values_num; for (int i = 0; i < params_num; ++i) { @@ -1339,8 +1341,11 @@ static int lv_config(oconfig_item_t *ci) { continue; } else if (strcasecmp(c->key, "PluginInstanceFormat") == 0) { /* this option can handle list of string parameters in one line*/ - if (check_config_multiple_string_entry(c) != 0) + if (check_config_multiple_string_entry(c) != 0) { + ERROR(PLUGIN_NAME + " plugin: Could not get 'PluginInstanceFormat' parameter"); return -1; + } const int params_num = c->values_num; for (int i = 0; i < params_num; ++i) { @@ -1368,10 +1373,8 @@ static int lv_config(oconfig_item_t *ci) { continue; } else if (strcasecmp(c->key, "InterfaceFormat") == 0) { char *format = NULL; - if (cf_util_get_string(c, &format) != 0 || format == NULL) { - ERROR(PLUGIN_NAME " plugin: could not get 'InterfaceFormat' parameter"); + if (cf_util_get_string(c, &format) != 0) return -1; - } if (strcasecmp(format, "name") == 0) interface_format = if_name; @@ -1388,10 +1391,8 @@ static int lv_config(oconfig_item_t *ci) { sfree(format); continue; } else if (strcasecmp(c->key, "Instances") == 0) { - if (cf_util_get_int(c, &nr_instances) != 0) { - ERROR(PLUGIN_NAME " plugin: could not get 'Instances' parameter"); + if (cf_util_get_int(c, &nr_instances) != 0) return -1; - } if (nr_instances <= 0) { ERROR(PLUGIN_NAME " plugin: Instances <= 0 makes no sense."); @@ -1409,10 +1410,8 @@ static int lv_config(oconfig_item_t *ci) { } else if (strcasecmp(c->key, "ExtraStats") == 0) { char *ex_str = NULL; - if (cf_util_get_string(c, &ex_str) != 0 || ex_str == NULL) { - ERROR(PLUGIN_NAME " plugin: could not get 'ExtraStats' parameter"); + if (cf_util_get_string(c, &ex_str) != 0) return -1; - } char *exstats[EX_STATS_MAX_FIELDS]; int numexstats = strsplit(ex_str, exstats, STATIC_ARRAY_SIZE(exstats)); @@ -1433,30 +1432,21 @@ static int lv_config(oconfig_item_t *ci) { } #endif - /* ExtraStats parsed successfully*/ + /* ExtraStats parsed successfully */ continue; } else if (strcasecmp(c->key, "PersistentNotification") == 0) { - if (cf_util_get_boolean(c, &persistent_notification) != 0) { - ERROR(PLUGIN_NAME - " plugin: could not get 'PersistentNotification' parameter"); + if (cf_util_get_boolean(c, &persistent_notification) != 0) return -1; - } continue; } else if (strcasecmp(c->key, "ReportBlockDevices") == 0) { - if (cf_util_get_boolean(c, &report_block_devices) != 0) { - ERROR(PLUGIN_NAME - " plugin: could not get 'ReportBlockDevices' parameter"); + if (cf_util_get_boolean(c, &report_block_devices) != 0) return -1; - } continue; } else if (strcasecmp(c->key, "ReportNetworkInterfaces") == 0) { - if (cf_util_get_boolean(c, &report_network_interfaces) != 0) { - ERROR(PLUGIN_NAME - " plugin: could not get 'ReportNetworkInterfaces' parameter"); + if (cf_util_get_boolean(c, &report_network_interfaces) != 0) return -1; - } continue; } else { @@ -1471,6 +1461,11 @@ static int lv_config(oconfig_item_t *ci) { static int lv_connect(void) { if (conn == NULL) { + /* event implementation must be registered before connection is opened */ + if (!persistent_notification) + if (register_event_impl() != 0) + return -1; + /* `conn_string == NULL' is acceptable */ #ifdef HAVE_FS_INFO /* virDomainGetFSInfo requires full read-write access connection */ @@ -1488,8 +1483,17 @@ static int lv_connect(void) { int status = virNodeGetInfo(conn, &nodeinfo); if (status != 0) { ERROR(PLUGIN_NAME " plugin: virNodeGetInfo failed"); + virConnectClose(conn); + conn = NULL; return -1; } + + if (!persistent_notification) + if (start_event_loop(¬if_thread) != 0) { + virConnectClose(conn); + conn = NULL; + return -1; + } } c_release(LOG_NOTICE, &conn_complain, PLUGIN_NAME " plugin: Connection established."); @@ -1574,7 +1578,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); - snprintf(type_instance, sizeof(type_instance), "vcpu_%d-cpu_%d", vcpu, cpu); + ssnprintf(type_instance, sizeof(type_instance), "vcpu_%d-cpu_%d", vcpu, cpu); submit(dom, "cpu_affinity", type_instance, &(value_t){.gauge = is_set}, 1); } } @@ -1723,8 +1727,42 @@ static int get_memory_stats(virDomainPtr domain) { return mem_stats; } - for (int i = 0; i < mem_stats; i++) - memory_stats_submit((gauge_t)minfo[i].val * 1024, domain, minfo[i].tag); + derive_t swap_in = -1; + derive_t swap_out = -1; + derive_t min_flt = -1; + derive_t maj_flt = -1; + + for (int i = 0; i < mem_stats; i++) { + if (minfo[i].tag == VIR_DOMAIN_MEMORY_STAT_SWAP_IN) + swap_in = minfo[i].val; + else if (minfo[i].tag == VIR_DOMAIN_MEMORY_STAT_SWAP_OUT) + swap_out = minfo[i].val; + else if (minfo[i].tag == VIR_DOMAIN_MEMORY_STAT_MAJOR_FAULT) + min_flt = minfo[i].val; + else if (minfo[i].tag == VIR_DOMAIN_MEMORY_STAT_MINOR_FAULT) + maj_flt = minfo[i].val; +#ifdef LIBVIR_CHECK_VERSION +#if LIBVIR_CHECK_VERSION(2, 1, 0) + else if (minfo[i].tag == VIR_DOMAIN_MEMORY_STAT_LAST_UPDATE) + /* Skip 'last_update' reporting as that is not memory but timestamp */ + continue; +#endif +#endif + else + memory_stats_submit((gauge_t)minfo[i].val * 1024, domain, minfo[i].tag); + } + + if (swap_in > 0 || swap_out > 0) { + submit(domain, "swap_io", "in", &(value_t){.gauge = swap_in}, 1); + submit(domain, "swap_io", "out", &(value_t){.gauge = swap_out}, 1); + } + + if (min_flt > 0 || maj_flt > 0) { + value_t values[] = { + {.gauge = (gauge_t)min_flt}, {.gauge = (gauge_t)maj_flt}, + }; + submit(domain, "ps_pagefaults", NULL, values, STATIC_ARRAY_SIZE(values)); + } sfree(minfo); return 0; @@ -2121,10 +2159,9 @@ static void *event_loop_worker(void *arg) { } 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); + + int 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; @@ -2153,11 +2190,15 @@ static int start_event_loop(virt_notif_thread_t *thread_data) { return -1; } + DEBUG(PLUGIN_NAME " plugin: starting event loop"); + 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"); + virt_notif_thread_set_active(thread_data, 0); virConnectDomainEventDeregisterAny(conn, thread_data->domain_event_cb_id); + thread_data->domain_event_cb_id = -1; return -1; } @@ -2166,13 +2207,21 @@ static int start_event_loop(virt_notif_thread_t *thread_data) { /* 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"); + DEBUG(PLUGIN_NAME " plugin: stopping event loop"); + + /* Stopping loop */ + if (virt_notif_thread_is_active(thread_data)) { + virt_notif_thread_set_active(thread_data, 0); + if (pthread_join(notif_thread.event_loop_tid, NULL) != 0) + ERROR(PLUGIN_NAME " plugin: stopping notification thread failed"); + } + + /* ... and de-registering event handler */ + if (conn != NULL && thread_data->domain_event_cb_id != -1) { + virConnectDomainEventDeregisterAny(conn, thread_data->domain_event_cb_id); + thread_data->domain_event_cb_id = -1; + } } static int persistent_domains_state_notification(void) { @@ -2248,33 +2297,26 @@ static int persistent_domains_state_notification(void) { } 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; - - bool reconnect = conn == NULL ? true : false; - /* event implementation must be registered before connection is opened */ - if (inst->id == 0) { - if (!persistent_notification && reconnect) - if (register_event_impl() != 0) - return -1; + struct lv_read_instance *inst = ud->data; + struct lv_read_state *state = &inst->read_state; + if (inst->id == 0) if (lv_connect() < 0) return -1; - if (!persistent_notification && reconnect && conn != NULL) - if (start_event_loop(¬if_thread) != 0) - return -1; + /* Wait until inst#0 establish connection */ + if (conn == NULL) { + DEBUG(PLUGIN_NAME " plugin#%s: Wait until inst#0 establish connection", + inst->tag); + return 0; } + time_t t; time(&t); /* Need to refresh domain or device lists? */ @@ -2360,7 +2402,7 @@ static int lv_init_instance(size_t i, plugin_read_cb callback) { memset(lv_ud, 0, sizeof(*lv_ud)); - snprintf(inst->tag, sizeof(inst->tag), "%s-%" PRIsz, PLUGIN_NAME, i); + ssnprintf(inst->tag, sizeof(inst->tag), "%s-%" PRIsz, PLUGIN_NAME, i); inst->id = i; user_data_t *ud = &(lv_ud->ud); @@ -2395,21 +2437,11 @@ static int lv_init(void) { if (lv_init_ignorelists() != 0) return -1; - /* event implementation must be registered before connection is opened */ if (!persistent_notification) - if (register_event_impl() != 0) + if (virt_notif_thread_init(¬if_thread) != 0) return -1; - if (lv_connect() != 0) - return -1; - - DEBUG(PLUGIN_NAME " plugin: starting event loop"); - - if (!persistent_notification) { - virt_notif_thread_init(¬if_thread); - if (start_event_loop(¬if_thread) != 0) - return -1; - } + lv_connect(); DEBUG(PLUGIN_NAME " plugin: starting %i instances", nr_instances); @@ -2440,7 +2472,7 @@ static int lv_domain_get_tag(xmlXPathContextPtr xpath_ctx, const char *dom_name, goto done; } - snprintf(xpath_str, sizeof(xpath_str), "/domain/metadata/%s:%s/text()", + ssnprintf(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) { @@ -2643,7 +2675,7 @@ static void lv_add_network_interfaces(struct lv_read_state *state, break; case if_number: { char number_string[4]; - snprintf(number_string, sizeof(number_string), "%d", itf_number); + ssnprintf(number_string, sizeof(number_string), "%d", itf_number); if (ignore_device_match(il_interface_devices, domname, number_string) != 0) device_ignored = true; @@ -2944,7 +2976,7 @@ static int add_interface_device(struct lv_read_state *state, virDomainPtr dom, } char number_string[21]; - snprintf(number_string, sizeof(number_string), "interface-%u", number); + ssnprintf(number_string, sizeof(number_string), "interface-%u", number); char *number_copy = strdup(number_string); if (!number_copy) { sfree(path_copy); @@ -2983,7 +3015,7 @@ static int ignore_device_match(ignorelist_t *il, const char *domname, ERROR(PLUGIN_NAME " plugin: malloc failed."); return 0; } - snprintf(name, n, "%s:%s", domname, devpath); + ssnprintf(name, n, "%s:%s", domname, devpath); int r = ignorelist_match(il, name); sfree(name); return r; @@ -2994,8 +3026,6 @@ static int lv_shutdown(void) { lv_fini_instance(i); } - DEBUG(PLUGIN_NAME " plugin: stopping event loop"); - if (!persistent_notification) stop_event_loop(¬if_thread);