phpfpm: add socket fcgi support

This commit is contained in:
鲁晓敏 2015-11-12 19:07:38 +08:00 committed by Cameron Sparr
parent e619845ffe
commit 019585f0db
2 changed files with 38 additions and 7 deletions

View File

@ -30,13 +30,14 @@ Using this configuration:
# An array of address to gather stats about. Specify an ip on hostname
# with optional port and path. ie localhost, 10.10.3.33/server-status, etc.
#
# We can configure int two modes:
# We can configure in three modes:
# - unixsocket: the string is the path to fpm socket like
# /var/run/php5-fpm.sock
# - http: the URL has to start with http:// or https://
# - fcgi: the URL has to start with fcgi:// or cgi://, and socket port must present
#
# If no servers are specified, then default to 127.0.0.1/server-status
urls = ["http://localhost/status", "10.0.0.12:/var/run/php5-fpm-www2.sock"]
urls = ["http://localhost/status", "10.0.0.12:/var/run/php5-fpm-www2.sock", "fcgi://10.0.0.12:9000/status"]
```
When run with:
@ -70,4 +71,15 @@ It produces:
> [url="localhost" pool="www2"] phpfpm_max_active_processes value=2
> [url="localhost" pool="www2"] phpfpm_accepted_conn value=306
> [url="localhost" pool="www2"] phpfpm_listen_queue value=0
> [url="10.0.0.12:9000" pool="www3"] phpfpm_max_children_reached value=0
> [url="10.0.0.12:9000" pool="www3"] phpfpm_slow_requests value=1
> [url="10.0.0.12:9000" pool="www3"] phpfpm_max_listen_queue value=0
> [url="10.0.0.12:9000" pool="www3"] phpfpm_active_processes value=1
> [url="10.0.0.12:9000" pool="www3"] phpfpm_listen_queue_len value=0
> [url="10.0.0.12:9000" pool="www3"] phpfpm_idle_processes value=2
> [url="10.0.0.12:9000" pool="www3"] phpfpm_total_processes value=2
> [url="10.0.0.12:9000" pool="www3"] phpfpm_max_active_processes value=2
> [url="10.0.0.12:9000" pool="www3"] phpfpm_accepted_conn value=307
> [url="10.0.0.12:9000" pool="www3"] phpfpm_listen_queue value=0
```

View File

@ -42,13 +42,16 @@ var sampleConfig = `
# An array of addresses to gather stats about. Specify an ip or hostname
# with optional port and path.
#
# Plugin can be configured in two modes (both can be used):
# Plugin can be configured in three modes (both can be used):
# - http: the URL must start with http:// or https://, ex:
# "http://localhost/status"
# "http://192.168.130.1/status?full"
# - unixsocket: path to fpm socket, ex:
# "/var/run/php5-fpm.sock"
# "192.168.10.10:/var/run/php5-fpm-www2.sock"
# - fcgi: the URL mush start with fcgi:// or cgi://, and port must present, ex:
# "fcgi://10.0.0.12:9000/status"
# "cgi://10.0.10.12:9001/status"
#
# If no servers are specified, then default to 127.0.0.1/server-status
urls = ["http://localhost/status"]
@ -114,10 +117,26 @@ func (g *phpfpm) gatherServer(addr string, acc plugins.Accumulator) error {
}
importMetric(res.Body, acc, u.Host)
} else {
var (
fcgi *FCGIClient
fcgiAddr string
)
if strings.HasPrefix(addr, "fcgi://") || strings.HasPrefix(addr, "cgi://") {
u, err := url.Parse(addr)
if err != nil {
return fmt.Errorf("Unable parse server address '%s': %s", addr, err)
}
socketAddr := strings.Split(u.Host, ":")
fcgiIp := socketAddr[0]
fcgiPort, _ := strconv.Atoi(socketAddr[1])
fcgiAddr = u.Host
fcgi, _ = NewClient(fcgiIp, fcgiPort)
} else {
socketAddr := strings.Split(addr, ":")
fcgi, _ := NewClient("unix", socketAddr[1])
fcgiAddr = socketAddr[0]
fcgi, _ = NewClient("unix", socketAddr[1])
}
resOut, resErr, err := fcgi.Request(map[string]string{
"SCRIPT_NAME": "/status",
"SCRIPT_FILENAME": "status",
@ -125,7 +144,7 @@ func (g *phpfpm) gatherServer(addr string, acc plugins.Accumulator) error {
}, "")
if len(resErr) == 0 && err == nil {
importMetric(bytes.NewReader(resOut), acc, socketAddr[0])
importMetric(bytes.NewReader(resOut), acc, fcgiAddr)
}
}