diff --git a/README.md b/README.md index 1a97e7e..b45821a 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,8 @@ This config matches Grafana's config for InfluxDB data source | INFLUX_BUCKET | InfluxDB2 bucket | | test | | INFLUX_ORG | InfluxDB2 org | | home | | INFLUX_TOKEN | Secret token, generated by InfluxDB2 | | | +| INFLUX_NETWORK_NAME | Tag value for network name | | Home LAN | +| INFLUX_DEVICE_LOCATION | Tag value for device location | | Server Rack | ### Prometheus config This config configures the Prometheus data source @@ -143,7 +145,9 @@ host: 0.0.0.0 ifaces: enp4s0 influx_addr: "" influx_bucket: "" +influx_device_location: "" influx_enable: false +influx_network_name: "" influx_org: "" influx_skip_tls: false influx_token: "" diff --git a/backend/internal/api/config.go b/backend/internal/api/config.go index 9f1999c..64e7538 100644 --- a/backend/internal/api/config.go +++ b/backend/internal/api/config.go @@ -66,6 +66,8 @@ func saveInfluxHandler(c *gin.Context) { conf.AppConfig.InfluxToken = c.PostForm("token") conf.AppConfig.InfluxOrg = c.PostForm("org") conf.AppConfig.InfluxBucket = c.PostForm("bucket") + conf.AppConfig.InfluxNetworkName = c.PostForm("network_name") + conf.AppConfig.InfluxDeviceLocation = c.PostForm("device_location") enable := c.PostForm("enable") skip := c.PostForm("skip") diff --git a/backend/internal/conf/read.go b/backend/internal/conf/read.go index 6f5d0ae..4775076 100644 --- a/backend/internal/conf/read.go +++ b/backend/internal/conf/read.go @@ -28,6 +28,8 @@ func read(path string) (config models.Conf) { viper.SetDefault("PG_CONNECT", "") viper.SetDefault("INFLUX_ENABLE", false) + viper.SetDefault("INFLUX_NETWORK_NAME", "") + viper.SetDefault("INFLUX_DEVICE_LOCATION", "") viper.SetDefault("PROMETHEUS_ENABLE", false) @@ -60,6 +62,8 @@ func read(path string) (config models.Conf) { config.InfluxToken, _ = viper.Get("INFLUX_TOKEN").(string) config.InfluxOrg, _ = viper.Get("INFLUX_ORG").(string) config.InfluxBucket, _ = viper.Get("INFLUX_BUCKET").(string) + config.InfluxNetworkName, _ = viper.Get("INFLUX_NETWORK_NAME").(string) + config.InfluxDeviceLocation, _ = viper.Get("INFLUX_DEVICE_LOCATION").(string) config.PrometheusEnable = viper.GetBool("PROMETHEUS_ENABLE") diff --git a/backend/internal/conf/write.go b/backend/internal/conf/write.go index 201ecb9..6013cbe 100644 --- a/backend/internal/conf/write.go +++ b/backend/internal/conf/write.go @@ -40,6 +40,8 @@ func Write(config models.Conf) { viper.Set("influx_token", config.InfluxToken) viper.Set("influx_org", config.InfluxOrg) viper.Set("influx_bucket", config.InfluxBucket) + viper.Set("influx_network_name", config.InfluxNetworkName) + viper.Set("influx_device_location", config.InfluxDeviceLocation) viper.Set("PROMETHEUS_ENABLE", config.PrometheusEnable) diff --git a/backend/internal/influx/influx.go b/backend/internal/influx/influx.go index 0c32050..8ab798b 100644 --- a/backend/internal/influx/influx.go +++ b/backend/internal/influx/influx.go @@ -13,6 +13,14 @@ import ( "github.com/aceberg/WatchYourLAN/internal/models" ) +func tagValueEscaped(value string) string { + value = strings.ReplaceAll(value, " ", "\\ ") + value = strings.ReplaceAll(value, ",", "\\,") + value = strings.ReplaceAll(value, "=", "\\=") + + return value +} + // Add - write data to InfluxDB2 func Add(appConfig models.Conf, oneHist models.Host) { var ctx context.Context @@ -29,15 +37,16 @@ func Add(appConfig models.Conf, oneHist models.Host) { if ping { writeAPI := client.WriteAPIBlocking(appConfig.InfluxOrg, appConfig.InfluxBucket) - // Escape special characters in strings - oneHist.Name = strings.ReplaceAll(oneHist.Name, " ", "\\ ") - oneHist.Name = strings.ReplaceAll(oneHist.Name, ",", "\\,") - oneHist.Name = strings.ReplaceAll(oneHist.Name, "=", "\\=") + // Escape special characters in tag values + oneHist.Name = tagValueEscaped(oneHist.Name) if oneHist.Name == "" { oneHist.Name = "unknown" } - line := fmt.Sprintf("WatchYourLAN,IP=%s,iface=%s,name=%s,mac=%s,known=%d state=%d", oneHist.IP, oneHist.Iface, oneHist.Name, oneHist.Mac, oneHist.Known, oneHist.Now) + networkName := tagValueEscaped(appConfig.InfluxNetworkName) + deviceLocation := tagValueEscaped(appConfig.InfluxDeviceLocation) + + line := fmt.Sprintf("WatchYourLAN,IP=%s,iface=%s,name=%s,mac=%s,known=%d,network_name=%s,device_location=%s state=%d", oneHist.IP, oneHist.Iface, oneHist.Name, oneHist.Mac, oneHist.Known, networkName, deviceLocation, oneHist.Now) // slog.Debug("Writing to InfluxDB", "line", line) err = writeAPI.WriteRecord(context.Background(), line) diff --git a/backend/internal/models/models.go b/backend/internal/models/models.go index 7fcfd16..9872446 100644 --- a/backend/internal/models/models.go +++ b/backend/internal/models/models.go @@ -27,6 +27,8 @@ type Conf struct { InfluxToken string InfluxOrg string InfluxBucket string + InfluxNetworkName string + InfluxDeviceLocation string InfluxSkipTLS bool // Prometheus PrometheusEnable bool diff --git a/frontend/src/components/Config/Influx.tsx b/frontend/src/components/Config/Influx.tsx index fa6b658..4d0ceab 100644 --- a/frontend/src/components/Config/Influx.tsx +++ b/frontend/src/components/Config/Influx.tsx @@ -36,6 +36,14 @@ function Influx() {