Skip to content

Commit e980a33

Browse files
tabellFelicianoTech
authored andcommitted
Add a ping module
1 parent a1dd39c commit e980a33

3 files changed

Lines changed: 154 additions & 0 deletions

File tree

‎app/widget_maker.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import (
8888
"github.com/wtfutil/wtf/modules/weatherservices/prettyweather"
8989
"github.com/wtfutil/wtf/modules/weatherservices/weather"
9090
"github.com/wtfutil/wtf/modules/zendesk"
91+
"github.com/wtfutil/wtf/modules/ping"
9192
"github.com/wtfutil/wtf/wtf"
9293
)
9394

@@ -274,6 +275,9 @@ func MakeWidget(
274275
case "pihole":
275276
settings := pihole.NewSettingsFromYAML(moduleName, moduleConfig, config)
276277
widget = pihole.NewWidget(tviewApp, redrawChan, pages, settings)
278+
case "ping":
279+
settings := ping.NewSettingsFromYAML(moduleName, moduleConfig, config)
280+
widget = ping.NewWidget(tviewApp, redrawChan, settings)
277281
case "power":
278282
settings := power.NewSettingsFromYAML(moduleName, moduleConfig, config)
279283
widget = power.NewWidget(tviewApp, redrawChan, settings)

‎modules/ping/settings.go‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ping
2+
3+
import (
4+
"github.com/olebedev/config"
5+
"github.com/wtfutil/wtf/cfg"
6+
)
7+
8+
const (
9+
defaultFocusable = false
10+
defaultTitle = "Pings"
11+
)
12+
13+
type Target struct {
14+
Name string `help: "Name: The label to use for the host you want to ping"`
15+
Host string `help: "Host: IP address or hostname to ping"`
16+
Up bool // not meant to be set by user
17+
}
18+
19+
type Settings struct {
20+
common *cfg.Common
21+
targets []Target
22+
}
23+
24+
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
25+
settings := Settings{
26+
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
27+
targets: buildTargets(ymlConfig),
28+
}
29+
30+
return &settings
31+
}
32+
33+
func buildTargets(ymlConfig *config.Config) []Target {
34+
targets := []Target{}
35+
yaml := ymlConfig.UList("targets")
36+
for _, target := range yaml {
37+
if target,ok := target.(map[string]interface{}); ok {
38+
for k,v := range target {
39+
name := k
40+
host := v.(string)
41+
t := Target{Name: name, Host: host, Up: false}
42+
targets = append(targets, t)
43+
}
44+
}
45+
}
46+
return targets
47+
}
48+

‎modules/ping/widget.go‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package ping
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strings"
7+
"sync"
8+
"time"
9+
"github.com/rivo/tview"
10+
"github.com/wtfutil/wtf/view"
11+
"github.com/prometheus-community/pro-bing"
12+
)
13+
14+
// Widget is the container for your module's data
15+
type Widget struct {
16+
view.TextWidget
17+
targets []Target
18+
19+
settings *Settings
20+
}
21+
22+
// NewWidget creates and returns an instance of Widget
23+
func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Settings) *Widget {
24+
widget := Widget{
25+
TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.common),
26+
27+
settings: settings,
28+
}
29+
widget.targets = widget.settings.targets
30+
31+
return &widget
32+
}
33+
34+
/* -------------------- Exported Functions -------------------- */
35+
36+
37+
func (widget *Widget) doPings() {
38+
var wg sync.WaitGroup
39+
for i := range widget.targets {
40+
idx := i
41+
target := widget.targets[idx]
42+
widget.targets[idx].Up = false // reset to false each time
43+
wg.Add(1)
44+
go func() {
45+
defer wg.Done()
46+
pinger, err := probing.NewPinger(target.Host)
47+
if err == nil {
48+
pinger.Count = 1
49+
pinger.Timeout = 10*time.Second
50+
err = pinger.Run() // Blocks until finished.
51+
if err == nil {
52+
stats := pinger.Statistics() // get send/receive/duplicate/rtt stats
53+
if stats.PacketsRecv > 0 {
54+
widget.targets[idx].Up = true
55+
} else {
56+
widget.targets[idx].Up = false
57+
}
58+
} else {
59+
log.Fatalf("error sending ping: %v", err)
60+
}
61+
}
62+
63+
}()
64+
}
65+
wg.Wait()
66+
}
67+
func (widget *Widget) Refresh() {
68+
69+
widget.doPings()
70+
widget.display()
71+
}
72+
73+
/* -------------------- Unexported Functions -------------------- */
74+
75+
func (widget *Widget) content() string {
76+
nameWidth := 12
77+
for _,t := range widget.targets {
78+
if len(t.Name) > nameWidth {
79+
nameWidth = len(t.Name) + 2
80+
}
81+
}
82+
83+
s := []string{}
84+
for _,t := range widget.targets {
85+
var status string
86+
if t.Up == true {
87+
status = "[green]Up"
88+
} else {
89+
status = "[red]DOWN"
90+
}
91+
statusLine := fmt.Sprintf("[white]%-*s: %s", nameWidth, t.Name, status)
92+
s = append(s, statusLine)
93+
}
94+
95+
return strings.Join(s, "\n")
96+
}
97+
98+
func (widget *Widget) display() {
99+
widget.Redraw(func() (string, string, bool) {
100+
return widget.CommonSettings().Title, widget.content(), false
101+
})
102+
}

0 commit comments

Comments
 (0)
⌂ 换网址