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

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