feat(inputs.internet_speed): server ID include and exclude filter (#12617)

This commit is contained in:
Joshua Powers 2023-02-07 10:52:21 -07:00 committed by GitHub
parent fc1fb2fcd9
commit c42d8e30b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 46 deletions

View File

@ -34,20 +34,38 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## Caches the closest server location
# cache = false
## Server ID exclude filter
## Allows the user to exclude or include specific server IDs received by
## speedtest-go. Values in the exclude option will be skipped over. Values in
## the include option are the only options that will be picked from.
##
## See the list of servers speedtest-go will return at:
## https://www.speedtest.net/api/js/servers?engine=js&limit=10
##
# server_id_exclude = []
# server_id_include = []
```
## Metrics
It collects latency, download speed and upload speed
It collects the following fields:
| Name | filed name | type | Unit |
| Name | field name | type | Unit |
| -------------- | ---------- | ------- | ---- |
| Download Speed | download | float64 | Mbps |
| Upload Speed | upload | float64 | Mbps |
| Latency | latency | float64 | ms |
And the following tags:
| Name | tag name |
| --------- | --------- |
| Host | host |
| Server ID | server_id |
## Example Output
```sh
internet_speed,host=Sanyam-Ubuntu download=41.791,latency=28.518,upload=59.798 1631031183000000000
internet_speed,host=speedtest02.z4internet.com:8080,server_id=54619 download=318.37580265897725,upload=30.444407341274385,latency=37.73174 1675458921000000000
```

View File

@ -9,6 +9,7 @@ import (
"github.com/showwin/speedtest-go/speedtest"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf/plugins/inputs"
)
@ -17,11 +18,16 @@ var sampleConfig string
// InternetSpeed is used to store configuration values.
type InternetSpeed struct {
EnableFileDownload bool `toml:"enable_file_download" deprecated:"1.25.0;use 'memory_saving_mode' instead"`
MemorySavingMode bool `toml:"memory_saving_mode"`
Cache bool `toml:"cache"`
Log telegraf.Logger `toml:"-"`
serverCache *speedtest.Server
ServerIDInclude []string `toml:"server_id_include"`
ServerIDExclude []string `toml:"server_id_exclude"`
EnableFileDownload bool `toml:"enable_file_download" deprecated:"1.25.0;use 'memory_saving_mode' instead"`
MemorySavingMode bool `toml:"memory_saving_mode"`
Cache bool `toml:"cache"`
Log telegraf.Logger `toml:"-"`
server *speedtest.Server
serverFilter filter.Filter
}
const measurement = "internet_speed"
@ -33,63 +39,76 @@ func (*InternetSpeed) SampleConfig() string {
func (is *InternetSpeed) Init() error {
is.MemorySavingMode = is.MemorySavingMode || is.EnableFileDownload
var err error
is.serverFilter, err = filter.NewIncludeExcludeFilter(is.ServerIDInclude, is.ServerIDExclude)
if err != nil {
return fmt.Errorf("error compiling server ID filters: %w", err)
}
return nil
}
func (is *InternetSpeed) Gather(acc telegraf.Accumulator) error {
// 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.FetchServers(user)
if err != nil {
return fmt.Errorf("fetching server list failed: %v", err)
}
if len(serverList) < 1 {
return fmt.Errorf("no servers found")
}
s = serverList[0]
is.Log.Debugf("Found server: %v", s)
if is.Cache {
is.serverCache = s
// if not caching, go find closest server each time
if !is.Cache || is.server == nil {
if err := is.findClosestServer(); err != nil {
return fmt.Errorf("unable to find closest server: %w", err)
}
}
is.Log.Debug("Starting Speed Test")
is.Log.Debug("Running Ping...")
err := s.PingTest()
err := is.server.PingTest()
if err != nil {
return fmt.Errorf("ping test failed: %v", err)
return fmt.Errorf("ping test failed: %w", err)
}
is.Log.Debug("Running Download...")
err = s.DownloadTest(is.MemorySavingMode)
err = is.server.DownloadTest(is.MemorySavingMode)
if err != nil {
is.Log.Debug("try `memory_saving_mode = true` if this fails consistently")
return fmt.Errorf("download test failed: %v", err)
return fmt.Errorf("download test failed, try `memory_saving_mode = true` if this fails consistently: %w", err)
}
is.Log.Debug("Running Upload...")
err = s.UploadTest(is.MemorySavingMode)
err = is.server.UploadTest(is.MemorySavingMode)
if err != nil {
is.Log.Debug("try `memory_saving_mode = true` if this fails consistently")
return fmt.Errorf("upload test failed failed: %v", err)
return fmt.Errorf("upload test failed failed, try `memory_saving_mode = true` if this fails consistently: %w", err)
}
is.Log.Debug("Test finished.")
fields := make(map[string]interface{})
fields["download"] = s.DLSpeed
fields["upload"] = s.ULSpeed
fields["latency"] = timeDurationMillisecondToFloat64(s.Latency)
tags := make(map[string]string)
fields := map[string]any{
"download": is.server.DLSpeed,
"upload": is.server.ULSpeed,
"latency": timeDurationMillisecondToFloat64(is.server.Latency),
}
tags := map[string]string{
"server_id": is.server.ID,
"host": is.server.Host,
}
acc.AddFields(measurement, fields, tags)
return nil
}
func (is *InternetSpeed) findClosestServer() error {
user, err := speedtest.FetchUserInfo()
if err != nil {
return fmt.Errorf("fetching user info failed: %w", err)
}
serverList, err := speedtest.FetchServers(user)
if err != nil {
return fmt.Errorf("fetching server list failed: %w", err)
}
if len(serverList) < 1 {
return fmt.Errorf("no servers found")
}
// return the first match
for _, server := range serverList {
if is.serverFilter.Match(server.ID) {
is.server = server
is.Log.Debugf("using server %s in %s (%s)\n", is.server.ID, is.server.Name, is.server.Host)
return nil
}
}
return fmt.Errorf("no server set: filter excluded all servers")
}
func init() {
inputs.Add("internet_speed", func() telegraf.Input {
return &InternetSpeed{}

View File

@ -10,3 +10,14 @@
## Caches the closest server location
# cache = false
## Server ID exclude filter
## Allows the user to exclude or include specific server IDs received by
## speedtest-go. Values in the exclude option will be skipped over. Values in
## the include option are the only options that will be picked from.
##
## See the list of servers speedtest-go will return at:
## https://www.speedtest.net/api/js/servers?engine=js&limit=10
##
# server_id_exclude = []
# server_id_include = []