github.com/netdata/go.d.plugin@v0.58.1/modules/pgbouncer/pgbouncer.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package pgbouncer 4 5 import ( 6 "database/sql" 7 _ "embed" 8 "time" 9 10 "github.com/netdata/go.d.plugin/agent/module" 11 "github.com/netdata/go.d.plugin/pkg/web" 12 13 "github.com/blang/semver/v4" 14 _ "github.com/jackc/pgx/v4/stdlib" 15 ) 16 17 //go:embed "config_schema.json" 18 var configSchema string 19 20 func init() { 21 module.Register("pgbouncer", module.Creator{ 22 JobConfigSchema: configSchema, 23 Create: func() module.Module { return New() }, 24 }) 25 } 26 27 func New() *PgBouncer { 28 return &PgBouncer{ 29 Config: Config{ 30 Timeout: web.Duration{Duration: time.Second}, 31 DSN: "postgres://postgres:postgres@127.0.0.1:6432/pgbouncer", 32 }, 33 charts: globalCharts.Copy(), 34 recheckSettingsEvery: time.Minute * 5, 35 metrics: &metrics{ 36 dbs: make(map[string]*dbMetrics), 37 }, 38 } 39 } 40 41 type Config struct { 42 DSN string `yaml:"dsn"` 43 Timeout web.Duration `yaml:"timeout"` 44 } 45 46 type PgBouncer struct { 47 module.Base 48 Config `yaml:",inline"` 49 50 charts *module.Charts 51 52 db *sql.DB 53 version *semver.Version 54 55 recheckSettingsTime time.Time 56 recheckSettingsEvery time.Duration 57 maxClientConn int64 58 59 metrics *metrics 60 } 61 62 func (p *PgBouncer) Init() bool { 63 err := p.validateConfig() 64 if err != nil { 65 p.Errorf("config validation: %v", err) 66 return false 67 } 68 69 return true 70 } 71 72 func (p *PgBouncer) Check() bool { 73 return len(p.Collect()) > 0 74 } 75 76 func (p *PgBouncer) Charts() *module.Charts { 77 return p.charts 78 } 79 80 func (p *PgBouncer) Collect() map[string]int64 { 81 mx, err := p.collect() 82 if err != nil { 83 p.Error(err) 84 } 85 86 if len(mx) == 0 { 87 return nil 88 } 89 return mx 90 } 91 92 func (p *PgBouncer) Cleanup() { 93 if p.db == nil { 94 return 95 } 96 if err := p.db.Close(); err != nil { 97 p.Warningf("cleanup: error on closing the PgBouncer database [%s]: %v", p.DSN, err) 98 } 99 p.db = nil 100 }