pihole-exporter/internal/server/server.go

115 lines
2.8 KiB
Go
Raw Normal View History

2019-05-08 23:45:04 +02:00
package server
import (
"fmt"
2019-05-08 23:45:04 +02:00
"log"
"net/http"
"strconv"
2021-12-10 15:39:22 +01:00
"strings"
2019-05-08 23:45:04 +02:00
"time"
"github.com/eko/pihole-exporter/internal/pihole"
2021-12-10 15:39:22 +01:00
"github.com/prometheus/client_golang/prometheus/promhttp"
2019-05-08 23:45:04 +02:00
"golang.org/x/net/context"
)
2019-05-09 21:11:31 +02:00
// Server is the struct for the HTTP server.
2019-05-08 23:45:04 +02:00
type Server struct {
httpServer *http.Server
}
2019-05-09 21:11:31 +02:00
// NewServer method initializes a new HTTP server instance and associates
// the different routes that will be used by Prometheus (metrics) or for monitoring (readiness, liveness).
func NewServer(port uint16, clients []*pihole.Client) *Server {
2019-05-08 23:45:04 +02:00
mux := http.NewServeMux()
httpServer := &http.Server{
Addr: ":" + strconv.Itoa(int(port)),
Handler: mux,
}
2019-05-08 23:45:04 +02:00
s := &Server{
httpServer: httpServer,
}
mux.HandleFunc("/metrics", func(writer http.ResponseWriter, request *http.Request) {
log.Printf("request.Header: %v\n", request.Header)
2022-01-05 21:58:06 +01:00
for _, client := range clients {
go client.CollectMetricsAsync(writer, request)
}
2021-12-10 15:39:22 +01:00
for _, client := range clients {
2022-01-05 21:58:06 +01:00
status := <-client.Status
if status.Status == pihole.MetricsCollectionError {
log.Printf("An error occured while contacting %s: %s", client.GetHostname(), status.Err.Error())
}
}
2021-12-10 15:39:22 +01:00
promhttp.Handler().ServeHTTP(writer, request)
})
2019-05-08 23:45:04 +02:00
mux.Handle("/readiness", s.readinessHandler())
mux.Handle("/liveness", s.livenessHandler())
return s
}
2019-05-09 21:11:31 +02:00
// ListenAndServe method serves HTTP requests.
2019-05-08 23:45:04 +02:00
func (s *Server) ListenAndServe() {
log.Println("Starting HTTP server")
err := s.httpServer.ListenAndServe()
if err != nil {
log.Printf("Failed to start serving HTTP requests: %v", err)
}
}
2019-05-09 21:11:31 +02:00
// Stop method stops the HTTP server (so the exporter become unavailable).
2019-05-08 23:45:04 +02:00
func (s *Server) Stop() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
s.httpServer.Shutdown(ctx)
}
func (s *Server) handleMetrics(clients []*pihole.Client) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
errors := make([]string, 0)
for _, client := range clients {
if err := client.CollectMetrics(writer, request); err != nil {
errors = append(errors, err.Error())
fmt.Printf("Error %s\n", err)
}
}
if len(errors) == len(clients) {
writer.WriteHeader(http.StatusBadRequest)
body := strings.Join(errors, "\n")
_, _ = writer.Write([]byte(body))
}
promhttp.Handler().ServeHTTP(writer, request)
}
}
2019-05-08 23:45:04 +02:00
func (s *Server) readinessHandler() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
2019-05-08 23:45:04 +02:00
if s.isReady() {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
2019-05-08 23:45:04 +02:00
}
func (s *Server) livenessHandler() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
2019-05-08 23:45:04 +02:00
w.WriteHeader(http.StatusOK)
}
2019-05-08 23:45:04 +02:00
}
func (s *Server) isReady() bool {
return s.httpServer != nil
}