diff options
Diffstat (limited to 'src/go/plugin/go.d/modules/nginx')
l--------- | src/go/plugin/go.d/modules/nginx/README.md | 1 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/apiclient.go | 168 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/charts.go | 58 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/collect.go | 17 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/config_schema.json | 183 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/integrations/nginx.md | 267 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/metadata.yaml | 226 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/metrics.go | 34 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/nginx.go | 106 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/nginx_test.go | 156 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/testdata/config.json | 20 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/testdata/config.yaml | 17 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/testdata/status.txt | 4 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/nginx/testdata/tengine-status.txt | 4 |
14 files changed, 1261 insertions, 0 deletions
diff --git a/src/go/plugin/go.d/modules/nginx/README.md b/src/go/plugin/go.d/modules/nginx/README.md new file mode 120000 index 000000000..7b19fe44f --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/README.md @@ -0,0 +1 @@ +integrations/nginx.md
\ No newline at end of file diff --git a/src/go/plugin/go.d/modules/nginx/apiclient.go b/src/go/plugin/go.d/modules/nginx/apiclient.go new file mode 100644 index 000000000..53d9f2245 --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/apiclient.go @@ -0,0 +1,168 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package nginx + +import ( + "bufio" + "fmt" + "io" + "net/http" + "regexp" + "strconv" + "strings" + + "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web" +) + +const ( + connActive = "connActive" + connAccepts = "connAccepts" + connHandled = "connHandled" + requests = "requests" + requestTime = "requestTime" + connReading = "connReading" + connWriting = "connWriting" + connWaiting = "connWaiting" +) + +var ( + nginxSeq = []string{ + connActive, + connAccepts, + connHandled, + requests, + connReading, + connWriting, + connWaiting, + } + tengineSeq = []string{ + connActive, + connAccepts, + connHandled, + requests, + requestTime, + connReading, + connWriting, + connWaiting, + } + + reStatus = regexp.MustCompile(`^Active connections: ([0-9]+)\n[^\d]+([0-9]+) ([0-9]+) ([0-9]+) ?([0-9]+)?\nReading: ([0-9]+) Writing: ([0-9]+) Waiting: ([0-9]+)`) +) + +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) getStubStatus() (*stubStatus, 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 := parseStubStatus(resp.Body) + if err != nil { + return nil, fmt.Errorf("error on parsing response : %v", err) + } + + return status, nil +} + +func (a apiClient) doRequestOK(req *http.Request) (*http.Response, error) { + resp, err := a.httpClient.Do(req) + if err != nil { + return resp, fmt.Errorf("error on request : %v", err) + } + + if resp.StatusCode != http.StatusOK { + return resp, fmt.Errorf("%s returned HTTP status %d", req.URL, resp.StatusCode) + } + + return resp, err +} + +func closeBody(resp *http.Response) { + if resp != nil && resp.Body != nil { + _, _ = io.Copy(io.Discard, resp.Body) + _ = resp.Body.Close() + } +} + +func parseStubStatus(r io.Reader) (*stubStatus, error) { + sc := bufio.NewScanner(r) + var lines []string + + for sc.Scan() { + lines = append(lines, strings.Trim(sc.Text(), "\r\n ")) + } + + parsed := reStatus.FindStringSubmatch(strings.Join(lines, "\n")) + + if len(parsed) == 0 { + return nil, fmt.Errorf("can't parse '%v'", lines) + } + + parsed = parsed[1:] + + var ( + seq []string + status stubStatus + ) + + switch len(parsed) { + default: + return nil, fmt.Errorf("invalid number of fields, got %d, expect %d or %d", len(parsed), len(nginxSeq), len(tengineSeq)) + case len(nginxSeq): + seq = nginxSeq + case len(tengineSeq): + seq = tengineSeq + } + + for i, key := range seq { + strValue := parsed[i] + if strValue == "" { + continue + } + value := mustParseInt(strValue) + switch key { + default: + return nil, fmt.Errorf("unknown key in seq : %s", key) + case connActive: + status.Connections.Active = value + case connAccepts: + status.Connections.Accepts = value + case connHandled: + status.Connections.Handled = value + case requests: + status.Requests.Total = value + case connReading: + status.Connections.Reading = value + case connWriting: + status.Connections.Writing = value + case connWaiting: + status.Connections.Waiting = value + case requestTime: + status.Requests.Time = &value + } + } + + return &status, nil +} + +func mustParseInt(value string) int64 { + v, err := strconv.ParseInt(value, 10, 64) + if err != nil { + panic(err) + } + return v +} diff --git a/src/go/plugin/go.d/modules/nginx/charts.go b/src/go/plugin/go.d/modules/nginx/charts.go new file mode 100644 index 000000000..3415fbae8 --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/charts.go @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package nginx + +import "github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module" + +type ( + // Charts is an alias for module.Charts + Charts = module.Charts + // Dims is an alias for module.Dims + Dims = module.Dims +) + +var charts = Charts{ + { + ID: "connections", + Title: "Active Client Connections Including Waiting Connections", + Units: "connections", + Fam: "connections", + Ctx: "nginx.connections", + Dims: Dims{ + {ID: "active"}, + }, + }, + { + ID: "connections_statuses", + Title: "Active Connections Per Status", + Units: "connections", + Fam: "connections", + Ctx: "nginx.connections_status", + Dims: Dims{ + {ID: "reading"}, + {ID: "writing"}, + {ID: "waiting", Name: "idle"}, + }, + }, + { + ID: "connections_accepted_handled", + Title: "Accepted And Handled Connections", + Units: "connections/s", + Fam: "connections", + Ctx: "nginx.connections_accepted_handled", + Dims: Dims{ + {ID: "accepts", Name: "accepted", Algo: module.Incremental}, + {ID: "handled", Algo: module.Incremental}, + }, + }, + { + ID: "requests", + Title: "Client Requests", + Units: "requests/s", + Fam: "requests", + Ctx: "nginx.requests", + Dims: Dims{ + {ID: "requests", Algo: module.Incremental}, + }, + }, +} diff --git a/src/go/plugin/go.d/modules/nginx/collect.go b/src/go/plugin/go.d/modules/nginx/collect.go new file mode 100644 index 000000000..459570ae5 --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/collect.go @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package nginx + +import ( + "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/stm" +) + +func (n *Nginx) collect() (map[string]int64, error) { + status, err := n.apiClient.getStubStatus() + + if err != nil { + return nil, err + } + + return stm.ToMap(status), nil +} diff --git a/src/go/plugin/go.d/modules/nginx/config_schema.json b/src/go/plugin/go.d/modules/nginx/config_schema.json new file mode 100644 index 000000000..25fead781 --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/config_schema.json @@ -0,0 +1,183 @@ +{ + "jsonSchema": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "NGINX collector configuration.", + "type": "object", + "properties": { + "update_every": { + "title": "Update every", + "description": "Data collection interval, measured in seconds.", + "type": "integer", + "minimum": 1, + "default": 1 + }, + "url": { + "title": "URL", + "description": "The URL of the NGINX [status page](https://nginx.org/en/docs/http/ngx_http_stub_status_module.html).", + "type": "string", + "default": "http://127.0.0.1/stub_status", + "format": "uri" + }, + "timeout": { + "title": "Timeout", + "description": "The timeout in seconds for the HTTP request.", + "type": "number", + "minimum": 0.5, + "default": 1 + }, + "not_follow_redirects": { + "title": "Not follow redirects", + "description": "If set, the client will not follow HTTP redirects automatically.", + "type": "boolean" + }, + "username": { + "title": "Username", + "description": "The username for basic authentication.", + "type": "string", + "sensitive": true + }, + "password": { + "title": "Password", + "description": "The password for basic authentication.", + "type": "string", + "sensitive": true + }, + "proxy_url": { + "title": "Proxy URL", + "description": "The URL of the proxy server.", + "type": "string" + }, + "proxy_username": { + "title": "Proxy username", + "description": "The username for proxy authentication.", + "type": "string", + "sensitive": true + }, + "proxy_password": { + "title": "Proxy password", + "description": "The password for proxy authentication.", + "type": "string", + "sensitive": true + }, + "headers": { + "title": "Headers", + "description": "Additional HTTP headers to include in the request.", + "type": [ + "object", + "null" + ], + "additionalProperties": { + "type": "string" + } + }, + "tls_skip_verify": { + "title": "Skip TLS verification", + "description": "If set, TLS certificate verification will be skipped.", + "type": "boolean" + }, + "tls_ca": { + "title": "TLS CA", + "description": "The path to the CA certificate file for TLS verification.", + "type": "string", + "pattern": "^$|^/" + }, + "tls_cert": { + "title": "TLS certificate", + "description": "The path to the client certificate file for TLS authentication.", + "type": "string", + "pattern": "^$|^/" + }, + "tls_key": { + "title": "TLS key", + "description": "The path to the client key file for TLS authentication.", + "type": "string", + "pattern": "^$|^/" + }, + "body": { + "title": "Body", + "type": "string" + }, + "method": { + "title": "Method", + "type": "string" + } + }, + "required": [ + "url" + ], + "additionalProperties": false, + "patternProperties": { + "^name$": {} + } + }, + "uiSchema": { + "ui:flavour": "tabs", + "ui:options": { + "tabs": [ + { + "title": "Base", + "fields": [ + "update_every", + "url", + "timeout", + "not_follow_redirects" + ] + }, + { + "title": "Auth", + "fields": [ + "username", + "password" + ] + }, + { + "title": "TLS", + "fields": [ + "tls_skip_verify", + "tls_ca", + "tls_cert", + "tls_key" + ] + }, + { + "title": "Proxy", + "fields": [ + "proxy_url", + "proxy_username", + "proxy_password" + ] + }, + { + "title": "Headers", + "fields": [ + "headers" + ] + } + ] + }, + "uiOptions": { + "fullPage": true + }, + "body": { + "ui:widget": "hidden" + }, + "method": { + "ui:widget": "hidden" + }, + "timeout": { + "ui:help": "Accepts decimals for precise control (e.g., type 1.5 for 1.5 seconds)." + }, + "username": { + "ui:widget": "password" + }, + "proxy_username": { + "ui:widget": "password" + }, + "password": { + "ui:widget": "password" + }, + "proxy_password": { + "ui:widget": "password" + } + } +} diff --git a/src/go/plugin/go.d/modules/nginx/integrations/nginx.md b/src/go/plugin/go.d/modules/nginx/integrations/nginx.md new file mode 100644 index 000000000..6d8338a10 --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/integrations/nginx.md @@ -0,0 +1,267 @@ +<!--startmeta +custom_edit_url: "https://github.com/netdata/netdata/edit/master/src/go/plugin/go.d/modules/nginx/README.md" +meta_yaml: "https://github.com/netdata/netdata/edit/master/src/go/plugin/go.d/modules/nginx/metadata.yaml" +sidebar_label: "NGINX" +learn_status: "Published" +learn_rel_path: "Collecting Metrics/Web Servers and Web Proxies" +most_popular: True +message: "DO NOT EDIT THIS FILE DIRECTLY, IT IS GENERATED BY THE COLLECTOR'S metadata.yaml FILE" +endmeta--> + +# NGINX + + +<img src="https://netdata.cloud/img/nginx.svg" width="150"/> + + +Plugin: go.d.plugin +Module: nginx + +<img src="https://img.shields.io/badge/maintained%20by-Netdata-%2300ab44" /> + +## Overview + +This collector monitors the activity and performance of NGINX servers, and collects metrics such as the number of connections, their status, and client requests. + + +It sends HTTP requests to the NGINX location [stub-status](https://nginx.org/en/docs/http/ngx_http_stub_status_module.html), which is a built-in location that provides metrics about the NGINX server. + + +This collector is supported on all platforms. + +This collector supports collecting metrics from multiple instances of this integration, including remote instances. + + +### Default Behavior + +#### Auto-Detection + +By default, it detects NGINX instances running on localhost that are listening on port 80. +On startup, it tries to collect metrics from: + +- http://127.0.0.1/basic_status +- http://localhost/stub_status +- http://127.0.0.1/stub_status +- http://127.0.0.1/nginx_status +- http://127.0.0.1/status + + +#### Limits + +The default configuration for this integration does not impose any limits on data collection. + +#### Performance Impact + +The default configuration for this integration is not expected to impose a significant performance impact on the system. + + +## Metrics + +Metrics grouped by *scope*. + +The scope defines the instance that the metric belongs to. An instance is uniquely identified by a set of labels. + + + +### Per NGINX instance + +These metrics refer to the entire monitored application. + +This scope has no labels. + +Metrics: + +| Metric | Dimensions | Unit | +|:------|:----------|:----| +| nginx.connections | active | connections | +| nginx.connections_status | reading, writing, idle | connections | +| nginx.connections_accepted_handled | accepted, handled | connections/s | +| nginx.requests | requests | requests/s | + + + +## Alerts + +There are no alerts configured by default for this integration. + + +## Setup + +### Prerequisites + +#### Enable status support + +Configure [ngx_http_stub_status_module](https://nginx.org/en/docs/http/ngx_http_stub_status_module.html). + + + +### Configuration + +#### File + +The configuration file name for this integration is `go.d/nginx.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). + +```bash +cd /etc/netdata 2>/dev/null || cd /opt/netdata/etc/netdata +sudo ./edit-config go.d/nginx.conf +``` +#### Options + +The following options can be defined globally: update_every, autodetection_retry. + + +<details open><summary>Config options</summary> + +| Name | Description | Default | Required | +|:----|:-----------|:-------|:--------:| +| update_every | Data collection frequency. | 1 | no | +| autodetection_retry | Recheck interval in seconds. Zero means no recheck will be scheduled. | 0 | no | +| url | Server URL. | http://127.0.0.1/stub_status | yes | +| timeout | HTTP request timeout. | 1 | no | +| username | Username for basic HTTP authentication. | | no | +| password | Password for basic HTTP authentication. | | no | +| proxy_url | Proxy URL. | | no | +| proxy_username | Username for proxy basic HTTP authentication. | | no | +| proxy_password | Password for proxy basic HTTP authentication. | | no | +| method | HTTP request method. | GET | no | +| body | HTTP request body. | | no | +| headers | HTTP request headers. | | no | +| not_follow_redirects | Redirect handling policy. Controls whether the client follows redirects. | no | no | +| tls_skip_verify | Server certificate chain and hostname validation policy. Controls whether the client performs this check. | no | no | +| tls_ca | Certification authority that the client uses when verifying the server's certificates. | | no | +| tls_cert | Client TLS certificate. | | no | +| tls_key | Client TLS key. | | no | + +</details> + +#### Examples + +##### Basic + +A basic example configuration. + +```yaml +jobs: + - name: local + url: http://127.0.0.1/stub_status + +``` +##### HTTP authentication + +Basic HTTP authentication. + +<details open><summary>Config</summary> + +```yaml +jobs: + - name: local + url: http://127.0.0.1/stub_status + username: username + password: password + +``` +</details> + +##### HTTPS with self-signed certificate + +NGINX with enabled HTTPS and self-signed certificate. + +<details open><summary>Config</summary> + +```yaml +jobs: + - name: local + url: http://127.0.0.1/stub_status + tls_skip_verify: yes + +``` +</details> + +##### Multi-instance + +> **Note**: When you define multiple jobs, their names must be unique. + +Collecting metrics from local and remote instances. + + +<details open><summary>Config</summary> + +```yaml +jobs: + - name: local + url: http://127.0.0.1/stub_status + + - name: remote + url: http://192.0.2.1/stub_status + +``` +</details> + + + +## Troubleshooting + +### Debug Mode + +**Important**: Debug mode is not supported for data collection jobs created via the UI using the Dyncfg feature. + +To troubleshoot issues with the `nginx` collector, run the `go.d.plugin` with the debug option enabled. The output +should give you clues as to why the collector isn't working. + +- Navigate to the `plugins.d` directory, usually at `/usr/libexec/netdata/plugins.d/`. If that's not the case on + your system, open `netdata.conf` and look for the `plugins` setting under `[directories]`. + + ```bash + cd /usr/libexec/netdata/plugins.d/ + ``` + +- Switch to the `netdata` user. + + ```bash + sudo -u netdata -s + ``` + +- Run the `go.d.plugin` to debug the collector: + + ```bash + ./go.d.plugin -d -m nginx + ``` + +### Getting Logs + +If you're encountering problems with the `nginx` collector, follow these steps to retrieve logs and identify potential issues: + +- **Run the command** specific to your system (systemd, non-systemd, or Docker container). +- **Examine the output** for any warnings or error messages that might indicate issues. These messages should provide clues about the root cause of the problem. + +#### System with systemd + +Use the following command to view logs generated since the last Netdata service restart: + +```bash +journalctl _SYSTEMD_INVOCATION_ID="$(systemctl show --value --property=InvocationID netdata)" --namespace=netdata --grep nginx +``` + +#### System without systemd + +Locate the collector log file, typically at `/var/log/netdata/collector.log`, and use `grep` to filter for collector's name: + +```bash +grep nginx /var/log/netdata/collector.log +``` + +**Note**: This method shows logs from all restarts. Focus on the **latest entries** for troubleshooting current issues. + +#### Docker Container + +If your Netdata runs in a Docker container named "netdata" (replace if different), use this command: + +```bash +docker logs netdata 2>&1 | grep nginx +``` + + diff --git a/src/go/plugin/go.d/modules/nginx/metadata.yaml b/src/go/plugin/go.d/modules/nginx/metadata.yaml new file mode 100644 index 000000000..49b12c4ec --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/metadata.yaml @@ -0,0 +1,226 @@ +plugin_name: go.d.plugin +modules: + - meta: + id: collector-go.d.plugin-nginx + plugin_name: go.d.plugin + module_name: nginx + monitored_instance: + name: NGINX + link: https://www.nginx.com/ + categories: + - data-collection.web-servers-and-web-proxies + icon_filename: nginx.svg + related_resources: + integrations: + list: + - plugin_name: go.d.plugin + module_name: httpcheck + - plugin_name: go.d.plugin + module_name: web_log + - plugin_name: apps.plugin + module_name: apps + - plugin_name: cgroups.plugin + module_name: cgroups + alternative_monitored_instances: [] + info_provided_to_referring_integrations: + description: "" + keywords: + - nginx + - web + - webserver + - http + - proxy + most_popular: true + overview: + data_collection: + metrics_description: | + This collector monitors the activity and performance of NGINX servers, and collects metrics such as the number of connections, their status, and client requests. + method_description: | + It sends HTTP requests to the NGINX location [stub-status](https://nginx.org/en/docs/http/ngx_http_stub_status_module.html), which is a built-in location that provides metrics about the NGINX server. + default_behavior: + auto_detection: + description: | + By default, it detects NGINX instances running on localhost that are listening on port 80. + On startup, it tries to collect metrics from: + + - http://127.0.0.1/basic_status + - http://localhost/stub_status + - http://127.0.0.1/stub_status + - http://127.0.0.1/nginx_status + - http://127.0.0.1/status + limits: + description: "" + performance_impact: + description: "" + additional_permissions: + description: "" + multi_instance: true + supported_platforms: + include: [] + exclude: [] + setup: + prerequisites: + list: + - title: Enable status support + description: | + Configure [ngx_http_stub_status_module](https://nginx.org/en/docs/http/ngx_http_stub_status_module.html). + configuration: + file: + name: go.d/nginx.conf + options: + description: | + The following options can be defined globally: update_every, autodetection_retry. + folding: + title: Config options + enabled: true + list: + - name: update_every + description: Data collection frequency. + default_value: 1 + required: false + - name: autodetection_retry + description: Recheck interval in seconds. Zero means no recheck will be scheduled. + default_value: 0 + required: false + - name: url + description: Server URL. + default_value: http://127.0.0.1/stub_status + required: true + - name: timeout + description: HTTP request timeout. + default_value: 1 + required: false + - name: username + description: Username for basic HTTP authentication. + default_value: "" + required: false + - name: password + description: Password for basic HTTP authentication. + default_value: "" + required: false + - name: proxy_url + description: Proxy URL. + default_value: "" + required: false + - name: proxy_username + description: Username for proxy basic HTTP authentication. + default_value: "" + required: false + - name: proxy_password + description: Password for proxy basic HTTP authentication. + default_value: "" + required: false + - name: method + description: HTTP request method. + default_value: GET + required: false + - name: body + description: HTTP request body. + default_value: "" + required: false + - name: headers + description: HTTP request headers. + default_value: "" + required: false + - name: not_follow_redirects + description: Redirect handling policy. Controls whether the client follows redirects. + default_value: false + required: false + - name: tls_skip_verify + description: Server certificate chain and hostname validation policy. Controls whether the client performs this check. + default_value: false + required: false + - name: tls_ca + description: Certification authority that the client uses when verifying the server's certificates. + default_value: "" + required: false + - name: tls_cert + description: Client TLS certificate. + default_value: "" + required: false + - name: tls_key + description: Client TLS key. + default_value: "" + required: false + examples: + folding: + title: Config + enabled: true + list: + - name: Basic + description: A basic example configuration. + folding: + enabled: false + config: | + jobs: + - name: local + url: http://127.0.0.1/stub_status + - name: HTTP authentication + description: Basic HTTP authentication. + config: | + jobs: + - name: local + url: http://127.0.0.1/stub_status + username: username + password: password + - name: HTTPS with self-signed certificate + description: NGINX with enabled HTTPS and self-signed certificate. + config: | + jobs: + - name: local + url: http://127.0.0.1/stub_status + tls_skip_verify: yes + - name: Multi-instance + description: | + > **Note**: When you define multiple jobs, their names must be unique. + + Collecting metrics from local and remote instances. + config: | + jobs: + - name: local + url: http://127.0.0.1/stub_status + + - name: remote + url: http://192.0.2.1/stub_status + troubleshooting: + problems: + list: [] + alerts: [] + metrics: + folding: + title: Metrics + enabled: false + description: "" + availability: [] + scopes: + - name: global + description: These metrics refer to the entire monitored application. + labels: [] + metrics: + - name: nginx.connections + description: Active Client Connections Including Waiting Connections + unit: connections + chart_type: line + dimensions: + - name: active + - name: nginx.connections_status + description: Active Connections Per Status + unit: connections + chart_type: line + dimensions: + - name: reading + - name: writing + - name: idle + - name: nginx.connections_accepted_handled + description: Accepted And Handled Connections + unit: connections/s + chart_type: line + dimensions: + - name: accepted + - name: handled + - name: nginx.requests + description: Client Requests + unit: requests/s + chart_type: line + dimensions: + - name: requests diff --git a/src/go/plugin/go.d/modules/nginx/metrics.go b/src/go/plugin/go.d/modules/nginx/metrics.go new file mode 100644 index 000000000..66e6a160e --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/metrics.go @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package nginx + +type stubStatus struct { + Connections struct { + // The current number of active client connections including Waiting connections. + Active int64 `stm:"active"` + + // The total number of accepted client connections. + Accepts int64 `stm:"accepts"` + + // The total number of handled connections. + // Generally, the parameter value is the same as accepts unless some resource limits have been reached. + Handled int64 `stm:"handled"` + + // The current number of connections where nginx is reading the request header. + Reading int64 `stm:"reading"` + + // The current number of connections where nginx is writing the response back to the client. + Writing int64 `stm:"writing"` + + // The current number of idle client connections waiting for a request. + Waiting int64 `stm:"waiting"` + } `stm:""` + Requests struct { + // The total number of client requests. + Total int64 `stm:"requests"` + + // Note: tengine specific + // The total requests' response time, which is in millisecond + Time *int64 `stm:"request_time"` + } `stm:""` +} diff --git a/src/go/plugin/go.d/modules/nginx/nginx.go b/src/go/plugin/go.d/modules/nginx/nginx.go new file mode 100644 index 000000000..4a8e77439 --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/nginx.go @@ -0,0 +1,106 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package nginx + +import ( + _ "embed" + "errors" + "time" + + "github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module" + "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web" +) + +//go:embed "config_schema.json" +var configSchema string + +func init() { + module.Register("nginx", module.Creator{ + JobConfigSchema: configSchema, + Create: func() module.Module { return New() }, + Config: func() any { return &Config{} }, + }) +} + +func New() *Nginx { + return &Nginx{ + Config: Config{ + HTTP: web.HTTP{ + Request: web.Request{ + URL: "http://127.0.0.1/stub_status", + }, + Client: web.Client{ + Timeout: web.Duration(time.Second * 1), + }, + }, + }} +} + +type Config struct { + UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` + web.HTTP `yaml:",inline" json:""` +} + +type Nginx struct { + module.Base + Config `yaml:",inline" json:""` + + apiClient *apiClient +} + +func (n *Nginx) Configuration() any { + return n.Config +} + +func (n *Nginx) Init() error { + if n.URL == "" { + n.Error("URL not set") + return errors.New("url not set") + } + + client, err := web.NewHTTPClient(n.Client) + if err != nil { + n.Error(err) + return err + } + + n.apiClient = newAPIClient(client, n.Request) + + n.Debugf("using URL %s", n.URL) + n.Debugf("using timeout: %s", n.Timeout) + + return nil +} + +func (n *Nginx) Check() error { + mx, err := n.collect() + if err != nil { + n.Error(err) + return err + } + if len(mx) == 0 { + return errors.New("no metrics collected") + + } + return nil +} + +func (n *Nginx) Charts() *Charts { + return charts.Copy() +} + +func (n *Nginx) Collect() map[string]int64 { + mx, err := n.collect() + if err != nil { + n.Error(err) + return nil + } + + return mx +} + +func (n *Nginx) Cleanup() { + if n.apiClient != nil && n.apiClient.httpClient != nil { + n.apiClient.httpClient.CloseIdleConnections() + } +} diff --git a/src/go/plugin/go.d/modules/nginx/nginx_test.go b/src/go/plugin/go.d/modules/nginx/nginx_test.go new file mode 100644 index 000000000..255ea384c --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/nginx_test.go @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package nginx + +import ( + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var ( + dataConfigJSON, _ = os.ReadFile("testdata/config.json") + dataConfigYAML, _ = os.ReadFile("testdata/config.yaml") + + dataStatusMetrics, _ = os.ReadFile("testdata/status.txt") + dataTengineStatusMetrics, _ = os.ReadFile("testdata/tengine-status.txt") +) + +func Test_testDataIsValid(t *testing.T) { + for name, data := range map[string][]byte{ + "dataConfigJSON": dataConfigJSON, + "dataConfigYAML": dataConfigYAML, + "dataStatusMetrics": dataStatusMetrics, + "dataTengineStatusMetrics": dataTengineStatusMetrics, + } { + require.NotNil(t, data, name) + } +} + +func TestNginx_ConfigurationSerialize(t *testing.T) { + module.TestConfigurationSerialize(t, &Nginx{}, dataConfigJSON, dataConfigYAML) +} + +func TestNginx_Cleanup(t *testing.T) { + New().Cleanup() +} + +func TestNginx_Init(t *testing.T) { + job := New() + + require.NoError(t, job.Init()) + assert.NotNil(t, job.apiClient) +} + +func TestNginx_Check(t *testing.T) { + ts := httptest.NewServer( + http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write(dataStatusMetrics) + })) + defer ts.Close() + + job := New() + job.URL = ts.URL + require.NoError(t, job.Init()) + assert.NoError(t, job.Check()) +} + +func TestNginx_CheckNG(t *testing.T) { + job := New() + + job.URL = "http://127.0.0.1:38001/us" + require.NoError(t, job.Init()) + assert.Error(t, job.Check()) +} + +func TestNginx_Charts(t *testing.T) { + assert.NotNil(t, New().Charts()) +} + +func TestNginx_Collect(t *testing.T) { + ts := httptest.NewServer( + http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write(dataStatusMetrics) + })) + defer ts.Close() + + job := New() + job.URL = ts.URL + require.NoError(t, job.Init()) + require.NoError(t, job.Check()) + + expected := map[string]int64{ + "accepts": 36, + "active": 1, + "handled": 36, + "reading": 0, + "requests": 126, + "waiting": 0, + "writing": 1, + } + + assert.Equal(t, expected, job.Collect()) +} + +func TestNginx_CollectTengine(t *testing.T) { + ts := httptest.NewServer( + http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write(dataTengineStatusMetrics) + })) + defer ts.Close() + + job := New() + job.URL = ts.URL + require.NoError(t, job.Init()) + require.NoError(t, job.Check()) + + expected := map[string]int64{ + "accepts": 1140, + "active": 1, + "handled": 1140, + "reading": 0, + "request_time": 75806, + "requests": 1140, + "waiting": 0, + "writing": 1, + } + + assert.Equal(t, expected, job.Collect()) +} + +func TestNginx_InvalidData(t *testing.T) { + ts := httptest.NewServer( + http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte("hello and goodbye")) + })) + defer ts.Close() + + job := New() + job.URL = ts.URL + require.NoError(t, job.Init()) + assert.Error(t, job.Check()) +} + +func TestNginx_404(t *testing.T) { + ts := httptest.NewServer( + http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + })) + defer ts.Close() + + job := New() + job.URL = ts.URL + require.NoError(t, job.Init()) + assert.Error(t, job.Check()) +} diff --git a/src/go/plugin/go.d/modules/nginx/testdata/config.json b/src/go/plugin/go.d/modules/nginx/testdata/config.json new file mode 100644 index 000000000..984c3ed6e --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/testdata/config.json @@ -0,0 +1,20 @@ +{ + "update_every": 123, + "url": "ok", + "body": "ok", + "method": "ok", + "headers": { + "ok": "ok" + }, + "username": "ok", + "password": "ok", + "proxy_url": "ok", + "proxy_username": "ok", + "proxy_password": "ok", + "timeout": 123.123, + "not_follow_redirects": true, + "tls_ca": "ok", + "tls_cert": "ok", + "tls_key": "ok", + "tls_skip_verify": true +} diff --git a/src/go/plugin/go.d/modules/nginx/testdata/config.yaml b/src/go/plugin/go.d/modules/nginx/testdata/config.yaml new file mode 100644 index 000000000..8558b61cc --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/testdata/config.yaml @@ -0,0 +1,17 @@ +update_every: 123 +url: "ok" +body: "ok" +method: "ok" +headers: + ok: "ok" +username: "ok" +password: "ok" +proxy_url: "ok" +proxy_username: "ok" +proxy_password: "ok" +timeout: 123.123 +not_follow_redirects: yes +tls_ca: "ok" +tls_cert: "ok" +tls_key: "ok" +tls_skip_verify: yes diff --git a/src/go/plugin/go.d/modules/nginx/testdata/status.txt b/src/go/plugin/go.d/modules/nginx/testdata/status.txt new file mode 100644 index 000000000..f4835bef4 --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/testdata/status.txt @@ -0,0 +1,4 @@ +Active connections: 1 +server accepts handled requests +36 36 126 +Reading: 0 Writing: 1 Waiting: 0
\ No newline at end of file diff --git a/src/go/plugin/go.d/modules/nginx/testdata/tengine-status.txt b/src/go/plugin/go.d/modules/nginx/testdata/tengine-status.txt new file mode 100644 index 000000000..1e6a62c21 --- /dev/null +++ b/src/go/plugin/go.d/modules/nginx/testdata/tengine-status.txt @@ -0,0 +1,4 @@ +Active connections: 1 +server accepts handled requests request_time +1140 1140 1140 75806 +Reading: 0 Writing: 1 Waiting: 0
\ No newline at end of file |