Compare commits

..

2 Commits

Author SHA1 Message Date
43f1d844df Export metrics for all targets
Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
2025-11-09 10:36:21 +00:00
aac44d3c8a Add upper limit check
Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
2025-11-09 10:24:51 +00:00

130
main.go
View File

@@ -5,7 +5,6 @@ import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/prometheus/client_golang/prometheus"
@@ -21,12 +20,22 @@ var (
interval int
port int
serverID int
cutOff float64
allTargets = map[string]Results{}
)
type Results struct {
Latency time.Duration
Downspeed float64
Upspeed float64
}
func init() {
flag.IntVar(&interval, "interval", 30, "Duration, in minutes, between speedtest runs")
flag.IntVar(&port, "port", 9091, "The port to listen on")
flag.IntVar(&serverID, "server", 55137, "The ID of the server to test against")
flag.Float64Var(&cutOff, "cut-off", 1024, "The upper limit expected from speed test. Values above this are treated as invalid")
flag.Parse()
}
@@ -74,57 +83,69 @@ func checkSpeed() error {
}
}
target := targets[0]
log.Printf("Testing against server: %s - %s [%s]\n", target.Name, target.Sponsor, target.ID)
log.Println("Testing main server...")
latency, downspeed, upspeed, err = TestServer(targets[0])
if err != nil {
return err
}
err = target.PingTest()
if err != nil {
log.Printf("Error running ping test: %v\n", err)
return err
}
err = target.DownloadTest(false)
if err != nil {
log.Printf("Error running download test: %v\n", err)
return err
}
err = target.UploadTest(false)
if err != nil {
log.Printf("Error runing upload test: %v\n", err)
return err
}
latency = target.Latency
downspeed = target.DLSpeed
upspeed = target.ULSpeed
log.Printf("Finished speedtest. DL=%f UL=%f Ping=%v\n", downspeed, upspeed, latency)
if os.Getenv("DEBUG") != "" {
log.Println("-------------------------------------")
log.Println("Debug enabled, testing all servers...")
for _, target := range serverList {
target.PingTest()
target.DownloadTest(false)
target.UploadTest(false)
if target.DLSpeed > 0 && target.ULSpeed > 0 {
log.Printf("Testing against server: %s - %s [%s] - DL=%f UL=%f Ping=%v\n",
target.Name, target.Sponsor, target.ID,
target.DLSpeed, target.ULSpeed, target.Latency,
)
} else {
log.Printf("Testing against server: %s - %s [%s] - Failed to get valid results\n",
target.Name, target.Sponsor, target.ID,
)
}
log.Println("Testing all servers...")
for _, target := range serverList {
latency, downspeed, upspeed, err := TestServer(target)
if err != nil {
continue
}
allTargets[fmt.Sprintf("%s - %s - %s", target.ID, target.Name, target.Sponsor)] = Results{
Latency: latency,
Downspeed: downspeed,
Upspeed: upspeed,
}
log.Println("-------------------------------------")
}
return nil
}
func TestServer(target *speedtest.Server) (time.Duration, float64, float64, error) {
var err error
err = target.PingTest()
if err != nil {
log.Printf("Error running ping test: %v\n", err)
return 0, 0, 0, err
}
err = target.DownloadTest(false)
if err != nil {
log.Printf("Error running download test: %v\n", err)
return 0, 0, 0, err
}
err = target.UploadTest(false)
if err != nil {
log.Printf("Error runing upload test: %v\n", err)
return 0, 0, 0, err
}
if target.DLSpeed > 0 && target.ULSpeed > 0 && target.DLSpeed < cutOff && target.ULSpeed < cutOff {
log.Printf("Testing against server: %s - %s [%s] - DL=%f UL=%f Ping=%v\n",
target.Name, target.Sponsor, target.ID,
target.DLSpeed, target.ULSpeed, target.Latency,
)
} else {
log.Printf("Testing against server: %s - %s [%s] - Failed to get valid results\n",
target.Name, target.Sponsor, target.ID,
)
err = fmt.Errorf("invalid test results")
}
return target.Latency, target.DLSpeed, target.ULSpeed, err
}
type speedCollector struct {
downMetric *prometheus.Desc
upMetric *prometheus.Desc
latencyMetric *prometheus.Desc
downMetric *prometheus.Desc
upMetric *prometheus.Desc
latencyMetric *prometheus.Desc
downMetricTarget *prometheus.Desc
upMetricTarget *prometheus.Desc
latencyMetricTarget *prometheus.Desc
}
func newSpeedCollector() *speedCollector {
@@ -141,6 +162,18 @@ func newSpeedCollector() *speedCollector {
"Latency in ms",
nil, nil,
),
downMetricTarget: prometheus.NewDesc("speedtest_download_speed_target",
"Download speed in Mbit/s for target",
[]string{"target"}, nil,
),
upMetricTarget: prometheus.NewDesc("speedtest_upload_speed_target",
"Upload speed in Mbit/s for target",
[]string{"target"}, nil,
),
latencyMetricTarget: prometheus.NewDesc("speedtest_latency_target",
"Latency in ms for target",
[]string{"target"}, nil,
),
}
}
@@ -148,10 +181,19 @@ func (collector *speedCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.downMetric
ch <- collector.upMetric
ch <- collector.latencyMetric
ch <- collector.downMetricTarget
ch <- collector.upMetricTarget
ch <- collector.latencyMetricTarget
}
func (collector *speedCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(collector.downMetric, prometheus.CounterValue, downspeed)
ch <- prometheus.MustNewConstMetric(collector.upMetric, prometheus.CounterValue, upspeed)
ch <- prometheus.MustNewConstMetric(collector.latencyMetric, prometheus.CounterValue, float64(latency.Milliseconds()))
for target, result := range allTargets {
ch <- prometheus.MustNewConstMetric(collector.downMetricTarget, prometheus.CounterValue, result.Downspeed, target)
ch <- prometheus.MustNewConstMetric(collector.upMetricTarget, prometheus.CounterValue, result.Upspeed, target)
ch <- prometheus.MustNewConstMetric(collector.latencyMetricTarget, prometheus.CounterValue, float64(result.Latency.Milliseconds()), target)
}
}