adguard-exporter/main.go

54 lines
1.2 KiB
Go
Raw Normal View History

2020-11-01 22:09:36 +00:00
package main
import (
2020-11-02 18:19:37 +00:00
"fmt"
"os"
"os/signal"
"syscall"
"time"
2020-11-02 18:19:37 +00:00
"github.com/ebrianne/adguard-exporter/config"
"github.com/ebrianne/adguard-exporter/internal/adguard"
"github.com/ebrianne/adguard-exporter/internal/metrics"
"github.com/ebrianne/adguard-exporter/internal/server"
2020-11-01 22:09:36 +00:00
)
const (
2020-11-02 18:19:37 +00:00
name = "adguard-exporter"
2020-11-01 22:09:36 +00:00
)
var (
2020-11-02 18:19:37 +00:00
s *server.Server
2020-11-01 22:09:36 +00:00
)
func main() {
2020-11-02 18:19:37 +00:00
conf := config.Load()
2020-11-02 18:19:37 +00:00
metrics.Init()
2020-11-01 22:09:36 +00:00
initAdguardClient(conf.AdguardProtocol, conf.AdguardHostname, conf.AdguardUsername, conf.AdguardPassword, conf.AdguardPort, conf.Interval, conf.LogLimit, conf.RDnsEnabled)
initHttpServer(conf.ServerPort)
2020-11-01 22:09:36 +00:00
2020-11-02 18:19:37 +00:00
handleExitSignal()
}
2020-11-01 22:09:36 +00:00
func initAdguardClient(protocol, hostname, username, password, port string, interval time.Duration, logLimit string, rdnsenabled bool) {
client := adguard.NewClient(protocol, hostname, username, password, port, interval, logLimit, rdnsenabled)
2020-11-02 18:19:37 +00:00
go client.Scrape()
2020-11-01 22:09:36 +00:00
}
func initHttpServer(port string) {
2020-11-02 18:19:37 +00:00
s = server.NewServer(port)
go s.ListenAndServe()
2020-11-01 22:09:36 +00:00
}
func handleExitSignal() {
2020-11-02 18:19:37 +00:00
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
2020-11-01 22:09:36 +00:00
2020-11-02 18:19:37 +00:00
<-stop
2020-11-01 22:09:36 +00:00
2020-11-02 18:19:37 +00:00
s.Stop()
fmt.Println(fmt.Sprintf("\n%s HTTP server stopped", name))
2020-11-01 22:09:36 +00:00
}