From ac635281d1821d893c6dcfadcff9521fb11748a7 Mon Sep 17 00:00:00 2001 From: Morgan Yandow Date: Mon, 24 Feb 2020 20:36:09 +0000 Subject: [PATCH 1/2] add support to specify the target port for iperf testing --- iperf3_exporter.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/iperf3_exporter.go b/iperf3_exporter.go index af732d4..797f8cd 100644 --- a/iperf3_exporter.go +++ b/iperf3_exporter.go @@ -64,6 +64,7 @@ type iperfResult struct { // the prometheus metrics package. type Exporter struct { target string + port int period time.Duration timeout time.Duration mutex sync.RWMutex @@ -76,9 +77,10 @@ type Exporter struct { } // NewExporter returns an initialized Exporter. -func NewExporter(target string, period time.Duration, timeout time.Duration) *Exporter { +func NewExporter(target string, port int, period time.Duration, timeout time.Duration) *Exporter { return &Exporter{ target: target, + port: port, period: period, timeout: timeout, success: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "success"), "Was the last iperf3 probe successful.", nil, nil), @@ -108,7 +110,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { ctx, cancel := context.WithTimeout(context.Background(), e.timeout) defer cancel() - out, err := exec.CommandContext(ctx, iperfCmd, "-J", "-t", strconv.FormatFloat(e.period.Seconds(), 'f', 0, 64), "-c", e.target).Output() + out, err := exec.CommandContext(ctx, iperfCmd, "-J", "-t", strconv.FormatFloat(e.period.Seconds(), 'f', 0, 64), "-c", e.target, "-p", strconv.Itoa(e.port)).Output() if err != nil { ch <- prometheus.MustNewConstMetric(e.success, prometheus.GaugeValue, 0) iperfErrors.Inc() @@ -138,7 +140,22 @@ func handler(w http.ResponseWriter, r *http.Request) { iperfErrors.Inc() return } - + + var targetPort int + port := r.URL.Query().Get("port") + if port != "" { + var err error + targetPort, err = strconv.Atoi(port) + if err != nil { + http.Error(w, fmt.Sprintf("'port' parameter must be an integer: %s", err), http.StatusBadRequest) + iperfErrors.Inc() + return + } + } + if targetPort == 0 { + targetPort = 5201 + } + var runPeriod time.Duration period := r.URL.Query().Get("period") if period != "" { @@ -181,7 +198,7 @@ func handler(w http.ResponseWriter, r *http.Request) { start := time.Now() registry := prometheus.NewRegistry() - exporter := NewExporter(target, runPeriod, runTimeout) + exporter := NewExporter(target, targetPort, runPeriod, runTimeout) registry.MustRegister(exporter) // Delegate http serving to Prometheus client library, which will call collector.Collect. From 34a0f3b555f10677cb6eebd5e8e82f88aea52cb1 Mon Sep 17 00:00:00 2001 From: Morgan Yandow Date: Mon, 24 Feb 2020 20:44:21 +0000 Subject: [PATCH 2/2] update readme with port parameter --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d1e6ec2..ad184ed 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ This can be also be limited by the `iperf3.timeout` command-line flag. If neithe ## Prometheus Configuration The iPerf3 exporter needs to be passed the target as a parameter, this can be done with relabelling. +Optional: pass the port that the target iperf3 server is lisenting on as the "port" parameter. Example config: ```yml