mirror of
https://github.com/wneessen/logranger.git
synced 2024-11-22 12:50:50 +01:00
Implement Ruleset configuration in logranger
Added a Ruleset configuration to the logranger application, along with associated error handling. The code now loads a ruleset file from configuration, checks for duplicate rules, and logs each rule as it is found. The ruleset is also integrated within the server setup. The PIDFile now includes a default RuleFile reference in config.go.
This commit is contained in:
parent
77ad132bc7
commit
0fae461408
3 changed files with 65 additions and 2 deletions
|
@ -20,7 +20,8 @@ import (
|
|||
type Config struct {
|
||||
// Server holds server specific configuration values
|
||||
Server struct {
|
||||
PIDFile string `fig:"pid_file" default:"/var/run/logranger.pid"`
|
||||
PIDFile string `fig:"pid_file" default:"/var/run/logranger.pid"`
|
||||
RuleFile string `fig:"rule_file" default:"etc/logranger.rules.toml"`
|
||||
}
|
||||
Listener struct {
|
||||
ListenerUnix struct {
|
||||
|
|
48
rule.go
Normal file
48
rule.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package logranger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/kkyr/fig"
|
||||
)
|
||||
|
||||
type Ruleset struct {
|
||||
Rule []struct {
|
||||
ID string `fig:"id" validate:"required"`
|
||||
Regexp *regexp.Regexp `fig:"regexp" validate:"required"`
|
||||
} `fig:"rule"`
|
||||
}
|
||||
|
||||
func NewRuleset(c *Config) (*Ruleset, error) {
|
||||
rs := &Ruleset{}
|
||||
p := filepath.Dir(c.Server.RuleFile)
|
||||
f := filepath.Base(c.Server.RuleFile)
|
||||
_, err := os.Stat(fmt.Sprintf("%s/%s", p, f))
|
||||
if err != nil {
|
||||
return rs, fmt.Errorf("failed to read config: %w", err)
|
||||
}
|
||||
|
||||
if err = fig.Load(rs, fig.Dirs(p), fig.File(f), fig.UseStrict()); err != nil {
|
||||
return rs, fmt.Errorf("failed to load ruleset: %w", err)
|
||||
}
|
||||
|
||||
rna := make([]string, 0)
|
||||
for _, r := range rs.Rule {
|
||||
for _, rn := range rna {
|
||||
if strings.EqualFold(r.ID, rn) {
|
||||
return nil, fmt.Errorf("duplicate rule found: %s", r.ID)
|
||||
}
|
||||
}
|
||||
rna = append(rna, r.ID)
|
||||
}
|
||||
|
||||
return rs, nil
|
||||
}
|
16
server.go
16
server.go
|
@ -35,6 +35,8 @@ type Server struct {
|
|||
log *slog.Logger
|
||||
// parser is a parsesyslog.Parser
|
||||
parser parsesyslog.Parser
|
||||
// ruleset is a pointer to the ruleset
|
||||
ruleset *Ruleset
|
||||
// wg is a sync.WaitGroup
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
@ -55,11 +57,22 @@ func (s *Server) Run() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p, err := parsesyslog.New(s.conf.internal.ParserType)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize syslog parser: %w", err)
|
||||
}
|
||||
s.parser = p
|
||||
|
||||
rs, err := NewRuleset(s.conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read ruleset: %w", err)
|
||||
}
|
||||
s.ruleset = rs
|
||||
for _, r := range rs.Rule {
|
||||
s.log.Debug("found rule", slog.String("ID", r.ID))
|
||||
}
|
||||
|
||||
return s.RunWithListener(l)
|
||||
}
|
||||
|
||||
|
@ -159,7 +172,8 @@ ReadLoop:
|
|||
s.log.Debug("log message successfully received",
|
||||
slog.String("message", lm.Message.String()),
|
||||
slog.String("facility", lm.Facility.String()),
|
||||
slog.String("severity", lm.Severity.String()))
|
||||
slog.String("severity", lm.Severity.String()),
|
||||
slog.Time("server_time", lm.Timestamp))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue