Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nginx Plus stats #2405

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions plugins/inputs/nginx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,24 @@ It produces:
* Plugin: nginx, Collection 1
> nginx,port=80,server=localhost accepts=605i,active=2i,handled=605i,reading=0i,requests=12132i,waiting=1i,writing=1i 1456690994701784331
```

### Reference material

Structures for Nginx Plus have been built based on history of
[status module documentation](http://nginx.org/en/docs/http/ngx_http_status_module.html)

Subsequent versions of status response structure available here:

- [version 1](http://web.archive.org/web/20130805111222/http://nginx.org/en/docs/http/ngx_http_status_module.html)

- [version 2](http://web.archive.org/web/20131218101504/http://nginx.org/en/docs/http/ngx_http_status_module.html)

- version 3 - not available

- [version 4](http://web.archive.org/web/20141218170938/http://nginx.org/en/docs/http/ngx_http_status_module.html)

- [version 5](http://web.archive.org/web/20150414043916/http://nginx.org/en/docs/http/ngx_http_status_module.html)

- [version 6](http://web.archive.org/web/20150918163811/http://nginx.org/en/docs/http/ngx_http_status_module.html)

- [version 7](http://web.archive.org/web/20161107221028/http://nginx.org/en/docs/http/ngx_http_status_module.html)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does someone know what version they have? Does this plugin support all versions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably cover all the version by linking to https://nginx.org/en/docs/http/ngx_http_status_module.html#compatibility

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plugin supports all versions. Version is sent in the very first attribute.

19 changes: 14 additions & 5 deletions plugins/inputs/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Nginx struct {
}

var sampleConfig = `
## An array of Nginx stub_status URI to gather stats.
## An array of ngx_http_stub_status_module or ngx_http_status_module or status URI to gather stats.
urls = ["http://localhost/status"]
`

Expand All @@ -30,7 +30,7 @@ func (n *Nginx) SampleConfig() string {
}

func (n *Nginx) Description() string {
return "Read Nginx's basic status information (ngx_http_stub_status_module)"
return "Read Nginx's basic status information (ngx_http_stub_status_module) or Nginx's Plus full status information (ngx_http_status_module)"
}

func (n *Nginx) Gather(acc telegraf.Accumulator) error {
Expand Down Expand Up @@ -72,10 +72,20 @@ func (n *Nginx) gatherUrl(addr *url.URL, acc telegraf.Accumulator) error {
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s returned HTTP status %s", addr.String(), resp.Status)
}
r := bufio.NewReader(resp.Body)
contentType := strings.Split(resp.Header.Get("Content-Type"), ";")[0]
switch contentType {
case "text/plain":
return gatherStubStatusUrl(bufio.NewReader(resp.Body), getTags(addr), acc)
case "application/json":
return gatherStatusUrl(bufio.NewReader(resp.Body), getTags(addr), acc)
default:
return fmt.Errorf("%s returned unexpected content type %s", addr.String(), contentType)
}
}

func gatherStubStatusUrl(r *bufio.Reader, tags map[string]string, acc telegraf.Accumulator) error {
// Active connections
_, err = r.ReadString(':')
_, err := r.ReadString(':')
if err != nil {
return err
}
Expand Down Expand Up @@ -131,7 +141,6 @@ func (n *Nginx) gatherUrl(addr *url.URL, acc telegraf.Accumulator) error {
return err
}

tags := getTags(addr)
fields := map[string]interface{}{
"active": active,
"accepts": accepts,
Expand Down
Loading