Add option to only compare MAC for alerting

This commit is contained in:
bugbounce 2022-10-22 00:26:28 +05:30
parent d67af210da
commit 7ca8feeec9
3 changed files with 25 additions and 10 deletions

View file

@ -6,12 +6,24 @@ import (
) )
func host_in_db(host Host, dbHosts []Host) bool { // Check if host is already in DB func host_in_db(host Host, dbHosts []Host) bool { // Check if host is already in DB
for _, oneHost := range dbHosts {
if host.Ip == oneHost.Ip && host.Mac == oneHost.Mac && host.Hw == oneHost.Hw { if AppConfig.OnlyCompareMac {
oneHost.Date = host.Date for _, oneHost := range dbHosts {
oneHost.Now = 1 if host.Mac == oneHost.Mac {
db_update(oneHost) oneHost.Date = host.Date
return true oneHost.Now = 1
db_update(oneHost)
return true
}
}
} else {
for _, oneHost := range dbHosts {
if host.Ip == oneHost.Ip && host.Mac == oneHost.Mac && host.Hw == oneHost.Hw {
oneHost.Date = host.Date
oneHost.Now = 1
db_update(oneHost)
return true
}
} }
} }
return false return false
@ -27,4 +39,4 @@ func hosts_compare(foundHosts []Host, dbHosts []Host) {
db_insert(oneHost) db_insert(oneHost)
} }
} }
} }

View file

@ -14,10 +14,11 @@ func get_config() (config Conf) {
viper.SetDefault("TIMEOUT", "60") viper.SetDefault("TIMEOUT", "60")
viper.SetDefault("SHOUTRRR_URL", "") viper.SetDefault("SHOUTRRR_URL", "")
viper.SetDefault("THEME", "solar") viper.SetDefault("THEME", "solar")
viper.SetDefault("ONLY_COMPARE_MAC", false)
viper.SetConfigFile(configPath) viper.SetConfigFile(configPath)
viper.SetConfigType("env") viper.SetConfigType("env")
viper.ReadInConfig() viper.ReadInConfig()
viper.AutomaticEnv() // Get ENVIRONMENT variables viper.AutomaticEnv() // Get ENVIRONMENT variables
@ -28,6 +29,7 @@ func get_config() (config Conf) {
config.Timeout = viper.GetInt("TIMEOUT") config.Timeout = viper.GetInt("TIMEOUT")
config.ShoutUrl = viper.Get("SHOUTRRR_URL").(string) config.ShoutUrl = viper.Get("SHOUTRRR_URL").(string)
config.Theme = viper.Get("THEME").(string) config.Theme = viper.Get("THEME").(string)
config.OnlyCompareMac = viper.GetBool("ONLY_COMPARE_MAC")
return config return config
} }
@ -37,4 +39,4 @@ func write_config() {
viper.SetConfigType("env") viper.SetConfigType("env")
viper.Set("THEME", AppConfig.Theme) viper.Set("THEME", AppConfig.Theme)
viper.WriteConfig() viper.WriteConfig()
} }

View file

@ -23,6 +23,7 @@ type Conf struct {
Timeout int Timeout int
ShoutUrl string ShoutUrl string
Theme string Theme string
OnlyCompareMac bool
} }
var AppConfig Conf var AppConfig Conf