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

Add a ping module #1623

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/widget_maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import (
"github.com/wtfutil/wtf/modules/weatherservices/prettyweather"
"github.com/wtfutil/wtf/modules/weatherservices/weather"
"github.com/wtfutil/wtf/modules/zendesk"
"github.com/wtfutil/wtf/modules/ping"
"github.com/wtfutil/wtf/wtf"
)

Expand Down Expand Up @@ -273,6 +274,9 @@ func MakeWidget(
case "pihole":
settings := pihole.NewSettingsFromYAML(moduleName, moduleConfig, config)
widget = pihole.NewWidget(tviewApp, redrawChan, pages, settings)
case "ping":
settings := ping.NewSettingsFromYAML(moduleName, moduleConfig, config)
widget = ping.NewWidget(tviewApp, redrawChan, settings)
case "power":
settings := power.NewSettingsFromYAML(moduleName, moduleConfig, config)
widget = power.NewWidget(tviewApp, redrawChan, settings)
Expand Down
48 changes: 48 additions & 0 deletions modules/ping/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ping

import (
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)

const (
defaultFocusable = false
defaultTitle = "Pings"
)

type Target struct {
Name string `help: "Name: The label to use for the host you want to ping"`
Host string `help: "Host: IP address or hostname to ping"`
Up bool // not meant to be set by user
}

type Settings struct {
common *cfg.Common
targets []Target
}

func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
settings := Settings{
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
targets: buildTargets(ymlConfig),
}

return &settings
}

func buildTargets(ymlConfig *config.Config) []Target {
targets := []Target{}
yaml := ymlConfig.UList("targets")
for _, target := range yaml {
if target,ok := target.(map[string]interface{}); ok {
for k,v := range target {
name := k
host := v.(string)
t := Target{Name: name, Host: host, Up: false}
targets = append(targets, t)
}
}
}
return targets
}

102 changes: 102 additions & 0 deletions modules/ping/widget.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package ping

import (
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/view"
"github.com/prometheus-community/pro-bing"
)

// Widget is the container for your module's data
type Widget struct {
view.TextWidget
targets []Target

settings *Settings
}

// NewWidget creates and returns an instance of Widget
func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Settings) *Widget {
widget := Widget{
TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.common),

settings: settings,
}
widget.targets = widget.settings.targets

return &widget
}

/* -------------------- Exported Functions -------------------- */


func (widget *Widget) doPings() {
var wg sync.WaitGroup
for i := range widget.targets {
idx := i
target := widget.targets[idx]
widget.targets[idx].Up = false // reset to false each time
wg.Add(1)
go func() {
defer wg.Done()
pinger, err := probing.NewPinger(target.Host)

Check failure on line 46 in modules/ping/widget.go

View workflow job for this annotation

GitHub Actions / lint

undefined: probing (typecheck)
if err == nil {
pinger.Count = 1
pinger.Timeout = 10*time.Second
err = pinger.Run() // Blocks until finished.
if err == nil {
stats := pinger.Statistics() // get send/receive/duplicate/rtt stats
if stats.PacketsRecv > 0 {
widget.targets[idx].Up = true
} else {
widget.targets[idx].Up = false
}
} else {
log.Fatalf("error sending ping: %v", err)
}
}

}()
}
wg.Wait()
}
func (widget *Widget) Refresh() {

widget.doPings()
widget.display()
}

/* -------------------- Unexported Functions -------------------- */

func (widget *Widget) content() string {
nameWidth := 12
for _,t := range widget.targets {
if len(t.Name) > nameWidth {
nameWidth = len(t.Name) + 2
}
}

s := []string{}
for _,t := range widget.targets {
var status string
if t.Up == true {
status = "[green]Up"
} else {
status = "[red]DOWN"
}
statusLine := fmt.Sprintf("[white]%-*s: %s", nameWidth, t.Name, status)
s = append(s, statusLine)
}

return strings.Join(s, "\n")
}

func (widget *Widget) display() {
widget.Redraw(func() (string, string, bool) {
return widget.CommonSettings().Title, widget.content(), false
})
}
Loading