forked from mackerelio/mackerel-agent-plugins
/
nginx.go
194 lines (174 loc) · 4.82 KB
/
nginx.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package mpnginx
import (
"bufio"
"flag"
"fmt"
"io"
"errors"
"net/http"
"regexp"
"strconv"
"strings"
mp "github.com/mackerelio/go-mackerel-plugin-helper"
)
var graphdef = map[string]mp.Graphs{
"nginx.connections": {
Label: "Nginx Connections",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "connections", Label: "Active connections", Diff: false},
},
},
"nginx.requests": {
Label: "Nginx requests",
Unit: "float",
Metrics: []mp.Metrics{
{Name: "accepts", Label: "Accepted connections", Diff: true, Type: "uint64"},
{Name: "handled", Label: "Handled connections", Diff: true, Type: "uint64"},
{Name: "requests", Label: "Handled requests", Diff: true, Type: "uint64"},
},
},
"nginx.queue": {
Label: "Nginx connection status",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "reading", Label: "Reading", Diff: false},
{Name: "writing", Label: "Writing", Diff: false},
{Name: "waiting", Label: "Waiting", Diff: false},
},
},
}
type stringSlice []string
func (s *stringSlice) Set(v string) error {
*s = append(*s, v)
return nil
}
func (s *stringSlice) String() string {
return fmt.Sprintf("%v", *s)
}
// NginxPlugin mackerel plugin for Nginx
type NginxPlugin struct {
URI string
Header stringSlice
}
// % wget -qO- http://localhost:8080/nginx_status
// Active connections: 123
// server accepts handled requests
// 1693613501 1693613501 7996986318
// Reading: 66 Writing: 16 Waiting: 41
// FetchMetrics interface for mackerelplugin
func (n NginxPlugin) FetchMetrics() (map[string]interface{}, error) {
req, err := http.NewRequest("GET", n.URI, nil)
if err != nil {
return nil, err
}
for _, h := range n.Header {
kv := strings.SplitN(h, ":", 2)
var k, v string
k = strings.TrimSpace(kv[0])
if len(kv) == 2 {
v = strings.TrimSpace(kv[1])
}
if http.CanonicalHeaderKey(k) == "Host" {
req.Host = v
} else {
req.Header.Set(k, v)
}
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return n.parseStats(resp.Body)
}
func (n NginxPlugin) parseStats(body io.Reader) (map[string]interface{}, error) {
stat := make(map[string]interface{})
r := bufio.NewReader(body)
line, _, err := r.ReadLine()
if err != nil {
return nil, errors.New("cannot get values")
}
re := regexp.MustCompile("Active connections: ([0-9]+)")
res := re.FindStringSubmatch(string(line))
if res == nil || len(res) != 2 {
return nil, errors.New("cannot get values")
}
stat["connections"], err = strconv.ParseFloat(res[1], 64)
if err != nil {
return nil, errors.New("cannot get values")
}
line, _, err = r.ReadLine()
if err != nil {
return nil, errors.New("cannot get values")
}
line, _, err = r.ReadLine()
if err != nil {
return nil, errors.New("cannot get values")
}
re = regexp.MustCompile("([0-9]+) ([0-9]+) ([0-9]+)")
res = re.FindStringSubmatch(string(line))
if res == nil || len(res) != 4 {
return nil, errors.New("cannot get values")
}
stat["accepts"], err = strconv.ParseFloat(res[1], 64)
if err != nil {
return nil, errors.New("cannot get values")
}
stat["handled"], err = strconv.ParseFloat(res[2], 64)
if err != nil {
return nil, errors.New("cannot get values")
}
stat["requests"], err = strconv.ParseFloat(res[3], 64)
if err != nil {
return nil, errors.New("cannot get values")
}
line, _, err = r.ReadLine()
if err != nil {
return nil, errors.New("cannot get values")
}
re = regexp.MustCompile("Reading: ([0-9]+) Writing: ([0-9]+) Waiting: ([0-9]+)")
res = re.FindStringSubmatch(string(line))
if res == nil || len(res) != 4 {
return nil, errors.New("cannot get values")
}
stat["reading"], err = strconv.ParseFloat(res[1], 64)
if err != nil {
return nil, errors.New("cannot get values")
}
stat["writing"], err = strconv.ParseFloat(res[2], 64)
if err != nil {
return nil, errors.New("cannot get values")
}
stat["waiting"], err = strconv.ParseFloat(res[3], 64)
if err != nil {
return nil, errors.New("cannot get values")
}
return stat, nil
}
// GraphDefinition interface for mackerelplugin
func (n NginxPlugin) GraphDefinition() map[string]mp.Graphs {
return graphdef
}
// Do the plugin
func Do() {
optURI := flag.String("uri", "", "URI")
optScheme := flag.String("scheme", "http", "Scheme")
optHost := flag.String("host", "localhost", "Hostname")
optPort := flag.String("port", "8080", "Port")
optPath := flag.String("path", "/nginx_status", "Path")
optTempfile := flag.String("tempfile", "", "Temp file name")
optHeader := &stringSlice{}
flag.Var(optHeader, "header", "Set http header (e.g. \"Host: servername\")")
flag.Parse()
var nginx NginxPlugin
if *optURI != "" {
nginx.URI = *optURI
} else {
nginx.URI = fmt.Sprintf("%s://%s:%s%s", *optScheme, *optHost, *optPort, *optPath)
}
nginx.Header = *optHeader
helper := mp.NewMackerelPlugin(nginx)
helper.Tempfile = *optTempfile
helper.Run()
}