Skip to content

Commit

Permalink
Update rendering and add json option
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Foerster <tim.foerster@hetzner.de>
  • Loading branch information
tonobo committed Dec 19, 2017
1 parent e5043ed commit 6c5b090
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
18 changes: 17 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

tm "github.com/buger/goterm"
pj "github.com/hokaccha/go-prettyjson"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +17,7 @@ var (
INTERVAL = 100 * time.Millisecond
MAX_HOPS = 64
RING_BUFFER_SIZE = 50
jsonFmt = false
)

// rootCmd represents the root command
Expand All @@ -25,8 +27,21 @@ var RootCmd = &cobra.Command{
if len(args) != 1 {
return errors.New("No target provided")
}
fmt.Println("Start:", time.Now())
m, ch := NewMTR(args[0], TIMEOUT, INTERVAL)
if jsonFmt {
go func(ch chan struct{}) {
for {
<-ch
}
}(ch)
for i := 0; i < COUNT; i++ {
m.Run(ch)
}
s, _ := pj.Marshal(m)
fmt.Println(string(s))
return nil
}
fmt.Println("Start:", time.Now())
tm.Clear()
mu := &sync.Mutex{}
go func(ch chan struct{}) {
Expand Down Expand Up @@ -60,4 +75,5 @@ func init() {
RootCmd.Flags().DurationVarP(&INTERVAL, "interval", "i", INTERVAL, "Wait time between icmp packets before sending new one")
RootCmd.Flags().IntVar(&MAX_HOPS, "max-hops", MAX_HOPS, "Maximal TTL count")
RootCmd.Flags().IntVar(&RING_BUFFER_SIZE, "buffer-size", RING_BUFFER_SIZE, "Cached packet buffer size")
RootCmd.Flags().BoolVar(&jsonFmt, "json", jsonFmt, "Print json results")
}
9 changes: 7 additions & 2 deletions mtr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func (m *MTR) registerStatistic(ttl int, r ICMPReturn) {
if m.Statistic[ttl] == nil {
m.Statistic[ttl] = &HopStatistic{
Sent: 1,
TTL: ttl,
Target: r.Addr,
Last: r,
Best: r,
Worst: r,
Expand All @@ -47,7 +49,11 @@ func (m *MTR) registerStatistic(ttl int, r ICMPReturn) {
return
}
s := m.Statistic[ttl]
s.Packets = s.Packets.Prev()
s.Packets.Value = r
if s.Target == "" {
s.Target = r.Addr
}
s.Sent++
s.SumElapsed = r.Elapsed + s.SumElapsed
if !r.Success {
Expand All @@ -60,7 +66,6 @@ func (m *MTR) registerStatistic(ttl int, r ICMPReturn) {
if s.Worst.Elapsed < r.Elapsed {
s.Worst = r
}
s.Packets = s.Packets.Next()
}

func (m *MTR) Render(offset int) {
Expand All @@ -70,7 +75,7 @@ func (m *MTR) Render(offset int) {
for i := 1; i <= len(m.Statistic); i++ {
gm.MoveCursor(1, offset+i)
m.mutex.RLock()
m.Statistic[i].Render(i)
m.Statistic[i].Render()
m.mutex.RUnlock()
}
return
Expand Down

0 comments on commit 6c5b090

Please sign in to comment.