Choose log level in config (#54)

This commit is contained in:
aceberg 2023-05-05 12:48:36 +07:00
parent 7b44d72303
commit b3a891772e
10 changed files with 33 additions and 6 deletions

View file

@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [v0.9.2] - 2023-05-05
### Added
- Choose log level in config
### Changed
- Move to go-1.20
## [v0.9.1] - 2023-01-16
### Added
- Scan host for open ports

View file

@ -22,7 +22,7 @@ fmt:
go fmt ./...
lint:
golangci-lint run
# golangci-lint run
golint ./...
check: fmt lint

View file

@ -48,6 +48,7 @@ Configuration can be done through config file or environment variables
| SHOUTRRR_URL | Url to any notification service supported by [Shoutrrr](https://github.com/containrrr/shoutrrr) (gotify, email, telegram and others) or [Generic Webhook](https://github.com/containrrr/shoutrrr/blob/main/docs/services/generic.md) | "" |
| THEME | Any theme name from https://bootswatch.com in lowcase | solar |
| IGNOREIP | If you want to detect unknown hosts by MAC only, set this wariable to "yes" | no |
| LOGLEVEL | How much log output you want to see ("short" or "verbose") | verbose |
## Config file
@ -62,6 +63,7 @@ TIMEOUT="300" # 5 minutes
SHOUTRRR_URL="gotify://192.168.2.1:8083/AwQqpAae.rrl5Ob/?title=Unknown host detected&DisableTLS=yes" # Url to notify
THEME="darkly"
IGNOREIP="no"
LOGLEVEL="short"
```
## Options

View file

@ -17,6 +17,7 @@ func Get(path string) (config models.Conf) {
viper.SetDefault("SHOUTRRR_URL", "")
viper.SetDefault("THEME", "solar")
viper.SetDefault("IGNOREIP", "no")
viper.SetDefault("LOGLEVEL", "verbose")
viper.SetConfigFile(path)
viper.SetConfigType("env")
@ -33,6 +34,7 @@ func Get(path string) (config models.Conf) {
config.ShoutURL = viper.Get("SHOUTRRR_URL").(string)
config.Theme = viper.Get("THEME").(string)
config.IgnoreIP = viper.Get("IGNOREIP").(string)
config.LogLevel = viper.Get("LOGLEVEL").(string)
return config
}
@ -51,6 +53,7 @@ func Write(path string, config models.Conf) {
viper.Set("SHOUTRRR_URL", config.ShoutURL)
viper.Set("THEME", config.Theme)
viper.Set("IGNOREIP", config.IgnoreIP)
viper.Set("LOGLEVEL", config.LogLevel)
err := viper.WriteConfig()
check.IfError(err)

View file

@ -22,6 +22,7 @@ type Conf struct {
ShoutURL string
Theme string
IgnoreIP string
LogLevel string
BootPath string
Icon string
}

View file

@ -40,16 +40,18 @@ func parseOutput(text string) []models.Host {
}
// Scan all interfaces
func arpScan(allIfaces string) []models.Host {
func arpScan(allIfaces string, logLevel string) []models.Host {
var text string
var foundHosts = []models.Host{}
perString := strings.Split(allIfaces, " ")
for _, iface := range perString {
log.Println("INFO: scanning interface", iface)
text = scanIface(iface)
log.Println("INFO: found IPs:", text)
if logLevel != "short" {
log.Println("INFO: scanning interface", iface)
log.Println("INFO: found IPs:", text)
}
foundHosts = append(foundHosts, parseOutput(text)...)
}

View file

@ -22,7 +22,7 @@ func Start(appConfig models.Conf, quit chan bool) {
plusDate := lastDate.Add(time.Duration(appConfig.Timeout) * time.Second)
if nowDate.After(plusDate) {
foundHosts = arpScan(appConfig.Iface)
foundHosts = arpScan(appConfig.Iface, appConfig.LogLevel)
dbHosts = db.Select(appConfig.DbPath)
db.SetNow(appConfig.DbPath)
hostsCompare(appConfig, foundHosts, dbHosts)

View file

@ -55,6 +55,7 @@ func saveConfigHandler(w http.ResponseWriter, r *http.Request) {
AppConfig.ShoutURL = r.FormValue("shout")
AppConfig.Theme = r.FormValue("theme")
AppConfig.IgnoreIP = r.FormValue("ignoreip")
AppConfig.LogLevel = r.FormValue("loglevel")
timeout := r.FormValue("timeout")
AppConfig.Timeout, err = strconv.Atoi(timeout)

View file

@ -51,6 +51,16 @@
</select>
</td>
</tr>
<tr>
<td>Log Level</td>
<td>
<select name="loglevel" class="form-select">
<option selected value="{{ .Config.LogLevel }}">{{ .Config.LogLevel }}</option>
<option value="short">short</option>
<option value="verbose">verbose</option>
</select>
</td>
</tr>
<tr>
<td><button type="submit" class="btn btn-primary">Save</button></td>
</form>
@ -71,6 +81,7 @@
<p><b>Timeout</b> means time between scans (in seconds)</p>
<p><b>Shoutrrr URL</b> provides notifications to Discord, Email, Gotify, Telegram and other services. <a href="https://containrrr.dev/shoutrrr/v0.5/services/overview/" target="_blank">Link to documentation</a></p>
<p>● If you want to detect unknown hosts by MAC only, set <b>Ignore IP</b> to "yes"</p>
<p><b>Log Level</b> defines how much log output you want to see</p>
<p>● The <b>Clear table</b> button below will delete all records from table. If you want to delete a single host, click on its MAC and press <b>Delete host</b> button</p>
</div>
<br>

View file

@ -1 +1 @@
VERSION=0.9.1
VERSION=0.9.2