-
Notifications
You must be signed in to change notification settings - Fork 20
/
phpfpm_exporter.go
348 lines (307 loc) · 10.3 KB
/
phpfpm_exporter.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright 2017 Kumina, https://kumina.nl/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"os"
"path"
"regexp"
"strconv"
"strings"
"time"
"path/filepath"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
client_model "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/version"
"github.com/tomasen/fcgi_client"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
phpfpmSocketPathLabel = "socket_path"
phpfpmScriptPathLabel = "script_path"
phpfpmUpDesc = prometheus.NewDesc(
prometheus.BuildFQName("php", "fpm", "up"),
"Whether scraping PHP-FPM's metrics was successful.",
[]string{phpfpmSocketPathLabel}, nil)
phpfpmAcceptedConnections = prometheus.NewDesc(
prometheus.BuildFQName("php", "fpm", "accepted_connections_total"),
"Number of request accepted by the pool.",
[]string{phpfpmSocketPathLabel}, nil)
phpfpmStartTime = prometheus.NewDesc(
prometheus.BuildFQName("php", "fpm", "start_time_seconds"),
"Unix time when FPM has started or reloaded.",
[]string{phpfpmSocketPathLabel}, nil)
phpfpmGauges = map[string]*prometheus.Desc{
"listen queue": prometheus.NewDesc(
prometheus.BuildFQName("php", "fpm", "listen_queue"),
"Number of request in the queue of pending connections.",
[]string{phpfpmSocketPathLabel}, nil),
"max listen queue": prometheus.NewDesc(
prometheus.BuildFQName("php", "fpm", "max_listen_queue"),
"Maximum number of requests in the queue of pending connections since FPM has started.",
[]string{phpfpmSocketPathLabel}, nil),
"listen queue len": prometheus.NewDesc(
prometheus.BuildFQName("php", "fpm", "listen_queue_length"),
"The size of the socket queue of pending connections.",
[]string{phpfpmSocketPathLabel}, nil),
"idle processes": prometheus.NewDesc(
prometheus.BuildFQName("php", "fpm", "idle_processes"),
"Number of idle processes.",
[]string{phpfpmSocketPathLabel}, nil),
"active processes": prometheus.NewDesc(
prometheus.BuildFQName("php", "fpm", "active_processes"),
"Number of active processes.",
[]string{phpfpmSocketPathLabel}, nil),
"total processes": prometheus.NewDesc(
prometheus.BuildFQName("php", "fpm", "total_processes"),
"Number of total processes.",
[]string{phpfpmSocketPathLabel}, nil),
"max active processes": prometheus.NewDesc(
prometheus.BuildFQName("php", "fpm", "max_active_processes"),
"Maximum number of active processes since FPM has started.",
[]string{phpfpmSocketPathLabel}, nil),
"max children reached": prometheus.NewDesc(
prometheus.BuildFQName("php", "fpm", "max_children_reached"),
"Number of times, the process limit has been reached.",
[]string{phpfpmSocketPathLabel}, nil),
"slow requests": prometheus.NewDesc(
prometheus.BuildFQName("php", "fpm", "slow_requests"),
"Enable php-fpm slow-log before you consider this. If this value is non-zero you may have slow php processes.",
[]string{phpfpmSocketPathLabel}, nil),
}
)
func CollectStatusFromReader(reader io.Reader, socketPath string, ch chan<- prometheus.Metric) error {
scanner := bufio.NewScanner(reader)
re := regexp.MustCompile("^(.*): +(.*)$")
// Scrape the interesting values:
for scanner.Scan() {
fields := re.FindStringSubmatch(scanner.Text())
if fields == nil {
return fmt.Errorf("Failed to parse %s", scanner.Text())
}
if gauge, ok := phpfpmGauges[fields[1]]; ok {
f, err := strconv.ParseFloat(fields[2], 64)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
gauge,
prometheus.GaugeValue,
f,
socketPath)
} else if fields[1] == "accepted conn" {
f, err := strconv.ParseFloat(fields[2], 64)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
phpfpmAcceptedConnections,
prometheus.CounterValue,
f,
socketPath)
} else if fields[1] == "start time" {
location, err := time.LoadLocation("Local")
if err != nil {
return err
}
since, err := time.ParseInLocation("02/Jan/2006:15:04:05 -0700", fields[2], location)
if err != nil {
return err
}
f := float64(since.Unix())
ch <- prometheus.MustNewConstMetric(
phpfpmStartTime,
prometheus.GaugeValue,
f,
socketPath)
}
}
return nil
}
func CollectStatusFromSocket(path *SocketPath, statusPath string, ch chan<- prometheus.Metric) error {
env := make(map[string]string)
env["SCRIPT_FILENAME"] = statusPath
env["SCRIPT_NAME"] = statusPath
env["REQUEST_METHOD"] = "GET"
fcgi, err := fcgiclient.Dial(path.Network, path.Address)
if err != nil {
return err
}
defer fcgi.Close()
resp, err := fcgi.Get(env)
if err != nil {
return err
}
return CollectStatusFromReader(resp.Body, path.FormatStr(), ch)
}
func CollectMetricsFromScript(socketPaths []*SocketPath, scriptPaths []string) ([]*client_model.MetricFamily, error) {
var result []*client_model.MetricFamily
for _, socketPath := range socketPaths {
for _, scriptPath := range scriptPaths {
fcgi, err := fcgiclient.Dial(socketPath.Network, socketPath.Address)
if err != nil {
return result, err
}
defer fcgi.Close()
env := make(map[string]string)
env["DOCUMENT_ROOT"] = path.Dir(scriptPath)
env["SCRIPT_FILENAME"] = scriptPath
env["SCRIPT_NAME"] = path.Base(scriptPath)
env["REQUEST_METHOD"] = "GET"
resp, err := fcgi.Get(env)
if err != nil {
return result, err
}
var parser expfmt.TextParser
metricFamilies, err := parser.TextToMetricFamilies(resp.Body)
if err != nil {
return result, err
}
for _, metricFamily := range metricFamilies {
for _, metric := range metricFamily.Metric {
socketPathCopy := socketPath.FormatStr()
scriptPathCopy := scriptPath
metric.Label = append(
metric.Label,
&client_model.LabelPair{
Name: &phpfpmSocketPathLabel,
Value: &socketPathCopy,
},
&client_model.LabelPair{
Name: &phpfpmScriptPathLabel,
Value: &scriptPathCopy,
})
}
result = append(result, metricFamily)
}
}
}
return result, nil
}
type PhpfpmExporter struct {
socketPaths []*SocketPath
statusPath string
}
func NewPhpfpmExporter(socketPaths []*SocketPath, statusPath string) (*PhpfpmExporter, error) {
return &PhpfpmExporter{
socketPaths: socketPaths,
statusPath: statusPath,
}, nil
}
func (e *PhpfpmExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- phpfpmUpDesc
ch <- phpfpmAcceptedConnections
ch <- phpfpmStartTime
for _, desc := range phpfpmGauges {
ch <- desc
}
}
func (e *PhpfpmExporter) Collect(ch chan<- prometheus.Metric) {
for _, socketPath := range e.socketPaths {
err := CollectStatusFromSocket(socketPath, e.statusPath, ch)
if err == nil {
ch <- prometheus.MustNewConstMetric(
phpfpmUpDesc,
prometheus.GaugeValue,
1.0,
socketPath.FormatStr())
} else {
log.Printf("Failed to scrape socket: %s", err)
ch <- prometheus.MustNewConstMetric(
phpfpmUpDesc,
prometheus.GaugeValue,
0.0,
socketPath.FormatStr())
}
}
}
type SocketPath struct {
Network string
Address string
}
func (s *SocketPath) FormatStr() string {
return s.Network + "://" + s.Address
}
func NewSocketPath(socketPath string) *SocketPath {
i := strings.Index(socketPath, "://")
if i < 0 {
return &SocketPath{"unix", socketPath}
}
network := socketPath[:i]
address := socketPath[i+3:]
return &SocketPath{network, address}
}
func main() {
var (
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9253").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
socketPaths = kingpin.Flag("phpfpm.socket-paths", "Paths of the PHP-FPM sockets.").Strings()
socketDirectories = kingpin.Flag("phpfpm.socket-directories", "Path(s) of the directory where PHP-FPM sockets are located.").Strings()
statusPath = kingpin.Flag("phpfpm.status-path", "Path which has been configured in PHP-FPM to show status page.").Default("/status").String()
scriptCollectorPaths = kingpin.Flag("phpfpm.script-collector-paths", "Paths of the PHP file whose output needs to be collected.").Strings()
showVersion = kingpin.Flag("version", "Print version information.").Bool()
)
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.Parse()
var sockets []*SocketPath
for _, socketDirectory := range *socketDirectories {
_ = filepath.Walk(socketDirectory, func(path string, info os.FileInfo, err error) error {
if err == nil && info.Mode()&os.ModeSocket != 0 {
sockets = append(sockets, NewSocketPath(path))
}
return nil
})
}
for _, socket := range *socketPaths {
sockets = append(sockets, NewSocketPath(socket))
}
if *showVersion {
fmt.Println(version.Print("phpfpm_exporter"))
os.Exit(0)
}
exporter, err := NewPhpfpmExporter(sockets, *statusPath)
if err != nil {
panic(err)
}
prometheus.MustRegister(exporter)
gatherer := prometheus.DefaultGatherer
if len(*scriptCollectorPaths) != 0 {
gatherer = prometheus.Gatherers{
prometheus.DefaultGatherer,
prometheus.GathererFunc(func() ([]*client_model.MetricFamily, error) {
return CollectMetricsFromScript(sockets, *scriptCollectorPaths)
}),
}
}
log.Println("Starting phpfpm_exporter", version.Info())
log.Println("Build context", version.BuildContext())
log.Printf("Starting Server: %s", *listenAddress)
http.Handle(*metricsPath, promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`
<html>
<head><title>PHP-FPM Exporter</title></head>
<body>
<h1>PHP-FPM Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}