diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c45817..b422a99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/internal/conf/getconfig.go b/internal/conf/getconfig.go index 7f405dd..2a8b591 100644 --- a/internal/conf/getconfig.go +++ b/internal/conf/getconfig.go @@ -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 } diff --git a/internal/influx/influx.go b/internal/influx/influx.go index 7d5fc06..4e0b2d7 100644 --- a/internal/influx/influx.go +++ b/internal/influx/influx.go @@ -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() } diff --git a/internal/web/scan-routine.go b/internal/web/scan-routine.go index fc105c5..1dfa5af 100644 --- a/internal/web/scan-routine.go +++ b/internal/web/scan-routine.go @@ -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) } }