Compare commits
2 Commits
650391bd2c
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
43f1d844df
|
|||
|
aac44d3c8a
|
130
main.go
130
main.go
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
@@ -21,12 +20,22 @@ var (
|
|||||||
interval int
|
interval int
|
||||||
port int
|
port int
|
||||||
serverID int
|
serverID int
|
||||||
|
cutOff float64
|
||||||
|
|
||||||
|
allTargets = map[string]Results{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Results struct {
|
||||||
|
Latency time.Duration
|
||||||
|
Downspeed float64
|
||||||
|
Upspeed float64
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.IntVar(&interval, "interval", 30, "Duration, in minutes, between speedtest runs")
|
flag.IntVar(&interval, "interval", 30, "Duration, in minutes, between speedtest runs")
|
||||||
flag.IntVar(&port, "port", 9091, "The port to listen on")
|
flag.IntVar(&port, "port", 9091, "The port to listen on")
|
||||||
flag.IntVar(&serverID, "server", 55137, "The ID of the server to test against")
|
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()
|
flag.Parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,57 +83,69 @@ func checkSpeed() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
target := targets[0]
|
log.Println("Testing main server...")
|
||||||
log.Printf("Testing against server: %s - %s [%s]\n", target.Name, target.Sponsor, target.ID)
|
latency, downspeed, upspeed, err = TestServer(targets[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
err = target.PingTest()
|
log.Println("Testing all servers...")
|
||||||
if err != nil {
|
for _, target := range serverList {
|
||||||
log.Printf("Error running ping test: %v\n", err)
|
latency, downspeed, upspeed, err := TestServer(target)
|
||||||
return err
|
if err != nil {
|
||||||
}
|
continue
|
||||||
err = target.DownloadTest(false)
|
}
|
||||||
if err != nil {
|
allTargets[fmt.Sprintf("%s - %s - %s", target.ID, target.Name, target.Sponsor)] = Results{
|
||||||
log.Printf("Error running download test: %v\n", err)
|
Latency: latency,
|
||||||
return err
|
Downspeed: downspeed,
|
||||||
}
|
Upspeed: upspeed,
|
||||||
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("-------------------------------------")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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 {
|
type speedCollector struct {
|
||||||
downMetric *prometheus.Desc
|
downMetric *prometheus.Desc
|
||||||
upMetric *prometheus.Desc
|
upMetric *prometheus.Desc
|
||||||
latencyMetric *prometheus.Desc
|
latencyMetric *prometheus.Desc
|
||||||
|
downMetricTarget *prometheus.Desc
|
||||||
|
upMetricTarget *prometheus.Desc
|
||||||
|
latencyMetricTarget *prometheus.Desc
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSpeedCollector() *speedCollector {
|
func newSpeedCollector() *speedCollector {
|
||||||
@@ -141,6 +162,18 @@ func newSpeedCollector() *speedCollector {
|
|||||||
"Latency in ms",
|
"Latency in ms",
|
||||||
nil, nil,
|
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.downMetric
|
||||||
ch <- collector.upMetric
|
ch <- collector.upMetric
|
||||||
ch <- collector.latencyMetric
|
ch <- collector.latencyMetric
|
||||||
|
ch <- collector.downMetricTarget
|
||||||
|
ch <- collector.upMetricTarget
|
||||||
|
ch <- collector.latencyMetricTarget
|
||||||
}
|
}
|
||||||
|
|
||||||
func (collector *speedCollector) Collect(ch chan<- prometheus.Metric) {
|
func (collector *speedCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
ch <- prometheus.MustNewConstMetric(collector.downMetric, prometheus.CounterValue, downspeed)
|
ch <- prometheus.MustNewConstMetric(collector.downMetric, prometheus.CounterValue, downspeed)
|
||||||
ch <- prometheus.MustNewConstMetric(collector.upMetric, prometheus.CounterValue, upspeed)
|
ch <- prometheus.MustNewConstMetric(collector.upMetric, prometheus.CounterValue, upspeed)
|
||||||
ch <- prometheus.MustNewConstMetric(collector.latencyMetric, prometheus.CounterValue, float64(latency.Milliseconds()))
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user