summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2019-07-20 13:30:50 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2019-08-01 17:46:23 +0200
commit39ee05cf0fac091572a235cb23daed7bb68f5a8f (patch)
tree670b0b1c9cf6309fc07f2a59750420a6e420268c /src
parent310cabd8353607a0882c7dea83a8bd328d32d5c9 (diff)
Allow to set host+port using params in config.jsonsandbox/SebD/wip
Default influxDB host and port can now be set using params field of plugins section of harvester-config.json file. For example : "plugins": [ { "uid": "influxdb", "info": "Plugins that handle influxdb read and write", "libs": "influxdb.ctlso", "params": { "host": "localhost", "port": 8086 } } ], Change-Id: Iff0fffe1d0883304413d887986991a207b840aa7 Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'src')
-rw-r--r--src/plugins/influxdb-writer.c4
-rw-r--r--src/plugins/influxdb.c35
2 files changed, 33 insertions, 6 deletions
diff --git a/src/plugins/influxdb-writer.c b/src/plugins/influxdb-writer.c
index 2ef3813..3838036 100644
--- a/src/plugins/influxdb-writer.c
+++ b/src/plugins/influxdb-writer.c
@@ -142,13 +142,13 @@ CURL* make_curl_write_post(afb_api_t apiHandle, const char* url, json_object* me
/* Check that we just do not broke the for loop before trying preparing CURL
request object */
curl = i == lpd ? curl_wrap_prepare_post_unescaped(url, NULL, "\n", (const char* const*)post_data) : NULL;
- free(serie);
+ free(serie);
for (i = 0; i < lpd; i++) {
if (post_data[i]) {
free(post_data[i]);
}
}
- free(post_data);
+ free(post_data);
return curl;
}
diff --git a/src/plugins/influxdb.c b/src/plugins/influxdb.c
index a175c0e..3403eb3 100644
--- a/src/plugins/influxdb.c
+++ b/src/plugins/influxdb.c
@@ -25,6 +25,9 @@
#include "tsdb.h"
#include "wrap-json.h"
+static char* default_db_host = DEFAULT_DBHOST;
+static char default_db_port[64] = DEFAULT_DBPORT;
+
CTLP_CAPI_REGISTER("influxdb");
CTLP_ONLOAD(plugin, ret)
@@ -32,6 +35,30 @@ CTLP_ONLOAD(plugin, ret)
int err = 0;
char* result;
size_t result_size;
+
+ // Check if default host & port are set in harvester-config.json file
+ if (plugin->paramsJ) {
+ const char* host = NULL;
+ int port = -1;
+ int ret;
+
+ AFB_API_NOTICE(plugin->api, "params detected to change default host+port : %s", json_object_to_json_string(plugin->paramsJ));
+ ret = wrap_json_unpack(plugin->paramsJ, "{ss si}",
+ "host", &host, "port", &port);
+ if (ret == 0) {
+ if (host && strlen(host) > 0)
+ // SEB do we need strdup ??
+ default_db_host = strdup(host);
+ if (port > 0) {
+ snprintf(default_db_port, sizeof(default_db_host) - 1, "%d", port);
+ }
+ } else {
+ AFB_API_ERROR(plugin->api, "decoding params : %s", wrap_json_get_error_string(ret));
+ }
+ }
+
+ //
+
CURL* request = curl_wrap_prepare_get(get_url(NULL, NULL, "/ping"), NULL, NULL);
struct reader_args r_args = { NULL, NULL, 1000000 };
@@ -75,8 +102,8 @@ size_t make_url(char* url, size_t l_url, const char* host, const char* port, con
bzero(url, l_url);
/* Handle default host and port */
- host = host ? host : DEFAULT_DBHOST;
- port = port ? port : DEFAULT_DBPORT;
+ host = host ? host : default_db_host;
+ port = port ? port : default_db_port;
strncat(url, host, l_url - strlen(url) - 1);
strcat(url, ":");
@@ -104,8 +131,8 @@ char* get_url(const char* host, const char* port, const char* endpoint)
static char* url = NULL;
static size_t urlSz = 0;
- host = host ? host : DEFAULT_DBHOST;
- port = port ? port : DEFAULT_DBPORT;
+ host = host ? host : default_db_host;
+ port = port ? port : default_db_port;
size_t newSz = strlen(host) + 1 + strlen(port) + 1 + strlen(endpoint) + 1;
if (urlSz < newSz) {