-
Notifications
You must be signed in to change notification settings - Fork 13
/
nginx_plus_api.go
133 lines (106 loc) · 2.84 KB
/
nginx_plus_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package nginxplusapi
import (
"context"
"fmt"
"net/http"
"net/url"
"sync"
"time"
"github.com/circonus-labs/circonus-unified-agent/cua"
"github.com/circonus-labs/circonus-unified-agent/internal"
"github.com/circonus-labs/circonus-unified-agent/plugins/common/tls"
"github.com/circonus-labs/circonus-unified-agent/plugins/inputs"
)
type NginxPlusAPI struct {
Urls []string `toml:"urls"`
APIVersion int64 `toml:"api_version"`
ResponseTimeout internal.Duration `toml:"response_timeout"`
tls.ClientConfig
client *http.Client
}
const (
// Default settings
defaultAPIVersion = 3
// Paths
processesPath = "processes"
connectionsPath = "connections"
sslPath = "ssl"
httpRequestsPath = "http/requests"
httpServerZonesPath = "http/server_zones"
httpLocationZonesPath = "http/location_zones"
httpUpstreamsPath = "http/upstreams"
httpCachesPath = "http/caches"
resolverZonesPath = "resolvers"
streamServerZonesPath = "stream/server_zones"
streamUpstreamsPath = "stream/upstreams"
)
var sampleConfig = `
## An array of API URI to gather stats.
urls = ["http://localhost/api"]
# Nginx API version, default: 3
# api_version = 3
# HTTP response timeout (default: 5s)
response_timeout = "5s"
## Optional TLS Config
# tls_ca = "/etc/circonus-unified-agent/ca.pem"
# tls_cert = "/etc/circonus-unified-agent/cert.pem"
# tls_key = "/etc/circonus-unified-agent/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
`
func (n *NginxPlusAPI) SampleConfig() string {
return sampleConfig
}
func (n *NginxPlusAPI) Description() string {
return "Read Nginx Plus Api documentation"
}
func (n *NginxPlusAPI) Gather(ctx context.Context, acc cua.Accumulator) error {
var wg sync.WaitGroup
// Create an HTTP client that is re-used for each
// collection interval
if n.APIVersion == 0 {
n.APIVersion = defaultAPIVersion
}
if n.client == nil {
client, err := n.createHTTPClient()
if err != nil {
return err
}
n.client = client
}
for _, u := range n.Urls {
addr, err := url.Parse(u)
if err != nil {
acc.AddError(fmt.Errorf("Unable to parse address '%s': %w", u, err))
continue
}
wg.Add(1)
go func(addr *url.URL) {
defer wg.Done()
n.gatherMetrics(addr, acc)
}(addr)
}
wg.Wait()
return nil
}
func (n *NginxPlusAPI) createHTTPClient() (*http.Client, error) {
if n.ResponseTimeout.Duration < time.Second {
n.ResponseTimeout.Duration = time.Second * 5
}
tlsConfig, err := n.ClientConfig.TLSConfig()
if err != nil {
return nil, fmt.Errorf("TLSConfig: %w", err)
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
Timeout: n.ResponseTimeout.Duration,
}
return client, nil
}
func init() {
inputs.Add("nginx_plus_api", func() cua.Input {
return &NginxPlusAPI{}
})
}