feat(inputs.fail2ban): Allow specification of socket (#13452)

This commit is contained in:
Sven Rebhan 2023-06-15 20:34:59 +02:00 committed by GitHub
parent 4d22b9106e
commit 1cfa48eea9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 8 deletions

View File

@ -24,7 +24,10 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
# Read metrics from fail2ban.
[[inputs.fail2ban]]
## Use sudo to run fail2ban-client
use_sudo = false
# use_sudo = false
## Use the given socket instead of the default one
# socket = "/var/run/fail2ban/fail2ban.sock"
```
## Using sudo

View File

@ -21,8 +21,9 @@ var (
)
type Fail2ban struct {
UseSudo bool `toml:"use_sudo"`
Socket string `toml:"socket"`
path string
UseSudo bool
}
var metricsTargets = []struct {
@ -69,14 +70,17 @@ func (f *Fail2ban) Gather(acc telegraf.Accumulator) error {
}
name := f.path
var arg []string
var args []string
if f.UseSudo {
name = "sudo"
arg = append(arg, f.path)
args = append(args, f.path)
}
args := append(arg, "status")
if f.Socket != "" {
args = append(args, "--socket", f.Socket)
}
args = append(args, "status")
cmd := execCommand(name, args...)
out, err := cmd.Output()
@ -97,9 +101,12 @@ func (f *Fail2ban) Gather(acc telegraf.Accumulator) error {
}
for _, jail := range jails {
// Skip over empty jails
if jail == "" {
continue
}
fields := make(map[string]interface{})
args := append(arg, "status", jail)
cmd := execCommand(name, args...)
cmd := execCommand(name, append(args, jail)...)
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to run command %q: %w - %s", strings.Join(cmd.Args, " "), err, string(out))

View File

@ -1,4 +1,7 @@
# Read metrics from fail2ban.
[[inputs.fail2ban]]
## Use sudo to run fail2ban-client
use_sudo = false
# use_sudo = false
## Use the given socket instead of the default one
# socket = "/var/run/fail2ban/fail2ban.sock"