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

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