From fdbd1a678fefe50f65e2e2ffb83eea20d819994e Mon Sep 17 00:00:00 2001 From: Galorhallen Date: Fri, 10 Dec 2021 04:48:28 +0100 Subject: [PATCH 1/4] Add initial support to multiple pihole servers --- config/configuration.go | 77 ++++++++++++++++++++++++++++----------- internal/pihole/client.go | 29 ++++++++++++++- internal/server/server.go | 22 +++++++++-- main.go | 11 +++++- 4 files changed, 112 insertions(+), 27 deletions(-) diff --git a/config/configuration.go b/config/configuration.go index f16f408..f26b272 100644 --- a/config/configuration.go +++ b/config/configuration.go @@ -15,29 +15,37 @@ import ( // Config is the exporter CLI configuration. type Config struct { - PIHoleProtocol string `config:"pihole_protocol"` - PIHoleHostname string `config:"pihole_hostname"` - PIHolePort uint16 `config:"pihole_port"` - PIHolePassword string `config:"pihole_password"` - PIHoleApiToken string `config:"pihole_api_token"` - Port string `config:"port"` + PIHoleProtocol string `config:"pihole_protocol"` + PIHoleHostname string `config:"pihole_hostname"` + PIHolePort uint16 `config:"pihole_port"` + PIHolePassword string `config:"pihole_password"` + PIHoleApiToken string `config:"pihole_api_token"` +} + +type EnvConfig struct { + PIHoleProtocol []string `config:"pihole_protocol"` + PIHoleHostname []string `config:"pihole_hostname"` + PIHolePort []uint16 `config:"pihole_port"` + PIHolePassword []string `config:"pihole_password"` + PIHoleApiToken []string `config:"pihole_api_token"` + Port uint16 `config:"port"` Interval time.Duration `config:"interval"` } -func getDefaultConfig() *Config { - return &Config{ - PIHoleProtocol: "http", - PIHoleHostname: "127.0.0.1", - PIHolePort: 80, - PIHolePassword: "", - PIHoleApiToken: "", - Port: "9617", +func getDefaultEnvConfig() *EnvConfig { + return &EnvConfig{ + PIHoleProtocol: []string{"http"}, + PIHoleHostname: []string{"127.0.0.1"}, + PIHolePort: []uint16{80}, + PIHolePassword: []string{}, + PIHoleApiToken: []string{}, + Port: 9617, Interval: 10 * time.Second, } } // Load method loads the configuration by using both flag or environment variables. -func Load() *Config { +func Load() (*EnvConfig, []Config) { loaders := []backend.Backend{ env.NewBackend(), flags.NewBackend(), @@ -45,7 +53,7 @@ func Load() *Config { loader := confita.NewLoader(loaders...) - cfg := getDefaultConfig() + cfg := getDefaultEnvConfig() err := loader.Load(context.Background(), cfg) if err != nil { panic(err) @@ -53,7 +61,7 @@ func Load() *Config { cfg.show() - return cfg + return cfg, cfg.Split() } //Validate check if the config is valid @@ -64,6 +72,33 @@ func (c Config) Validate() error { return nil } +func (c EnvConfig) Split() []Config { + result := make([]Config, 0, len(c.PIHoleHostname)) + + for i, hostname := range c.PIHoleHostname { + config := Config{ + PIHoleHostname: hostname, + PIHoleProtocol: c.PIHoleProtocol[i], + PIHolePort: c.PIHolePort[i], + } + + if c.PIHoleApiToken != nil && len(c.PIHoleApiToken) > 0 { + if c.PIHoleApiToken[i] != "" { + config.PIHoleApiToken = c.PIHoleApiToken[i] + } + } + + if c.PIHolePassword != nil && len(c.PIHolePassword) > 0 { + if c.PIHolePassword[i] != "" { + config.PIHolePassword = c.PIHolePassword[i] + } + } + + result = append(result, config) + } + return result +} + func (c Config) hostnameURL() string { s := fmt.Sprintf("%s://%s", c.PIHoleProtocol, c.PIHoleHostname) if c.PIHolePort != 0 { @@ -82,7 +117,7 @@ func (c Config) PIHoleLoginURL() string { return c.hostnameURL() + "/admin/index.php?login" } -func (c Config) show() { +func (c EnvConfig) show() { val := reflect.ValueOf(&c).Elem() log.Println("------------------------------------") log.Println("- PI-Hole exporter configuration -") @@ -95,14 +130,14 @@ func (c Config) show() { if typeField.Name != "PIHolePassword" && typeField.Name != "PIHoleApiToken" { log.Println(fmt.Sprintf("%s : %v", typeField.Name, valueField.Interface())) } else { - showAuthenticationMethod(typeField.Name, valueField.String()) + showAuthenticationMethod(typeField.Name, valueField.Len()) } } log.Println("------------------------------------") } -func showAuthenticationMethod(name, value string) { - if len(value) > 0 { +func showAuthenticationMethod(name string, length int) { + if length > 0 { log.Println(fmt.Sprintf("Pi-Hole Authentication Method : %s", name)) } } diff --git a/internal/pihole/client.go b/internal/pihole/client.go index 9f88491..38c7a7d 100644 --- a/internal/pihole/client.go +++ b/internal/pihole/client.go @@ -32,7 +32,9 @@ func NewClient(config *config.Config) *Client { os.Exit(1) } - return &Client{ + fmt.Printf("Creating client with config %s\n", config) + + client := &Client{ config: config, httpClient: http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { @@ -40,6 +42,14 @@ func NewClient(config *config.Config) *Client { }, }, } + + fmt.Printf("Client created with config %s\n", client) + + return client +} + +func (c *Client) String() string { + return c.config.PIHoleHostname } // Metrics scrapes pihole and sets them @@ -58,6 +68,23 @@ func (c *Client) Metrics() http.HandlerFunc { } } +func (c *Client) CollectMetrics(writer http.ResponseWriter, request *http.Request) { + stats, err := c.getStatistics() + if err != nil { + writer.WriteHeader(http.StatusBadRequest) + _, _ = writer.Write([]byte(err.Error())) + return + } + c.setMetrics(stats) + + log.Printf("New tick of statistics: %s", stats.ToString()) + promhttp.Handler().ServeHTTP(writer, request) +} + +func (c *Client) GetHostname() string { + return c.config.PIHoleHostname +} + func (c *Client) setMetrics(stats *Stats) { metrics.DomainsBlocked.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.DomainsBeingBlocked)) metrics.DNSQueriesToday.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.DNSQueriesToday)) diff --git a/internal/server/server.go b/internal/server/server.go index 481d2f7..b33bd9e 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -1,8 +1,10 @@ package server import ( + "fmt" "log" "net/http" + "strconv" "time" "github.com/eko/pihole-exporter/internal/pihole" @@ -16,15 +18,29 @@ type Server struct { // 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 string, client *pihole.Client) *Server { +func NewServer(port uint16, clients []*pihole.Client) *Server { mux := http.NewServeMux() - httpServer := &http.Server{Addr: ":" + port, Handler: mux} + httpServer := &http.Server{Addr: ":" + strconv.Itoa(int(port)), Handler: mux} s := &Server{ httpServer: httpServer, } - mux.Handle("/metrics", client.Metrics()) + /*fmt.Printf("Server received clients -> %s\n", clients) + for i, client := range clients { + fmt.Printf("Server received clients -> idx: %d, Hostname: %s\n", i, &client) + }*/ + + mux.HandleFunc("/metrics", + func(writer http.ResponseWriter, request *http.Request) { + for i, client := range clients { + fmt.Printf("Idx: %d, Hostname: %s\n", i, client) + client.CollectMetrics(writer, request) + } + }, + ) + + //mux.Handle("/metrics", client.Metrics()) mux.Handle("/readiness", s.readinessHandler()) mux.Handle("/liveness", s.livenessHandler()) diff --git a/main.go b/main.go index 22aa73b..8ea80d0 100644 --- a/main.go +++ b/main.go @@ -11,12 +11,19 @@ import ( ) func main() { - conf := config.Load() + envConf, clientConfigs := config.Load() metrics.Init() serverDead := make(chan struct{}) - s := server.NewServer(conf.Port, pihole.NewClient(conf)) + clients := make([]*pihole.Client, 0, len(clientConfigs)) + for i, _ := range clientConfigs { + client := pihole.NewClient(&clientConfigs[i]) + clients = append(clients, client) + fmt.Printf("Append client %s\n", clients) + } + + s := server.NewServer(envConf.Port, clients) go func() { s.ListenAndServe() close(serverDead) From fc19ac5d295d0d260863fe0778650c0129874e1a Mon Sep 17 00:00:00 2001 From: Galorhallen Date: Fri, 10 Dec 2021 15:39:22 +0100 Subject: [PATCH 2/4] Better /metrics handling --- config/configuration.go | 15 +++++++++++++++ internal/pihole/client.go | 21 ++++++++------------- internal/pihole/model.go | 2 +- internal/server/server.go | 20 +++++++++++++++++--- main.go | 19 +++++++++++++------ 5 files changed, 54 insertions(+), 23 deletions(-) diff --git a/config/configuration.go b/config/configuration.go index f26b272..0e41d03 100644 --- a/config/configuration.go +++ b/config/configuration.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "reflect" + "strings" "time" "github.com/heetch/confita" @@ -64,6 +65,20 @@ func Load() (*EnvConfig, []Config) { return cfg, cfg.Split() } +func (c *Config) String() string { + ref := reflect.ValueOf(c) + fields := ref.Elem() + + buffer := make([]string, fields.NumField(), fields.NumField()) + for i := 0; i < fields.NumField(); i++ { + valueField := fields.Field(i) + typeField := fields.Type().Field(i) + buffer[i] = fmt.Sprintf("%s=%v", typeField.Name, valueField.Interface()) + } + + return fmt.Sprintf("", &c, strings.Join(buffer, ", ")) +} + //Validate check if the config is valid func (c Config) Validate() error { if c.PIHoleProtocol != "http" && c.PIHoleProtocol != "https" { diff --git a/internal/pihole/client.go b/internal/pihole/client.go index 38c7a7d..c46bd53 100644 --- a/internal/pihole/client.go +++ b/internal/pihole/client.go @@ -14,7 +14,6 @@ import ( "github.com/eko/pihole-exporter/config" "github.com/eko/pihole-exporter/internal/metrics" - "github.com/prometheus/client_golang/prometheus/promhttp" ) // Client struct is a PI-Hole client to request an instance of a PI-Hole ad blocker. @@ -34,7 +33,7 @@ func NewClient(config *config.Config) *Client { fmt.Printf("Creating client with config %s\n", config) - client := &Client{ + return &Client{ config: config, httpClient: http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { @@ -42,16 +41,13 @@ func NewClient(config *config.Config) *Client { }, }, } - - fmt.Printf("Client created with config %s\n", client) - - return client } func (c *Client) String() string { return c.config.PIHoleHostname } +/* // Metrics scrapes pihole and sets them func (c *Client) Metrics() http.HandlerFunc { return func(writer http.ResponseWriter, request *http.Request) { @@ -66,19 +62,18 @@ func (c *Client) Metrics() http.HandlerFunc { log.Printf("New tick of statistics: %s", stats.ToString()) promhttp.Handler().ServeHTTP(writer, request) } -} +}*/ + +func (c *Client) CollectMetrics(writer http.ResponseWriter, request *http.Request) error { -func (c *Client) CollectMetrics(writer http.ResponseWriter, request *http.Request) { stats, err := c.getStatistics() if err != nil { - writer.WriteHeader(http.StatusBadRequest) - _, _ = writer.Write([]byte(err.Error())) - return + return err } c.setMetrics(stats) - log.Printf("New tick of statistics: %s", stats.ToString()) - promhttp.Handler().ServeHTTP(writer, request) + log.Printf("New tick of statistics from %s: %s", c, stats) + return nil } func (c *Client) GetHostname() string { diff --git a/internal/pihole/model.go b/internal/pihole/model.go index d800d0b..8067085 100644 --- a/internal/pihole/model.go +++ b/internal/pihole/model.go @@ -31,6 +31,6 @@ type Stats struct { } // ToString method returns a string of the current statistics struct. -func (s *Stats) ToString() string { +func (s *Stats) String() string { return fmt.Sprintf("%d ads blocked / %d total DNS queries", s.AdsBlockedToday, s.DNSQueriesAllTypes) } diff --git a/internal/server/server.go b/internal/server/server.go index b33bd9e..41d651a 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -5,9 +5,11 @@ import ( "log" "net/http" "strconv" + "strings" "time" "github.com/eko/pihole-exporter/internal/pihole" + "github.com/prometheus/client_golang/prometheus/promhttp" "golang.org/x/net/context" ) @@ -33,10 +35,22 @@ func NewServer(port uint16, clients []*pihole.Client) *Server { mux.HandleFunc("/metrics", func(writer http.ResponseWriter, request *http.Request) { - for i, client := range clients { - fmt.Printf("Idx: %d, Hostname: %s\n", i, client) - client.CollectMetrics(writer, 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) }, ) diff --git a/main.go b/main.go index 8ea80d0..ea8e7a9 100644 --- a/main.go +++ b/main.go @@ -16,12 +16,8 @@ func main() { metrics.Init() serverDead := make(chan struct{}) - clients := make([]*pihole.Client, 0, len(clientConfigs)) - for i, _ := range clientConfigs { - client := pihole.NewClient(&clientConfigs[i]) - clients = append(clients, client) - fmt.Printf("Append client %s\n", clients) - } + + clients := buildClients(clientConfigs) s := server.NewServer(envConf.Port, clients) go func() { @@ -43,3 +39,14 @@ func main() { fmt.Println("pihole-exporter HTTP server stopped") } + +func buildClients(clientConfigs []config.Config) []*pihole.Client { + clients := make([]*pihole.Client, 0, len(clientConfigs)) + for i := range clientConfigs { + clientConfig := &clientConfigs[i] + + client := pihole.NewClient(clientConfig) + clients = append(clients, client) + } + return clients +} From 8170a4c0f7c0eccc6f03023d8f287d008546aeb1 Mon Sep 17 00:00:00 2001 From: Galorhallen Date: Sat, 18 Dec 2021 00:25:23 +0100 Subject: [PATCH 3/4] Add readme --- README.md | 27 +++++++++++++++++++++++++++ config/configuration.go | 24 ++++++++++++++++++------ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3c35378..c5cc2a3 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,33 @@ $ docker run \ ekofr/pihole-exporter:latest ``` +A single instance of pihole-exporter can monitor multiple pi-holes instances. +To do so, you can specify a list of hostnames, protocols, passwords/API tokens and ports by separating them with commas in their respective environment variable: + +``` +$ docker run \ + -e 'PIHOLE_PROTOCOL="http,http,http" \ + -e 'PIHOLE_HOSTNAME="192.168.1.2,192.168.1.3,192.168.1.4"' \ + -e "PIHOLE_API_TOKEN="$API_TOKEN1,$API_TOKEN2,$API_TOKEN3" \ + -e "PIHOLE_PORT="8080,8081,8080" \ + -e 'INTERVAL=30s' \ + -e 'PORT=9617' \ + ekofr/pihole-exporter:latest +``` + +If port, protocol and API token/password is the same for all instances, you can specify them only once: + +``` +$ docker run \ + -e 'PIHOLE_PROTOCOL=",http" \ + -e 'PIHOLE_HOSTNAME="192.168.1.2,192.168.1.3,192.168.1.4"' \ + -e "PIHOLE_API_TOKEN="$API_TOKEN" \ + -e "PIHOLE_PORT="8080" \ + -e 'INTERVAL=30s' \ + -e 'PORT=9617' \ + ekofr/pihole-exporter:latest +``` + ### From sources Optionally, you can download and build it from the sources. You have to retrieve the project sources by using one of the following way: diff --git a/config/configuration.go b/config/configuration.go index 0e41d03..45d2383 100644 --- a/config/configuration.go +++ b/config/configuration.go @@ -97,15 +97,27 @@ func (c EnvConfig) Split() []Config { PIHolePort: c.PIHolePort[i], } - if c.PIHoleApiToken != nil && len(c.PIHoleApiToken) > 0 { - if c.PIHoleApiToken[i] != "" { - config.PIHoleApiToken = c.PIHoleApiToken[i] + if c.PIHoleApiToken != nil { + if len(c.PIHoleApiToken) == 1 { + if c.PIHoleApiToken[0] != "" { + config.PIHoleApiToken = c.PIHoleApiToken[0] + } + } else if len(c.PIHoleApiToken) > 1 { + if c.PIHoleApiToken[i] != "" { + config.PIHoleApiToken = c.PIHoleApiToken[i] + } } } - if c.PIHolePassword != nil && len(c.PIHolePassword) > 0 { - if c.PIHolePassword[i] != "" { - config.PIHolePassword = c.PIHolePassword[i] + if c.PIHolePassword != nil { + if len(c.PIHolePassword) == 1 { + if c.PIHolePassword[0] != "" { + config.PIHolePassword = c.PIHolePassword[0] + } + } else if len(c.PIHolePassword) > 1 { + if c.PIHolePassword[i] != "" { + config.PIHolePassword = c.PIHolePassword[i] + } } } From 3cf4ad6781487ecabe9d2e5577f37582c2dd6c79 Mon Sep 17 00:00:00 2001 From: Galorhallen Date: Tue, 21 Dec 2021 14:59:21 +0100 Subject: [PATCH 4/4] Removed commented code --- internal/server/server.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/internal/server/server.go b/internal/server/server.go index 41d651a..55968b5 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -28,11 +28,6 @@ func NewServer(port uint16, clients []*pihole.Client) *Server { httpServer: httpServer, } - /*fmt.Printf("Server received clients -> %s\n", clients) - for i, client := range clients { - fmt.Printf("Server received clients -> idx: %d, Hostname: %s\n", i, &client) - }*/ - mux.HandleFunc("/metrics", func(writer http.ResponseWriter, request *http.Request) { errors := make([]string, 0) @@ -54,7 +49,6 @@ func NewServer(port uint16, clients []*pihole.Client) *Server { }, ) - //mux.Handle("/metrics", client.Metrics()) mux.Handle("/readiness", s.readinessHandler()) mux.Handle("/liveness", s.livenessHandler())