2019-05-08 23:45:04 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2021-12-10 04:48:28 +01:00
|
|
|
"fmt"
|
2019-05-08 23:45:04 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2021-12-10 04:48:28 +01:00
|
|
|
"strconv"
|
2021-12-10 15:39:22 +01:00
|
|
|
"strings"
|
2019-05-08 23:45:04 +02:00
|
|
|
"time"
|
|
|
|
|
2021-08-16 23:55:05 +02:00
|
|
|
"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).
|
2021-12-10 04:48:28 +01:00
|
|
|
func NewServer(port uint16, clients []*pihole.Client) *Server {
|
2019-05-08 23:45:04 +02:00
|
|
|
mux := http.NewServeMux()
|
2021-12-26 23:36:30 +01:00
|
|
|
httpServer := &http.Server{
|
|
|
|
Addr: ":" + strconv.Itoa(int(port)),
|
|
|
|
Handler: mux,
|
|
|
|
}
|
2019-05-08 23:45:04 +02:00
|
|
|
|
|
|
|
s := &Server{
|
|
|
|
httpServer: httpServer,
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:36:30 +01:00
|
|
|
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
|
|
|
|
2021-12-26 23:36:30 +01:00
|
|
|
for _, client := range clients {
|
|
|
|
go client.CollectMetricsAsync(writer, request)
|
|
|
|
}
|
2021-12-10 15:39:22 +01:00
|
|
|
|
2021-12-26 23:36:30 +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-26 23:36:30 +01:00
|
|
|
}
|
2021-12-10 15:39:22 +01:00
|
|
|
|
2021-12-26 23:36:30 +01:00
|
|
|
promhttp.Handler().ServeHTTP(writer, request)
|
|
|
|
})
|
2021-12-10 04:48:28 +01:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:36:30 +01:00
|
|
|
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 {
|
2021-08-16 23:55:05 +02:00
|
|
|
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)
|
|
|
|
}
|
2021-08-16 23:55:05 +02:00
|
|
|
}
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) livenessHandler() http.HandlerFunc {
|
2021-08-16 23:55:05 +02:00
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
2019-05-08 23:45:04 +02:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-08-16 23:55:05 +02:00
|
|
|
}
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) isReady() bool {
|
|
|
|
return s.httpServer != nil
|
|
|
|
}
|