From fee5041e271e776190b2ef26061a023f083b4f20 Mon Sep 17 00:00:00 2001 From: Jakub Pinkas Date: Mon, 10 Jun 2024 16:19:47 +0200 Subject: [PATCH 1/3] added counting of connection state for sockets --- phpfpm_exporter.go | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/phpfpm_exporter.go b/phpfpm_exporter.go index 964ecbf..5786942 100644 --- a/phpfpm_exporter.go +++ b/phpfpm_exporter.go @@ -94,17 +94,38 @@ var ( "Enable php-fpm slow-log before you consider this. If this value is non-zero you may have slow php processes.", []string{phpfpmSocketPathLabel}, nil), } + + + phpfpmStateGauge = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: "php", + Subsystem: "fpm", + Name: "state_count", + Help: "Count of PHP-FPM processes in each state.", + }, + []string{phpfpmSocketPathLabel, "state"}, + ) ) func CollectStatusFromReader(reader io.Reader, socketPath string, ch chan<- prometheus.Metric) error { scanner := bufio.NewScanner(reader) re := regexp.MustCompile("^(.*): +(.*)$") + // Map to store the counts of each state + stateCounts := make(map[string]float64) + // Scrape the interesting values: for scanner.Scan() { fields := re.FindStringSubmatch(scanner.Text()) + //fmt.Println(fields) if fields == nil { - return fmt.Errorf("Failed to parse %s", scanner.Text()) + continue // Can't return error since there are 2 rows empy and asterisks + //return fmt.Errorf("Failed to parse %s", scanner.Text()) + } + + if fields[1] == "state" { + // Increment the count for the state + stateCounts[fields[2]]++ } if gauge, ok := phpfpmGauges[fields[1]]; ok { @@ -144,6 +165,12 @@ func CollectStatusFromReader(reader io.Reader, socketPath string, ch chan<- prom socketPath) } } + + // Export the state counts as metrics + for state, count := range stateCounts { + phpfpmStateGauge.WithLabelValues(socketPath, state).Set(count) + } + return nil } @@ -153,6 +180,7 @@ func CollectStatusFromSocket(path *SocketPath, statusPath string, ch chan<- prom env["SCRIPT_FILENAME"] = statusPath env["SCRIPT_NAME"] = statusPath env["REQUEST_METHOD"] = "GET" + env["QUERY_STRING"] = "full" fcgi, err := fcgiclient.Dial(path.Network, path.Address) if err != nil { @@ -164,8 +192,16 @@ func CollectStatusFromSocket(path *SocketPath, statusPath string, ch chan<- prom if err != nil { return err } + defer resp.Body.Close() + + // Read and print the full response body + body, err := io.ReadAll(resp.Body) + if err != nil { + return err + } + //fmt.Println("Full response body from socket:", string(body)) - return CollectStatusFromReader(resp.Body, path.FormatStr(), ch) + return CollectStatusFromReader(strings.NewReader(string(body)), path.FormatStr(), ch) } func CollectMetricsFromScript(socketPaths []*SocketPath, scriptPaths []string) ([]*client_model.MetricFamily, error) { @@ -318,6 +354,7 @@ func main() { panic(err) } prometheus.MustRegister(exporter) + prometheus.MustRegister(phpfpmStateGauge) gatherer := prometheus.DefaultGatherer if len(*scriptCollectorPaths) != 0 { From 43271735ec1128a9841c60bc83a2c845712f2134 Mon Sep 17 00:00:00 2001 From: Jakub Pinkas Date: Fri, 14 Jun 2024 13:35:35 +0200 Subject: [PATCH 2/3] fixing CI/CD whitespace --- phpfpm_exporter.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/phpfpm_exporter.go b/phpfpm_exporter.go index 5786942..6d83b0e 100644 --- a/phpfpm_exporter.go +++ b/phpfpm_exporter.go @@ -95,13 +95,12 @@ var ( []string{phpfpmSocketPathLabel}, nil), } - phpfpmStateGauge = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Namespace: "php", Subsystem: "fpm", - Name: "state_count", - Help: "Count of PHP-FPM processes in each state.", + Name: "state_count", + Help: "Count of PHP-FPM processes in each state.", }, []string{phpfpmSocketPathLabel, "state"}, ) From a313c1dc59b2647e589ce40680798aea97237349 Mon Sep 17 00:00:00 2001 From: Jakub Pinkas Date: Fri, 14 Jun 2024 13:37:35 +0200 Subject: [PATCH 3/3] fixing CI/CD whitespace --- phpfpm_exporter.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpfpm_exporter.go b/phpfpm_exporter.go index 6d83b0e..245ea06 100644 --- a/phpfpm_exporter.go +++ b/phpfpm_exporter.go @@ -99,8 +99,8 @@ var ( prometheus.GaugeOpts{ Namespace: "php", Subsystem: "fpm", - Name: "state_count", - Help: "Count of PHP-FPM processes in each state.", + Name: "state_count", + Help: "Count of PHP-FPM processes in each state.", }, []string{phpfpmSocketPathLabel, "state"}, )