Add tag for target url to phpfpm input (#3928)

This commit is contained in:
Daniel Nelson 2018-03-23 11:50:52 -07:00 committed by GitHub
parent 3658ac8f53
commit be9d4f4be0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 63 deletions

View File

@ -1,19 +1,44 @@
# Telegraf plugin: phpfpm # PHP-FPM Input Plugin
Get phpfpm stat using either HTTP status page or fpm socket. Get phpfpm stats using either HTTP status page or fpm socket.
# Measurements ### Configuration:
Meta: ```toml
# Read metrics of phpfpm, via HTTP status page or socket
[[inputs.phpfpm]]
## 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"
##
## Example of multiple gathering from local socket and remove host
## urls = ["http://192.168.1.20/status", "/tmp/fpm.sock"]
urls = ["http://localhost/status"]
```
- tags: `pool=poolname` When using `unixsocket`, you have to ensure that telegraf runs on same
host, and socket path is accessible to telegraf user.
Measurement names: ### Metrics:
- phpfpm - phpfpm
- tags:
Measurement field: - pool
- url
- fields:
- accepted_conn - accepted_conn
- listen_queue - listen_queue
- max_listen_queue - max_listen_queue
@ -25,41 +50,10 @@ Measurement field:
- max_children_reached - max_children_reached
- slow_requests - slow_requests
# Example output # Example Output
Using this configuration:
``` ```
[phpfpm] phpfpm,pool=www accepted_conn=13i,active_processes=2i,idle_processes=1i,listen_queue=0i,listen_queue_len=0i,max_active_processes=2i,max_children_reached=0i,max_listen_queue=0i,slow_requests=0i,total_processes=3i 1453011293083331187
# An array of address to gather stats about. Specify an ip on hostname phpfpm,pool=www2 accepted_conn=12i,active_processes=1i,idle_processes=2i,listen_queue=0i,listen_queue_len=0i,max_active_processes=2i,max_children_reached=0i,max_listen_queue=0i,slow_requests=0i,total_processes=3i 1453011293083691422
# with optional port and path. ie localhost, 10.10.3.33/server-status, etc. phpfpm,pool=www3 accepted_conn=11i,active_processes=1i,idle_processes=2i,listen_queue=0i,listen_queue_len=0i,max_active_processes=2i,max_children_reached=0i,max_listen_queue=0i,slow_requests=0i,total_processes=3i 1453011293083691658
#
# 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", "fcgi://10.0.0.12:9000/status"]
``` ```
When run with:
```
./telegraf --config telegraf.conf --input-filter phpfpm --test
```
It produces:
```
* Plugin: phpfpm, Collection 1
> phpfpm,pool=www accepted_conn=13i,active_processes=2i,idle_processes=1i,listen_queue=0i,listen_queue_len=0i,max_active_processes=2i,max_children_reached=0i,max_listen_queue=0i,slow_requests=0i,total_processes=3i 1453011293083331187
> phpfpm,pool=www2 accepted_conn=12i,active_processes=1i,idle_processes=2i,listen_queue=0i,listen_queue_len=0i,max_active_processes=2i,max_children_reached=0i,max_listen_queue=0i,slow_requests=0i,total_processes=3i 1453011293083691422
> phpfpm,pool=www3 accepted_conn=11i,active_processes=1i,idle_processes=2i,listen_queue=0i,listen_queue_len=0i,max_active_processes=2i,max_children_reached=0i,max_listen_queue=0i,slow_requests=0i,total_processes=3i 1453011293083691658
```
## Note
When using `unixsocket`, you have to ensure that telegraf runs on same
host, and socket path is accessible to telegraf user.

View File

@ -148,11 +148,11 @@ func (g *phpfpm) gatherServer(addr string, acc telegraf.Accumulator) error {
return err return err
} }
return g.gatherFcgi(fcgi, statusPath, acc) return g.gatherFcgi(fcgi, statusPath, acc, addr)
} }
// Gather stat using fcgi protocol // Gather stat using fcgi protocol
func (g *phpfpm) gatherFcgi(fcgi *conn, statusPath string, acc telegraf.Accumulator) error { func (g *phpfpm) gatherFcgi(fcgi *conn, statusPath string, acc telegraf.Accumulator, addr string) error {
fpmOutput, fpmErr, err := fcgi.Request(map[string]string{ fpmOutput, fpmErr, err := fcgi.Request(map[string]string{
"SCRIPT_NAME": "/" + statusPath, "SCRIPT_NAME": "/" + statusPath,
"SCRIPT_FILENAME": statusPath, "SCRIPT_FILENAME": statusPath,
@ -164,7 +164,7 @@ func (g *phpfpm) gatherFcgi(fcgi *conn, statusPath string, acc telegraf.Accumula
}, "/"+statusPath) }, "/"+statusPath)
if len(fpmErr) == 0 && err == nil { if len(fpmErr) == 0 && err == nil {
importMetric(bytes.NewReader(fpmOutput), acc) importMetric(bytes.NewReader(fpmOutput), acc, addr)
return nil return nil
} else { } else {
return fmt.Errorf("Unable parse phpfpm status. Error: %v %v", string(fpmErr), err) return fmt.Errorf("Unable parse phpfpm status. Error: %v %v", string(fpmErr), err)
@ -192,12 +192,12 @@ func (g *phpfpm) gatherHttp(addr string, acc telegraf.Accumulator) error {
addr, err) addr, err)
} }
importMetric(res.Body, acc) importMetric(res.Body, acc, addr)
return nil return nil
} }
// Import stat data into Telegraf system // Import stat data into Telegraf system
func importMetric(r io.Reader, acc telegraf.Accumulator) (poolStat, error) { func importMetric(r io.Reader, acc telegraf.Accumulator, addr string) (poolStat, error) {
stats := make(poolStat) stats := make(poolStat)
var currentPool string var currentPool string
@ -240,6 +240,7 @@ func importMetric(r io.Reader, acc telegraf.Accumulator) (poolStat, error) {
for pool := range stats { for pool := range stats {
tags := map[string]string{ tags := map[string]string{
"pool": pool, "pool": pool,
"url": addr,
} }
fields := make(map[string]interface{}) fields := make(map[string]interface{})
for k, v := range stats[pool] { for k, v := range stats[pool] {

View File

@ -40,6 +40,7 @@ func TestPhpFpmGeneratesMetrics_From_Http(t *testing.T) {
tags := map[string]string{ tags := map[string]string{
"pool": "www", "pool": "www",
"url": ts.URL,
} }
fields := map[string]interface{}{ fields := map[string]interface{}{
@ -80,6 +81,7 @@ func TestPhpFpmGeneratesMetrics_From_Fcgi(t *testing.T) {
tags := map[string]string{ tags := map[string]string{
"pool": "www", "pool": "www",
"url": r.Urls[0],
} }
fields := map[string]interface{}{ fields := map[string]interface{}{
@ -124,6 +126,7 @@ func TestPhpFpmGeneratesMetrics_From_Socket(t *testing.T) {
tags := map[string]string{ tags := map[string]string{
"pool": "www", "pool": "www",
"url": r.Urls[0],
} }
fields := map[string]interface{}{ fields := map[string]interface{}{
@ -168,6 +171,7 @@ func TestPhpFpmGeneratesMetrics_From_Socket_Custom_Status_Path(t *testing.T) {
tags := map[string]string{ tags := map[string]string{
"pool": "www", "pool": "www",
"url": r.Urls[0],
} }
fields := map[string]interface{}{ fields := map[string]interface{}{