github.com/netdata/go.d.plugin@v0.58.1/modules/couchbase/couchbase.go (about)

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