| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package phpfpm |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 11 | ) |
| 12 | |
| 13 | func (c *Collector) initClient() (client, error) { |
| 14 | if c.Socket != "" { |
| 15 | return c.initSocketClient() |
| 16 | } |
| 17 | if c.Address != "" { |
| 18 | return c.initTcpClient() |
| 19 | } |
| 20 | if c.URL != "" { |
| 21 | return c.initHTTPClient() |
| 22 | } |
| 23 | |
| 24 | return nil, errors.New("neither 'socket' nor 'url' set") |
| 25 | } |
| 26 | |
| 27 | func (c *Collector) initHTTPClient() (*httpClient, error) { |
| 28 | cli, err := web.NewHTTPClient(c.ClientConfig) |
| 29 | if err != nil { |
| 30 | return nil, fmt.Errorf("create HTTP client: %v", err) |
| 31 | } |
| 32 | |
| 33 | c.Debugf("using HTTP client: url='%s', timeout='%s'", c.URL, c.Timeout) |
| 34 | |
| 35 | return newHTTPClient(cli, c.RequestConfig) |
| 36 | } |
| 37 | |
| 38 | func (c *Collector) initSocketClient() (*socketClient, error) { |
| 39 | if _, err := os.Stat(c.Socket); err != nil { |
| 40 | return nil, fmt.Errorf("the socket '%s' does not exist: %v", c.Socket, err) |
| 41 | } |
| 42 | |
| 43 | c.Debugf("using socket client: socket='%s', timeout='%s', fcgi_path='%s'", c.Socket, c.Timeout, c.FcgiPath) |
| 44 | |
| 45 | return newSocketClient(c.Logger, c.Socket, c.Timeout.Duration(), c.FcgiPath), nil |
| 46 | } |
| 47 | |
| 48 | func (c *Collector) initTcpClient() (*tcpClient, error) { |
| 49 | c.Debugf("using tcp client: address='%s', timeout='%s', fcgi_path='%s'", c.Address, c.Timeout, c.FcgiPath) |
| 50 | |
| 51 | return newTcpClient(c.Logger, c.Address, c.Timeout.Duration(), c.FcgiPath), nil |
| 52 | } |