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