Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Sheiko committed Aug 3, 2022
0 parents commit 8a4014f
Show file tree
Hide file tree
Showing 11 changed files with 688 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cloudping
*.py
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2022 Nomadic

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# cloudping

This util performs a ping to measure the network latency from your location to the various cloud data centers around the world.

### Supported providers

- Alibaba Cloud
- Amazon Web Services
- Digital Ocean
- Google Cloud Platform
- Hetzner Cloud
- IBM Cloud
- Linode
- Microsoft Azure
- Oracle Cloud
- OVH Cloud
- Scaleway
- Vultr

## Install

```
go install github.com/n0madic/cloudping@latest
```

## Usage

```
Usage: cloudping [--all] [--count COUNT] [--region REGION] [--location LOCATION] [--provider PROVIDER] [--timeout TIMEOUT]
Options:
--all, -a Scan all providers
--count COUNT, -c COUNT
Number of pings to send [default: 4]
--region REGION, -r REGION
Filter by regions, can be specified multiple times
--location LOCATION, -l LOCATION
Filter by location, can be specified multiple times
--provider PROVIDER, -p PROVIDER
Choose provider for ping [default: aws]
--timeout TIMEOUT, -t TIMEOUT
Timeout before ping ends [default: 5s]
--help, -h display this help and exit
```

```
$ cloudping --provider vultr --region us --region ca
+----+-------+-----------+--------------------+-----------+--------+
| | CLOUD | REGION | LOCATION | RTT | STATUS |
+----+-------+-----------+--------------------+-----------+--------+
| 1 | Vultr | nj-us | New Jersey, US | 123.422ms | OK |
| 2 | Vultr | tor-ca | Toronto, CA | 134.604ms | OK |
| 3 | Vultr | ga-us | Atlanta, US | 137.333ms | OK |
| 4 | Vultr | fl-us | Miami, US | 145.317ms | OK |
| 5 | Vultr | il-us | Chicago, US | 149.162ms | OK |
| 6 | Vultr | tx-us | Dallas, US | 161.395ms | OK |
| 7 | Vultr | lax-ca-us | Los Angeles, US | 185.347ms | OK |
| 8 | Vultr | wa-us | Seattle, US | 185.407ms | OK |
| 9 | Vultr | sjo-ca-us | Silicon Valley, US | 185.909ms | OK |
| 10 | Vultr | hon-hi-us | Honolulu, US | 238.706ms | OK |
+----+-------+-----------+--------------------+-----------+--------+
```
29 changes: 29 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"sort"
"time"
)

type config struct {
All bool `arg:"--all,-a" help:"Scan all providers"`
Count int `arg:"--count,-c" default:"4" help:"Number of pings to send"`
FilterRegion []string `arg:"--region,-r,separate" help:"Filter by regions, can be specified multiple times"`
FilterLocation []string `arg:"--location,-l,separate" help:"Filter by location, can be specified multiple times"`
Provider string `arg:"--provider,-p" default:"aws" help:"Choose provider for ping"`
Timeout time.Duration `arg:"--timeout,-t" default:"5s" help:"Timeout before ping ends"`
}

func (c *config) Description() string {
keys := make([]string, 0, len(providers))
for k := range providers {
keys = append(keys, k)
}
sort.Strings(keys)
desc := "Supported providers for cloud ping:\n"
for _, k := range keys {
desc += fmt.Sprintf("%s - %s\n", k, providers[k].name)
}
return desc
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/n0madic/cloudping

go 1.16

require (
github.com/alexflint/go-arg v1.4.3
github.com/go-ping/ping v1.1.0
github.com/jedib0t/go-pretty/v6 v6.3.6
github.com/valyala/fasthttp v1.38.0
)
61 changes: 61 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
github.com/alexflint/go-arg v1.4.3 h1:9rwwEBpMXfKQKceuZfYcwuc/7YY7tWJbFsgG5cAU/uo=
github.com/alexflint/go-arg v1.4.3/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA=
github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM=
github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ping/ping v1.1.0 h1:3MCGhVX4fyEUuhsfwPrsEdQw6xspHkv5zHsiSoDFZYw=
github.com/go-ping/ping v1.1.0/go.mod h1:xIFjORFzTxqIV/tDVGO4eDy/bLuSyawEeojSm3GfRGk=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jedib0t/go-pretty/v6 v6.3.6 h1:A6w2BuyPMtf7M82BGRBys9bAba2C26ZX9lrlrZ7uH6U=
github.com/jedib0t/go-pretty/v6 v6.3.6/go.mod h1:MgmISkTWDSFu0xOqiZ0mKNntMQ2mDgOcwOkwBEkMDJI=
github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U=
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4 h1:wZRexSlwd7ZXfKINDLsO4r7WBt3gTKONc6K/VesHvHM=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.38.0 h1:yTjSSNjuDi2PPvXY2836bIwLmiTS2T4T9p1coQshpco=
github.com/valyala/fasthttp v1.38.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
92 changes: 92 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"fmt"
"os"
"sort"
"strings"
"sync"
"time"

"github.com/alexflint/go-arg"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
)

