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

View file

@ -1,9 +1,27 @@
package config package config
import ( import (
"fmt"
"net/url" "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 { type URL struct {
URL *url.URL URL *url.URL
} }

View file

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