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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package nginx
     4  
     5  import (
     6  	_ "embed"
     7  	"time"
     8  
     9  	"github.com/netdata/go.d.plugin/pkg/web"
    10  
    11  	"github.com/netdata/go.d.plugin/agent/module"
    12  )
    13  
    14  //go:embed "config_schema.json"
    15  var configSchema string
    16  
    17  func init() {
    18  	module.Register("nginx", module.Creator{
    19  		JobConfigSchema: configSchema,
    20  		Create:          func() module.Module { return New() },
    21  	})
    22  }
    23  
    24  const (
    25  	defaultURL         = "http://127.0.0.1/stub_status"
    26  	defaultHTTPTimeout = time.Second
    27  )
    28  
    29  // New creates Nginx with default values.
    30  func New() *Nginx {
    31  	config := Config{
    32  		HTTP: web.HTTP{
    33  			Request: web.Request{
    34  				URL: defaultURL,
    35  			},
    36  			Client: web.Client{
    37  				Timeout: web.Duration{Duration: defaultHTTPTimeout},
    38  			},
    39  		},
    40  	}
    41  
    42  	return &Nginx{Config: config}
    43  }
    44  
    45  // Config is the Nginx module configuration.
    46  type Config struct {
    47  	web.HTTP `yaml:",inline"`
    48  }
    49  
    50  // Nginx nginx module.
    51  type Nginx struct {
    52  	module.Base
    53  	Config `yaml:",inline"`
    54  
    55  	apiClient *apiClient
    56  }
    57  
    58  // Cleanup makes cleanup.
    59  func (Nginx) Cleanup() {}
    60  
    61  // Init makes initialization.
    62  func (n *Nginx) Init() bool {
    63  	if n.URL == "" {
    64  		n.Error("URL not set")
    65  		return false
    66  	}
    67  
    68  	client, err := web.NewHTTPClient(n.Client)
    69  	if err != nil {
    70  		n.Error(err)
    71  		return false
    72  	}
    73  
    74  	n.apiClient = newAPIClient(client, n.Request)
    75  
    76  	n.Debugf("using URL %s", n.URL)
    77  	n.Debugf("using timeout: %s", n.Timeout.Duration)
    78  
    79  	return true
    80  }
    81  
    82  // Check makes check.
    83  func (n *Nginx) Check() bool { return len(n.Collect()) > 0 }
    84  
    85  // Charts creates Charts.
    86  func (Nginx) Charts() *Charts { return charts.Copy() }
    87  
    88  // Collect collects metrics.
    89  func (n *Nginx) Collect() map[string]int64 {
    90  	mx, err := n.collect()
    91  
    92  	if err != nil {
    93  		n.Error(err)
    94  		return nil
    95  	}
    96  
    97  	return mx
    98  }