smartctl_exporter/main.go

69 lines
1.8 KiB
Go
Raw Normal View History

2019-08-14 22:34:49 +02:00
package main
import (
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
2019-08-14 22:34:49 +02:00
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
options Options
logger Logger
)
// SMARTctlManagerCollector implements the Collector interface.
type SMARTctlManagerCollector struct {
}
// Describe sends the super-set of all possible descriptors of metrics
func (i SMARTctlManagerCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(i, ch)
}
// Collect is called by the Prometheus registry when collecting metrics.
func (i SMARTctlManagerCollector) Collect(ch chan<- prometheus.Metric) {
info := NewSMARTctlInfo(ch)
2019-08-14 22:34:49 +02:00
for _, device := range options.SMARTctl.Devices {
if json, err := readData(device); err == nil {
info.SetJSON(json)
smart := NewSMARTctl(json, ch)
smart.Collect()
} else {
logger.Error(err.Error())
}
2019-08-14 22:34:49 +02:00
}
info.Collect()
2019-08-14 22:34:49 +02:00
}
func init() {
options = loadOptions()
if len(options.SMARTctl.Devices) == 0 {
logger.Debug("No devices specified, trying to load them automatically")
json := readSMARTctlDevices()
devices := json.Get("devices").Array()
for _, d := range devices {
device := d.Get("name").String()
logger.Debug("Found device: %s", device)
options.SMARTctl.Devices = append(options.SMARTctl.Devices, device)
}
}
2019-08-14 22:34:49 +02:00
}
func main() {
reg := prometheus.NewPedanticRegistry()
reg.MustRegister(
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
collectors.NewGoCollector(),
2019-08-14 22:34:49 +02:00
)
prometheus.WrapRegistererWithPrefix("", reg).MustRegister(SMARTctlManagerCollector{})
logger.Info("Starting on %s%s", options.SMARTctl.BindTo, options.SMARTctl.URLPath)
http.Handle(options.SMARTctl.URLPath, promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
log.Fatal(http.ListenAndServe(options.SMARTctl.BindTo, nil))
}