X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fvirt.c;h=830db5136db00a015857a58b53d6d84643d11500;hb=cb262c0e23692b6374d57ad3b3decb0d6c397949;hp=86f83ae8e009c3fd31c76c0b5c3b1567911d1d9c;hpb=b9f877bc827ea4c0097682fc87cc5bcfd0000807;p=collectd.git diff --git a/src/virt.c b/src/virt.c index 86f83ae8..830db513 100644 --- a/src/virt.c +++ b/src/virt.c @@ -504,6 +504,29 @@ static int lv_config(const char *key, const char *value) { return -1; } +static int lv_connect(void) { + if (conn == NULL) { + /* `conn_string == NULL' is acceptable. */ + conn = virConnectOpenReadOnly(conn_string); + if (conn == NULL) { + c_complain(LOG_ERR, &conn_complain, + PLUGIN_NAME " plugin: Unable to connect: " + "virConnectOpenReadOnly 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_read(user_data_t *ud) { time_t t; struct lv_read_instance *inst = NULL; @@ -517,18 +540,10 @@ static int lv_read(user_data_t *ud) { inst = ud->data; state = &inst->read_state; - if (inst->id == 0 && conn == NULL) { - /* `conn_string == NULL' is acceptable. */ - conn = virConnectOpenReadOnly(conn_string); - if (conn == NULL) { - c_complain(LOG_ERR, &conn_complain, - PLUGIN_NAME " plugin: Unable to connect: " - "virConnectOpenReadOnly failed."); + if (inst->id == 0) { + if (lv_connect() < 0) return -1; - } } - c_release(LOG_NOTICE, &conn_complain, - PLUGIN_NAME " plugin: Connection established."); time(&t); @@ -536,9 +551,8 @@ static int lv_read(user_data_t *ud) { if ((last_refresh == (time_t)0) || ((interval > 0) && ((last_refresh + interval) <= t))) { if (refresh_lists(inst) != 0) { - if (conn != NULL) - virConnectClose(conn); - conn = NULL; + if (inst->id == 0) + lv_disconnect(); return -1; } last_refresh = t; @@ -732,6 +746,9 @@ static int lv_init(void) { if (virInitialize() != 0) return -1; + if (lv_connect() != 0) + return -1; + DEBUG(PLUGIN_NAME " plugin: starting %i instances", nr_instances); for (int i = 0; i < nr_instances; ++i) @@ -740,12 +757,16 @@ static int lv_init(void) { return 0; } +/* + * returns 0 on success and <0 on error + */ static int lv_domain_get_tag(xmlXPathContextPtr xpath_ctx, const char *dom_name, char *dom_tag) { char xpath_str[BUFFER_MAX_LEN] = {'\0'}; xmlXPathObjectPtr xpath_obj = NULL; xmlNodePtr xml_node = NULL; - int err = -1; + int ret = -1; + int err; err = xmlXPathRegisterNs(xpath_ctx, (const xmlChar *)METADATA_VM_PARTITION_PREFIX, @@ -776,8 +797,7 @@ static int lv_domain_get_tag(xmlXPathContextPtr xpath_ctx, const char *dom_name, * from now on there is no real error, it's ok if a domain * doesn't have the metadata partition tag. */ - err = 0; - + ret = 0; if (xpath_obj->nodesetval == NULL || xpath_obj->nodesetval->nodeNr != 1) { DEBUG(PLUGIN_NAME " plugin: xmlXPathEval(%s) return nodeset size=%i " "expected=1 on domain %s", @@ -793,11 +813,16 @@ done: /* deregister to clean up */ err = xmlXPathRegisterNs(xpath_ctx, (const xmlChar *)METADATA_VM_PARTITION_PREFIX, NULL); - + if (err) { + /* we can't really recover here */ + ERROR(PLUGIN_NAME + " plugin: deregistration of namespace %s failed for domain %s", + METADATA_VM_PARTITION_PREFIX, dom_name); + } if (xpath_obj) xmlXPathFreeObject(xpath_obj); - return err; + return ret; } static int is_known_tag(const char *dom_tag) { @@ -911,10 +936,10 @@ static int refresh_lists(struct lv_read_instance *inst) { } /* Block devices. */ - char *bd_xmlpath = "/domain/devices/disk/target[@dev]"; + const char *bd_xmlpath = "/domain/devices/disk/target[@dev]"; if (blockdevice_format == source) bd_xmlpath = "/domain/devices/disk/source[@dev]"; - xpath_obj = xmlXPathEval((xmlChar *)bd_xmlpath, xpath_ctx); + xpath_obj = xmlXPathEval((const xmlChar *)bd_xmlpath, xpath_ctx); if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET || xpath_obj->nodesetval == NULL) @@ -1153,9 +1178,7 @@ static int lv_shutdown(void) { lv_fini_instance(i); } - if (conn != NULL) - virConnectClose(conn); - conn = NULL; + lv_disconnect(); ignorelist_free(il_domains); il_domains = NULL;