Skip to content

Commit

Permalink
Merge pull request #3 from morganyandow/master
Browse files Browse the repository at this point in the history
allow for iperf3 target ports to be specified
  • Loading branch information
edgard authored Feb 25, 2020
2 parents a8ec208 + 34a0f3b commit 3ec09ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 21 additions & 4 deletions iperf3_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 3ec09ef

Please sign in to comment.