From 62f51680c5cf5e9cec09acf8f5dee68b0620bd9f Mon Sep 17 00:00:00 2001 From: Justin M Date: Mon, 1 May 2023 09:52:36 -0700 Subject: [PATCH] Teach pihole-exporter how to bind to a specific interface. --- README.md | 3 +++ config/configuration.go | 4 ++++ internal/server/server.go | 4 ++-- main.go | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 615044e..70f7d1f 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,9 @@ scrape_configs: # WEBPASSWORD / api token defined on the PI-Hole interface at `/etc/pihole/setupVars.conf` -pihole_api_token string (optional) +# Address to be used for the exporter + -bind_addr string (optional) (default "0.0.0.0") + # Port to be used for the exporter -port string (optional) (default "9617") ``` diff --git a/config/configuration.go b/config/configuration.go index 967ef03..53912ae 100644 --- a/config/configuration.go +++ b/config/configuration.go @@ -24,6 +24,8 @@ type Config struct { PIHolePort uint16 `config:"pihole_port"` PIHolePassword string `config:"pihole_password"` PIHoleApiToken string `config:"pihole_api_token"` + BindAddr string `config:"bind_addr"` + Port uint16 `config:"port"` } type EnvConfig struct { @@ -32,6 +34,7 @@ type EnvConfig struct { PIHolePort []uint16 `config:"pihole_port"` PIHolePassword []string `config:"pihole_password"` PIHoleApiToken []string `config:"pihole_api_token"` + BindAddr string `config:"bind_addr"` Port uint16 `config:"port"` Timeout time.Duration `config:"timeout"` } @@ -43,6 +46,7 @@ func getDefaultEnvConfig() *EnvConfig { PIHolePort: []uint16{80}, PIHolePassword: []string{}, PIHoleApiToken: []string{}, + BindAddr: "0.0.0.0", Port: 9617, Timeout: 5 * time.Second, } diff --git a/internal/server/server.go b/internal/server/server.go index 270b99f..eb5df0f 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -20,10 +20,10 @@ 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 uint16, clients []*pihole.Client) *Server { +func NewServer(addr string, port uint16, clients []*pihole.Client) *Server { mux := http.NewServeMux() httpServer := &http.Server{ - Addr: ":" + strconv.Itoa(int(port)), + Addr: addr + ":" + strconv.Itoa(int(port)), Handler: mux, } diff --git a/main.go b/main.go index ad37555..bc9fb78 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,7 @@ func main() { clients := buildClients(clientConfigs, envConf) - s := server.NewServer(envConf.Port, clients) + s := server.NewServer(envConf.BindAddr, envConf.Port, clients) go func() { s.ListenAndServe() close(serverDead)