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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package phpfpm
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/netdata/netdata/go/go.d.plugin/pkg/web"
fcgiclient "github.com/tomasen/fcgi_client"
)
type (
status struct {
Active int64 `json:"active processes" stm:"active"`
MaxActive int64 `json:"max active processes" stm:"maxActive"`
Idle int64 `json:"idle processes" stm:"idle"`
Requests int64 `json:"accepted conn" stm:"requests"`
Reached int64 `json:"max children reached" stm:"reached"`
Slow int64 `json:"slow requests" stm:"slow"`
Processes []proc `json:"processes"`
}
proc struct {
PID int64 `json:"pid"`
State string `json:"state"`
Duration requestDuration `json:"request duration"`
CPU float64 `json:"last request cpu"`
Memory int64 `json:"last request memory"`
}
requestDuration int64
)
// UnmarshalJSON customise JSON for timestamp.
func (rd *requestDuration) UnmarshalJSON(b []byte) error {
if rdc, err := strconv.Atoi(string(b)); err != nil {
*rd = 0
} else {
*rd = requestDuration(rdc)
}
return nil
}
type client interface {
getStatus() (*status, error)
}
type httpClient struct {
client *http.Client
req web.Request
dec decoder
}
func newHTTPClient(c *http.Client, r web.Request) (*httpClient, error) {
u, err := url.Parse(r.URL)
if err != nil {
return nil, err
}
dec := decodeText
if _, ok := u.Query()["json"]; ok {
dec = decodeJSON
}
return &httpClient{
client: c,
req: r,
dec: dec,
}, nil
}
func (c *httpClient) getStatus() (*status, error) {
req, err := web.NewHTTPRequest(c.req)
if err != nil {
return nil, fmt.Errorf("error on creating HTTP request: %v", err)
}
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error on HTTP request to '%s': %v", req.URL, err)
}
defer func() {
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s returned HTTP status %d", req.URL, resp.StatusCode)
}
st := &status{}
if err := c.dec(resp.Body, st); err != nil {
return nil, fmt.Errorf("error parsing HTTP response from '%s': %v", req.URL, err)
}
return st, nil
}
type socketClient struct {
socket string
timeout time.Duration
env map[string]string
}
func newSocketClient(socket string, timeout time.Duration, fcgiPath string) *socketClient {
return &socketClient{
socket: socket,
timeout: timeout,
env: map[string]string{
"SCRIPT_NAME": fcgiPath,
"SCRIPT_FILENAME": fcgiPath,
"SERVER_SOFTWARE": "go / fcgiclient ",
"REMOTE_ADDR": "127.0.0.1",
"QUERY_STRING": "json&full",
"REQUEST_METHOD": "GET",
"CONTENT_TYPE": "application/json",
},
}
}
func (c *socketClient) getStatus() (*status, error) {
socket, err := fcgiclient.DialTimeout("unix", c.socket, c.timeout)
if err != nil {
return nil, fmt.Errorf("error on connecting to socket '%s': %v", c.socket, err)
}
defer socket.Close()
resp, err := socket.Get(c.env)
if err != nil {
return nil, fmt.Errorf("error on getting data from socket '%s': %v", c.socket, err)
}
content, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error on reading response from socket '%s': %v", c.socket, err)
}
st := &status{}
if err := json.Unmarshal(content, st); err != nil {
return nil, fmt.Errorf("error on decoding response from socket '%s': %v", c.socket, err)
}
return st, nil
}
type tcpClient struct {
address string
timeout time.Duration
env map[string]string
}
func newTcpClient(address string, timeout time.Duration, fcgiPath string) *tcpClient {
return &tcpClient{
address: address,
timeout: timeout,
env: map[string]string{
"SCRIPT_NAME": fcgiPath,
"SCRIPT_FILENAME": fcgiPath,
"SERVER_SOFTWARE": "go / fcgiclient ",
"REMOTE_ADDR": "127.0.0.1",
"QUERY_STRING": "json&full",
"REQUEST_METHOD": "GET",
"CONTENT_TYPE": "application/json",
},
}
}
func (c *tcpClient) getStatus() (*status, error) {
client, err := fcgiclient.DialTimeout("tcp", c.address, c.timeout)
if err != nil {
return nil, fmt.Errorf("error on connecting to address '%s': %v", c.address, err)
}
defer client.Close()
resp, err := client.Get(c.env)
if err != nil {
return nil, fmt.Errorf("error on getting data from address '%s': %v", c.address, err)
}
content, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error on reading response from address '%s': %v", c.address, err)
}
st := &status{}
if err := json.Unmarshal(content, st); err != nil {
return nil, fmt.Errorf("error on decoding response from address '%s': %v", c.address, err)
}
return st, nil
}
|