| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package varnish |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "strconv" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/logger" |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/dockerhost" |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/ndexec" |
| 13 | ) |
| 14 | |
| 15 | type varnishstatBinary interface { |
| 16 | statistics() ([]byte, error) |
| 17 | } |
| 18 | |
| 19 | func newVarnishstatExecBinary(cfg Config, log *logger.Logger) varnishstatBinary { |
| 20 | return &varnishstatExec{ |
| 21 | Logger: log, |
| 22 | timeout: cfg.Timeout.Duration(), |
| 23 | instanceName: cfg.InstanceName, |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | type varnishstatExec struct { |
| 28 | *logger.Logger |
| 29 | |
| 30 | timeout time.Duration |
| 31 | instanceName string |
| 32 | } |
| 33 | |
| 34 | func (e *varnishstatExec) statistics() ([]byte, error) { |
| 35 | return ndexec.RunNDSudo(e.Logger, e.timeout, "varnishstat-stats", "--instanceName", e.instanceName) |
| 36 | } |
| 37 | |
| 38 | func newVarnishstatDockerExecBinary(cfg Config, log *logger.Logger) varnishstatBinary { |
| 39 | return &varnishstatDockerExec{ |
| 40 | Logger: log, |
| 41 | timeout: cfg.Timeout.Duration(), |
| 42 | instanceName: cfg.InstanceName, |
| 43 | container: cfg.DockerContainer, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | type varnishstatDockerExec struct { |
| 48 | *logger.Logger |
| 49 | |
| 50 | timeout time.Duration |
| 51 | instanceName string |
| 52 | container string |
| 53 | } |
| 54 | |
| 55 | func (e *varnishstatDockerExec) statistics() ([]byte, error) { |
| 56 | ctx, cancel := context.WithTimeout(context.Background(), e.timeout) |
| 57 | defer cancel() |
| 58 | |
| 59 | timeS := strconv.Itoa(max(int(e.timeout.Seconds()), 1)) |
| 60 | |
| 61 | args := []string{"-1", "-t", timeS} |
| 62 | if e.instanceName != "" { |
| 63 | args = append(args, "-n", e.instanceName) |
| 64 | } |
| 65 | |
| 66 | return dockerhost.Exec(ctx, e.container, "varnishstat", args...) |
| 67 | } |