2023-12-15 16:35:41 +01:00
|
|
|
// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2023-12-14 12:09:01 +01:00
|
|
|
package logranger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-12-22 15:24:58 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2023-12-14 12:09:01 +01:00
|
|
|
|
|
|
|
"github.com/kkyr/fig"
|
2023-12-22 15:24:58 +01:00
|
|
|
"github.com/wneessen/go-parsesyslog"
|
|
|
|
"github.com/wneessen/go-parsesyslog/rfc3164"
|
|
|
|
"github.com/wneessen/go-parsesyslog/rfc5424"
|
2023-12-14 12:09:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config holds all the global configuration settings that are parsed by fig
|
|
|
|
type Config struct {
|
|
|
|
// Server holds server specific configuration values
|
|
|
|
Server struct {
|
2023-12-22 17:31:41 +01:00
|
|
|
PIDFile string `fig:"pid_file" default:"/var/run/logranger.pid"`
|
|
|
|
RuleFile string `fig:"rule_file" default:"etc/logranger.rules.toml"`
|
2023-12-14 12:09:01 +01:00
|
|
|
}
|
|
|
|
Listener struct {
|
|
|
|
ListenerUnix struct {
|
|
|
|
Path string `fig:"path" default:"/var/tmp/logranger.sock"`
|
|
|
|
} `fig:"unix"`
|
|
|
|
ListenerTCP struct {
|
|
|
|
Addr string `fig:"addr" default:"0.0.0.0"`
|
|
|
|
Port uint `fig:"port" default:"9099"`
|
|
|
|
} `fig:"tcp"`
|
|
|
|
ListenerTLS struct {
|
|
|
|
Addr string `fig:"addr" default:"0.0.0.0"`
|
|
|
|
Port uint `fig:"port" default:"9099"`
|
|
|
|
CertPath string `fig:"cert_path"`
|
|
|
|
KeyPath string `fig:"key_path"`
|
|
|
|
} `fig:"tls"`
|
|
|
|
Type ListenerType `fig:"type" default:"unix"`
|
|
|
|
} `fig:"listener"`
|
|
|
|
Log struct {
|
2023-12-22 15:24:58 +01:00
|
|
|
Level string `fig:"level" default:"info"`
|
|
|
|
Extended bool `fig:"extended"`
|
2023-12-14 12:09:01 +01:00
|
|
|
} `fig:"log"`
|
2023-12-22 15:24:58 +01:00
|
|
|
Parser struct {
|
|
|
|
Type string `fig:"type" validate:"required"`
|
|
|
|
Timeout time.Duration `fig:"timeout" default:"500ms"`
|
2023-12-22 16:08:54 +01:00
|
|
|
} `fig:"parser"`
|
2023-12-22 15:24:58 +01:00
|
|
|
internal struct {
|
|
|
|
ParserType parsesyslog.ParserType
|
|
|
|
}
|
2023-12-14 12:09:01 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 20:17:56 +01:00
|
|
|
// NewConfig creates a new instance of the Config object by reading and loading
|
|
|
|
// configuration values. It takes in the file path and file name of the configuration
|
|
|
|
// file as parameters. It returns a pointer to the Config object and an error if
|
|
|
|
// there was a problem reading or loading the configuration.
|
2023-12-14 12:09:01 +01:00
|
|
|
func NewConfig(p, f string) (*Config, error) {
|
|
|
|
co := Config{}
|
|
|
|
_, err := os.Stat(fmt.Sprintf("%s/%s", p, f))
|
|
|
|
if err != nil {
|
|
|
|
return &co, fmt.Errorf("failed to read config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fig.Load(&co, fig.Dirs(p), fig.File(f), fig.UseEnv("logranger")); err != nil {
|
|
|
|
return &co, fmt.Errorf("failed to load config: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-12-22 15:24:58 +01:00
|
|
|
switch {
|
|
|
|
case strings.EqualFold(co.Parser.Type, "rfc3164"):
|
|
|
|
co.internal.ParserType = rfc3164.Type
|
|
|
|
case strings.EqualFold(co.Parser.Type, "rfc5424"):
|
|
|
|
co.internal.ParserType = rfc5424.Type
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown parser type: %s", co.Parser.Type)
|
|
|
|
}
|
|
|
|
|
2023-12-14 12:09:01 +01:00
|
|
|
return &co, nil
|
|
|
|
}
|