syslog filter could take several filters

This commit is contained in:
nemunaire 2026-01-01 22:37:02 +07:00
commit 69594c2fe4
4 changed files with 48 additions and 23 deletions

View file

@ -14,7 +14,7 @@ func declareFlags(o *Config) {
flag.StringVar(&o.ARPTablePath, "arp-table-path", "/proc/net/arp", "Path to ARP table file")
flag.BoolVar(&o.SyslogEnabled, "syslog-enabled", false, "Enable syslog tailing for iwd messages")
flag.StringVar(&o.SyslogPath, "syslog-path", "/var/log/messages", "Path to syslog file")
flag.StringVar(&o.SyslogFilter, "syslog-filter", "daemon.info iwd:", "Filter string for syslog lines")
flag.Var(&StringArray{&o.SyslogFilter}, "daemon.info iwd:", "Filter string for syslog lines")
flag.StringVar(&o.SyslogSource, "syslog-source", "iwd", "Source name for syslog entries in logs")
}

View file

@ -17,7 +17,7 @@ type Config struct {
ARPTablePath string
SyslogEnabled bool
SyslogPath string
SyslogFilter string
SyslogFilter []string
SyslogSource string
}
@ -35,7 +35,7 @@ func ConsolidateConfig() (opts *Config, err error) {
ARPTablePath: "/proc/net/arp",
SyslogEnabled: false,
SyslogPath: "/var/log/messages",
SyslogFilter: "daemon.info iwd:",
SyslogFilter: []string{"daemon.info wpa_supplicant:", "daemon.info iwd:", "daemon.info hostapd:"},
SyslogSource: "iwd",
}

View file

@ -1,9 +1,27 @@
package config
import (
"fmt"
"net/url"
)
// StringArray is a custom type for handling multiple string values in flags.
type StringArray struct {
Array *[]string
}
// String returns a string representation of the StringArray.
func (i *StringArray) String() string {
return fmt.Sprintf("%v", i.Array)
}
// Set appends a new string value to the StringArray.
func (i *StringArray) Set(value string) error {
*i.Array = append(*i.Array, value)
return nil
}
type URL struct {
URL *url.URL
}

View file

@ -15,7 +15,7 @@ import (
// SyslogTailer tails a syslog file and filters messages to the logging system.
type SyslogTailer struct {
path string
filter string
filters []string
source string
file *os.File
@ -29,10 +29,10 @@ type SyslogTailer struct {
// path: Path to the syslog file (e.g., "/var/log/messages")
// filter: Filter string to match in lines (e.g., "daemon.info iwd:")
// source: Source name for logging (e.g., "iwd")
func NewSyslogTailer(path, filter, source string) *SyslogTailer {
func NewSyslogTailer(path string, filters []string, source string) *SyslogTailer {
return &SyslogTailer{
path: path,
filter: filter,
filters: filters,
source: source,
done: make(chan struct{}),
}
@ -185,15 +185,22 @@ func (t *SyslogTailer) readLines(file *os.File) error {
line := scanner.Text()
// Check if the line contains the filter string
if !strings.Contains(line, t.filter) {
// Check if the line contains any of the filter strings
var matchedFilter string
for _, filter := range t.filters {
if strings.Contains(line, filter) {
matchedFilter = filter
break
}
}
if matchedFilter == "" {
continue
}
// Parse the syslog line to extract the message
// We look for "iwd:" (or whatever comes after the filter)
// The filter is "daemon.info iwd:" so we want to extract text after "iwd:"
daemonPrefix := extractDaemonPrefix(t.filter)
daemonPrefix := extractDaemonPrefix(matchedFilter)
message, ok := ParseSyslogLine(line, daemonPrefix)
if !ok {
// Couldn't parse the line, skip it