forked from wtfutil/wtf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package ping | ||
|
||
import ( | ||
"github.com/olebedev/config" | ||
"github.com/wtfutil/wtf/cfg" | ||
) | ||
|
||
const ( | ||
defaultFocusable = false | ||
defaultTitle = "Pings" | ||
) | ||
|
||
type Target struct { | ||
Name string | ||
Host string | ||
Up bool | ||
Latency int | ||
} | ||
|
||
// Settings defines the configuration properties for this module | ||
type Settings struct { | ||
common *cfg.Common | ||
targets []Target | ||
|
||
// Define your settings attributes here | ||
} | ||
|
||
// NewSettingsFromYAML creates a new settings instance from a YAML config block | ||
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package ping | ||
|
||
import ( | ||
"fmt" | ||
"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) | ||
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 | ||
} | ||
} | ||
} | ||
}() | ||
} | ||
wg.Wait() | ||
} | ||
// Refresh updates the onscreen contents of the widget | ||
func (widget *Widget) Refresh() { | ||
|
||
widget.doPings() | ||
// The last call should always be to the display function | ||
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 | ||
}) | ||
} |