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

feat: Add caching to internet_speed #10530

Merged
merged 14 commits into from
Feb 1, 2022
9 changes: 7 additions & 2 deletions plugins/inputs/internet_speed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ The `Internet Speed Monitor` collects data about the internet speed on the syste
# Monitors internet speed in the network
[[inputs.internet_speed]]
## Sets if runs file download test
## Default: false
enable_file_download = false
# enable_file_download = false

## Time to sleep before running the speed test
# offset = "0s"

## Caches the closest server location
# cache = false
```

## Metrics
Expand Down
42 changes: 28 additions & 14 deletions plugins/inputs/internet_speed/internet_speed.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ import (
// InternetSpeed is used to store configuration values.
type InternetSpeed struct {
EnableFileDownload bool `toml:"enable_file_download"`
Cache bool `toml:"cache"`
Log telegraf.Logger `toml:"-"`
serverCache *speedtest.Server
}

const sampleConfig = `
## Sets if runs file download test
## Default: false
enable_file_download = false
# enable_file_download = false

## Caches the closest server location
# cache = false
`

// Description returns information about the plugin.
Expand All @@ -34,22 +38,31 @@ func (is *InternetSpeed) SampleConfig() string {
const measurement = "internet_speed"

func (is *InternetSpeed) Gather(acc telegraf.Accumulator) error {
user, err := speedtest.FetchUserInfo()
if err != nil {
return fmt.Errorf("fetching user info failed: %v", err)
}
serverList, err := speedtest.FetchServerList(user)
if err != nil {
return fmt.Errorf("fetching server list failed: %v", err)
}

if len(serverList.Servers) < 1 {
return fmt.Errorf("no servers found")
// Get closest server
s := is.serverCache
if s == nil {
user, err := speedtest.FetchUserInfo()
if err != nil {
return fmt.Errorf("fetching user info failed: %v", err)
}
serverList, err := speedtest.FetchServerList(user)
if err != nil {
return fmt.Errorf("fetching server list failed: %v", err)
}
if len(serverList.Servers) < 1 {
return fmt.Errorf("no servers found")
}
s = serverList.Servers[0]
is.Log.Debugf("Found server: %v", s)
if is.Cache {
is.serverCache = s
}
}
s := serverList.Servers[0]

is.Log.Debug("Starting Speed Test")
is.Log.Debug("Running Ping...")
err = s.PingTest()
err := s.PingTest()
if err != nil {
return fmt.Errorf("ping test failed: %v", err)
}
Expand All @@ -76,6 +89,7 @@ func (is *InternetSpeed) Gather(acc telegraf.Accumulator) error {
acc.AddFields(measurement, fields, tags)
return nil
}

func init() {
inputs.Add("internet_speed", func() telegraf.Input {
return &InternetSpeed{}
Expand Down