2015-10-12 01:36:21 +00:00
|
|
|
package phpfpm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-01-14 22:20:59 +00:00
|
|
|
"os"
|
2015-10-12 01:36:21 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2016-01-20 20:19:03 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-10-12 01:36:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PF_POOL = "pool"
|
|
|
|
PF_PROCESS_MANAGER = "process manager"
|
|
|
|
PF_ACCEPTED_CONN = "accepted conn"
|
|
|
|
PF_LISTEN_QUEUE = "listen queue"
|
|
|
|
PF_MAX_LISTEN_QUEUE = "max listen queue"
|
|
|
|
PF_LISTEN_QUEUE_LEN = "listen queue len"
|
|
|
|
PF_IDLE_PROCESSES = "idle processes"
|
|
|
|
PF_ACTIVE_PROCESSES = "active processes"
|
|
|
|
PF_TOTAL_PROCESSES = "total processes"
|
|
|
|
PF_MAX_ACTIVE_PROCESSES = "max active processes"
|
|
|
|
PF_MAX_CHILDREN_REACHED = "max children reached"
|
|
|
|
PF_SLOW_REQUESTS = "slow requests"
|
|
|
|
)
|
|
|
|
|
|
|
|
type metric map[string]int64
|
|
|
|
type poolStat map[string]metric
|
|
|
|
|
|
|
|
type phpfpm struct {
|
|
|
|
Urls []string
|
|
|
|
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2015-10-15 21:53:29 +00:00
|
|
|
# An array of addresses to gather stats about. Specify an ip or hostname
|
2016-01-14 22:20:59 +00:00
|
|
|
# with optional port and path
|
2015-10-15 21:53:29 +00:00
|
|
|
#
|
2016-01-14 22:20:59 +00:00
|
|
|
# Plugin can be configured in three modes (either can be used):
|
|
|
|
# - http: the URL must start with http:// or https://, ie:
|
2015-10-15 21:53:29 +00:00
|
|
|
# "http://localhost/status"
|
|
|
|
# "http://192.168.130.1/status?full"
|
2016-01-14 22:20:59 +00:00
|
|
|
#
|
|
|
|
# - unixsocket: path to fpm socket, ie:
|
2015-10-15 21:53:29 +00:00
|
|
|
# "/var/run/php5-fpm.sock"
|
2016-01-14 22:20:59 +00:00
|
|
|
# or using a custom fpm status path:
|
|
|
|
# "/var/run/php5-fpm.sock:fpm-custom-status-path"
|
|
|
|
#
|
|
|
|
# - fcgi: the URL must start with fcgi:// or cgi://, and port must be present, ie:
|
2015-11-12 11:07:38 +00:00
|
|
|
# "fcgi://10.0.0.12:9000/status"
|
|
|
|
# "cgi://10.0.10.12:9001/status"
|
2015-10-15 21:53:29 +00:00
|
|
|
#
|
2016-01-14 22:20:59 +00:00
|
|
|
# Example of multiple gathering from local socket and remove host
|
|
|
|
# urls = ["http://192.168.1.20/status", "/tmp/fpm.sock"]
|
|
|
|
# If no servers are specified, then default to http://127.0.0.1/status
|
2015-10-15 21:53:29 +00:00
|
|
|
urls = ["http://localhost/status"]
|
2015-10-12 01:36:21 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (r *phpfpm) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *phpfpm) Description() string {
|
2016-01-14 22:20:59 +00:00
|
|
|
return "Read metrics of phpfpm, via HTTP status page or socket"
|
2015-10-12 01:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reads stats from all configured servers accumulates stats.
|
|
|
|
// Returns one of the errors encountered while gather stats (if any).
|
2016-01-07 20:39:43 +00:00
|
|
|
func (g *phpfpm) Gather(acc inputs.Accumulator) error {
|
2015-10-12 01:36:21 +00:00
|
|
|
if len(g.Urls) == 0 {
|
|
|
|
return g.gatherServer("http://127.0.0.1/status", acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
var outerr error
|
|
|
|
|
|
|
|
for _, serv := range g.Urls {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(serv string) {
|
|
|
|
defer wg.Done()
|
|
|
|
outerr = g.gatherServer(serv, acc)
|
|
|
|
}(serv)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return outerr
|
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
// Request status page to get stat raw data and import it
|
2016-01-07 20:39:43 +00:00
|
|
|
func (g *phpfpm) gatherServer(addr string, acc inputs.Accumulator) error {
|
2015-10-12 01:36:21 +00:00
|
|
|
if g.client == nil {
|
|
|
|
client := &http.Client{}
|
|
|
|
g.client = client
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(addr, "http://") || strings.HasPrefix(addr, "https://") {
|
2016-01-14 22:20:59 +00:00
|
|
|
return g.gatherHttp(addr, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
fcgi *conn
|
|
|
|
socketPath string
|
|
|
|
statusPath string
|
|
|
|
)
|
|
|
|
|
|
|
|
if strings.HasPrefix(addr, "fcgi://") || strings.HasPrefix(addr, "cgi://") {
|
2015-10-12 01:36:21 +00:00
|
|
|
u, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable parse server address '%s': %s", addr, err)
|
|
|
|
}
|
2016-01-14 22:20:59 +00:00
|
|
|
socketAddr := strings.Split(u.Host, ":")
|
|
|
|
fcgiIp := socketAddr[0]
|
|
|
|
fcgiPort, _ := strconv.Atoi(socketAddr[1])
|
|
|
|
fcgi, _ = NewClient(fcgiIp, fcgiPort)
|
|
|
|
} else {
|
|
|
|
socketAddr := strings.Split(addr, ":")
|
|
|
|
if len(socketAddr) >= 2 {
|
|
|
|
socketPath = socketAddr[0]
|
|
|
|
statusPath = socketAddr[1]
|
|
|
|
} else {
|
|
|
|
socketPath = socketAddr[0]
|
|
|
|
statusPath = "status"
|
2015-10-12 01:36:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
if _, err := os.Stat(socketPath); os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("Socket doesn't exist '%s': %s", socketPath, err)
|
2015-10-12 01:36:21 +00:00
|
|
|
}
|
2016-01-14 22:20:59 +00:00
|
|
|
fcgi, _ = NewClient("unix", socketPath)
|
|
|
|
}
|
|
|
|
return g.gatherFcgi(fcgi, statusPath, acc)
|
|
|
|
}
|
2015-10-12 01:36:21 +00:00
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
// Gather stat using fcgi protocol
|
|
|
|
func (g *phpfpm) gatherFcgi(fcgi *conn, statusPath string, acc inputs.Accumulator) error {
|
|
|
|
fpmOutput, fpmErr, err := fcgi.Request(map[string]string{
|
|
|
|
"SCRIPT_NAME": "/" + statusPath,
|
|
|
|
"SCRIPT_FILENAME": statusPath,
|
|
|
|
"REQUEST_METHOD": "GET",
|
|
|
|
"CONTENT_LENGTH": "0",
|
|
|
|
"SERVER_PROTOCOL": "HTTP/1.0",
|
|
|
|
"SERVER_SOFTWARE": "go / fcgiclient ",
|
|
|
|
"REMOTE_ADDR": "127.0.0.1",
|
|
|
|
}, "/"+statusPath)
|
|
|
|
|
|
|
|
if len(fpmErr) == 0 && err == nil {
|
|
|
|
importMetric(bytes.NewReader(fpmOutput), acc)
|
|
|
|
return nil
|
2015-10-12 01:36:21 +00:00
|
|
|
} else {
|
2016-01-14 22:20:59 +00:00
|
|
|
return fmt.Errorf("Unable parse phpfpm status. Error: %v %v", string(fpmErr), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather stat using http protocol
|
|
|
|
func (g *phpfpm) gatherHttp(addr string, acc inputs.Accumulator) error {
|
|
|
|
u, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable parse server address '%s': %s", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s://%s%s", u.Scheme,
|
|
|
|
u.Host, u.Path), nil)
|
|
|
|
res, err := g.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to connect to phpfpm status page '%s': %v",
|
|
|
|
addr, err)
|
|
|
|
}
|
2015-10-12 01:36:21 +00:00
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("Unable to get valid stat result from '%s': %v",
|
|
|
|
addr, err)
|
2015-10-12 01:36:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
importMetric(res.Body, acc)
|
2015-10-12 01:36:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
// Import stat data into Telegraf system
|
|
|
|
func importMetric(r io.Reader, acc inputs.Accumulator) (poolStat, error) {
|
2015-10-12 01:36:21 +00:00
|
|
|
stats := make(poolStat)
|
|
|
|
var currentPool string
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
statLine := scanner.Text()
|
|
|
|
keyvalue := strings.Split(statLine, ":")
|
|
|
|
|
|
|
|
if len(keyvalue) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fieldName := strings.Trim(keyvalue[0], " ")
|
|
|
|
// We start to gather data for a new pool here
|
|
|
|
if fieldName == PF_POOL {
|
|
|
|
currentPool = strings.Trim(keyvalue[1], " ")
|
|
|
|
stats[currentPool] = make(metric)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start to parse metric for current pool
|
|
|
|
switch fieldName {
|
|
|
|
case PF_ACCEPTED_CONN,
|
|
|
|
PF_LISTEN_QUEUE,
|
|
|
|
PF_MAX_LISTEN_QUEUE,
|
|
|
|
PF_LISTEN_QUEUE_LEN,
|
|
|
|
PF_IDLE_PROCESSES,
|
|
|
|
PF_ACTIVE_PROCESSES,
|
|
|
|
PF_TOTAL_PROCESSES,
|
|
|
|
PF_MAX_ACTIVE_PROCESSES,
|
|
|
|
PF_MAX_CHILDREN_REACHED,
|
|
|
|
PF_SLOW_REQUESTS:
|
|
|
|
fieldValue, err := strconv.ParseInt(strings.Trim(keyvalue[1], " "), 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
stats[currentPool][fieldName] = fieldValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we push the pool metric
|
|
|
|
for pool := range stats {
|
|
|
|
tags := map[string]string{
|
|
|
|
"pool": pool,
|
|
|
|
}
|
2015-12-19 00:09:01 +00:00
|
|
|
fields := make(map[string]interface{})
|
2015-10-12 01:36:21 +00:00
|
|
|
for k, v := range stats[pool] {
|
2015-12-19 00:09:01 +00:00
|
|
|
fields[strings.Replace(k, " ", "_", -1)] = v
|
2015-10-12 01:36:21 +00:00
|
|
|
}
|
2015-12-19 00:09:01 +00:00
|
|
|
acc.AddFields("phpfpm", fields, tags)
|
2015-10-12 01:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-07 20:39:43 +00:00
|
|
|
inputs.Add("phpfpm", func() inputs.Input {
|
2015-10-12 01:36:21 +00:00
|
|
|
return &phpfpm{}
|
|
|
|
})
|
|
|
|
}
|