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-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2019-04-17 00:56:56 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2020-02-28 22:47:04 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/globpath"
|
2019-04-17 00:56:56 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/tls"
|
2016-01-20 18:57:35 +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"
|
2019-06-17 19:55:09 +00:00
|
|
|
PF_START_SINCE = "start since"
|
2015-10-12 01:36:21 +00:00
|
|
|
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 {
|
2019-04-17 00:56:56 +00:00
|
|
|
Urls []string
|
|
|
|
Timeout internal.Duration
|
|
|
|
tls.ClientConfig
|
2015-10-12 01:36:21 +00:00
|
|
|
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## An array of addresses to gather stats about. Specify an ip or hostname
|
|
|
|
## with optional port and path
|
|
|
|
##
|
|
|
|
## Plugin can be configured in three modes (either can be used):
|
|
|
|
## - http: the URL must start with http:// or https://, ie:
|
|
|
|
## "http://localhost/status"
|
|
|
|
## "http://192.168.130.1/status?full"
|
|
|
|
##
|
|
|
|
## - unixsocket: path to fpm socket, ie:
|
|
|
|
## "/var/run/php5-fpm.sock"
|
|
|
|
## 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:
|
|
|
|
## "fcgi://10.0.0.12:9000/status"
|
|
|
|
## "cgi://10.0.10.12:9001/status"
|
|
|
|
##
|
2019-04-17 00:56:56 +00:00
|
|
|
## Example of multiple gathering from local socket and remote host
|
2016-02-18 21:26:51 +00:00
|
|
|
## urls = ["http://192.168.1.20/status", "/tmp/fpm.sock"]
|
2015-10-15 21:53:29 +00:00
|
|
|
urls = ["http://localhost/status"]
|
2019-04-17 00:56:56 +00:00
|
|
|
|
|
|
|
## Duration allowed to complete HTTP requests.
|
|
|
|
# timeout = "5s"
|
|
|
|
|
|
|
|
## Optional TLS Config
|
|
|
|
# tls_ca = "/etc/telegraf/ca.pem"
|
|
|
|
# tls_cert = "/etc/telegraf/cert.pem"
|
|
|
|
# tls_key = "/etc/telegraf/key.pem"
|
|
|
|
## Use TLS but skip chain & host verification
|
|
|
|
# insecure_skip_verify = false
|
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-27 21:21:36 +00:00
|
|
|
func (g *phpfpm) Gather(acc telegraf.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
|
|
|
|
|
2020-02-28 22:47:04 +00:00
|
|
|
urls, err := expandUrls(g.Urls)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, serv := range urls {
|
2015-10-12 01:36:21 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(serv string) {
|
|
|
|
defer wg.Done()
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(g.gatherServer(serv, acc))
|
2015-10-12 01:36:21 +00:00
|
|
|
}(serv)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-04-24 18:13:26 +00:00
|
|
|
return nil
|
2015-10-12 01:36:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
// Request status page to get stat raw data and import it
|
2016-01-27 21:21:36 +00:00
|
|
|
func (g *phpfpm) gatherServer(addr string, acc telegraf.Accumulator) error {
|
2015-10-12 01:36:21 +00:00
|
|
|
if g.client == nil {
|
2019-04-17 00:56:56 +00:00
|
|
|
tlsCfg, err := g.ClientConfig.TLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: tlsCfg,
|
|
|
|
}
|
|
|
|
g.client = &http.Client{
|
|
|
|
Transport: tr,
|
|
|
|
Timeout: g.Timeout.Duration,
|
|
|
|
}
|
2015-10-12 01:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2016-03-09 12:11:59 +00:00
|
|
|
var err error
|
2016-01-14 22:20:59 +00:00
|
|
|
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])
|
2016-03-09 12:11:59 +00:00
|
|
|
fcgi, err = newFcgiClient(fcgiIp, fcgiPort)
|
2016-10-12 10:58:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-25 15:24:46 +00:00
|
|
|
if len(u.Path) > 1 {
|
|
|
|
statusPath = strings.Trim(u.Path, "/")
|
|
|
|
} else {
|
|
|
|
statusPath = "status"
|
|
|
|
}
|
2016-01-14 22:20:59 +00:00
|
|
|
} else {
|
2020-02-28 22:47:04 +00:00
|
|
|
socketPath, statusPath = unixSocketPaths(addr)
|
|
|
|
if statusPath == "" {
|
2016-01-14 22:20:59 +00:00
|
|
|
statusPath = "status"
|
2015-10-12 01:36:21 +00:00
|
|
|
}
|
2016-03-09 12:11:59 +00:00
|
|
|
fcgi, err = newFcgiClient("unix", socketPath)
|
2016-01-14 22:20:59 +00:00
|
|
|
}
|
2016-03-09 12:11:59 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-23 18:50:52 +00:00
|
|
|
return g.gatherFcgi(fcgi, statusPath, acc, addr)
|
2016-01-14 22:20:59 +00:00
|
|
|
}
|
2015-10-12 01:36:21 +00:00
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
// Gather stat using fcgi protocol
|
2018-03-23 18:50:52 +00:00
|
|
|
func (g *phpfpm) gatherFcgi(fcgi *conn, statusPath string, acc telegraf.Accumulator, addr string) error {
|
2016-01-14 22:20:59 +00:00
|
|
|
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 {
|
2018-03-23 18:50:52 +00:00
|
|
|
importMetric(bytes.NewReader(fpmOutput), acc, addr)
|
2016-01-14 22:20:59 +00:00
|
|
|
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
|
2016-01-27 21:21:36 +00:00
|
|
|
func (g *phpfpm) gatherHttp(addr string, acc telegraf.Accumulator) error {
|
2016-01-14 22:20:59 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-04-26 15:24:32 +00:00
|
|
|
defer res.Body.Close()
|
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
|
|
|
}
|
|
|
|
|
2018-03-23 18:50:52 +00:00
|
|
|
importMetric(res.Body, acc, addr)
|
2015-10-12 01:36:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
// Import stat data into Telegraf system
|
2018-03-23 18:50:52 +00:00
|
|
|
func importMetric(r io.Reader, acc telegraf.Accumulator, addr string) (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 {
|
2019-06-17 19:55:09 +00:00
|
|
|
case PF_START_SINCE,
|
|
|
|
PF_ACCEPTED_CONN,
|
2015-10-12 01:36:21 +00:00
|
|
|
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,
|
2018-03-23 18:50:52 +00:00
|
|
|
"url": addr,
|
2015-10-12 01:36:21 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-28 22:47:04 +00:00
|
|
|
func expandUrls(urls []string) ([]string, error) {
|
|
|
|
addrs := make([]string, 0, len(urls))
|
|
|
|
for _, url := range urls {
|
|
|
|
if isNetworkURL(url) {
|
|
|
|
addrs = append(addrs, url)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
paths, err := globUnixSocket(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addrs = append(addrs, paths...)
|
|
|
|
}
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func globUnixSocket(url string) ([]string, error) {
|
|
|
|
pattern, status := unixSocketPaths(url)
|
|
|
|
glob, err := globpath.Compile(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not compile glob %q: %v", pattern, err)
|
|
|
|
}
|
|
|
|
paths := glob.Match()
|
|
|
|
if len(paths) == 0 {
|
|
|
|
if _, err := os.Stat(paths[0]); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("Socket doesn't exist '%s': %s", pattern, err)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs := make([]string, 0, len(paths))
|
|
|
|
|
|
|
|
for _, path := range paths {
|
|
|
|
if status != "" {
|
|
|
|
status = fmt.Sprintf(":%s", status)
|
|
|
|
}
|
|
|
|
addrs = append(addrs, fmt.Sprintf("%s%s", path, status))
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unixSocketPaths(addr string) (string, string) {
|
|
|
|
var socketPath, statusPath string
|
|
|
|
|
|
|
|
socketAddr := strings.Split(addr, ":")
|
|
|
|
if len(socketAddr) >= 2 {
|
|
|
|
socketPath = socketAddr[0]
|
|
|
|
statusPath = socketAddr[1]
|
|
|
|
} else {
|
|
|
|
socketPath = socketAddr[0]
|
|
|
|
statusPath = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return socketPath, statusPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func isNetworkURL(addr string) bool {
|
|
|
|
return strings.HasPrefix(addr, "http://") || strings.HasPrefix(addr, "https://") || strings.HasPrefix(addr, "fcgi://") || strings.HasPrefix(addr, "cgi://")
|
|
|
|
}
|
|
|
|
|
2015-10-12 01:36:21 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("phpfpm", func() telegraf.Input {
|
2015-10-12 01:36:21 +00:00
|
|
|
return &phpfpm{}
|
|
|
|
})
|
|
|
|
}
|