2021-04-08 14:19:37 +02:00
|
|
|
import datetime
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
def export_to_influxdb(action, measurements):
|
|
|
|
from influxdb import InfluxDBClient
|
|
|
|
|
|
|
|
influx_client = InfluxDBClient(
|
2021-05-30 14:09:27 +02:00
|
|
|
host=os.getenv("INFLUXDB_HOST", "localhost"),
|
|
|
|
port=os.getenv("INFLUXDB_PORT", 8086),
|
|
|
|
username=os.getenv("INFLUXDB_USERNAME", "root"),
|
|
|
|
password=os.getenv("INFLUXDB_PASSWORD", "root"),
|
|
|
|
database=os.getenv("INFLUXDB_DATABASE"),
|
2021-04-08 14:19:37 +02:00
|
|
|
)
|
|
|
|
json_body = [
|
|
|
|
{
|
|
|
|
"measurement": action,
|
|
|
|
"time": datetime.datetime.utcnow().replace(microsecond=0).isoformat(),
|
|
|
|
"fields": measurements,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
try:
|
|
|
|
influx_client.write_points(json_body)
|
|
|
|
except Exception as e:
|
2021-05-30 14:09:27 +02:00
|
|
|
print("Failed to write to influxdb: ", e)
|