diff --git a/README.md b/README.md index 0854843e..fdef3f51 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,15 @@ The `server` command runs the server required for prometheus to retrieve the sta ### Options and defaults -| Option | Description | Environment variable | Default value | -|------------------------|-------------------------------------------------------|------------------------------|-----------------| -| `--web.listen-address` | Address on which to expose metrics and web interface. | `PHP_FPM_WEB_LISTEN_ADDRESS` | [`:9253`](https://github.com/prometheus/prometheus/wiki/Default-port-allocations) | -| `--web.telemetry-path` | Path under which to expose metrics. | `PHP_FPM_WEB_TELEMETRY_PATH` | `/metrics` | -| `--phpfpm.scrape-uri` | FastCGI address, e.g. unix:///tmp/php.sock;/status or tcp://127.0.0.1:9000/status | `PHP_FPM_SCRAPE_URI` | `tcp://127.0.0.1:9000/status` | -| `--phpfpm.fix-process-count` | Enable to calculate process numbers via php-fpm_exporter since PHP-FPM sporadically reports wrong active/idle/total process numbers. | `PHP_FPM_FIX_PROCESS_COUNT`| `false` | -| `--log.level` | Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal] (default "error") | `PHP_FPM_LOG_LEVEL` | info | +| Option | Description | Environment variable | Default value | +|------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------|------------------------------|-----------------| +| `--web.listen-address` | Address on which to expose metrics and web interface. | `PHP_FPM_WEB_LISTEN_ADDRESS` | [`:9253`](https://github.com/prometheus/prometheus/wiki/Default-port-allocations) | +| `--web.telemetry-path` | Path under which to expose metrics. | `PHP_FPM_WEB_TELEMETRY_PATH` | `/metrics` | +| `--phpfpm.scrape-uri` | FastCGI address, e.g. unix:///tmp/php.sock;/status or tcp://127.0.0.1:9000/status | `PHP_FPM_SCRAPE_URI` | `tcp://127.0.0.1:9000/status` | +| `--phpfpm.sockets-directory` | Path of the directory where PHP-FPM sockets are located, e.g. /run/php/. When using phpfpm.sockets-directory, phpfpm.scrape-uri is ignored. | `PHP_FPM_SOCKETS_DIRECTORY` | | +| `--phpfpm.sockets-status` | URI of a status page. Used with phpfpm.sockets-directory. Has to be same for all pools in directory. | `PHP_FPM_SOCKETS_STATUS` | `/status` | +| `--phpfpm.fix-process-count` | Enable to calculate process numbers via php-fpm_exporter since PHP-FPM sporadically reports wrong active/idle/total process numbers. | `PHP_FPM_FIX_PROCESS_COUNT`| `false` | +| `--log.level` | Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal] (default "error") | `PHP_FPM_LOG_LEVEL` | info | ### Why `--phpfpm.fix-process-count`? @@ -94,6 +96,11 @@ If you like to have a more granular reporting please use `phpfpm_process_state`. php-fpm_exporter get --phpfpm.scrape-uri tcp://127.0.0.1:9000/status,tcp://127.0.0.1:9001/status ``` +* Retrieve information from all PHP-FPM pools `/var/run/php/` directory with status endpoint being `/fpm-status`: + ``` + php-fpm_exporter get --phpfpm.sockets-directory /var/run/php/ --phpfpm.sockets-status /fpm-status + ``` + * Run as server with 2 pools: ``` php-fpm_exporter server --phpfpm.scrape-uri tcp://127.0.0.1:9000/status,tcp://127.0.0.1:9001/status @@ -104,6 +111,11 @@ If you like to have a more granular reporting please use `phpfpm_process_state`. PHP_FPM_FIX_PROCESS_COUNT=1 go run main.go server --web.listen-address ":12345" --log.level=debug ``` +* Run as server with all pools in `/run/php/` directory with status endpoint being `/status`: + ``` + php-fpm_exporter server --phpfpm.sockets-directory /run/php/ + ``` + ### Docker Examples * Run docker manually diff --git a/cmd/get.go b/cmd/get.go index 94d44dbe..c64b0dac 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -16,6 +16,8 @@ package cmd import ( "encoding/json" "fmt" + "os" + "path/filepath" "time" "github.com/davecgh/go-spew/spew" @@ -37,12 +39,24 @@ var getCmd = &cobra.Command{ * php-fpm_exporter get --phpfpm.scrape-uri 127.0.0.1:9000 --phpfpm.scrape-uri 127.0.0.1:9001 [...] * php-fpm_exporter get --phpfpm.scrape-uri 127.0.0.1:9000,127.0.0.1:9001,[...] +* php-fpm_exporter get --phpfpm.sockets-directory /run/php/ `, Run: func(cmd *cobra.Command, args []string) { pm := phpfpm.PoolManager{} - for _, uri := range scrapeURIs { - pm.Add(uri) + // Check if we are using directory. + if socketsDirectory == "" { + for _, uri := range scrapeURIs { + pm.Add(uri) + } + } else { + // Traverse directory for php sockets. + _ = filepath.Walk(socketsDirectory, func(path string, info os.FileInfo, err error) error { + if err == nil && info.Mode()&os.ModeSocket != 0 { + pm.Add("unix://" + path + ";" + socketsStatus) + } + return nil + }) } if err := pm.Update(); err != nil { @@ -102,5 +116,7 @@ func init() { // Cobra supports local flags which will only run when this command // is called directly, e.g.: getCmd.Flags().StringSliceVar(&scrapeURIs, "phpfpm.scrape-uri", []string{"tcp://127.0.0.1:9000/status"}, "FastCGI address, e.g. unix:///tmp/php.sock;/status or tcp://127.0.0.1:9000/status") + getCmd.Flags().StringVar(&socketsDirectory, "phpfpm.sockets-directory", "", "Path of the directory where PHP-FPM sockets are located, e.g. /run/php/. When using phpfpm.sockets-directory, phpfpm.scrape-uri is ignored.") + getCmd.Flags().StringVar(&socketsStatus, "phpfpm.sockets-status", "/status", "URI of a status page. Used with phpfpm.sockets-directory. Has to be same for all pools in directory. Defaults to /status") getCmd.Flags().StringVar(&output, "out", "text", "Output format. One of: text, json, spew") } diff --git a/cmd/server.go b/cmd/server.go index d1d82028..b9654662 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -18,6 +18,7 @@ import ( "net/http" "os" "os/signal" + "path/filepath" "syscall" "time" @@ -33,6 +34,8 @@ var ( metricsEndpoint string scrapeURIs []string fixProcessCount bool + socketsDirectory string + socketsStatus string ) // serverCmd represents the server command @@ -50,8 +53,19 @@ to quickly create a Cobra application.`, pm := phpfpm.PoolManager{} - for _, uri := range scrapeURIs { - pm.Add(uri) + // Check if we are using directory. + if socketsDirectory == "" { + for _, uri := range scrapeURIs { + pm.Add(uri) + } + } else { + // Traverse directory for php sockets. + _ = filepath.Walk(socketsDirectory, func(path string, info os.FileInfo, err error) error { + if err == nil && info.Mode()&os.ModeSocket != 0 { + pm.Add("unix://" + path + ";" + socketsStatus) + } + return nil + }) } exporter := phpfpm.NewExporter(pm) @@ -124,6 +138,8 @@ func init() { serverCmd.Flags().StringVar(&listeningAddress, "web.listen-address", ":9253", "Address on which to expose metrics and web interface.") serverCmd.Flags().StringVar(&metricsEndpoint, "web.telemetry-path", "/metrics", "Path under which to expose metrics.") serverCmd.Flags().StringSliceVar(&scrapeURIs, "phpfpm.scrape-uri", []string{"tcp://127.0.0.1:9000/status"}, "FastCGI address, e.g. unix:///tmp/php.sock;/status or tcp://127.0.0.1:9000/status") + serverCmd.Flags().StringVar(&socketsDirectory, "phpfpm.sockets-directory", "", "Path of the directory where PHP-FPM sockets are located, e.g. /run/php/. When using phpfpm.sockets-directory, phpfpm.scrape-uri is ignored.") + serverCmd.Flags().StringVar(&socketsStatus, "phpfpm.sockets-status", "/status", "URI of a status page. Used with phpfpm.sockets-directory. Has to be same for all pools in directory.") serverCmd.Flags().BoolVar(&fixProcessCount, "phpfpm.fix-process-count", false, "Enable to calculate process numbers via php-fpm_exporter since PHP-FPM sporadically reports wrong active/idle/total process numbers.") // Workaround since vipers BindEnv is currently not working as expected (see https://github.com/spf13/viper/issues/461) @@ -132,6 +148,8 @@ func init() { "PHP_FPM_WEB_LISTEN_ADDRESS": "web.listen-address", "PHP_FPM_WEB_TELEMETRY_PATH": "web.telemetry-path", "PHP_FPM_SCRAPE_URI": "phpfpm.scrape-uri", + "PHP_FPM_SOCKETS_DIRECTORY": "phpfpm.sockets-directory", + "PHP_FPM_SOCKETS_STATUS": "phpfpm.sockets-status", "PHP_FPM_FIX_PROCESS_COUNT": "phpfpm.fix-process-count", }