You've already forked prometheus-fail2ban-exporter
chore: move source code to root folder
This commit is contained in:
60
collector/f2b/collector.go
Normal file
60
collector/f2b/collector.go
Normal file
@ -0,0 +1,60 @@
|
||||
package f2b
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/cfg"
|
||||
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/socket"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Collector struct {
|
||||
socketPath string
|
||||
exporterVersion string
|
||||
lastError error
|
||||
socketConnectionErrorCount int
|
||||
socketRequestErrorCount int
|
||||
exitOnSocketConnError bool
|
||||
}
|
||||
|
||||
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector {
|
||||
log.Printf("reading metrics from fail2ban socket: %s", appSettings.Fail2BanSocketPath)
|
||||
return &Collector{
|
||||
socketPath: appSettings.Fail2BanSocketPath,
|
||||
exporterVersion: exporterVersion,
|
||||
lastError: nil,
|
||||
socketConnectionErrorCount: 0,
|
||||
socketRequestErrorCount: 0,
|
||||
exitOnSocketConnError: appSettings.ExitOnSocketConnError,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
|
||||
ch <- metricServerUp
|
||||
ch <- metricJailCount
|
||||
ch <- metricJailFailedCurrent
|
||||
ch <- metricJailFailedTotal
|
||||
ch <- metricJailBannedCurrent
|
||||
ch <- metricJailBannedTotal
|
||||
ch <- metricErrorCount
|
||||
}
|
||||
|
||||
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
|
||||
s, err := socket.ConnectToSocket(c.socketPath)
|
||||
if err != nil {
|
||||
log.Printf("error opening socket: %v", err)
|
||||
c.socketConnectionErrorCount++
|
||||
if c.exitOnSocketConnError {
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
defer s.Close()
|
||||
}
|
||||
|
||||
c.collectServerUpMetric(ch, s)
|
||||
if err == nil && s != nil {
|
||||
c.collectJailMetrics(ch, s)
|
||||
c.collectVersionMetric(ch, s)
|
||||
}
|
||||
c.collectErrorCountMetric(ch)
|
||||
}
|
179
collector/f2b/socket.go
Normal file
179
collector/f2b/socket.go
Normal file
@ -0,0 +1,179 @@
|
||||
package f2b
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/socket"
|
||||
"log"
|
||||
)
|
||||
|
||||
const (
|
||||
namespace = "f2b"
|
||||
)
|
||||
|
||||
var (
|
||||
metricErrorCount = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "", "errors"),
|
||||
"Number of errors found since startup",
|
||||
[]string{"type"}, nil,
|
||||
)
|
||||
metricServerUp = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "", "up"),
|
||||
"Check if the fail2ban server is up",
|
||||
nil, nil,
|
||||
)
|
||||
metricJailCount = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "", "jail_count"),
|
||||
"Number of defined jails",
|
||||
nil, nil,
|
||||
)
|
||||
metricJailFailedCurrent = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "", "jail_failed_current"),
|
||||
"Number of current failures on this jail's filter",
|
||||
[]string{"jail"}, nil,
|
||||
)
|
||||
metricJailFailedTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "", "jail_failed_total"),
|
||||
"Number of total failures on this jail's filter",
|
||||
[]string{"jail"}, nil,
|
||||
)
|
||||
metricJailBannedCurrent = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "", "jail_banned_current"),
|
||||
"Number of IPs currently banned in this jail",
|
||||
[]string{"jail"}, nil,
|
||||
)
|
||||
metricJailBannedTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "", "jail_banned_total"),
|
||||
"Total number of IPs banned by this jail (includes expired bans)",
|
||||
[]string{"jail"}, nil,
|
||||
)
|
||||
metricJailBanTime = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "config", "jail_ban_time"),
|
||||
"How long an IP is banned for in this jail (in seconds)",
|
||||
[]string{"jail"}, nil,
|
||||
)
|
||||
metricJailFindTime = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "config", "jail_find_time"),
|
||||
"How far back will the filter look for failures in this jail (in seconds)",
|
||||
[]string{"jail"}, nil,
|
||||
)
|
||||
metricJailMaxRetry = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "config", "jail_max_retries"),
|
||||
"The number of failures allowed until the IP is banned by this jail",
|
||||
[]string{"jail"}, nil,
|
||||
)
|
||||
metricVersionInfo = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "", "version"),
|
||||
"Version of the exporter and fail2ban server",
|
||||
[]string{"exporter", "fail2ban"}, nil,
|
||||
)
|
||||
)
|
||||
|
||||
func (c *Collector) collectErrorCountMetric(ch chan<- prometheus.Metric) {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricErrorCount, prometheus.CounterValue, float64(c.socketConnectionErrorCount), "socket_conn",
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricErrorCount, prometheus.CounterValue, float64(c.socketRequestErrorCount), "socket_req",
|
||||
)
|
||||
}
|
||||
|
||||
func (c *Collector) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||
var serverUp float64 = 0
|
||||
if s != nil {
|
||||
pingSuccess, err := s.Ping()
|
||||
if err != nil {
|
||||
c.socketRequestErrorCount++
|
||||
log.Print(err)
|
||||
}
|
||||
if err == nil && pingSuccess {
|
||||
serverUp = 1
|
||||
}
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricServerUp, prometheus.GaugeValue, serverUp,
|
||||
)
|
||||
}
|
||||
|
||||
func (c *Collector) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||
jails, err := s.GetJails()
|
||||
var count float64 = 0
|
||||
if err != nil {
|
||||
c.socketRequestErrorCount++
|
||||
log.Print(err)
|
||||
}
|
||||
if err == nil {
|
||||
count = float64(len(jails))
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricJailCount, prometheus.GaugeValue, count,
|
||||
)
|
||||
|
||||
for i := range jails {
|
||||
c.collectJailStatsMetric(ch, s, jails[i])
|
||||
c.collectJailConfigMetrics(ch, s, jails[i])
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Collector) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) {
|
||||
stats, err := s.GetJailStats(jail)
|
||||
if err != nil {
|
||||
c.socketRequestErrorCount++
|
||||
log.Printf("failed to get stats for jail %s: %v", jail, err)
|
||||
return
|
||||
}
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricJailFailedCurrent, prometheus.GaugeValue, float64(stats.FailedCurrent), jail,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricJailFailedTotal, prometheus.GaugeValue, float64(stats.FailedTotal), jail,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricJailBannedCurrent, prometheus.GaugeValue, float64(stats.BannedCurrent), jail,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricJailBannedTotal, prometheus.GaugeValue, float64(stats.BannedTotal), jail,
|
||||
)
|
||||
}
|
||||
|
||||
func (c *Collector) collectJailConfigMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) {
|
||||
banTime, err := s.GetJailBanTime(jail)
|
||||
if err != nil {
|
||||
c.socketRequestErrorCount++
|
||||
log.Printf("failed to get ban time for jail %s: %v", jail, err)
|
||||
} else {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricJailBanTime, prometheus.GaugeValue, float64(banTime), jail,
|
||||
)
|
||||
}
|
||||
findTime, err := s.GetJailFindTime(jail)
|
||||
if err != nil {
|
||||
c.socketRequestErrorCount++
|
||||
log.Printf("failed to get find time for jail %s: %v", jail, err)
|
||||
} else {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricJailFindTime, prometheus.GaugeValue, float64(findTime), jail,
|
||||
)
|
||||
}
|
||||
maxRetry, err := s.GetJailMaxRetries(jail)
|
||||
if err != nil {
|
||||
c.socketRequestErrorCount++
|
||||
log.Printf("failed to get max retries for jail %s: %v", jail, err)
|
||||
} else {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricJailMaxRetry, prometheus.GaugeValue, float64(maxRetry), jail,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Collector) collectVersionMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||
fail2banVersion, err := s.GetServerVersion()
|
||||
if err != nil {
|
||||
c.socketRequestErrorCount++
|
||||
log.Printf("failed to get fail2ban server version: %v", err)
|
||||
}
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricVersionInfo, prometheus.GaugeValue, float64(1), c.exporterVersion, fail2banVersion,
|
||||
)
|
||||
}
|
48
collector/textfile/collector.go
Normal file
48
collector/textfile/collector.go
Normal file
@ -0,0 +1,48 @@
|
||||
package textfile
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/cfg"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Collector struct {
|
||||
enabled bool
|
||||
folderPath string
|
||||
fileMap map[string]*fileData
|
||||
}
|
||||
|
||||
type fileData struct {
|
||||
readErrors int
|
||||
fileName string
|
||||
fileContents []byte
|
||||
}
|
||||
|
||||
func NewCollector(appSettings *cfg.AppSettings) *Collector {
|
||||
collector := &Collector{
|
||||
enabled: appSettings.FileCollectorPath != "",
|
||||
folderPath: appSettings.FileCollectorPath,
|
||||
fileMap: make(map[string]*fileData),
|
||||
}
|
||||
if collector.enabled {
|
||||
log.Printf("reading textfile metrics from: %s", collector.folderPath)
|
||||
}
|
||||
return collector
|
||||
}
|
||||
|
||||
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
|
||||
if c.enabled {
|
||||
ch <- metricReadError
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
|
||||
if c.enabled {
|
||||
c.collectFileContents()
|
||||
c.collectFileErrors(ch)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Collector) appendErrorForPath(path string) {
|
||||
c.fileMap[path].readErrors++
|
||||
}
|
55
collector/textfile/file.go
Normal file
55
collector/textfile/file.go
Normal file
@ -0,0 +1,55 @@
|
||||
package textfile
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const namespace = "textfile"
|
||||
|
||||
var (
|
||||
metricReadError = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "", "error"),
|
||||
"Checks for errors while reading text files",
|
||||
[]string{"path"}, nil,
|
||||
)
|
||||
)
|
||||
|
||||
func (c *Collector) collectFileContents() {
|
||||
files, err := ioutil.ReadDir(c.folderPath)
|
||||
if err != nil {
|
||||
log.Printf("error reading directory '%s': %v", c.folderPath, err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
fileName := file.Name()
|
||||
if !strings.HasSuffix(strings.ToLower(fileName), ".prom") {
|
||||
continue
|
||||
}
|
||||
c.fileMap[fileName] = &fileData{
|
||||
readErrors: 0,
|
||||
fileName: fileName,
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(c.folderPath, fileName)
|
||||
content, err := ioutil.ReadFile(fullPath)
|
||||
if err != nil {
|
||||
c.appendErrorForPath(fileName)
|
||||
log.Printf("error reading contents of file '%s': %v", fileName, err)
|
||||
}
|
||||
|
||||
c.fileMap[fileName].fileContents = content
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Collector) collectFileErrors(ch chan<- prometheus.Metric) {
|
||||
for _, f := range c.fileMap {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricReadError, prometheus.GaugeValue, float64(f.readErrors), f.fileName,
|
||||
)
|
||||
}
|
||||
}
|
20
collector/textfile/writer.go
Normal file
20
collector/textfile/writer.go
Normal file
@ -0,0 +1,20 @@
|
||||
package textfile
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Collector) WriteTextFileMetrics(w http.ResponseWriter, r *http.Request) {
|
||||
if !c.enabled {
|
||||
return
|
||||
}
|
||||
|
||||
for _, f := range c.fileMap {
|
||||
_, err := w.Write(f.fileContents)
|
||||
if err != nil {
|
||||
c.appendErrorForPath(f.fileName)
|
||||
log.Printf("error writing file contents to response writer '%s': %v", f.fileName, err)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user