From b10afdf084c2f25a593cbbb027060facbd07ee16 Mon Sep 17 00:00:00 2001
From: Piotr Kochan
Date: Fri, 24 Jun 2022 13:34:46 +0200
Subject: [PATCH] Add http client
---
phpfpm/phpfpm.go | 61 +++++++++++++++++++++++++++++--------------
phpfpm/phpfpm_test.go | 1 +
2 files changed, 42 insertions(+), 20 deletions(-)
diff --git a/phpfpm/phpfpm.go b/phpfpm/phpfpm.go
index 400c2330..81eede59 100644
--- a/phpfpm/phpfpm.go
+++ b/phpfpm/phpfpm.go
@@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
+ "net/http"
"net/url"
"regexp"
"strconv"
@@ -47,6 +48,9 @@ const PoolProcessRequestInfo74 string = "Getting request information"
// PoolProcessRequestEnding defines a process that is about to end.
const PoolProcessRequestEnding string = "Ending"
+// ProgramIdentifier is send to the php-fpm as SERVER_SOFTWARE
+const ProgramIdentifier string = "go / php-fpm_exporter"
+
var log logger
type logger interface {
@@ -156,35 +160,52 @@ func (p *Pool) Update() (err error) {
return p.error(err)
}
- fcgi, err := fcgiclient.DialTimeout(scheme, address, time.Duration(3)*time.Second)
- if err != nil {
- return p.error(err)
- }
-
- defer fcgi.Close()
-
- env := map[string]string{
- "SCRIPT_FILENAME": path,
- "SCRIPT_NAME": path,
- "SERVER_SOFTWARE": "go / php-fpm_exporter",
- "REMOTE_ADDR": "127.0.0.1",
- "QUERY_STRING": "json&full",
- }
-
- resp, err := fcgi.Get(env)
- if err != nil {
- return p.error(err)
+ var t = 3 * time.Second
+ var resp *http.Response
+
+ if scheme == "http" || scheme == "https" {
+ req, err := http.NewRequest("GET", p.Address, nil)
+ req.URL.RawQuery = "json&full"
+ req.Header = http.Header{
+ "SCRIPT_FILENAME": []string{path},
+ "SCRIPT_NAME": []string{path},
+ "SERVER_SOFTWARE": []string{ProgramIdentifier},
+ "REMOTE_ADDR": []string{"127.0.0.1"},
+ }
+ if err != nil {
+ return p.error(err)
+ }
+ client := http.Client{Timeout: t}
+ resp, err = client.Do(req)
+ if err != nil {
+ return p.error(err)
+ }
+ } else {
+ fcgi, err := fcgiclient.DialTimeout(scheme, address, t)
+ if err != nil {
+ return p.error(err)
+ }
+ defer fcgi.Close()
+ env := map[string]string{
+ "SCRIPT_FILENAME": path,
+ "SCRIPT_NAME": path,
+ "SERVER_SOFTWARE": ProgramIdentifier,
+ "REMOTE_ADDR": "127.0.0.1",
+ "QUERY_STRING": "json&full",
+ }
+ resp, err = fcgi.Get(env)
+ if err != nil {
+ return p.error(err)
+ }
}
defer resp.Body.Close()
-
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return p.error(err)
}
content = JSONResponseFixer(content)
-
log.Debugf("Pool[%v]: %v", p.Address, string(content))
if err = json.Unmarshal(content, &p); err != nil {
diff --git a/phpfpm/phpfpm_test.go b/phpfpm/phpfpm_test.go
index dde01b7a..291ccc8e 100644
--- a/phpfpm/phpfpm_test.go
+++ b/phpfpm/phpfpm_test.go
@@ -122,6 +122,7 @@ func TestParseURL(t *testing.T) {
}{
{"tcp://127.0.0.1:9000/status", []string{"tcp", "127.0.0.1:9000", "/status"}, nil},
{"tcp://127.0.0.1", []string{"tcp", "127.0.0.1", ""}, nil},
+ {"http://127.0.0.1/status", []string{"http", "127.0.0.1", "/status"}, nil},
{"unix:///tmp/php.sock;/status", []string{"unix", "/tmp/php.sock", "/status"}, nil},
{"unix:///tmp/php.sock", []string{"unix", "/tmp/php.sock", ""}, nil},
}