netdata/netdata

View on GitHub
src/go/collectors/go.d.plugin/modules/fail2ban/fail2ban.go

Summary

Maintainability
A
0 mins
Test Coverage
// SPDX-License-Identifier: GPL-3.0-or-later

package fail2ban

import (
    _ "embed"
    "errors"
    "time"

    "github.com/netdata/netdata/go/go.d.plugin/agent/module"
    "github.com/netdata/netdata/go/go.d.plugin/pkg/web"
)

//go:embed "config_schema.json"
var configSchema string

func init() {
    module.Register("fail2ban", module.Creator{
        JobConfigSchema: configSchema,
        Defaults: module.Defaults{
            UpdateEvery: 10,
        },
        Create: func() module.Module { return New() },
    })
}

func New() *Fail2Ban {
    return &Fail2Ban{
        Config: Config{
            Timeout: web.Duration(time.Second * 2),
        },
        charts:        &module.Charts{},
        discoverEvery: time.Minute * 5,
        seenJails:     make(map[string]bool),
    }
}

type Config struct {
    UpdateEvery int          `yaml:"update_every" json:"update_every"`
    Timeout     web.Duration `yaml:"timeout" json:"timeout"`
}

type (
    Fail2Ban struct {
        module.Base
        Config `yaml:",inline" json:""`

        charts *module.Charts

        exec fail2banClientCli

        discoverEvery    time.Duration
        lastDiscoverTime time.Time
        forceDiscover    bool
        jails            []string

        seenJails map[string]bool
    }
    fail2banClientCli interface {
        status() ([]byte, error)
        jailStatus(s string) ([]byte, error)
    }
)

func (f *Fail2Ban) Configuration() any {
    return f.Config
}

func (f *Fail2Ban) Init() error {
    f2bClientExec, err := f.initFail2banClientCliExec()
    if err != nil {
        f.Errorf("fail2ban-client exec initialization: %v", err)
        return err
    }
    f.exec = f2bClientExec

    return nil
}

func (f *Fail2Ban) Check() error {
    mx, err := f.collect()
    if err != nil {
        f.Error(err)
        return err
    }

    if len(mx) == 0 {
        return errors.New("no metrics collected")
    }

    return nil
}

func (f *Fail2Ban) Charts() *module.Charts {
    return f.charts
}

func (f *Fail2Ban) Collect() map[string]int64 {
    mx, err := f.collect()
    if err != nil {
        f.Error(err)
    }

    if len(mx) == 0 {
        return nil
    }

    return mx
}

func (f *Fail2Ban) Cleanup() {}