From: Francesco Romani Date: Mon, 12 Dec 2016 11:03:43 +0000 (+0100) Subject: virt plugin: Open connection on init() X-Git-Tag: collectd-5.8.0~267^2~1 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=b2541eb463165a5973fedb5d51eab8288fcd7ebc;p=collectd.git virt plugin: Open connection on init() If we use more than one reader instance, we can spot errors like this in the system logs: Dec 12 09:59:24 $HOST collectd[19338]: reading number of domains: invalid connection pointer in virConnectNumOfDomains Dec 12 09:59:24 benji.rokugan.lan collectd[19338]: read-function of plugin `virt-2' failed. Will suspend it for 20.000 seconds. This causes unnecessary delay in the sampling of libvirt. The reason for this is just one instance (always #0) takes care of establishing the libvirt connection. But this could be done safely in the plugin init callback: according to doc, this function is called at least once before all the read instances. Signed-off-by: Francesco Romani --- diff --git a/src/virt.c b/src/virt.c index d3012d92..242d0d33 100644 --- a/src/virt.c +++ b/src/virt.c @@ -504,6 +504,22 @@ 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 int lv_read(user_data_t *ud) { time_t t; struct lv_read_instance *inst = NULL; @@ -517,18 +533,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); @@ -732,6 +740,8 @@ static int lv_init(void) { if (virInitialize() != 0) return -1; + lv_connect(); + DEBUG(PLUGIN_NAME " plugin: starting %i instances", nr_instances); for (int i = 0; i < nr_instances; ++i)