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

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