mirror of
https://github.com/eko/pihole-exporter.git
synced 2025-01-20 20:31:20 +01:00
Merge pull request #252 from pierCo/master
Make the context of the admin application configurable
This commit is contained in:
commit
009f5bcb04
@ -200,6 +200,9 @@ scrape_configs:
|
||||
# Address to be used for the exporter
|
||||
-bind_addr string (optional) (default "0.0.0.0")
|
||||
|
||||
# URL Context (first segments of URL path) to the PI-hole admin application
|
||||
-pihole_admin_context string (optional) (default "admin")
|
||||
|
||||
# Port to be used for the exporter
|
||||
-port string (optional) (default "9617")
|
||||
```
|
||||
|
@ -19,36 +19,39 @@ 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"`
|
||||
BindAddr string `config:"bind_addr"`
|
||||
Port uint16 `config:"port"`
|
||||
PIHoleProtocol string `config:"pihole_protocol"`
|
||||
PIHoleHostname string `config:"pihole_hostname"`
|
||||
PIHolePort uint16 `config:"pihole_port"`
|
||||
PIHoleAdminContext string `config:"pihole_admin_context"`
|
||||
PIHolePassword string `config:"pihole_password"`
|
||||
PIHoleApiToken string `config:"pihole_api_token"`
|
||||
BindAddr string `config:"bind_addr"`
|
||||
Port uint16 `config:"port"`
|
||||
}
|
||||
|
||||
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"`
|
||||
BindAddr string `config:"bind_addr"`
|
||||
Port uint16 `config:"port"`
|
||||
Timeout time.Duration `config:"timeout"`
|
||||
PIHoleProtocol []string `config:"pihole_protocol"`
|
||||
PIHoleHostname []string `config:"pihole_hostname"`
|
||||
PIHolePort []uint16 `config:"pihole_port"`
|
||||
PIHoleAdminContext []string `config:"pihole_admin_context"`
|
||||
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"`
|
||||
}
|
||||
|
||||
func getDefaultEnvConfig() *EnvConfig {
|
||||
return &EnvConfig{
|
||||
PIHoleProtocol: []string{"http"},
|
||||
PIHoleHostname: []string{"127.0.0.1"},
|
||||
PIHolePort: []uint16{80},
|
||||
PIHolePassword: []string{},
|
||||
PIHoleApiToken: []string{},
|
||||
BindAddr: "0.0.0.0",
|
||||
Port: 9617,
|
||||
Timeout: 5 * time.Second,
|
||||
PIHoleProtocol: []string{"http"},
|
||||
PIHoleHostname: []string{"127.0.0.1"},
|
||||
PIHolePort: []uint16{80},
|
||||
PIHoleAdminContext: []string{"admin"},
|
||||
PIHolePassword: []string{},
|
||||
PIHoleApiToken: []string{},
|
||||
BindAddr: "0.0.0.0",
|
||||
Port: 9617,
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
@ -120,6 +123,12 @@ func (c EnvConfig) Split() ([]Config, error) {
|
||||
return nil, errors.New("Wrong number of ports. Port can be empty to use default, one value to use for all hosts, or match the number of hosts")
|
||||
}
|
||||
|
||||
if hasData, data, isValid := extractStringConfig(c.PIHoleAdminContext, i, hostsCount); hasData {
|
||||
config.PIHoleAdminContext = data
|
||||
} else if !isValid {
|
||||
return nil, errors.New("Wrong number of PIHoleAdminContext. PIHoleAdminContext can be empty to use default, one value to use for all hosts, or match the number of hosts")
|
||||
}
|
||||
|
||||
if hasData, data, isValid := extractStringConfig(c.PIHoleProtocol, i, hostsCount); hasData {
|
||||
config.PIHoleProtocol = data
|
||||
} else if !isValid {
|
||||
@ -173,22 +182,25 @@ func removeEmptyString(source []string) []string {
|
||||
return result
|
||||
}
|
||||
|
||||
func (c Config) hostnameURL() string {
|
||||
func (c Config) piHoleAdminURL() string {
|
||||
s := fmt.Sprintf("%s://%s", c.PIHoleProtocol, c.PIHoleHostname)
|
||||
if c.PIHolePort != 0 {
|
||||
s += fmt.Sprintf(":%d", c.PIHolePort)
|
||||
}
|
||||
if c.PIHoleAdminContext != "" {
|
||||
s += fmt.Sprintf("/%s", c.PIHoleAdminContext)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// PIHoleStatsURL returns the stats url
|
||||
func (c Config) PIHoleStatsURL() string {
|
||||
return c.hostnameURL() + "/admin/api.php?summaryRaw&overTimeData&topItems&recentItems&getQueryTypes&getForwardDestinations&getQuerySources&overTimeData10mins&jsonForceObject"
|
||||
return c.piHoleAdminURL() + "/api.php?summaryRaw&overTimeData&topItems&recentItems&getQueryTypes&getForwardDestinations&getQuerySources&overTimeData10mins&jsonForceObject"
|
||||
}
|
||||
|
||||
// PIHoleLoginURL returns the login url
|
||||
func (c Config) PIHoleLoginURL() string {
|
||||
return c.hostnameURL() + "/admin/index.php?login"
|
||||
return c.piHoleAdminURL() + "/index.php?login"
|
||||
}
|
||||
|
||||
func (c EnvConfig) show() {
|
||||
|
@ -8,10 +8,11 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
PIHOLE_HOSTNAME = "PIHOLE_HOSTNAME"
|
||||
PIHOLE_PORT = "PIHOLE_PORT"
|
||||
PIHOLE_API_TOKEN = "PIHOLE_API_TOKEN"
|
||||
PIHOLE_PROTOCOL = "PIHOLE_PROTOCOL"
|
||||
PIHOLE_HOSTNAME = "PIHOLE_HOSTNAME"
|
||||
PIHOLE_PORT = "PIHOLE_PORT"
|
||||
PIHOLE_ADMIN_CONTEXT = "PIHOLE_ADMIN_CONTEXT"
|
||||
PIHOLE_API_TOKEN = "PIHOLE_API_TOKEN"
|
||||
PIHOLE_PROTOCOL = "PIHOLE_PROTOCOL"
|
||||
)
|
||||
|
||||
type EnvInitiazlier func(*testing.T)
|
||||
@ -32,6 +33,7 @@ func TestSplitDefault(t *testing.T) {
|
||||
clientConfig := clientConfigs[0]
|
||||
assert.Equal("127.0.0.1", clientConfig.PIHoleHostname)
|
||||
assert.Equal("http", clientConfig.PIHoleProtocol)
|
||||
assert.Equal("admin", clientConfig.PIHoleAdminContext)
|
||||
assert.Equal(uint16(80), clientConfig.PIHolePort)
|
||||
assert.Empty(clientConfig.PIHoleApiToken)
|
||||
assert.Empty(clientConfig.PIHolePassword)
|
||||
@ -43,6 +45,7 @@ func TestSplitMultipleHostWithSameConfig(t *testing.T) {
|
||||
env := getDefaultEnvConfig()
|
||||
env.PIHoleHostname = []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"}
|
||||
env.PIHoleApiToken = []string{"api-token"}
|
||||
env.PIHoleAdminContext = []string{"foo"}
|
||||
env.PIHolePort = []uint16{8080}
|
||||
|
||||
clientConfigs, err := env.Split()
|
||||
@ -50,24 +53,28 @@ func TestSplitMultipleHostWithSameConfig(t *testing.T) {
|
||||
assert.Len(clientConfigs, 3)
|
||||
|
||||
testCases := []struct {
|
||||
Host string
|
||||
Port uint16
|
||||
Protocol string
|
||||
Host string
|
||||
Port uint16
|
||||
AdminContext string
|
||||
Protocol string
|
||||
}{
|
||||
{
|
||||
Host: "127.0.0.1",
|
||||
Port: 8080,
|
||||
Protocol: "http",
|
||||
Host: "127.0.0.1",
|
||||
Port: 8080,
|
||||
AdminContext: "foo",
|
||||
Protocol: "http",
|
||||
},
|
||||
{
|
||||
Host: "127.0.0.2",
|
||||
Port: 8080,
|
||||
Protocol: "http",
|
||||
Host: "127.0.0.2",
|
||||
Port: 8080,
|
||||
AdminContext: "foo",
|
||||
Protocol: "http",
|
||||
},
|
||||
{
|
||||
Host: "127.0.0.3",
|
||||
Port: 8080,
|
||||
Protocol: "http",
|
||||
Host: "127.0.0.3",
|
||||
Port: 8080,
|
||||
AdminContext: "foo",
|
||||
Protocol: "http",
|
||||
},
|
||||
}
|
||||
|
||||
@ -90,6 +97,7 @@ func TestSplitMultipleHostWithMultipleConfigs(t *testing.T) {
|
||||
env := getDefaultEnvConfig()
|
||||
env.PIHoleHostname = []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"}
|
||||
env.PIHoleApiToken = []string{"api-token1", "", "api-token3"}
|
||||
env.PIHoleAdminContext = []string{"", "foo", "bar"}
|
||||
env.PIHolePassword = []string{"", "password2", ""}
|
||||
env.PIHolePort = []uint16{8081, 8082, 8083}
|
||||
|
||||
@ -98,32 +106,36 @@ func TestSplitMultipleHostWithMultipleConfigs(t *testing.T) {
|
||||
assert.Len(clientConfigs, 3)
|
||||
|
||||
testCases := []struct {
|
||||
Host string
|
||||
Port uint16
|
||||
Protocol string
|
||||
ApiToken string
|
||||
Password string
|
||||
Host string
|
||||
Port uint16
|
||||
AdminContext string
|
||||
Protocol string
|
||||
ApiToken string
|
||||
Password string
|
||||
}{
|
||||
{
|
||||
Host: "127.0.0.1",
|
||||
Port: 8081,
|
||||
Protocol: "http",
|
||||
ApiToken: "api-token1",
|
||||
Password: "",
|
||||
Host: "127.0.0.1",
|
||||
Port: 8081,
|
||||
AdminContext: "",
|
||||
Protocol: "http",
|
||||
ApiToken: "api-token1",
|
||||
Password: "",
|
||||
},
|
||||
{
|
||||
Host: "127.0.0.2",
|
||||
Port: 8082,
|
||||
Protocol: "http",
|
||||
ApiToken: "",
|
||||
Password: "password2",
|
||||
Host: "127.0.0.2",
|
||||
Port: 8082,
|
||||
AdminContext: "foo",
|
||||
Protocol: "http",
|
||||
ApiToken: "",
|
||||
Password: "password2",
|
||||
},
|
||||
{
|
||||
Host: "127.0.0.3",
|
||||
Port: 8083,
|
||||
Protocol: "http",
|
||||
ApiToken: "api-token3",
|
||||
Password: "",
|
||||
Host: "127.0.0.3",
|
||||
Port: 8083,
|
||||
AdminContext: "bar",
|
||||
Protocol: "http",
|
||||
ApiToken: "api-token3",
|
||||
Password: "",
|
||||
},
|
||||
}
|
||||
|
||||
@ -133,6 +145,7 @@ func TestSplitMultipleHostWithMultipleConfigs(t *testing.T) {
|
||||
|
||||
assert.Equal(tc.Host, clientConfig.PIHoleHostname)
|
||||
assert.Equal(tc.Protocol, clientConfig.PIHoleProtocol)
|
||||
assert.Equal(tc.AdminContext, clientConfig.PIHoleAdminContext)
|
||||
assert.Equal(tc.Port, clientConfig.PIHolePort)
|
||||
assert.Equal(tc.ApiToken, clientConfig.PIHoleApiToken)
|
||||
assert.Equal(tc.Password, clientConfig.PIHolePassword)
|
||||
@ -140,7 +153,7 @@ func TestSplitMultipleHostWithMultipleConfigs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrongParams(t *testing.T) {
|
||||
func TestWrongNumberOfApiTokenParams(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
env := getDefaultEnvConfig()
|
||||
@ -152,3 +165,17 @@ func TestWrongParams(t *testing.T) {
|
||||
assert.Errorf(err, "Wrong number of PIHoleApiToken. PIHoleApiToken can be empty to use default, one value to use for all hosts, or match the number of hosts")
|
||||
assert.Nil(clientConfigs)
|
||||
}
|
||||
|
||||
func TestWrongNumberOfAdminContextParams(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
env := getDefaultEnvConfig()
|
||||
env.PIHoleHostname = []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"}
|
||||
env.PIHoleApiToken = []string{"api-token"}
|
||||
env.PIHoleAdminContext = []string{"admin1", "admin2"}
|
||||
env.PIHolePort = []uint16{808}
|
||||
|
||||
clientConfigs, err := env.Split()
|
||||
assert.Errorf(err, "Wrong number of PIHoleAdminContext. PIHoleAdminContext can be empty to use default, one value to use for all hosts, or match the number of hosts")
|
||||
assert.Nil(clientConfigs)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user