Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reverse_mode GET param to trigger -R passed to iperf3 #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.*
Dockerfile
LICENSE*
NOTICE*
README*
iperf3_windows.go
18 changes: 14 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
FROM golang:alpine AS build-env

# Download dependencies
WORKDIR /go/src
COPY go.mod go.sum ./
RUN go mod download

# Build current sources
COPY . .
RUN go build -o /go/bin/iperf3_exporter

# Prepare runtime environment
FROM alpine:latest
LABEL maintainer="Edgard Castro <[email protected]>"

EXPOSE 9579
RUN apk add --no-cache iperf3
COPY iperf3_exporter /bin/iperf3_exporter

COPY --from=build-env /go/bin/iperf3_exporter /bin/
ENTRYPOINT ["/bin/iperf3_exporter"]
EXPOSE 9579
37 changes: 29 additions & 8 deletions iperf3_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ type iperfResult struct {
// the prometheus metrics package.
type Exporter struct {
target string
port int
port int
period time.Duration
timeout time.Duration
mutex sync.RWMutex
reverse bool

success *prometheus.Desc
sentSeconds *prometheus.Desc
Expand All @@ -77,11 +78,12 @@ type Exporter struct {
}

// NewExporter returns an initialized Exporter.
func NewExporter(target string, port int, period time.Duration, timeout time.Duration) *Exporter {
func NewExporter(target string, port int, period time.Duration, timeout time.Duration, reverse bool) *Exporter {
return &Exporter{
target: target,
port: port,
period: period,
reverse: reverse,
timeout: timeout,
success: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "success"), "Was the last iperf3 probe successful.", nil, nil),
sentSeconds: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "sent_seconds"), "Total seconds spent sending packets.", nil, nil),
Expand Down Expand Up @@ -110,7 +112,12 @@ 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, "-p", strconv.Itoa(e.port)).Output()
var iperfArgs []string
iperfArgs = []string{"-J", "-t", strconv.FormatFloat(e.period.Seconds(), 'f', 0, 64), "-c", e.target, "-p", strconv.Itoa(e.port)}
if e.reverse{
iperfArgs = append(iperfArgs, "-R")
}
out, err := exec.CommandContext(ctx, iperfCmd, iperfArgs...).Output()
if err != nil {
ch <- prometheus.MustNewConstMetric(e.success, prometheus.GaugeValue, 0)
iperfErrors.Inc()
Expand Down Expand Up @@ -140,22 +147,36 @@ 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
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 reverseMode bool
reverse_mode := r.URL.Query().Get("reverse_mode")
if reverse_mode != "" {
var err error
reverseMode, err = strconv.ParseBool(reverse_mode)
if err != nil {
http.Error(w, fmt.Sprintf("'reverse_mode' parameter must be true or false (boolean): %s", err), http.StatusBadRequest)
iperfErrors.Inc()
return
}
} else {
reverseMode = false
}

var runPeriod time.Duration
period := r.URL.Query().Get("period")
if period != "" {
Expand Down Expand Up @@ -198,7 +219,7 @@ func handler(w http.ResponseWriter, r *http.Request) {

start := time.Now()
registry := prometheus.NewRegistry()
exporter := NewExporter(target, targetPort, runPeriod, runTimeout)
exporter := NewExporter(target, targetPort, runPeriod, runTimeout, reverseMode)
registry.MustRegister(exporter)

// Delegate http serving to Prometheus client library, which will call collector.Collect.
Expand Down