Arp Strings from env try

This commit is contained in:
aceberg 2024-09-02 14:52:36 +07:00
parent 3d5b86cf4f
commit 2253ebc25d
4 changed files with 33 additions and 12 deletions

View file

@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
## [v2.0.1] - 2024-09-01
### Added
- Remember `sort` field
- InfluxDB error handling
### Fixed
- Bug [#103](https://github.com/aceberg/WatchYourLAN/issues/103)

View file

@ -1,6 +1,9 @@
package conf
import (
"log/slog"
"strings"
"github.com/spf13/viper"
"github.com/aceberg/WatchYourLAN/internal/check"
@ -17,6 +20,7 @@ func Get(path string) (config models.Conf) {
viper.SetDefault("NODEPATH", "")
viper.SetDefault("LOG_LEVEL", "info")
viper.SetDefault("ARP_ARGS", "")
viper.SetDefault("ARP_STRS_JOINED", "")
viper.SetDefault("IFACES", "")
viper.SetDefault("TIMEOUT", 120)
viper.SetDefault("TRIM_HIST", 48)
@ -59,6 +63,13 @@ func Get(path string) (config models.Conf) {
config.InfluxOrg, _ = viper.Get("INFLUX_ORG").(string)
config.InfluxBucket, _ = viper.Get("INFLUX_BUCKET").(string)
joined := viper.Get("ARP_STRS_JOINED").(string)
slog.Info("ARP_STRS_JOINED: " + joined)
if joined != "" {
config.ArpStrs = strings.Split(joined, ",")
}
return config
}

View file

@ -4,7 +4,7 @@ import (
"context"
"crypto/tls"
"fmt"
// "log"
"log/slog"
"strings"
"github.com/influxdata/influxdb-client-go/v2"
@ -15,6 +15,7 @@ import (
// Add - write data to InfluxDB2
func Add(appConfig models.Conf, oneHist models.Host) {
var ctx context.Context
client := influxdb2.NewClientWithOptions(appConfig.InfluxAddr, appConfig.InfluxToken,
influxdb2.DefaultOptions().
@ -23,18 +24,25 @@ func Add(appConfig models.Conf, oneHist models.Host) {
InsecureSkipVerify: appConfig.InfluxSkipTLS,
}))
writeAPI := client.WriteAPIBlocking(appConfig.InfluxOrg, appConfig.InfluxBucket)
ctx = context.Background()
ping, err := client.Ping(ctx)
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 strings
oneHist.Name = strings.ReplaceAll(oneHist.Name, " ", "\\ ")
oneHist.Name = strings.ReplaceAll(oneHist.Name, ",", "\\,")
oneHist.Name = strings.ReplaceAll(oneHist.Name, "=", "\\=")
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)
// log.Println("LINE:", line)
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)
// slog.Debug("Writing to InfluxDB", "line", line)
err := writeAPI.WriteRecord(context.Background(), line)
check.IfError(err)
err = writeAPI.WriteRecord(context.Background(), line)
check.IfError(err)
} else {
slog.Error("Can't connect to InfluxDB server")
check.IfError(err)
}
client.Close()
}

View file

@ -75,11 +75,12 @@ func compareHosts(foundHosts []models.Host) {
for _, fHost := range foundHostsMap {
msg := fmt.Sprintf("Unknown host IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface)
fHost.Name, fHost.DNS = updateDNS(fHost)
msg := fmt.Sprintf("Unknown host Names: '%s', IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.DNS, fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface)
slog.Warn(msg)
notify.Shout(msg, appConfig.ShoutURL) // Notify through Shoutrrr
fHost.Name, fHost.DNS = updateDNS(fHost)
db.Insert("now", fHost)
}
}