summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/tengine/apiclient.go
blob: 4f0251050807d974de1c0f3b587fa26a9b9cf944 (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// SPDX-License-Identifier: GPL-3.0-or-later

package tengine

import (
	"bufio"
	"fmt"
	"io"
	"net/http"
	"strconv"
	"strings"

	"github.com/netdata/netdata/go/go.d.plugin/pkg/web"
)

const (
	bytesIn               = "bytes_in"
	bytesOut              = "bytes_out"
	connTotal             = "conn_total"
	reqTotal              = "req_total"
	http2xx               = "http_2xx"
	http3xx               = "http_3xx"
	http4xx               = "http_4xx"
	http5xx               = "http_5xx"
	httpOtherStatus       = "http_other_status"
	rt                    = "rt"
	upsReq                = "ups_req"
	upsRT                 = "ups_rt"
	upsTries              = "ups_tries"
	http200               = "http_200"
	http206               = "http_206"
	http302               = "http_302"
	http304               = "http_304"
	http403               = "http_403"
	http404               = "http_404"
	http416               = "http_416"
	http499               = "http_499"
	http500               = "http_500"
	http502               = "http_502"
	http503               = "http_503"
	http504               = "http_504"
	http508               = "http_508"
	httpOtherDetailStatus = "http_other_detail_status"
	httpUps4xx            = "http_ups_4xx"
	httpUps5xx            = "http_ups_5xx"
)

var defaultLineFormat = []string{
	bytesIn,
	bytesOut,
	connTotal,
	reqTotal,
	http2xx,
	http3xx,
	http4xx,
	http5xx,
	httpOtherStatus,
	rt,
	upsReq,
	upsRT,
	upsTries,
	http200,
	http206,
	http302,
	http304,
	http403,
	http404,
	http416,
	http499,
	http500,
	http502,
	http503,
	http504,
	http508,
	httpOtherDetailStatus,
	httpUps4xx,
	httpUps5xx,
}

func newAPIClient(client *http.Client, request web.Request) *apiClient {
	return &apiClient{httpClient: client, request: request}
}

type apiClient struct {
	httpClient *http.Client
	request    web.Request
}

func (a apiClient) getStatus() (*tengineStatus, error) {
	req, err := web.NewHTTPRequest(a.request)
	if err != nil {
		return nil, fmt.Errorf("error on creating request : %v", err)
	}

	resp, err := a.doRequestOK(req)
	defer closeBody(resp)
	if err != nil {
		return nil, err
	}

	status, err := parseStatus(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("error on parsing response : %v", err)
	}

	return status, nil
}

func (a apiClient) doRequestOK(req *http.Request) (*http.Response, error) {
	resp, err := a.httpClient.Do(req)
	if err != nil {
		return nil, fmt.Errorf("error on request : %v", err)
	}
	if resp.StatusCode != http.StatusOK {
		return resp, fmt.Errorf("%s returned HTTP code %d", req.URL, resp.StatusCode)
	}
	return resp, nil
}

func closeBody(resp *http.Response) {
	if resp != nil && resp.Body != nil {
		_, _ = io.Copy(io.Discard, resp.Body)
		_ = resp.Body.Close()
	}
}

func parseStatus(r io.Reader) (*tengineStatus, error) {
	var status tengineStatus

	s := bufio.NewScanner(r)
	for s.Scan() {
		m, err := parseStatusLine(s.Text(), defaultLineFormat)
		if err != nil {
			return nil, err
		}
		status = append(status, *m)
	}

	return &status, nil
}

func parseStatusLine(line string, lineFormat []string) (*metric, error) {
	parts := strings.Split(line, ",")

	// NOTE: only default line format is supported
	// TODO: custom line format?
	// www.example.com,127.0.0.1:80,162,6242,1,1,1,0,0,0,0,10,1,10,1....
	i := findFirstInt(parts)
	if i == -1 {
		return nil, fmt.Errorf("invalid line : %s", line)
	}
	if len(parts[i:]) != len(lineFormat) {
		return nil, fmt.Errorf("invalid line length, got %d, expected %d, line : %s",
			len(parts[i:]), len(lineFormat), line)
	}

	// skip "$host,$server_addr:$server_port"
	parts = parts[i:]

	var m metric
	for i, key := range lineFormat {
		value := mustParseInt(parts[i])
		switch key {
		default:
			return nil, fmt.Errorf("unknown line format key: %s", key)
		case bytesIn:
			m.BytesIn = value
		case bytesOut:
			m.BytesOut = value
		case connTotal:
			m.ConnTotal = value
		case reqTotal:
			m.ReqTotal = value
		case http2xx:
			m.HTTP2xx = value
		case http3xx:
			m.HTTP3xx = value
		case http4xx:
			m.HTTP4xx = value
		case http5xx:
			m.HTTP5xx = value
		case httpOtherStatus:
			m.HTTPOtherStatus = value
		case rt:
			m.RT = value
		case upsReq:
			m.UpsReq = value
		case upsRT:
			m.UpsRT = value
		case upsTries:
			m.UpsTries = value
		case http200:
			m.HTTP200 = value
		case http206:
			m.HTTP206 = value
		case http302:
			m.HTTP302 = value
		case http304:
			m.HTTP304 = value
		case http403:
			m.HTTP403 = value
		case http404:
			m.HTTP404 = value
		case http416:
			m.HTTP416 = value
		case http499:
			m.HTTP499 = value
		case http500:
			m.HTTP500 = value
		case http502:
			m.HTTP502 = value
		case http503:
			m.HTTP503 = value
		case http504:
			m.HTTP504 = value
		case http508:
			m.HTTP508 = value
		case httpOtherDetailStatus:
			m.HTTPOtherDetailStatus = value
		case httpUps4xx:
			m.HTTPUps4xx = value
		case httpUps5xx:
			m.HTTPUps5xx = value
		}
	}
	return &m, nil
}

func findFirstInt(s []string) int {
	for i, v := range s {
		_, err := strconv.ParseInt(v, 10, 64)
		if err != nil {
			continue
		}
		return i
	}
	return -1
}

func mustParseInt(value string) *int64 {
	v, err := strconv.ParseInt(value, 10, 64)
	if err != nil {
		panic(err)
	}

	return &v
}