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: Internet Speed Monitor Input Plugin #9623

Merged
merged 12 commits into from
Sep 14, 2021
1 change: 1 addition & 0 deletions docs/LICENSE_OF_DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ following works:
- github.com/safchain/ethtool [Apache License 2.0](https://github.com/safchain/ethtool/blob/master/LICENSE)
- github.com/samuel/go-zookeeper [BSD 3-Clause Clear License](https://github.com/samuel/go-zookeeper/blob/master/LICENSE)
- github.com/shirou/gopsutil [BSD 3-Clause Clear License](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- github.com/showwin/speedtest-go [MIT License](https://github.com/showwin/speedtest-go/blob/master/LICENSE)
- github.com/signalfx/com_signalfx_metrics_protobuf [Apache License 2.0](https://github.com/signalfx/com_signalfx_metrics_protobuf/blob/master/LICENSE)
- github.com/signalfx/gohistogram [MIT License](https://github.com/signalfx/gohistogram/blob/master/LICENSE)
- github.com/signalfx/golib [Apache License 2.0](https://github.com/signalfx/golib/blob/master/LICENSE)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
github.com/aerospike/aerospike-client-go v1.27.0
github.com/alecthomas/participle v0.4.1 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1004
github.com/amir/raidman v0.0.0-20170415203553-1ccc43bfb9c9
github.com/antchfx/jsonquery v1.1.4
Expand Down Expand Up @@ -232,6 +232,7 @@ require (
github.com/sensu/sensu-go/api/core/v2 v2.9.0
github.com/shirou/gopsutil v3.21.6-0.20210624221800-cb512c850043+incompatible
github.com/shopspring/decimal v0.0.0-20200105231215-408a2507e114 // indirect
github.com/showwin/speedtest-go v1.1.4
github.com/signalfx/com_signalfx_metrics_protobuf v0.0.2 // indirect
github.com/signalfx/gohistogram v0.0.0-20160107210732-1ccfd2ff5083 // indirect
github.com/signalfx/golib/v3 v3.3.34
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 h1:AUNCr9CiJuwrRYS3XieqF+Z9B9gNxo/eANAJCF2eiN4=
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:CgnQgUtFrFz9mxFNtED3jI5tLDjKlOM+oUF/sTk6ps0=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1004 h1:YtaYjXmemIMyySUbs0VGFPqsLpsNHf4TW/L6yqpJQ9s=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1004/go.mod h1:pUKYbK5JQ+1Dfxk80P0qxGqe5dkxDoabbZS7zOcouyA=
Expand Down Expand Up @@ -1423,6 +1424,8 @@ github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/shopspring/decimal v0.0.0-20200105231215-408a2507e114 h1:Pm6R878vxWWWR+Sa3ppsLce/Zq+JNTs6aVvRu13jv9A=
github.com/shopspring/decimal v0.0.0-20200105231215-408a2507e114/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/showwin/speedtest-go v1.1.4 h1:pcY1W5LYZu44lH6Fuu80nu/Pj67n//VArlZudbAgR6E=
github.com/showwin/speedtest-go v1.1.4/go.mod h1:dJugxvC/AQDt4HQQKZ9lKNa2+b1c8nzj9IL0a/F8l1U=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/inputs/intel_powerstat"
_ "github.com/influxdata/telegraf/plugins/inputs/intel_rdt"
_ "github.com/influxdata/telegraf/plugins/inputs/internal"
_ "github.com/influxdata/telegraf/plugins/inputs/internet_speed"
_ "github.com/influxdata/telegraf/plugins/inputs/interrupts"
_ "github.com/influxdata/telegraf/plugins/inputs/ipmi_sensor"
_ "github.com/influxdata/telegraf/plugins/inputs/ipset"
Expand Down
30 changes: 30 additions & 0 deletions plugins/inputs/internet_speed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Internet Speed Monitor

The `Internet Speed Monitor` collects data about the internet speed on the system.

## Configuration

```toml
# Monitors internet speed in the network
[[inputs.internet_speed]]
## Sets if runs file download test
## Default: false
enable_file_download = false
```

## Metrics

It collects latency, download speed and upload speed


| Name | filed name | type | Unit |
| -------------- | ---------- | ------- | ---- |
| Download Speed | download | float64 | Mbps |
| Upload Speed | upload | float64 | Mbps |
| Latency | latency | float64 | ms |

## Example Output

```sh
internet_speed,host=Sanyam-Ubuntu download=41.791,latency=28.518,upload=59.798 1631031183000000000
```
84 changes: 84 additions & 0 deletions plugins/inputs/internet_speed/internet_speed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package internet_speed

import (
"fmt"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/showwin/speedtest-go/speedtest"
)

// InternetSpeed is used to store configuration values.
type InternetSpeed struct {
EnableFileDownload bool `toml:"enable_file_download"`
Log telegraf.Logger `toml:"-"`
}

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

// Description returns information about the plugin.
func (is *InternetSpeed) Description() string {
return "Monitors internet speed using speedtest.net service"
}

// SampleConfig displays configuration instructions.
func (is *InternetSpeed) SampleConfig() string {
return sampleConfig
}

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")
}
s := serverList.Servers[0]

is.Log.Debug("Starting Speed Test")
is.Log.Debug("Running Ping...")
err = s.PingTest()
if err != nil {
return fmt.Errorf("ping test failed: %v", err)
}
is.Log.Debug("Running Download...")
err = s.DownloadTest(is.EnableFileDownload)
if err != nil {
return fmt.Errorf("download test failed: %v", err)
}
is.Log.Debug("Running Upload...")
err = s.UploadTest(is.EnableFileDownload)
if err != nil {
return fmt.Errorf("upload test failed failed: %v", err)
}

is.Log.Debug("Test finished.")

fields := make(map[string]interface{})
fields["download"] = s.DLSpeed
fields["upload"] = s.ULSpeed
fields["latency"] = s.Latency

tags := make(map[string]string)

acc.AddFields(measurement, fields, tags)
return nil
}
func init() {
inputs.Add("internet_speed", func() telegraf.Input {
return &InternetSpeed{}
})
}
44 changes: 44 additions & 0 deletions plugins/inputs/internet_speed/internet_speed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package internet_speed

import (
"testing"

"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

func TestGathering(t *testing.T) {
if testing.Short() {
t.Skip("Skipping network-dependent test in short mode.")
}
internetSpeed := &InternetSpeed{
EnableFileDownload: false,
Log: testutil.Logger{},
}

acc := &testutil.Accumulator{}

require.NoError(t, internetSpeed.Gather(acc))
}

func TestDataGen(t *testing.T) {
if testing.Short() {
t.Skip("Skipping network-dependent test in short mode.")
}
internetSpeed := &InternetSpeed{
EnableFileDownload: false,
Log: testutil.Logger{},
}

acc := &testutil.Accumulator{}
require.NoError(t, internetSpeed.Gather(acc))

metric, ok := acc.Get("internet_speed")
require.True(t, ok)

tags := metric.Tags

fields := metric.Fields

acc.AssertContainsTaggedFields(t, "internet_speed", fields, tags)
}