Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion plugins/inputs/fail2ban/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions plugins/inputs/fail2ban/fail2ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ var (
)

type Fail2ban struct {
UseSudo bool `toml:"use_sudo"`
Socket string `toml:"socket"`
path string
UseSudo bool
}

var metricsTargets = []struct {
Expand Down Expand Up @@ -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()
Expand All @@ -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))
Expand Down
5 changes: 4 additions & 1 deletion plugins/inputs/fail2ban/sample.conf
Original file line number Diff line number Diff line change
@@ -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"