diff options
Diffstat (limited to 'src/go/plugin/go.d/modules/lighttpd')
-rw-r--r-- | src/go/plugin/go.d/modules/lighttpd/collect.go | 21 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/lighttpd/init.go | 29 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/lighttpd/integrations/lighttpd.md | 4 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/lighttpd/lighttpd.go | 49 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/lighttpd/lighttpd_test.go | 1 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/lighttpd/metrics.go | 33 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/lighttpd/status.go (renamed from src/go/plugin/go.d/modules/lighttpd/apiclient.go) | 78 |
7 files changed, 72 insertions, 143 deletions
diff --git a/src/go/plugin/go.d/modules/lighttpd/collect.go b/src/go/plugin/go.d/modules/lighttpd/collect.go index 84c88af4..30c3273f 100644 --- a/src/go/plugin/go.d/modules/lighttpd/collect.go +++ b/src/go/plugin/go.d/modules/lighttpd/collect.go @@ -4,22 +4,29 @@ package lighttpd import ( "fmt" + "io" "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/stm" + "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web" ) func (l *Lighttpd) collect() (map[string]int64, error) { - status, err := l.apiClient.getServerStatus() - + req, err := web.NewHTTPRequest(l.RequestConfig) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to create HTTP request: %v", err) } - mx := stm.ToMap(status) + var status *serverStatus + var perr error - if len(mx) == 0 { - return nil, fmt.Errorf("nothing was collected from %s", l.URL) + if err := web.DoHTTP(l.httpClient).Request(req, func(body io.Reader) error { + if status, perr = parseResponse(body); perr != nil { + return perr + } + return nil + }); err != nil { + return nil, err } - return mx, nil + return stm.ToMap(status), nil } diff --git a/src/go/plugin/go.d/modules/lighttpd/init.go b/src/go/plugin/go.d/modules/lighttpd/init.go deleted file mode 100644 index 0923262c..00000000 --- a/src/go/plugin/go.d/modules/lighttpd/init.go +++ /dev/null @@ -1,29 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later - -package lighttpd - -import ( - "errors" - "fmt" - "strings" - - "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web" -) - -func (l *Lighttpd) validateConfig() error { - if l.URL == "" { - return errors.New("url not set") - } - if !strings.HasSuffix(l.URL, "?auto") { - return fmt.Errorf("bad URL '%s', should ends in '?auto'", l.URL) - } - return nil -} - -func (l *Lighttpd) initApiClient() (*apiClient, error) { - client, err := web.NewHTTPClient(l.Client) - if err != nil { - return nil, err - } - return newAPIClient(client, l.Request), nil -} diff --git a/src/go/plugin/go.d/modules/lighttpd/integrations/lighttpd.md b/src/go/plugin/go.d/modules/lighttpd/integrations/lighttpd.md index bcf434fc..9a49615b 100644 --- a/src/go/plugin/go.d/modules/lighttpd/integrations/lighttpd.md +++ b/src/go/plugin/go.d/modules/lighttpd/integrations/lighttpd.md @@ -101,8 +101,8 @@ To enable status support, see the [official documentation](https://redmine.light The configuration file name for this integration is `go.d/lighttpd.conf`. -You can edit the configuration file using the `edit-config` script from the -Netdata [config directory](/docs/netdata-agent/configuration/README.md#the-netdata-config-directory). +You can edit the configuration file using the [`edit-config`](https://github.com/netdata/netdata/blob/master/docs/netdata-agent/configuration/README.md#edit-a-configuration-file-using-edit-config) script from the +Netdata [config directory](https://github.com/netdata/netdata/blob/master/docs/netdata-agent/configuration/README.md#the-netdata-config-directory). ```bash cd /etc/netdata 2>/dev/null || cd /opt/netdata/etc/netdata diff --git a/src/go/plugin/go.d/modules/lighttpd/lighttpd.go b/src/go/plugin/go.d/modules/lighttpd/lighttpd.go index 1b17833e..ce009191 100644 --- a/src/go/plugin/go.d/modules/lighttpd/lighttpd.go +++ b/src/go/plugin/go.d/modules/lighttpd/lighttpd.go @@ -5,9 +5,13 @@ package lighttpd import ( _ "embed" "errors" + "fmt" + "net/http" + "strings" "time" "github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module" + "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/confopt" "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web" ) @@ -24,27 +28,31 @@ func init() { func New() *Lighttpd { return &Lighttpd{Config: Config{ - HTTP: web.HTTP{ - Request: web.Request{ + HTTPConfig: web.HTTPConfig{ + RequestConfig: web.RequestConfig{ URL: "http://127.0.0.1/server-status?auto", }, - Client: web.Client{ - Timeout: web.Duration(time.Second * 2), + ClientConfig: web.ClientConfig{ + Timeout: confopt.Duration(time.Second * 2), }, }, - }} + }, + charts: charts.Copy(), + } } type Config struct { - UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` - web.HTTP `yaml:",inline" json:""` + UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` + web.HTTPConfig `yaml:",inline" json:""` } type Lighttpd struct { module.Base Config `yaml:",inline" json:""` - apiClient *apiClient + charts *module.Charts + + httpClient *http.Client } func (l *Lighttpd) Configuration() any { @@ -52,17 +60,18 @@ func (l *Lighttpd) Configuration() any { } func (l *Lighttpd) Init() error { - if err := l.validateConfig(); err != nil { - l.Errorf("config validation: %v", err) - return err + if l.URL == "" { + return errors.New("URL is required but not set") + } + if !strings.HasSuffix(l.URL, "?auto") { + return fmt.Errorf("bad URL '%s', should ends in '?auto'", l.URL) } - client, err := l.initApiClient() + httpClient, err := web.NewHTTPClient(l.ClientConfig) if err != nil { - l.Error(err) - return err + return fmt.Errorf("failed to create http client: %v", err) } - l.apiClient = client + l.httpClient = httpClient l.Debugf("using URL %s", l.URL) l.Debugf("using timeout: %s", l.Timeout.Duration()) @@ -73,22 +82,22 @@ func (l *Lighttpd) Init() error { func (l *Lighttpd) Check() error { mx, err := l.collect() if err != nil { - l.Error(err) return err } + if len(mx) == 0 { return errors.New("no metrics collected") } + return nil } func (l *Lighttpd) Charts() *Charts { - return charts.Copy() + return l.charts } func (l *Lighttpd) Collect() map[string]int64 { mx, err := l.collect() - if err != nil { l.Error(err) return nil @@ -98,7 +107,7 @@ func (l *Lighttpd) Collect() map[string]int64 { } func (l *Lighttpd) Cleanup() { - if l.apiClient != nil && l.apiClient.httpClient != nil { - l.apiClient.httpClient.CloseIdleConnections() + if l.httpClient != nil { + l.httpClient.CloseIdleConnections() } } diff --git a/src/go/plugin/go.d/modules/lighttpd/lighttpd_test.go b/src/go/plugin/go.d/modules/lighttpd/lighttpd_test.go index 05c7504e..9df5a90a 100644 --- a/src/go/plugin/go.d/modules/lighttpd/lighttpd_test.go +++ b/src/go/plugin/go.d/modules/lighttpd/lighttpd_test.go @@ -41,7 +41,6 @@ func TestLighttpd_Init(t *testing.T) { job := New() require.NoError(t, job.Init()) - assert.NotNil(t, job.apiClient) } func TestLighttpd_InitNG(t *testing.T) { diff --git a/src/go/plugin/go.d/modules/lighttpd/metrics.go b/src/go/plugin/go.d/modules/lighttpd/metrics.go deleted file mode 100644 index 6c39d2d0..00000000 --- a/src/go/plugin/go.d/modules/lighttpd/metrics.go +++ /dev/null @@ -1,33 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later - -package lighttpd - -type ( - serverStatus struct { - Total struct { - Accesses *int64 `stm:"accesses"` - KBytes *int64 `stm:"kBytes"` - } `stm:"total"` - Servers struct { - Busy *int64 `stm:"busy_servers"` - Idle *int64 `stm:"idle_servers"` - } `stm:""` - Uptime *int64 `stm:"uptime"` - Scoreboard *scoreboard `stm:"scoreboard"` - } - scoreboard struct { - Waiting int64 `stm:"waiting"` - Open int64 `stm:"open"` - Close int64 `stm:"close"` - HardError int64 `stm:"hard_error"` - KeepAlive int64 `stm:"keepalive"` - Read int64 `stm:"read"` - ReadPost int64 `stm:"read_post"` - Write int64 `stm:"write"` - HandleRequest int64 `stm:"handle_request"` - RequestStart int64 `stm:"request_start"` - RequestEnd int64 `stm:"request_end"` - ResponseStart int64 `stm:"response_start"` - ResponseEnd int64 `stm:"response_end"` - } -) diff --git a/src/go/plugin/go.d/modules/lighttpd/apiclient.go b/src/go/plugin/go.d/modules/lighttpd/status.go index 1686272c..a81eb98e 100644 --- a/src/go/plugin/go.d/modules/lighttpd/apiclient.go +++ b/src/go/plugin/go.d/modules/lighttpd/status.go @@ -6,11 +6,8 @@ import ( "bufio" "fmt" "io" - "net/http" "strconv" "strings" - - "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web" ) const ( @@ -25,49 +22,35 @@ const ( scoreBoard = "Scoreboard" ) -func newAPIClient(client *http.Client, request web.Request) *apiClient { - return &apiClient{httpClient: client, request: request} -} - -type apiClient struct { - httpClient *http.Client - request web.Request -} - -func (a apiClient) getServerStatus() (*serverStatus, error) { - req, err := web.NewHTTPRequest(a.request) - - if err != nil { - return nil, fmt.Errorf("error on creating request : %v", err) - } - - resp, err := a.doRequestOK(req) - - defer closeBody(resp) - - if err != nil { - return nil, err - } - - status, err := parseResponse(resp.Body) - - if err != nil { - return nil, fmt.Errorf("error on parsing response from %s : %v", req.URL, err) +type ( + serverStatus struct { + Total struct { + Accesses *int64 `stm:"accesses"` + KBytes *int64 `stm:"kBytes"` + } `stm:"total"` + Servers struct { + Busy *int64 `stm:"busy_servers"` + Idle *int64 `stm:"idle_servers"` + } `stm:""` + Uptime *int64 `stm:"uptime"` + Scoreboard *scoreboard `stm:"scoreboard"` } - - return status, nil -} - -func (a apiClient) doRequestOK(req *http.Request) (*http.Response, error) { - resp, err := a.httpClient.Do(req) - if err != nil { - return nil, fmt.Errorf("error on request : %v", err) + scoreboard struct { + Waiting int64 `stm:"waiting"` + Open int64 `stm:"open"` + Close int64 `stm:"close"` + HardError int64 `stm:"hard_error"` + KeepAlive int64 `stm:"keepalive"` + Read int64 `stm:"read"` + ReadPost int64 `stm:"read_post"` + Write int64 `stm:"write"` + HandleRequest int64 `stm:"handle_request"` + RequestStart int64 `stm:"request_start"` + RequestEnd int64 `stm:"request_end"` + ResponseStart int64 `stm:"response_start"` + ResponseEnd int64 `stm:"response_end"` } - if resp.StatusCode != http.StatusOK { - return resp, fmt.Errorf("%s returned HTTP status %d", req.URL, resp.StatusCode) - } - return resp, nil -} +) func parseResponse(r io.Reader) (*serverStatus, error) { s := bufio.NewScanner(r) @@ -161,10 +144,3 @@ func mustParseInt(value string) *int64 { } return &v } - -func closeBody(resp *http.Response) { - if resp != nil && resp.Body != nil { - _, _ = io.Copy(io.Discard, resp.Body) - _ = resp.Body.Close() - } -} |