64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"log"
|
||
|
|
||
|
client "github.com/influxdata/influxdb1-client/v2"
|
||
|
)
|
||
|
|
||
|
// Create InfluxDB Client
|
||
|
func influxDBClient(port string) client.Client {
|
||
|
c, err := client.NewHTTPClient(client.HTTPConfig{
|
||
|
Addr: conf.General.InfluxURL + ":" + port,
|
||
|
})
|
||
|
if err != nil {
|
||
|
log.Fatalln("Error: ", err)
|
||
|
}
|
||
|
return c
|
||
|
}
|
||
|
|
||
|
// Get a single Datapoint from InfluxDB
|
||
|
func getInfluxDataPoint(dp string, h string, p string) float64 {
|
||
|
|
||
|
//Build the Query
|
||
|
query := "SELECT last(" + dp + ") FROM system WHERE host = '" + h + "'"
|
||
|
|
||
|
c := influxDBClient(p)
|
||
|
q := client.NewQuery(query, "udp", "s")
|
||
|
response, err := c.Query(q)
|
||
|
if err != nil {
|
||
|
log.Println("Influx query error!")
|
||
|
}
|
||
|
res := 0.0
|
||
|
if len(response.Results) > 0 {
|
||
|
res, err := response.Results[0].Series[0].Values[0][1].(json.Number).Float64()
|
||
|
if err != nil {
|
||
|
log.Println("Error in type conversion")
|
||
|
}
|
||
|
return res
|
||
|
}
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
// Send Datapoints to InfluxDB, point map and InfluxDB Port needed
|
||
|
func sendInfluxBatchDataPoint(point *client.Point, influxPort string) {
|
||
|
|
||
|
// Open connection to InfluxDB
|
||
|
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
|
||
|
Database: "freifunk",
|
||
|
Precision: "s",
|
||
|
})
|
||
|
if err != nil {
|
||
|
log.Fatalln("Error: ", err)
|
||
|
}
|
||
|
|
||
|
bp.AddPoint(point)
|
||
|
c := influxDBClient(influxPort)
|
||
|
err = c.Write(bp)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
//
|
||
|
}
|