
Replace existing CLI flags to make them more consistent and follow a more standard format. Remove CLI flags and environment variables that are no longer relevant. Add short `-v` option for version flag. Update README with new documentation. BREAKING CHANGE: Replace `--socket` flag with `--collector.f2b.socket`. BREAKING CHANGE: Merge `--port` flag and `--web.listen-address` into a single flag. BREAKING CHANGE: Remove `--collector.textfile` flag, its value is now derived from `collector.textfile.directory`. BREAKING CHANGE: Remove `F2B_COLLECTOR_TEXT` and `F2B_WEB_PORT` environment variables
49 lines
977 B
Go
49 lines
977 B
Go
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.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++
|
|
}
|