update socket protocol to get version string

Add a new method to get the version data from the fail2ban socket and
return it to the metric collector. The version is now correctly exposed.
This commit is contained in:
Hector 2021-09-10 07:09:22 +01:00
parent 3f09e5af8c
commit 1a6cd7a1a9
3 changed files with 22 additions and 2 deletions

View File

@ -282,9 +282,14 @@ func (e *Exporter) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket
} }
func (e *Exporter) collectVersionMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) { func (e *Exporter) collectVersionMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
fail2banVersion := "9.9.9" var err error
var fail2banVersion = ""
if s != nil { if s != nil {
// TODO: Get fail2ban server version fail2banVersion, err = s.GetServerVersion()
if err != nil {
e.socketRequestErrorCount++
log.Printf("failed to get fail2ban server version: %v", err)
}
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(

View File

@ -118,6 +118,20 @@ func (s *Fail2BanSocket) GetJailStats(jail string) (JailStats, error) {
return stats, newBadFormatError(statusCommand, response) return stats, newBadFormatError(statusCommand, response)
} }
func (s *Fail2BanSocket) GetServerVersion() (string, error) {
response, err := s.sendCommand([]string{versionCommand})
if err != nil {
return "", err
}
if lvl1, ok := response.(*types.Tuple); ok {
if versionStr, ok := lvl1.Get(1).(string); ok {
return versionStr, nil
}
}
return "", newBadFormatError(versionCommand, response)
}
func newBadFormatError(command string, data interface{}) error { func newBadFormatError(command string, data interface{}) error {
return fmt.Errorf("(%s) unexpected response format - cannot parse: %v", command, data) return fmt.Errorf("(%s) unexpected response format - cannot parse: %v", command, data)
} }

View File

@ -11,6 +11,7 @@ const (
commandTerminator = "<F2B_END_COMMAND>" commandTerminator = "<F2B_END_COMMAND>"
pingCommand = "ping" pingCommand = "ping"
statusCommand = "status" statusCommand = "status"
versionCommand = "version"
socketReadBufferSize = 1024 socketReadBufferSize = 1024
) )