Skip to content

Commit 43c8a00

Browse files
w3st3rypablito-perez
authored andcommitted
Dumb ping plugin (#34)
Signed-off-by: Valentin Pichard <[email protected]>
1 parent a760803 commit 43c8a00

File tree

256 files changed

+19203
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+19203
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ Browse [builtin actions](./pkg/plugins/builtin)
428428
| || `to`: receiver(s) of your email
429429
| || `subject`: subject of your email
430430
| || `body`: content of your email
431+
|**`ping`** | Send a ping to an hostname *Warn: This plugin will keep running until the count is done*
432+
| || `hostname`: ping destination
433+
| || `count`: number of ping you want execute
434+
| || `interval_second`: interval between two pings
431435

432436

433437
#### Loops

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ require (
4848
github.com/satori/go.uuid v1.2.0 // indirect
4949
github.com/sirupsen/logrus v1.4.2
5050
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
51+
github.com/sparrc/go-ping v0.0.0-20190613174326-4e5b6552494c
5152
github.com/spf13/afero v1.2.2 // indirect
5253
github.com/spf13/cobra v0.0.5
5354
github.com/spf13/jwalterweatherman v1.1.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 h1:WN9BUFbd
227227
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
228228
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
229229
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
230+
github.com/sparrc/go-ping v0.0.0-20190613174326-4e5b6552494c h1:gqEdF4VwBu3lTKGHS9rXE9x1/pEaSwCXRLOZRF6qtlw=
231+
github.com/sparrc/go-ping v0.0.0-20190613174326-4e5b6552494c/go.mod h1:eMyUVp6f/5jnzM+3zahzl7q6UXLbgSc3MKg/+ow9QW0=
230232
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
231233
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
232234
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=

pkg/plugins/builtin/builtin.go

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
pluginemail "github.com/ovh/utask/pkg/plugins/builtin/email"
88
pluginhttp "github.com/ovh/utask/pkg/plugins/builtin/http"
99
pluginnotify "github.com/ovh/utask/pkg/plugins/builtin/notify"
10+
pluginping "github.com/ovh/utask/pkg/plugins/builtin/ping"
1011
pluginssh "github.com/ovh/utask/pkg/plugins/builtin/ssh"
1112
pluginsubtask "github.com/ovh/utask/pkg/plugins/builtin/subtask"
1213
"github.com/ovh/utask/pkg/plugins/taskplugin"
@@ -22,6 +23,7 @@ func Register() error {
2223
pluginnotify.Plugin,
2324
pluginecho.Plugin,
2425
pluginemail.Plugin,
26+
pluginping.Plugin,
2527
} {
2628
if err := step.RegisterRunner(p.PluginName(), p); err != nil {
2729
return err

pkg/plugins/builtin/ping/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# `ping` plugin
2+
3+
This plugin send a ping.
4+
5+
*Warn: This plugin will keep running until the count is done*
6+
7+
## Configuration
8+
9+
An action of type `ping` requires the following kind of configuration:
10+
11+
```yaml
12+
action:
13+
type: ping
14+
configuration:
15+
# mandatory, string
16+
hostname: example.org
17+
# mandatory, string as uint
18+
count: "2"
19+
# mandatory, string as uint
20+
interval_second: "1"
21+
```
22+
23+
## Note
24+
25+
The plugin returns two objects, the `Output` to fetch statistics about ping(s):
26+
27+
```json
28+
{
29+
"packets_received":1,
30+
"packets_sent":1,
31+
"packet_loss": 0.00,
32+
"ip_addr":"192.168.0.1",
33+
"rtts":"1s",
34+
"min_rtt":"1se",
35+
"max_rtt":"1s",
36+
"avg_rtt":"1s",
37+
"std_dev_rtt":"1s"
38+
}
39+
```
40+
41+
The `Metadata` to reuse the parameters in a future component:
42+
43+
```json
44+
{
45+
"hostname":"example.org",
46+
"count":"2",
47+
"interval_second": "1"
48+
}
49+
```

pkg/plugins/builtin/ping/ping.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package ping
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strconv"
7+
"time"
8+
9+
ping "github.com/sparrc/go-ping"
10+
11+
"github.com/ovh/utask/pkg/plugins/taskplugin"
12+
)
13+
14+
// the ping plugin send ping
15+
var (
16+
Plugin = taskplugin.New("ping", "0.1", exec,
17+
taskplugin.WithConfig(validConfig, Config{}),
18+
)
19+
)
20+
21+
// based on Statistics struct from github.com/sparrc/go-ping /w json tags
22+
type pingStats struct {
23+
PacketsRecv int `json:"packets_received"`
24+
PacketsSent int `json:"packets_sent"`
25+
PacketLoss float64 `json:"packet_loss"`
26+
IPAddr string `json:"ip_addr"`
27+
Rtts []time.Duration `json:"rtts"`
28+
MinRtt time.Duration `json:"min_rtt"`
29+
MaxRtt time.Duration `json:"max_rtt"`
30+
AvgRtt time.Duration `json:"avg_rtt"`
31+
StdDevRtt time.Duration `json:"std_dev_rtt"`
32+
}
33+
34+
// Config is the configuration needed to send a ping
35+
type Config struct {
36+
Hostname string `json:"hostname"`
37+
Count string `json:"count,omitempty"`
38+
Interval string `json:"interval_second,omitempty"`
39+
}
40+
41+
func validConfig(config interface{}) error {
42+
cfg := config.(*Config)
43+
44+
if cfg.Hostname == "" {
45+
return errors.New("hostname is missing")
46+
}
47+
48+
if cfg.Count != "" {
49+
if _, err := strconv.ParseUint(cfg.Count, 10, 64); err != nil {
50+
return fmt.Errorf("count is wrong %s", err.Error())
51+
}
52+
}
53+
54+
if cfg.Interval != "" {
55+
if _, err := strconv.ParseUint(cfg.Interval, 10, 64); err != nil {
56+
return fmt.Errorf("interval_second is wrong %s", err.Error())
57+
}
58+
}
59+
60+
return nil
61+
}
62+
63+
func exec(stepName string, config interface{}, ctx interface{}) (interface{}, interface{}, error) {
64+
cfg := config.(*Config)
65+
66+
pinger, err := ping.NewPinger(cfg.Hostname)
67+
if err != nil {
68+
return nil, nil, fmt.Errorf("Initiate ping failed: %s", err.Error())
69+
}
70+
71+
pinger.Count = pingDefault(cfg.Count)
72+
pinger.Interval = time.Duration(pingDefault(cfg.Interval)) * time.Second
73+
74+
// Run() is blocking until count is done
75+
pinger.Run()
76+
77+
so := pinger.Statistics()
78+
79+
return &pingStats{
80+
PacketsRecv: so.PacketsRecv,
81+
PacketsSent: so.PacketsSent,
82+
PacketLoss: so.PacketLoss,
83+
IPAddr: so.IPAddr.String(),
84+
Rtts: so.Rtts,
85+
MinRtt: so.MinRtt,
86+
MaxRtt: so.MaxRtt,
87+
AvgRtt: so.AvgRtt,
88+
StdDevRtt: so.StdDevRtt,
89+
}, cfg, nil
90+
}
91+
92+
func pingDefault(c string) int {
93+
if c == "" || c == "0" {
94+
return 1
95+
}
96+
97+
// count or interval is already checked, at validConfig() lvl
98+
// values must be correct so errors are not evaluated
99+
count, _ := strconv.Atoi(c)
100+
101+
return count
102+
}

vendor/github.com/sparrc/go-ping/.gitignore

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/sparrc/go-ping/LICENSE

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/sparrc/go-ping/README.md

+102
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)