2015-07-27 09:28:24 +00:00
|
|
|
package nginx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2017-06-08 00:52:10 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-07-27 09:28:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Nginx struct {
|
2017-06-08 00:52:10 +00:00
|
|
|
// List of status URLs
|
2015-07-27 09:28:24 +00:00
|
|
|
Urls []string
|
2017-06-08 00:52:10 +00:00
|
|
|
// Path to CA file
|
|
|
|
SSLCA string `toml:"ssl_ca"`
|
|
|
|
// Path to client cert file
|
|
|
|
SSLCert string `toml:"ssl_cert"`
|
|
|
|
// Path to cert key file
|
|
|
|
SSLKey string `toml:"ssl_key"`
|
|
|
|
// Use SSL but skip chain & host verification
|
|
|
|
InsecureSkipVerify bool
|
|
|
|
// HTTP client
|
|
|
|
client *http.Client
|
|
|
|
// Response timeout
|
|
|
|
ResponseTimeout internal.Duration
|
2015-07-27 09:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2017-06-08 00:52:10 +00:00
|
|
|
# An array of Nginx stub_status URI to gather stats.
|
|
|
|
urls = ["http://localhost/server_status"]
|
|
|
|
|
|
|
|
# TLS/SSL configuration
|
|
|
|
ssl_ca = "/etc/telegraf/ca.pem"
|
|
|
|
ssl_cert = "/etc/telegraf/cert.cer"
|
|
|
|
ssl_key = "/etc/telegraf/key.key"
|
|
|
|
insecure_skip_verify = false
|
|
|
|
|
|
|
|
# HTTP response timeout (default: 5s)
|
|
|
|
response_timeout = "5s"
|
2015-08-26 15:21:39 +00:00
|
|
|
`
|
2015-07-27 09:28:24 +00:00
|
|
|
|
|
|
|
func (n *Nginx) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Nginx) Description() string {
|
|
|
|
return "Read Nginx's basic status information (ngx_http_stub_status_module)"
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (n *Nginx) Gather(acc telegraf.Accumulator) error {
|
2015-07-27 09:28:24 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2017-06-08 00:52:10 +00:00
|
|
|
// Create an HTTP client that is re-used for each
|
|
|
|
// collection interval
|
|
|
|
if n.client == nil {
|
|
|
|
client, err := n.createHttpClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.client = client
|
|
|
|
}
|
|
|
|
|
2015-07-27 09:28:24 +00:00
|
|
|
for _, u := range n.Urls {
|
|
|
|
addr, err := url.Parse(u)
|
|
|
|
if err != nil {
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(fmt.Errorf("Unable to parse address '%s': %s", u, err))
|
2015-07-27 09:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(addr *url.URL) {
|
|
|
|
defer wg.Done()
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(n.gatherUrl(addr, acc))
|
2015-07-27 09:28:24 +00:00
|
|
|
}(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2017-04-24 18:13:26 +00:00
|
|
|
return nil
|
2015-07-27 09:28:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 00:52:10 +00:00
|
|
|
func (n *Nginx) createHttpClient() (*http.Client, error) {
|
|
|
|
tlsCfg, err := internal.GetTLSConfig(
|
|
|
|
n.SSLCert, n.SSLKey, n.SSLCA, n.InsecureSkipVerify)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.ResponseTimeout.Duration < time.Second {
|
|
|
|
n.ResponseTimeout.Duration = time.Second * 5
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: tlsCfg,
|
|
|
|
},
|
|
|
|
Timeout: n.ResponseTimeout.Duration,
|
|
|
|
}
|
2015-07-27 09:28:24 +00:00
|
|
|
|
2017-06-08 00:52:10 +00:00
|
|
|
return client, nil
|
2016-02-29 16:52:58 +00:00
|
|
|
}
|
2015-07-27 09:28:24 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (n *Nginx) gatherUrl(addr *url.URL, acc telegraf.Accumulator) error {
|
2017-06-08 00:52:10 +00:00
|
|
|
resp, err := n.client.Get(addr.String())
|
2015-07-27 09:28:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error making HTTP request to %s: %s", addr.String(), err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("%s returned HTTP status %s", addr.String(), resp.Status)
|
|
|
|
}
|
|
|
|
r := bufio.NewReader(resp.Body)
|
|
|
|
|
|
|
|
// Active connections
|
|
|
|
_, err = r.ReadString(':')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
line, err := r.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
active, err := strconv.ParseUint(strings.TrimSpace(line), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server accepts handled requests
|
|
|
|
_, err = r.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
line, err = r.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-21 13:22:51 +00:00
|
|
|
data := strings.Fields(line)
|
2015-07-27 09:28:24 +00:00
|
|
|
accepts, err := strconv.ParseUint(data[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-21 13:22:51 +00:00
|
|
|
|
2015-07-27 09:28:24 +00:00
|
|
|
handled, err := strconv.ParseUint(data[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
requests, err := strconv.ParseUint(data[2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading/Writing/Waiting
|
|
|
|
line, err = r.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-21 13:22:51 +00:00
|
|
|
data = strings.Fields(line)
|
2015-07-27 09:28:24 +00:00
|
|
|
reading, err := strconv.ParseUint(data[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
writing, err := strconv.ParseUint(data[3], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
waiting, err := strconv.ParseUint(data[5], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-20 17:26:49 +00:00
|
|
|
tags := getTags(addr)
|
2015-12-15 00:03:33 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"active": active,
|
|
|
|
"accepts": accepts,
|
|
|
|
"handled": handled,
|
|
|
|
"requests": requests,
|
|
|
|
"reading": reading,
|
|
|
|
"writing": writing,
|
|
|
|
"waiting": waiting,
|
|
|
|
}
|
|
|
|
acc.AddFields("nginx", fields, tags)
|
2015-07-27 09:28:24 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-20 17:26:49 +00:00
|
|
|
// Get tag(s) for the nginx plugin
|
|
|
|
func getTags(addr *url.URL) map[string]string {
|
2015-10-15 21:53:29 +00:00
|
|
|
h := addr.Host
|
|
|
|
host, port, err := net.SplitHostPort(h)
|
|
|
|
if err != nil {
|
|
|
|
host = addr.Host
|
|
|
|
if addr.Scheme == "http" {
|
|
|
|
port = "80"
|
|
|
|
} else if addr.Scheme == "https" {
|
|
|
|
port = "443"
|
|
|
|
} else {
|
|
|
|
port = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return map[string]string{"server": host, "port": port}
|
2015-08-20 17:26:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 09:28:24 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("nginx", func() telegraf.Input {
|
2015-07-27 09:28:24 +00:00
|
|
|
return &Nginx{}
|
|
|
|
})
|
|
|
|
}
|