From 39ee05cf0fac091572a235cb23daed7bb68f5a8f Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Sat, 20 Jul 2019 13:30:50 +0200 Subject: Allow to set host+port using params in config.json 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 --- README.md | 5 +++ conf.d/project/etc/harvester-config.json | 60 +++++++++++++++++--------------- src/plugins/influxdb-writer.c | 4 +-- src/plugins/influxdb.c | 35 ++++++++++++++++--- 4 files changed, 69 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index b9ad44b..6f5728c 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,11 @@ Typical example to write in a TimeSeries DB from source project directory: ```bash $ cd build/ && /opt/AGL/bin/afb-daemon --workdir=./package --name=afb-harvester --ldpaths=lib --roothttp=. --tracereq=common --token=1 -vvv [...] + +# Test connection to TSDB +* afb-client-demo -H ws://localhost:1234/api?token=1 harvester ping + + $ afb-client-demo ws://localhost:1234/api?token=1 harvester auth ON-REPLY 1:harvester/auth: {"jtype":"afb-reply","request":{"status":"success", "uuid":"03dc89fb-88b4-4204-ba9b-13dded3c38ab"}} diff --git a/conf.d/project/etc/harvester-config.json b/conf.d/project/etc/harvester-config.json index 368be7f..da9d6e4 100644 --- a/conf.d/project/etc/harvester-config.json +++ b/conf.d/project/etc/harvester-config.json @@ -1,31 +1,33 @@ { - "$schema": "http://iot.bzh/download/public/schema/json/ctl-schema.json", - "metadata": { - "uid": "Harvester", - "version": "1.0", - "api": "harvester", - "info": "Data collection binding" - }, - "plugins": [ - { - "uid": "influxdb", - "info": "Plugins that handle influxdb read and write", - "libs": "influxdb.ctlso" - } - ], - - "onload": [ - { - "uid": "init_db", - "info": "Ensure that InfluxDB is up", - "action": "plugin://influxdb#influxdb_ping" - } - ], - - "controls": [ - { - "uid": "write", - "action": "plugin://influxdb#write_to_influxdb" - } - ] + "$schema": "http://iot.bzh/download/public/schema/json/ctl-schema.json", + "metadata": { + "uid": "Harvester", + "version": "1.0", + "api": "harvester", + "info": "Data collection binding" + }, + "plugins": [ + { + "uid": "influxdb", + "info": "Plugins that handle influxdb read and write", + "libs": "influxdb.ctlso", + "params": { + "host": "localhost", + "port": 8086 + } + } + ], + "onload": [ + { + "uid": "init_db", + "info": "Ensure that InfluxDB is up", + "action": "plugin://influxdb#influxdb_ping", + } + ], + "controls": [ + { + "uid": "write", + "action": "plugin://influxdb#write_to_influxdb" + } + ] } 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) { -- cgit 1.2.3-korg