2015-07-27 09:28:24 +00:00
|
|
|
package nginx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-07-27 09:28:24 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2016-06-21 13:22:51 +00:00
|
|
|
const nginxSampleResponse = `
|
2015-08-20 17:26:49 +00:00
|
|
|
Active connections: 585
|
2015-07-27 09:28:24 +00:00
|
|
|
server accepts handled requests
|
2015-08-20 17:26:49 +00:00
|
|
|
85340 85340 35085
|
|
|
|
Reading: 4 Writing: 135 Waiting: 446
|
2015-07-27 09:28:24 +00:00
|
|
|
`
|
2016-06-21 13:22:51 +00:00
|
|
|
const tengineSampleResponse = `
|
|
|
|
Active connections: 403
|
|
|
|
server accepts handled requests request_time
|
|
|
|
853 8533 3502 1546565864
|
|
|
|
Reading: 8 Writing: 125 Waiting: 946
|
|
|
|
`
|
2015-07-27 09:28:24 +00:00
|
|
|
|
2015-08-20 17:26:49 +00:00
|
|
|
// Verify that nginx tags are properly parsed based on the server
|
|
|
|
func TestNginxTags(t *testing.T) {
|
|
|
|
urls := []string{"http://localhost/endpoint", "http://localhost:80/endpoint"}
|
|
|
|
var addr *url.URL
|
|
|
|
for _, url1 := range urls {
|
|
|
|
addr, _ = url.Parse(url1)
|
|
|
|
tagMap := getTags(addr)
|
|
|
|
assert.Contains(t, tagMap["server"], "localhost")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 09:28:24 +00:00
|
|
|
func TestNginxGeneratesMetrics(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var rsp string
|
|
|
|
|
|
|
|
if r.URL.Path == "/stub_status" {
|
2016-06-21 13:22:51 +00:00
|
|
|
rsp = nginxSampleResponse
|
|
|
|
} else if r.URL.Path == "/tengine_status" {
|
|
|
|
rsp = tengineSampleResponse
|
2015-07-27 09:28:24 +00:00
|
|
|
} else {
|
|
|
|
panic("Cannot handle request")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(w, rsp)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
n := &Nginx{
|
|
|
|
Urls: []string{fmt.Sprintf("%s/stub_status", ts.URL)},
|
|
|
|
}
|
|
|
|
|
2016-06-21 13:22:51 +00:00
|
|
|
nt := &Nginx{
|
|
|
|
Urls: []string{fmt.Sprintf("%s/tengine_status", ts.URL)},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc_nginx testutil.Accumulator
|
|
|
|
var acc_tengine testutil.Accumulator
|
|
|
|
|
|
|
|
err_nginx := n.Gather(&acc_nginx)
|
|
|
|
err_tengine := nt.Gather(&acc_tengine)
|
2015-07-27 09:28:24 +00:00
|
|
|
|
2016-06-21 13:22:51 +00:00
|
|
|
require.NoError(t, err_nginx)
|
|
|
|
require.NoError(t, err_tengine)
|
2015-07-27 09:28:24 +00:00
|
|
|
|
2016-06-21 13:22:51 +00:00
|
|
|
fields_nginx := map[string]interface{}{
|
2016-01-07 00:37:56 +00:00
|
|
|
"active": uint64(585),
|
|
|
|
"accepts": uint64(85340),
|
|
|
|
"handled": uint64(85340),
|
|
|
|
"requests": uint64(35085),
|
|
|
|
"reading": uint64(4),
|
|
|
|
"writing": uint64(135),
|
|
|
|
"waiting": uint64(446),
|
2015-07-27 09:28:24 +00:00
|
|
|
}
|
2016-06-21 13:22:51 +00:00
|
|
|
|
|
|
|
fields_tengine := map[string]interface{}{
|
|
|
|
"active": uint64(403),
|
|
|
|
"accepts": uint64(853),
|
|
|
|
"handled": uint64(8533),
|
|
|
|
"requests": uint64(3502),
|
|
|
|
"reading": uint64(8),
|
|
|
|
"writing": uint64(125),
|
|
|
|
"waiting": uint64(946),
|
|
|
|
}
|
|
|
|
|
2015-07-27 09:28:24 +00:00
|
|
|
addr, err := url.Parse(ts.URL)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-09-23 18:43:44 +00:00
|
|
|
|
2015-10-16 18:54:05 +00:00
|
|
|
host, port, err := net.SplitHostPort(addr.Host)
|
|
|
|
if err != nil {
|
|
|
|
host = addr.Host
|
|
|
|
if addr.Scheme == "http" {
|
|
|
|
port = "80"
|
|
|
|
} else if addr.Scheme == "https" {
|
|
|
|
port = "443"
|
|
|
|
} else {
|
|
|
|
port = ""
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 18:43:44 +00:00
|
|
|
|
|
|
|
tags := map[string]string{"server": host, "port": port}
|
2016-06-21 13:22:51 +00:00
|
|
|
acc_nginx.AssertContainsTaggedFields(t, "nginx", fields_nginx, tags)
|
|
|
|
acc_tengine.AssertContainsTaggedFields(t, "nginx", fields_tengine, tags)
|
2015-07-27 09:28:24 +00:00
|
|
|
}
|