github.com/netdata/go.d.plugin@v0.58.1/modules/upsd/upsd.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package upsd 4 5 import ( 6 "time" 7 8 "github.com/netdata/go.d.plugin/agent/module" 9 "github.com/netdata/go.d.plugin/pkg/web" 10 ) 11 12 func init() { 13 module.Register("upsd", module.Creator{ 14 Create: func() module.Module { return New() }, 15 }) 16 } 17 18 func New() *Upsd { 19 return &Upsd{ 20 Config: Config{ 21 Address: "127.0.0.1:3493", 22 Timeout: web.Duration{Duration: time.Second * 2}, 23 }, 24 newUpsdConn: newUpsdConn, 25 charts: &module.Charts{}, 26 upsUnits: make(map[string]bool), 27 } 28 } 29 30 type Config struct { 31 Address string `yaml:"address"` 32 Username string `yaml:"username"` 33 Password string `yaml:"password"` 34 Timeout web.Duration `yaml:"timeout"` 35 } 36 37 type ( 38 Upsd struct { 39 module.Base 40 41 Config `yaml:",inline"` 42 43 charts *module.Charts 44 45 newUpsdConn func(Config) upsdConn 46 conn upsdConn 47 48 upsUnits map[string]bool 49 } 50 51 upsdConn interface { 52 connect() error 53 disconnect() error 54 authenticate(string, string) error 55 upsUnits() ([]upsUnit, error) 56 } 57 ) 58 59 func (u *Upsd) Init() bool { 60 if u.Address == "" { 61 u.Error("config: 'address' not set") 62 return false 63 } 64 65 return true 66 } 67 68 func (u *Upsd) Check() bool { 69 return len(u.Collect()) > 0 70 } 71 72 func (u *Upsd) Charts() *module.Charts { 73 return u.charts 74 } 75 76 func (u *Upsd) Collect() map[string]int64 { 77 mx, err := u.collect() 78 if err != nil { 79 u.Error(err) 80 } 81 82 if len(mx) == 0 { 83 return nil 84 } 85 return mx 86 } 87 88 func (u *Upsd) Cleanup() { 89 if u.conn == nil { 90 return 91 } 92 if err := u.conn.disconnect(); err != nil { 93 u.Warningf("error on disconnect: %v", err) 94 } 95 u.conn = nil 96 }