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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package rabbitmq
     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("rabbitmq", module.Creator{
    19  		JobConfigSchema: configSchema,
    20  		Create:          func() module.Module { return New() },
    21  	})
    22  }
    23  
    24  func New() *RabbitMQ {
    25  	return &RabbitMQ{
    26  		Config: Config{
    27  			HTTP: web.HTTP{
    28  				Request: web.Request{
    29  					URL:      "http://localhost:15672",
    30  					Username: "guest",
    31  					Password: "guest",
    32  				},
    33  				Client: web.Client{
    34  					Timeout: web.Duration{Duration: time.Second},
    35  				},
    36  			},
    37  			CollectQueues: false,
    38  		},
    39  		charts: baseCharts.Copy(),
    40  		vhosts: make(map[string]bool),
    41  		queues: make(map[string]queueCache),
    42  	}
    43  }
    44  
    45  type Config struct {
    46  	web.HTTP      `yaml:",inline"`
    47  	CollectQueues bool `yaml:"collect_queues_metrics"`
    48  }
    49  
    50  type (
    51  	RabbitMQ struct {
    52  		module.Base
    53  		Config `yaml:",inline"`
    54  
    55  		charts *module.Charts
    56  
    57  		httpClient *http.Client
    58  
    59  		nodeName string
    60  
    61  		vhosts map[string]bool
    62  		queues map[string]queueCache
    63  	}
    64  	queueCache struct {
    65  		name, vhost string
    66  	}
    67  )
    68  
    69  func (r *RabbitMQ) Init() bool {
    70  	if r.URL == "" {
    71  		r.Error("'url' can not be empty")
    72  		return false
    73  	}
    74  
    75  	client, err := web.NewHTTPClient(r.Client)
    76  	if err != nil {
    77  		r.Errorf("init HTTP client: %v", err)
    78  		return false
    79  	}
    80  	r.httpClient = client
    81  
    82  	r.Debugf("using URL %s", r.URL)
    83  	r.Debugf("using timeout: %s", r.Timeout.Duration)
    84  
    85  	return true
    86  }
    87  
    88  func (r *RabbitMQ) Check() bool {
    89  	return len(r.Collect()) > 0
    90  }
    91  
    92  func (r *RabbitMQ) Charts() *module.Charts {
    93  	return r.charts
    94  }
    95  
    96  func (r *RabbitMQ) Collect() map[string]int64 {
    97  	mx, err := r.collect()
    98  	if err != nil {
    99  		r.Error(err)
   100  	}
   101  
   102  	if len(mx) == 0 {
   103  		return nil
   104  	}
   105  
   106  	return mx
   107  }
   108  
   109  func (r *RabbitMQ) Cleanup() {
   110  	if r.httpClient != nil {
   111  		r.httpClient.CloseIdleConnections()
   112  	}
   113  }