feat(inputs.internet_speed): server ID include and exclude filter (#12617)
This commit is contained in:
parent
fc1fb2fcd9
commit
c42d8e30b6
|
|
@ -34,20 +34,38 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||||
|
|
||||||
## Caches the closest server location
|
## Caches the closest server location
|
||||||
# cache = false
|
# 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
|
## 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 |
|
| Download Speed | download | float64 | Mbps |
|
||||||
| Upload Speed | upload | float64 | Mbps |
|
| Upload Speed | upload | float64 | Mbps |
|
||||||
| Latency | latency | float64 | ms |
|
| Latency | latency | float64 | ms |
|
||||||
|
|
||||||
|
And the following tags:
|
||||||
|
|
||||||
|
| Name | tag name |
|
||||||
|
| --------- | --------- |
|
||||||
|
| Host | host |
|
||||||
|
| Server ID | server_id |
|
||||||
|
|
||||||
## Example Output
|
## Example Output
|
||||||
|
|
||||||
```sh
|
```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
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/showwin/speedtest-go/speedtest"
|
"github.com/showwin/speedtest-go/speedtest"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
"github.com/influxdata/telegraf/filter"
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"github.com/influxdata/telegraf/plugins/inputs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -17,11 +18,16 @@ var sampleConfig string
|
||||||
|
|
||||||
// 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" deprecated:"1.25.0;use 'memory_saving_mode' instead"`
|
ServerIDInclude []string `toml:"server_id_include"`
|
||||||
MemorySavingMode bool `toml:"memory_saving_mode"`
|
ServerIDExclude []string `toml:"server_id_exclude"`
|
||||||
Cache bool `toml:"cache"`
|
EnableFileDownload bool `toml:"enable_file_download" deprecated:"1.25.0;use 'memory_saving_mode' instead"`
|
||||||
Log telegraf.Logger `toml:"-"`
|
MemorySavingMode bool `toml:"memory_saving_mode"`
|
||||||
serverCache *speedtest.Server
|
Cache bool `toml:"cache"`
|
||||||
|
|
||||||
|
Log telegraf.Logger `toml:"-"`
|
||||||
|
|
||||||
|
server *speedtest.Server
|
||||||
|
serverFilter filter.Filter
|
||||||
}
|
}
|
||||||
|
|
||||||
const measurement = "internet_speed"
|
const measurement = "internet_speed"
|
||||||
|
|
@ -33,63 +39,76 @@ func (*InternetSpeed) SampleConfig() string {
|
||||||
func (is *InternetSpeed) Init() error {
|
func (is *InternetSpeed) Init() error {
|
||||||
is.MemorySavingMode = is.MemorySavingMode || is.EnableFileDownload
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (is *InternetSpeed) Gather(acc telegraf.Accumulator) error {
|
func (is *InternetSpeed) Gather(acc telegraf.Accumulator) error {
|
||||||
// Get closest server
|
// if not caching, go find closest server each time
|
||||||
s := is.serverCache
|
if !is.Cache || is.server == nil {
|
||||||
if s == nil {
|
if err := is.findClosestServer(); err != nil {
|
||||||
user, err := speedtest.FetchUserInfo()
|
return fmt.Errorf("unable to find closest server: %w", err)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is.Log.Debug("Starting Speed Test")
|
err := is.server.PingTest()
|
||||||
is.Log.Debug("Running Ping...")
|
|
||||||
err := s.PingTest()
|
|
||||||
if err != nil {
|
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 = is.server.DownloadTest(is.MemorySavingMode)
|
||||||
err = s.DownloadTest(is.MemorySavingMode)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
is.Log.Debug("try `memory_saving_mode = true` if this fails consistently")
|
return fmt.Errorf("download test failed, try `memory_saving_mode = true` if this fails consistently: %w", err)
|
||||||
return fmt.Errorf("download test failed: %v", err)
|
|
||||||
}
|
}
|
||||||
is.Log.Debug("Running Upload...")
|
err = is.server.UploadTest(is.MemorySavingMode)
|
||||||
err = s.UploadTest(is.MemorySavingMode)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
is.Log.Debug("try `memory_saving_mode = true` if this fails consistently")
|
return fmt.Errorf("upload test failed failed, try `memory_saving_mode = true` if this fails consistently: %w", err)
|
||||||
return fmt.Errorf("upload test failed failed: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
is.Log.Debug("Test finished.")
|
fields := map[string]any{
|
||||||
|
"download": is.server.DLSpeed,
|
||||||
fields := make(map[string]interface{})
|
"upload": is.server.ULSpeed,
|
||||||
fields["download"] = s.DLSpeed
|
"latency": timeDurationMillisecondToFloat64(is.server.Latency),
|
||||||
fields["upload"] = s.ULSpeed
|
}
|
||||||
fields["latency"] = timeDurationMillisecondToFloat64(s.Latency)
|
tags := map[string]string{
|
||||||
|
"server_id": is.server.ID,
|
||||||
tags := make(map[string]string)
|
"host": is.server.Host,
|
||||||
|
}
|
||||||
|
|
||||||
acc.AddFields(measurement, fields, tags)
|
acc.AddFields(measurement, fields, tags)
|
||||||
return nil
|
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() {
|
func init() {
|
||||||
inputs.Add("internet_speed", func() telegraf.Input {
|
inputs.Add("internet_speed", func() telegraf.Input {
|
||||||
return &InternetSpeed{}
|
return &InternetSpeed{}
|
||||||
|
|
|
||||||
|
|
@ -10,3 +10,14 @@
|
||||||
|
|
||||||
## Caches the closest server location
|
## Caches the closest server location
|
||||||
# cache = false
|
# 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 = []
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue