You've already forked prometheus-fail2ban-exporter
feat: collect new up metric from fail2ban socket
Add support for connecting the exporter directly to the fail2ban server's socket to send requests and receive data. The path to the socket file is optional and specified on startup. Export a new metric based on the response of the `ping` command sent to the fail2ban server. The metric is set to 1 if the server responds with `pong` and 0 in any other case. This metric is only shown if the path to the socket file was provided on startup.
This commit is contained in:
53
src/socket/protocol.go
Normal file
53
src/socket/protocol.go
Normal file
@ -0,0 +1,53 @@
|
||||
package socket
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/nlpodyssey/gopickle/pickle"
|
||||
)
|
||||
|
||||
const (
|
||||
commandTerminator = "<F2B_END_COMMAND>"
|
||||
pingCommand = "ping"
|
||||
socketReadBufferSize = 10000
|
||||
)
|
||||
|
||||
func (s *Fail2BanSocket) sendCommand(command []string) (interface{}, error) {
|
||||
err := s.write(command)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.read()
|
||||
}
|
||||
|
||||
func (s *Fail2BanSocket) write(command []string) error {
|
||||
err := s.encoder.Encode(command)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.socket.Write([]byte(commandTerminator))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Fail2BanSocket) read() (interface{}, error) {
|
||||
buf := make([]byte, socketReadBufferSize)
|
||||
_, err := s.socket.Read(buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bufReader := bytes.NewReader(buf)
|
||||
unpickler := pickle.NewUnpickler(bufReader)
|
||||
|
||||
unpickler.FindClass = func(module, name string) (interface{}, error) {
|
||||
if module == "builtins" && name == "str" {
|
||||
return &Py_builtins_str{}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("class not found: " + module + " : " + name)
|
||||
}
|
||||
|
||||
return unpickler.Load()
|
||||
}
|
Reference in New Issue
Block a user