You've already forked prometheus-fail2ban-exporter
refactor: create new collector folder
Create a new `collector` folder to store the code for the different collectors. Move the existing f2b and textfile collectors to this folder. Minor refactors to the f2b collector to better match the code style of the newer textfile collector.
This commit is contained in:
48
src/collector/textfile/collector.go
Normal file
48
src/collector/textfile/collector.go
Normal file
@ -0,0 +1,48 @@
|
||||
package textfile
|
||||
|
||||
import (
|
||||
"fail2ban-prometheus-exporter/cfg"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"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.FileCollectorEnabled,
|
||||
folderPath: appSettings.FileCollectorPath,
|
||||
fileMap: make(map[string]*fileData),
|
||||
}
|
||||
if collector.enabled {
|
||||
log.Printf("collector.textfile directory: %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++
|
||||
}
|
Reference in New Issue
Block a user