2021-10-12 22:38:26 +02:00
|
|
|
package textfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-02-25 22:53:38 +01:00
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/cfg"
|
2021-10-12 22:38:26 +02:00
|
|
|
"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{
|
2022-02-19 18:01:49 +01:00
|
|
|
enabled: appSettings.FileCollectorPath != "",
|
2021-10-12 22:38:26 +02:00
|
|
|
folderPath: appSettings.FileCollectorPath,
|
|
|
|
fileMap: make(map[string]*fileData),
|
|
|
|
}
|
|
|
|
if collector.enabled {
|
2022-02-19 12:21:58 +01:00
|
|
|
log.Printf("reading textfile metrics from: %s", collector.folderPath)
|
2021-10-12 22:38:26 +02:00
|
|
|
}
|
|
|
|
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++
|
|
|
|
}
|