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 ## [v2.0.1] - 2024-09-01
### Added ### Added
- Remember `sort` field - Remember `sort` field
- InfluxDB error handling
### Fixed ### Fixed
- Bug [#103](https://github.com/aceberg/WatchYourLAN/issues/103) - Bug [#103](https://github.com/aceberg/WatchYourLAN/issues/103)

View file

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

View file

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

View file

@ -75,11 +75,12 @@ func compareHosts(foundHosts []models.Host) {
for _, fHost := range foundHostsMap { 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) slog.Warn(msg)
notify.Shout(msg, appConfig.ShoutURL) // Notify through Shoutrrr notify.Shout(msg, appConfig.ShoutURL) // Notify through Shoutrrr
fHost.Name, fHost.DNS = updateDNS(fHost)
db.Insert("now", fHost) db.Insert("now", fHost)
} }
} }