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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package haproxy
     4  
     5  import (
     6  	_ "embed"
     7  	"time"
     8  
     9  	"github.com/netdata/go.d.plugin/agent/module"
    10  	"github.com/netdata/go.d.plugin/pkg/prometheus"
    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("haproxy", module.Creator{
    19  		JobConfigSchema: configSchema,
    20  		Create:          func() module.Module { return New() },
    21  	})
    22  }
    23  
    24  func New() *Haproxy {
    25  	return &Haproxy{
    26  		Config: Config{
    27  			HTTP: web.HTTP{
    28  				Request: web.Request{
    29  					URL: "http://127.0.0.1:8404/metrics",
    30  				},
    31  				Client: web.Client{
    32  					Timeout: web.Duration{Duration: time.Second},
    33  				},
    34  			},
    35  		},
    36  
    37  		charts:          charts.Copy(),
    38  		proxies:         make(map[string]bool),
    39  		validateMetrics: true,
    40  	}
    41  }
    42  
    43  type Config struct {
    44  	web.HTTP `yaml:",inline"`
    45  }
    46  
    47  type Haproxy struct {
    48  	module.Base
    49  	Config `yaml:",inline"`
    50  
    51  	charts *module.Charts
    52  
    53  	prom            prometheus.Prometheus
    54  	validateMetrics bool
    55  	proxies         map[string]bool
    56  }
    57  
    58  func (h *Haproxy) Init() bool {
    59  	if err := h.validateConfig(); err != nil {
    60  		h.Errorf("config validation: %v", err)
    61  		return false
    62  	}
    63  
    64  	prom, err := h.initPrometheusClient()
    65  	if err != nil {
    66  		h.Errorf("prometheus client initialization: %v", err)
    67  		return false
    68  	}
    69  	h.prom = prom
    70  
    71  	return true
    72  }
    73  
    74  func (h *Haproxy) Check() bool {
    75  	return len(h.Collect()) > 0
    76  }
    77  
    78  func (h *Haproxy) Charts() *module.Charts {
    79  	return h.charts
    80  }
    81  
    82  func (h *Haproxy) Collect() map[string]int64 {
    83  	ms, err := h.collect()
    84  	if err != nil {
    85  		h.Error(err)
    86  		return nil
    87  	}
    88  
    89  	if len(ms) == 0 {
    90  		return nil
    91  	}
    92  	return ms
    93  }
    94  
    95  func (Haproxy) Cleanup() {
    96  	// TODO: close http idle connections
    97  }