Added support for Network Name and Device Position in InfluxDB consiguration

This commit is contained in:
Francesco Manghi 2026-02-16 11:58:19 +01:00
parent ca9f644bcc
commit 1d998ccee9
8 changed files with 40 additions and 5 deletions

View file

@ -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: ""

View file

@ -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")

View file

@ -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")

View file

@ -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)

View file

@ -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)

View file

@ -27,6 +27,8 @@ type Conf struct {
InfluxToken string
InfluxOrg string
InfluxBucket string
InfluxNetworkName string
InfluxDeviceLocation string
InfluxSkipTLS bool
// Prometheus
PrometheusEnable bool

View file

@ -36,6 +36,14 @@ function Influx() {
<td>Bucket</td>
<td><input name="bucket" type="text" class="form-control" value={appConfig().InfluxBucket}></input></td>
</tr>
<tr>
<td>Network Name</td>
<td><input name="network_name" type="text" class="form-control" value={appConfig().InfluxNetworkName}></input></td>
</tr>
<tr>
<td>Device Location</td>
<td><input name="device_location" type="text" class="form-control" value={appConfig().InfluxDeviceLocation}></input></td>
</tr>
<tr>
<td>Skip TLS verify</td>
<td>

View file

@ -36,6 +36,8 @@ export interface Conf {
InfluxToken: string;
InfluxOrg: string;
InfluxBucket: string;
InfluxNetworkName: string;
InfluxDeviceLocation: string;
InfluxSkipTLS: boolean;
// Prometheus
PrometheusEnable: boolean;
@ -75,6 +77,8 @@ export const emptyConf:Conf = {
InfluxToken: "",
InfluxOrg: "",
InfluxBucket: "",
InfluxNetworkName: "",
InfluxDeviceLocation: "",
InfluxSkipTLS: false,
PrometheusEnable: false,
};