Open JaromirCharles opened 4 years ago
Write new incoming data(added and changed data) directly to file with telegraf plugin:
https://github.com/influxdata/telegraf/tree/master/plugins/serializers/influx
or export entire data or data in timerange with sudo influx_inspect export
Using influx line-protocol format LineProtocol:(https://docs.influxdata.com/influxdb/v1.0/write_protocols/line_protocol_tutorial/) https://github.com/influxdata/telegraf/tree/master/plugins/serializers/influx
Receive and write data with telegraf plugins:
Further Examples:
Telegraf
can be used to write to multiple InfluxDB's
. The InfluxDB Line Protocol
points us in the right direction i believe. It can not however be used to sync the data between both databases. Or Can it? We'll need to implement a mechanism that checks which server is down, and then reflect the data onto the respective server.
I have also stumbled upon influxdb-relay: A service to replicate InfluxDB data for high availabilty. I have also read that one can use Telegraf
in place of influxdb-replay
. Configure Telegraf with a single influxdb_listener
input and multiple influxdb outputs.
On Rpi & Cloud server
telegraf.config
output/input
[agent]
interval = "10s"
round_interval = true
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "0s"
flush_interval = "10s"
precision = ""
hostname = ""
omit_hostname = false
[[outputs.influxdb]] urls = ["http://127.0.0.1:8086"]
database = "test2"
[[inputs.influxdb_listener]] service_address = ":8186" read_timeout = "10s" max_body_size = "500MiB" max_line_size = "64KiB"
[[outputs.file]] files = ["stdout", "/tmp/metrics.out"] data_format = "influx" influx_max_line_bytes = 0 influx_sort_fields = false influx_uint_support = false
* Allow tcp/udp access to port 8086. (sudo ufw allow 8086/tcp)
write influxdb entries with telegraf plugin to file?
-> telegraf caches failed writes
**Useful commands for testing**
ping server: `nc -vz 34.71.119.220 8086`
`curl -i -XPOST 'http://localhost:8186/write' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
`
`curl -i -XPOST "http://localhost:8186/write?db=test" --data-binary @metrics.txt`
this might also be an interesting way to implement the data replication between both influxdb instances
Current state
String line_protocol = String.format("event8,eventType=%s posLat=%s,poslong=%s,depth=%s,windSpeed=%s,windDirection=%s,heading=%s,cog=%s,StW=%s,SoG=%s,waterTemp=%s",
eventType, posValues[0], posValues[1], depth, windSpeed, windDirection, heading, cog, stw, sog, waterTemp);
//line_protocol = "event,eventType=MOB ";
String[] command = {"/bin/sh", "-c", "curl -i -XPOST 'http://localhost:8186/write' --data-binary '"+line_protocol+"'"};
String working = String.format("event4,eventType=%s,region=%s depth=%s,stw=0.64,SoG=%s",eventType, posValues[0], depth, stw, sog);
String[] works = {"/bin/sh", "-c", "curl -i -XPOST 'http://localhost:8186/write' --data-binary '"+working+"'"};
Process p = Runtime.getRuntime().exec(works);
Final Implementation
// send curl post request to telegraf
try {
String line_protocol = String.format("event,eventType=%s posLat=%s,posLong=%s,depth=%s,windSpeed=%s,windDirection=%s,heading=%s,cog=%s,stw=%s,SoG=%s,waterTemp=%s",
eventType, posValues[0].trim(), posValues[1].trim(), depth, windSpeed, windDirection, heading, cog, stw, sog, waterTemp);
System.out.println("line_protocol: " + line_protocol);
String[] command = {"/bin/sh", "-c", "curl -i -XPOST 'http://localhost:8186/write' --data-binary '" + line_protocol + "'"};
Process p = Runtime.getRuntime().exec(command);
//p.destroy();
} catch (Exception e) {
System.out.println(e);
}
@GITBSB The problem was that there was a white space within one of the string values :confused:
The code snippet creates a point in InfluxDB line protocol and sends it to telegraf. Telegraf's input plugin accepts the incoming message and then writes the point to our cloud InfluxDB instance.
The implementation can be found on the persistToInflux branch
tcp/udp bind failed:
Killed all processes including influx and java and telegraf
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:53 0.0.0.0:* LISTEN 638/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 704/sshd
tcp 0 0 127.0.0.1:8088 0.0.0.0:* LISTEN 2762/influxd
tcp6 0 0 127.0.0.1:61617 :::* LISTEN 2884/java
tcp6 0 0 :::53 :::* LISTEN 638/dnsmasq
tcp6 0 0 :::8086 :::* LISTEN 2762/influxd
tcp6 0 0 :::22 :::* LISTEN 704/sshd
tcp6 0 0 :::8375 :::* LISTEN 2646/node
tcp6 0 0 :::10110 :::* LISTEN 2646/node
tcp6 0 0 :::55555 :::* LISTEN 2884/java
tcp6 0 0 :::55557 :::* LISTEN 2884/java
tcp6 0 0 :::8080 :::* LISTEN 2884/java
tcp6 0 0 :::80 :::* LISTEN 1/init
udp 0 0 0.0.0.0:53 0.0.0.0:* 638/dnsmasq
udp 0 0 0.0.0.0:67 0.0.0.0:* 638/dnsmasq
udp 0 0 0.0.0.0:68 0.0.0.0:* 610/dhcpcd
udp 0 0 0.0.0.0:50876 0.0.0.0:* 372/avahi-daemon: r
udp 0 0 0.0.0.0:5353 0.0.0.0:* 2646/node
udp 0 0 0.0.0.0:5353 0.0.0.0:* 372/avahi-daemon: r
udp6 0 0 :::46545 :::* 372/avahi-daemon: r
udp6 0 0 :::546 :::* 610/dhcpcd
udp6 0 0 :::53 :::* 638/dnsmasq
udp6 0 0 :::5353 :::* 2884/java
udp6 0 0 :::5353 :::* 2884/java
udp6 0 0 :::5353 :::* 2884/java
udp6 0 0 :::5353 :::* 2884/java
udp6 0 0 :::5353 :::* 2884/java
udp6 0 0 :::5353 :::* 372/avahi-daemon: r
udp6 0 0 :::55554 :::* 2884/java
udp6 0 0 :::55556 :::* 2884/java
Telegraf Config for Server: [agent] interval = "10s" round_interval = true metric_batch_size = 1000 metric_buffer_limit = 10000 collection_jitter = "0s" flush_interval = "10s" precision = "" hostname = "" omit_hostname = false
[[outputs.influxdb]] urls = ["http://77.180.62.127:8086"] database = "test2"
[[inputs.influxdb_listener]] service_address = ":8186" read_timeout = "10s" max_body_size = "500MiB" max_line_size = "64KiB"
WIP: Add specification Telegraf probably best approach for syncing both databases. https://www.influxdata.com/blog/multiple-data-center-replication-influxdb/