Skip to content

Commit

Permalink
Test http
Browse files Browse the repository at this point in the history
  • Loading branch information
Kashkovsky committed Apr 14, 2022
1 parent 0ebc585 commit e3e6ac3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ func init() {
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.UpdateInterval, "updateInterval", "u", 60, "Config update interval in seconds")
watchCmd.Flags().IntVarP(&watchConfig.UpdateInterval, "updateInterval", "u", 600, "Config update interval in seconds")
}
17 changes: 15 additions & 2 deletions core/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"os/exec"
"runtime"
"strconv"
"sync"

"github.com/jedib0t/go-pretty/v6/table"
Expand All @@ -30,7 +31,14 @@ func NewPrinter() Printer {
t := table.NewWriter()
t.SetStyle(table.StyleColoredBright)
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Address", "Ping"})
t.AppendHeader(table.Row{"Address", "TCP", "HTTP status", "HTTP Latency"})
t.SetColumnConfigs([]table.ColumnConfig{
{
Name: "HTTP status",
WidthMin: 50,
WidthMax: 50,
},
})

return Printer{clearFns: clear, t: t}
}
Expand All @@ -41,7 +49,12 @@ func (p *Printer) ToTable(results *sync.Map) {
results.Range(func(k any, r interface{}) bool {
testResult, ok := r.(TestResult)
if ok {
p.t.AppendRow(table.Row{k, testResult.ping})
p.t.AppendRow(table.Row{
k,
testResult.tcp,
testResult.httpStatus,
strconv.FormatInt(testResult.duration.Milliseconds(), 10) + "ms",
})
}
return true
})
Expand Down
44 changes: 38 additions & 6 deletions core/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package core

import (
"fmt"
"net/http"
"net/url"
"regexp"
"time"
)

type TestResult struct {
Id string
url url.URL
ping string
Id string
url url.URL
tcp string
httpStatus string
duration time.Duration
}

type Tester struct {
Expand All @@ -34,15 +38,22 @@ func (t *Tester) Test(url *url.URL) {
case <-t.quit:
return
default:
pass := t.ping(url)
t.out <- TestResult{Id: url.Host, url: *url, ping: fmt.Sprintf("%d/10", pass)}
pass := t.tcp(url)
status, duration := t.http(url)
t.out <- TestResult{
Id: url.Host,
url: *url,
tcp: fmt.Sprintf("%d/10", pass),
httpStatus: status,
duration: duration,
}
time.Sleep(t.testInterval)
}

}
}

func (t *Tester) ping(url *url.URL) int {
func (t *Tester) tcp(url *url.URL) int {
tp := NewTransport(t.requestTimeout)
pass := 0
for i := 0; i < 10; i++ {
Expand All @@ -54,3 +65,24 @@ func (t *Tester) ping(url *url.URL) int {

return pass
}

func (t *Tester) http(url *url.URL) (status string, duration time.Duration) {
tp := NewTransport(t.requestTimeout)
client := http.Client{Transport: tp, Timeout: t.requestTimeout}
res, err := client.Get("http://" + url.Host)
duration = tp.Duration()
if err == nil {
status = res.Status
} else if duration >= t.requestTimeout {
status = "TIMEOUT"
} else {
status = formatError(err, url)
}

return
}

func formatError(err error, url *url.URL) string {
m := regexp.MustCompile(fmt.Sprintf(`(Get \"http://%s\": )|(net/http: )| \(.*\)|(dial .* %s: )`, url.Host, url.Host))
return m.ReplaceAllString(err.Error(), "")
}
Binary file modified monitor
Binary file not shown.

0 comments on commit e3e6ac3

Please sign in to comment.