var (
args config
wg sync.WaitGroup
)

func main() {
arg.MustParse(&args)

for key, provider := range providers {
if args.All || key == args.Provider {
for _, region := range provider.regions {
if (len(args.FilterRegion) > 0 && !isFiltered(region.name, args.FilterRegion)) ||
(len(args.FilterLocation) > 0 && !isFiltered(region.location, args.FilterLocation)) {
continue
}
wg.Add(1)
go endpointPing(provider.hostTemplate, region)
}
}
}
wg.Wait()

results := []*region{}
for key, provider := range providers {
if args.All || key == args.Provider {
for _, region := range provider.regions {
region.code = provider.name
results = append(results, region)
}
}
}
if len(results) == 0 {
fmt.Printf("Provider %s not found\n", args.Provider)
os.Exit(1)
}
sort.Slice(results, func(i, j int) bool {
return results[i].rtt < results[j].rtt
})

t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetAutoIndex(true)
t.AppendHeader(table.Row{"Cloud", "Region", "Location", "RTT", "Status"})
for _, region := range results {
status := "OK"
if region.err != nil {
status = fmt.Sprintf("ERROR: %s", region.err)
} else if region.rtt == 0 {
continue
}
rtt := region.rtt.Round(time.Microsecond)
if rtt > time.Second {
rtt = rtt.Round(time.Millisecond)
}
t.AppendRow(table.Row{region.code, region.name, region.location, rtt, status})
}
t.SetColumnConfigs([]table.ColumnConfig{
{
Name: "Status",
Transformer: text.Transformer(func(val interface{}) string {
color := text.FgGreen
str := val.(string)
if strings.Contains(str, "ERROR") {
color = text.FgRed
}
return color.Sprintf("%s", val)
}),
WidthMax: 55,
},
})
if t.Length() > 0 {
t.Render()
} else {
fmt.Println("No results found")
os.Exit(2)
}
}
83 changes: 83 additions & 0 deletions ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"fmt"
"strings"
"time"

"github.com/go-ping/ping"
"github.com/valyala/fasthttp"
)

func endpointPing(template string, region *region) {
defer wg.Done()

if region.host == "" {
code := region.name
if region.code != "" {
code = region.code
}
region.host = fmt.Sprintf(template, code)
}

if strings.HasPrefix(region.host, "http") {
var err error
region.rtt, err = httpPing(region.host, args.Count, args.Timeout)
if err != nil {
region.err = err
}
} else {
pinger, err := ping.NewPinger(region.host)
if err != nil {
region.err = err
return
}
defer pinger.Stop()

pinger.Count = args.Count
pinger.Timeout = args.Timeout

resolve_count := 0
for {
err = pinger.Resolve()
if err == nil || resolve_count > 3 {
break
}
resolve_count++
}

err = pinger.Run()
if err != nil {
region.err = err
return
}

region.rtt = pinger.Statistics().AvgRtt
}

if region.rtt == 0 && region.err == nil {
region.err = fmt.Errorf("timeout")
}
}

func httpPing(url string, count int, timeout time.Duration) (time.Duration, error) {
var rtt time.Duration
client := fasthttp.Client{
ReadTimeout: timeout,
}
req := fasthttp.AcquireRequest()
req.SetRequestURI(url)
req.Header.SetMethod(fasthttp.MethodGet)
defer fasthttp.ReleaseRequest(req)
for i := 0; i < count; i++ {
start := time.Now()
resp := fasthttp.AcquireResponse()
err := client.Do(req, resp)
fasthttp.ReleaseResponse(resp)
if err != nil {
return 0, err
}
rtt += time.Since(start)
}
return rtt / time.Duration(count), nil
}
Loading

0 comments on commit 8a4014f

Please sign in to comment.