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

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