pihole-exporter/main.go

54 lines
1.0 KiB
Go
Raw Normal View History

2019-05-08 23:45:04 +02:00
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/eko/pihole-exporter/config"
"github.com/eko/pihole-exporter/internal/metrics"
"github.com/eko/pihole-exporter/internal/pihole"
"github.com/eko/pihole-exporter/internal/server"
)
const (
name = "pihole-exporter"
)
var (
s *server.Server
)
func main() {
conf := config.Load()
metrics.Init()
initPiHoleClient(conf.PIHoleProtocol, conf.PIHoleHostname, conf.PIHolePort, conf.PIHolePassword, conf.PIHoleApiToken, conf.Interval)
2019-05-08 23:45:04 +02:00
initHttpServer(conf.Port)
handleExitSignal()
}
func initPiHoleClient(protocol, hostname string, port uint16, password, apiToken string, interval time.Duration) {
client := pihole.NewClient(protocol, hostname, port, password, apiToken, interval)
2019-05-09 21:11:31 +02:00
go client.Scrape()
2019-05-08 23:45:04 +02:00
}
func initHttpServer(port string) {
s = server.NewServer(port)
go s.ListenAndServe()
}
func handleExitSignal() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
s.Stop()
fmt.Println(fmt.Sprintf("\n%s HTTP server stopped", name))
}