Teach pihole-exporter how to bind to a specific interface.

This commit is contained in:
Justin M 2023-05-01 09:52:36 -07:00
parent 7095927491
commit 62f51680c5
4 changed files with 10 additions and 3 deletions

View file

@ -195,6 +195,9 @@ scrape_configs:
# WEBPASSWORD / api token defined on the PI-Hole interface at `/etc/pihole/setupVars.conf` # WEBPASSWORD / api token defined on the PI-Hole interface at `/etc/pihole/setupVars.conf`
-pihole_api_token string (optional) -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 to be used for the exporter
-port string (optional) (default "9617") -port string (optional) (default "9617")
``` ```

View file

@ -24,6 +24,8 @@ type Config struct {
PIHolePort uint16 `config:"pihole_port"` PIHolePort uint16 `config:"pihole_port"`
PIHolePassword string `config:"pihole_password"` PIHolePassword string `config:"pihole_password"`
PIHoleApiToken string `config:"pihole_api_token"` PIHoleApiToken string `config:"pihole_api_token"`
BindAddr string `config:"bind_addr"`
Port uint16 `config:"port"`
} }
type EnvConfig struct { type EnvConfig struct {
@ -32,6 +34,7 @@ type EnvConfig struct {
PIHolePort []uint16 `config:"pihole_port"` PIHolePort []uint16 `config:"pihole_port"`
PIHolePassword []string `config:"pihole_password"` PIHolePassword []string `config:"pihole_password"`
PIHoleApiToken []string `config:"pihole_api_token"` PIHoleApiToken []string `config:"pihole_api_token"`
BindAddr string `config:"bind_addr"`
Port uint16 `config:"port"` Port uint16 `config:"port"`
Timeout time.Duration `config:"timeout"` Timeout time.Duration `config:"timeout"`
} }
@ -43,6 +46,7 @@ func getDefaultEnvConfig() *EnvConfig {
PIHolePort: []uint16{80}, PIHolePort: []uint16{80},
PIHolePassword: []string{}, PIHolePassword: []string{},
PIHoleApiToken: []string{}, PIHoleApiToken: []string{},
BindAddr: "0.0.0.0",
Port: 9617, Port: 9617,
Timeout: 5 * time.Second, Timeout: 5 * time.Second,
} }

View file

@ -20,10 +20,10 @@ type Server struct {
// NewServer method initializes a new HTTP server instance and associates // 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). // 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() mux := http.NewServeMux()
httpServer := &http.Server{ httpServer := &http.Server{
Addr: ":" + strconv.Itoa(int(port)), Addr: addr + ":" + strconv.Itoa(int(port)),
Handler: mux, Handler: mux,
} }

View file

@ -22,7 +22,7 @@ func main() {
clients := buildClients(clientConfigs, envConf) clients := buildClients(clientConfigs, envConf)
s := server.NewServer(envConf.Port, clients) s := server.NewServer(envConf.BindAddr, envConf.Port, clients)
go func() { go func() {
s.ListenAndServe() s.ListenAndServe()
close(serverDead) close(serverDead)