diff --git a/cmd/watch.go b/cmd/watch.go index 4cc4b85..e0c532e 100644 --- a/cmd/watch.go +++ b/cmd/watch.go @@ -23,6 +23,8 @@ package cmd import ( "log" + "time" + "sync" "github.com/Kashkovsky/hostmonitor/core" @@ -43,17 +45,30 @@ func runWatch(cmd *cobra.Command, args []string) { resMap := sync.Map{} printer := core.NewPrinter() watcher := core.NewWatcher(&watchConfig) - + + go func() { + for { + printer.ToTable(&resMap) + time.Sleep(time.Second) + } + }() + watcher.Watch(func(res core.TestResult) { - resMap.Store(res.Id, res) - printer.ToTable(&resMap) + if res.InProgress { + _, ok := resMap.Load(res.Id) + if !ok { + resMap.Store(res.Id, res) + } + } else { + resMap.Store(res.Id, res) + } }) } func init() { rootCmd.AddCommand(watchCmd) watchCmd.Flags().StringVarP(&watchConfig.ConfigUrl, "configUrl", "c", core.ITArmyConfigURL, "Url of config containing url list") - watchCmd.Flags().IntVarP(&watchConfig.TestInterval, "testInterval", "i", 10, "Interval in seconds between test updates") - watchCmd.Flags().IntVarP(&watchConfig.RequestTimeout, "requestTimeout", "t", 5, "Request timeout") + watchCmd.Flags().IntVarP(&watchConfig.TestInterval, "testInterval", "i", 20, "Interval in seconds between test updates") + watchCmd.Flags().IntVarP(&watchConfig.RequestTimeout, "requestTimeout", "t", 10, "Request timeout") watchCmd.Flags().IntVarP(&watchConfig.UpdateInterval, "updateInterval", "u", 600, "Config update interval in seconds") } diff --git a/core/printer.go b/core/printer.go index d771f6d..87bf16f 100644 --- a/core/printer.go +++ b/core/printer.go @@ -4,7 +4,6 @@ import ( "os" "os/exec" "runtime" - "strconv" "sync" "github.com/jedib0t/go-pretty/v6/table" @@ -53,7 +52,7 @@ func (p *Printer) ToTable(results *sync.Map) { k, testResult.tcp, testResult.httpStatus, - strconv.FormatInt(testResult.duration.Milliseconds(), 10) + "ms", + testResult.duration, }) } return true diff --git a/core/tester.go b/core/tester.go index 4f8b3c2..ca4fab6 100644 --- a/core/tester.go +++ b/core/tester.go @@ -5,15 +5,17 @@ import ( "net/http" "net/url" "regexp" + "strconv" "time" ) type TestResult struct { Id string + InProgress bool url url.URL tcp string httpStatus string - duration time.Duration + duration string } type Tester struct { @@ -38,6 +40,11 @@ func (t *Tester) Test(url *url.URL) { case <-t.quit: return default: + t.out <- TestResult{ + Id: url.Host, + InProgress: true, + httpStatus: "Testing...", + } pass := t.tcp(url) status, duration := t.http(url) t.out <- TestResult{ @@ -45,11 +52,10 @@ func (t *Tester) Test(url *url.URL) { url: *url, tcp: fmt.Sprintf("%d/10", pass), httpStatus: status, - duration: duration, + duration: strconv.FormatInt(duration.Milliseconds(), 10) + "ms", } - time.Sleep(t.testInterval) } - + time.Sleep(t.testInterval) } } diff --git a/core/watcher.go b/core/watcher.go index 6987418..340990e 100644 --- a/core/watcher.go +++ b/core/watcher.go @@ -28,30 +28,19 @@ func NewWatcher(config *WatchConfig) Watcher { func (w *Watcher) Watch(f func(TestResult)) { for { - err := w.doWatch() + err := w.doWatch(f) if err != nil { log.Fatalf("Fatal: %v", err) return } - go func() { - for { - select { - case <-w.quit: - return - case rec := <-w.out: - f(rec) - } - } - }() - time.Sleep(time.Duration(w.config.UpdateInterval) * time.Second) w.quit <- true } } -func (w *Watcher) doWatch() error { +func (w *Watcher) doWatch(f func(TestResult)) error { log.Default().Println("Fetching new config...") config, err := w.config.Update() @@ -69,6 +58,16 @@ func (w *Watcher) doWatch() error { } go w.tester.Test(u) + go func() { + for { + select { + case <-w.quit: + return + case rec := <-w.out: + f(rec) + } + } + }() } return nil diff --git a/monitor b/monitor index d2e12dc..9860335 100755 Binary files a/monitor and b/monitor differ