feat: Add caching to internet_speed (#10530)
Co-authored-by: Sven Rebhan <36194019+srebhan@users.noreply.github.com>
This commit is contained in:
parent
6dc8e9d8b9
commit
85ee82584d
|
|
@ -8,8 +8,10 @@ The `Internet Speed Monitor` collects data about the internet speed on the syste
|
||||||
# Monitors internet speed in the network
|
# Monitors internet speed in the network
|
||||||
[[inputs.internet_speed]]
|
[[inputs.internet_speed]]
|
||||||
## Sets if runs file download test
|
## Sets if runs file download test
|
||||||
## Default: false
|
# enable_file_download = false
|
||||||
enable_file_download = false
|
|
||||||
|
## Caches the closest server location
|
||||||
|
# cache = false
|
||||||
```
|
```
|
||||||
|
|
||||||
## Metrics
|
## Metrics
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,17 @@ import (
|
||||||
// InternetSpeed is used to store configuration values.
|
// InternetSpeed is used to store configuration values.
|
||||||
type InternetSpeed struct {
|
type InternetSpeed struct {
|
||||||
EnableFileDownload bool `toml:"enable_file_download"`
|
EnableFileDownload bool `toml:"enable_file_download"`
|
||||||
|
Cache bool `toml:"cache"`
|
||||||
Log telegraf.Logger `toml:"-"`
|
Log telegraf.Logger `toml:"-"`
|
||||||
|
serverCache *speedtest.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
const sampleConfig = `
|
const sampleConfig = `
|
||||||
## Sets if runs file download test
|
## 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.
|
// Description returns information about the plugin.
|
||||||
|
|
@ -34,22 +38,31 @@ func (is *InternetSpeed) SampleConfig() string {
|
||||||
const measurement = "internet_speed"
|
const measurement = "internet_speed"
|
||||||
|
|
||||||
func (is *InternetSpeed) Gather(acc telegraf.Accumulator) error {
|
func (is *InternetSpeed) Gather(acc telegraf.Accumulator) error {
|
||||||
user, err := speedtest.FetchUserInfo()
|
|
||||||
if err != nil {
|
// Get closest server
|
||||||
return fmt.Errorf("fetching user info failed: %v", err)
|
s := is.serverCache
|
||||||
}
|
if s == nil {
|
||||||
serverList, err := speedtest.FetchServerList(user)
|
user, err := speedtest.FetchUserInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("fetching server list failed: %v", err)
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(serverList.Servers) < 1 {
|
|
||||||
return fmt.Errorf("no servers found")
|
|
||||||
}
|
|
||||||
s := serverList.Servers[0]
|
|
||||||
is.Log.Debug("Starting Speed Test")
|
is.Log.Debug("Starting Speed Test")
|
||||||
is.Log.Debug("Running Ping...")
|
is.Log.Debug("Running Ping...")
|
||||||
err = s.PingTest()
|
err := s.PingTest()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("ping test failed: %v", err)
|
return fmt.Errorf("ping test failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -76,6 +89,7 @@ func (is *InternetSpeed) Gather(acc telegraf.Accumulator) error {
|
||||||
acc.AddFields(measurement, fields, tags)
|
acc.AddFields(measurement, fields, tags)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
inputs.Add("internet_speed", func() telegraf.Input {
|
inputs.Add("internet_speed", func() telegraf.Input {
|
||||||
return &InternetSpeed{}
|
return &InternetSpeed{}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue