Skip to content

Commit

Permalink
Choose log level in config (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed May 5, 2023
1 parent 7b44d72 commit b3a8917
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fmt:
go fmt ./...

lint:
golangci-lint run
# golangci-lint run
golint ./...

check: fmt lint
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions internal/conf/get-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Conf struct {
ShoutURL string
Theme string
IgnoreIP string
LogLevel string
BootPath string
Icon string
}
Expand Down
8 changes: 5 additions & 3 deletions internal/scan/arpscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/scan/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions internal/web/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions internal/web/templates/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion internal/web/templates/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION=0.9.1
VERSION=0.9.2

0 comments on commit b3a8917

Please sign in to comment.