2017-10-15 22:02:43 +02:00
|
|
|
/* Node-type for InfluxDB.
|
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-10-15 22:02:43 +02:00
|
|
|
*/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cinttypes>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <cstring>
|
2018-08-06 11:24:49 +02:00
|
|
|
#include <netdb.h>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
2017-11-02 13:08:12 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node/config.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/node/memory.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node_compat.hpp>
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/influxdb.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/sample.hpp>
|
|
|
|
#include <villas/signal.hpp>
|
2021-06-21 16:11:42 -04:00
|
|
|
#include <villas/utils.hpp>
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
using namespace villas;
|
2021-05-10 00:12:30 +02:00
|
|
|
using namespace villas::node;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::influxdb_parse(NodeCompat *n, json_t *json) {
|
|
|
|
auto *i = n->getData<struct influxdb>();
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
json_error_t err;
|
|
|
|
int ret;
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
char *tmp, *host, *port, *lasts;
|
|
|
|
const char *server, *key;
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s: s, s: s }", "server", &server,
|
|
|
|
"key", &key);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(json, err, "node-config-node-influx");
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
tmp = strdup(server);
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
host = strtok_r(tmp, ":", &lasts);
|
|
|
|
port = strtok_r(nullptr, "", &lasts);
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
i->key = strdup(key);
|
|
|
|
i->host = strdup(host);
|
|
|
|
i->port = strdup(port ? port : "8089");
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
free(tmp);
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2017-10-15 22:02:43 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::influxdb_open(NodeCompat *n) {
|
|
|
|
int ret;
|
|
|
|
auto *i = n->getData<struct influxdb>();
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
struct addrinfo hints, *servinfo, *p;
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
memset(&hints, 0, sizeof hints);
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = getaddrinfo(i->host, i->port, &hints, &servinfo);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to lookup server: {}", gai_strerror(ret));
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Loop through all the results and connect to the first we can
|
|
|
|
for (p = servinfo; p != nullptr; p = p->ai_next) {
|
|
|
|
i->sd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
|
|
|
|
if (i->sd == -1)
|
|
|
|
throw SystemError("Failed to create socket");
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = connect(i->sd, p->ai_addr, p->ai_addrlen);
|
|
|
|
if (ret == -1) {
|
|
|
|
n->logger->warn("Connect failed: {}", strerror(errno));
|
|
|
|
close(i->sd);
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// If we get here, we must have connected successfully
|
|
|
|
break;
|
|
|
|
}
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return p ? 0 : -1;
|
2017-10-15 22:02:43 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::influxdb_close(NodeCompat *n) {
|
|
|
|
auto *i = n->getData<struct influxdb>();
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
close(i->sd);
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (i->host)
|
|
|
|
free(i->host);
|
|
|
|
if (i->port)
|
|
|
|
free(i->port);
|
|
|
|
if (i->key)
|
|
|
|
free(i->key);
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2017-10-15 22:02:43 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::influxdb_write(NodeCompat *n, struct Sample *const smps[],
|
|
|
|
unsigned cnt) {
|
|
|
|
auto *i = n->getData<struct influxdb>();
|
|
|
|
|
|
|
|
char *buf = strf("");
|
|
|
|
ssize_t sentlen, buflen;
|
|
|
|
|
|
|
|
for (unsigned k = 0; k < cnt; k++) {
|
|
|
|
const struct Sample *smp = smps[k];
|
|
|
|
|
|
|
|
// Key
|
|
|
|
strcatf(&buf, "%s", i->key);
|
|
|
|
|
|
|
|
// Fields
|
|
|
|
for (unsigned j = 0; j < smp->length; j++) {
|
|
|
|
const auto *data = &smp->data[j];
|
|
|
|
auto sig = smp->signals->getByIndex(j);
|
|
|
|
if (!sig)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (sig->type != SignalType::BOOLEAN && sig->type != SignalType::FLOAT &&
|
|
|
|
sig->type != SignalType::INTEGER &&
|
|
|
|
sig->type != SignalType::COMPLEX) {
|
|
|
|
n->logger->warn("Unsupported signal format. Skipping");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcatf(&buf, "%c", j == 0 ? ' ' : ',');
|
|
|
|
if (sig->type == SignalType::COMPLEX) {
|
|
|
|
strcatf(&buf, "%s_re=%f, %s_im=%f", sig->name.c_str(),
|
|
|
|
std::real(data->z), sig->name.c_str(), std::imag(data->z));
|
|
|
|
} else {
|
|
|
|
strcatf(&buf, "%s=", sig->name.c_str());
|
|
|
|
|
|
|
|
switch (sig->type) {
|
|
|
|
case SignalType::BOOLEAN:
|
|
|
|
strcatf(&buf, "%s", data->b ? "true" : "false");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SignalType::FLOAT:
|
|
|
|
strcatf(&buf, "%f", data->f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SignalType::INTEGER:
|
|
|
|
strcatf(&buf, "%" PRIi64, data->i);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timestamp
|
|
|
|
strcatf(&buf, " %lld%09lld\n", (long long)smp->ts.origin.tv_sec,
|
|
|
|
(long long)smp->ts.origin.tv_nsec);
|
|
|
|
}
|
|
|
|
|
|
|
|
buflen = strlen(buf) + 1;
|
|
|
|
sentlen = send(i->sd, buf, buflen, 0);
|
|
|
|
if (sentlen < 0)
|
|
|
|
return -1;
|
|
|
|
else if (sentlen < buflen)
|
|
|
|
n->logger->warn("Partial sent");
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
return cnt;
|
2017-10-15 22:02:43 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
char *villas::node::influxdb_print(NodeCompat *n) {
|
|
|
|
auto *i = n->getData<struct influxdb>();
|
|
|
|
char *buf = nullptr;
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
strcatf(&buf, "host=%s, port=%s, key=%s", i->host, i->port, i->key);
|
2017-10-15 22:02:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return buf;
|
2017-10-15 22:02:43 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
static NodeCompatType p;
|
|
|
|
|
|
|
|
__attribute__((constructor(110))) static void register_plugin() {
|
|
|
|
p.name = "influxdb";
|
|
|
|
p.description = "Write results to InfluxDB";
|
|
|
|
p.vectorize = 0;
|
|
|
|
p.size = sizeof(struct influxdb);
|
|
|
|
p.parse = influxdb_parse;
|
|
|
|
p.print = influxdb_print;
|
|
|
|
p.start = influxdb_open;
|
|
|
|
p.stop = influxdb_close;
|
|
|
|
p.write = influxdb_write;
|
|
|
|
|
|
|
|
static NodeCompatFactory ncp(&p);
|
2019-06-04 16:55:38 +02:00
|
|
|
}
|