summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/fail2ban/exec.go
blob: 06b5841e73336c6f23ddb06cc6a24b04e5f5197f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// SPDX-License-Identifier: GPL-3.0-or-later

package fail2ban

import (
	"context"
	"errors"
	"fmt"
	"os/exec"
	"strings"
	"time"

	"github.com/netdata/netdata/go/go.d.plugin/logger"
)

var errJailNotExist = errors.New("jail not exist")

func newFail2BanClientCliExec(ndsudoPath string, timeout time.Duration, log *logger.Logger) *fail2banClientCliExec {
	return &fail2banClientCliExec{
		Logger:     log,
		ndsudoPath: ndsudoPath,
		timeout:    timeout,
	}
}

type fail2banClientCliExec struct {
	*logger.Logger

	ndsudoPath string
	timeout    time.Duration
}

func (e *fail2banClientCliExec) status() ([]byte, error) {
	return e.execute("fail2ban-client-status")
}

func (e *fail2banClientCliExec) jailStatus(jail string) ([]byte, error) {
	return e.execute("fail2ban-client-status-jail", "--jail", jail)
}

func (e *fail2banClientCliExec) execute(args ...string) ([]byte, error) {
	ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
	defer cancel()

	cmd := exec.CommandContext(ctx, e.ndsudoPath, args...)
	e.Debugf("executing '%s'", cmd)

	bs, err := cmd.Output()
	if err != nil {
		if strings.HasPrefix(strings.TrimSpace(string(bs)), "Sorry but the jail") {
			return nil, errJailNotExist
		}
		return nil, fmt.Errorf("error on '%s': %v", cmd, err)
	}

	return bs, nil
}