github.com/netdata/go.d.plugin@v0.58.1/modules/pihole/pihole.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package pihole 4 5 import ( 6 _ "embed" 7 "net/http" 8 "sync" 9 "time" 10 11 "github.com/netdata/go.d.plugin/pkg/web" 12 13 "github.com/netdata/go.d.plugin/agent/module" 14 ) 15 16 //go:embed "config_schema.json" 17 var configSchema string 18 19 func init() { 20 module.Register("pihole", module.Creator{ 21 JobConfigSchema: configSchema, 22 Defaults: module.Defaults{ 23 UpdateEvery: 5, 24 }, 25 Create: func() module.Module { return New() }, 26 }) 27 } 28 29 func New() *Pihole { 30 return &Pihole{ 31 Config: Config{ 32 HTTP: web.HTTP{ 33 Request: web.Request{ 34 URL: "http://127.0.0.1", 35 }, 36 Client: web.Client{ 37 Timeout: web.Duration{Duration: time.Second * 5}}, 38 }, 39 SetupVarsPath: "/etc/pihole/setupVars.conf", 40 }, 41 checkVersion: true, 42 charts: baseCharts.Copy(), 43 addQueriesTypesOnce: &sync.Once{}, 44 addFwsDestinationsOnce: &sync.Once{}, 45 } 46 } 47 48 type Config struct { 49 web.HTTP `yaml:",inline"` 50 SetupVarsPath string `yaml:"setup_vars_path"` 51 } 52 53 type Pihole struct { 54 module.Base 55 Config `yaml:",inline"` 56 57 charts *module.Charts 58 addQueriesTypesOnce *sync.Once 59 addFwsDestinationsOnce *sync.Once 60 61 httpClient *http.Client 62 checkVersion bool 63 } 64 65 func (p *Pihole) Init() bool { 66 if err := p.validateConfig(); err != nil { 67 p.Errorf("config validation: %v", err) 68 return false 69 } 70 71 httpClient, err := p.initHTTPClient() 72 if err != nil { 73 p.Errorf("init http client: %v", err) 74 return false 75 } 76 p.httpClient = httpClient 77 78 p.Password = p.getWebPassword() 79 if p.Password == "" { 80 p.Warning("no web password, not all metrics available") 81 } else { 82 p.Debugf("web password: %s", p.Password) 83 } 84 85 return true 86 } 87 88 func (p *Pihole) Check() bool { 89 return len(p.Collect()) > 0 90 } 91 92 func (p *Pihole) Charts() *module.Charts { 93 return p.charts 94 } 95 96 func (p *Pihole) Collect() map[string]int64 { 97 mx, err := p.collect() 98 if err != nil { 99 p.Error(err) 100 } 101 102 if len(mx) == 0 { 103 return nil 104 } 105 106 return mx 107 } 108 109 func (p *Pihole) Cleanup() { 110 if p.httpClient != nil { 111 p.httpClient.CloseIdleConnections() 112 } 113 }