2021-10-12 20:38:26 +00:00
|
|
|
package textfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2023-06-22 14:33:56 +00:00
|
|
|
"os"
|
2021-10-12 20:38:26 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2023-06-22 14:33:56 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-10-12 20:38:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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() {
|
2023-06-22 14:33:56 +00:00
|
|
|
files, err := os.ReadDir(c.folderPath)
|
2021-10-12 20:38:26 +00:00
|
|
|
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)
|
2023-06-22 14:33:56 +00:00
|
|
|
content, err := os.ReadFile(fullPath)
|
2021-10-12 20:38:26 +00:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|