adguard-exporter/main.go

65 lines
1.4 KiB
Go
Raw Normal View History

2020-11-01 22:09:36 +00:00
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"encoding/base64"
"github.com/ebrianne/adguard-exporter/config"
"github.com/ebrianne/adguard-exporter/internal/metrics"
"github.com/ebrianne/adguard-exporter/internal/adguard"
"github.com/ebrianne/adguard-exporter/internal/server"
2020-11-01 22:09:36 +00:00
)
const (
name = "adguard-exporter"
2020-11-01 22:09:36 +00:00
)
var (
s *server.Server
2020-11-01 22:09:36 +00:00
)
func main() {
conf := config.Load()
metrics.Init()
2020-11-01 22:09:36 +00:00
initAdguardClient(conf.AdguardProtocol, conf.AdguardHostname, conf.AdguardPort, conf.AdguardUsername, conf.AdguardPassword, conf.Interval)
initHttpServer(conf.Port)
2020-11-01 22:09:36 +00:00
handleExitSignal()
}
2020-11-01 22:09:36 +00:00
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
2020-11-01 22:09:36 +00:00
}
func initAdguardClient(protocol, hostname string, port uint16, username, password string, interval time.Duration) {
b64password := ""
if len(username) > 0 && len(password) > 0 {
b64password = basicAuth(username, password)
}
client := adguard.NewClient(protocol, hostname, port, b64password, interval)
go client.Scrape()
2020-11-01 22:09:36 +00:00
}
func initHttpServer(port string) {
s = server.NewServer(port)
go s.ListenAndServe()
2020-11-01 22:09:36 +00:00
}
func handleExitSignal() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
2020-11-01 22:09:36 +00:00
<-stop
2020-11-01 22:09:36 +00:00
s.Stop()
fmt.Println(fmt.Sprintf("\n%s HTTP server stopped", name))
2020-11-01 22:09:36 +00:00
}