55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
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,
|
|
)
|
|
}
|
|
}
|