Skip to content

Commit 797be04

Browse files
authored
Merge pull request #10 from m-lab/cleanup
Cleanup and add support for sc_attach
2 parents 99281e8 + 81b7cbd commit 797be04

File tree

12 files changed

+303
-115
lines changed

12 files changed

+303
-115
lines changed

.travis.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: go
2+
3+
sudo: true
4+
5+
before_install:
6+
- go get github.com/mattn/goveralls
7+
# Install dependencies, including test dependencies.
8+
- sudo apt-get update && sudo apt-get install -y scamper
9+
- go get -v -t ./...
10+
11+
script:
12+
# Vet the code, build the code, and run all the tests.
13+
- go vet ./...
14+
- go build ./...
15+
- go test ./... -v -coverpkg=./... -coverprofile=_coverage.cov
16+
17+
# Build a Docker image to make sure that we can.
18+
- docker build .
19+
20+
# Coveralls
21+
# Upload coverage information for unit tests.
22+
- $HOME/gopath/bin/goveralls -coverprofile=_coverage.cov -service=travis-ci

Dockerfile

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
FROM golang:alpine as build
2-
RUN apk update && apk add bash git pkgconfig geoip-dev geoip gcc libc-dev
1+
FROM golang:1.12 as build
32
ADD . /go/src/github.com/m-lab/traceroute-caller
4-
RUN GOARCH=amd64 CGO_ENABLED=0 GOOS=linux go get github.com/m-lab/traceroute-caller
3+
ENV GOARCH amd64
4+
ENV CGO_ENABLED 0
5+
ENV GOOS linux
6+
WORKDIR /go/src/github.com/m-lab/traceroute-caller
7+
RUN go get -v \
8+
-ldflags "-X github.com/m-lab/go/prometheusx.GitShortCommit=$(git log -1 --format=%h)" \
9+
.
510
RUN chmod -R a+rx /go/bin/traceroute-caller
611

12+
713
FROM ubuntu:latest
814
# Install all the standard packages we need
915
RUN apt-get update && apt-get install -y python python-pip make iproute2 coreutils
1016

1117
RUN ls -l
1218
RUN mkdir /source
13-
ADD . /go/src/github.com/m-lab/traceroute-caller
14-
RUN mv /go/src/github.com/m-lab/traceroute-caller/vendor/scamper/scamper-cvs-20190113 /source
19+
ADD ./vendor/scamper/ /source
1520
RUN chmod +x /source/scamper-cvs-20190113/configure
16-
RUN /source/scamper-cvs-20190113/configure
17-
RUN cd /source/scamper-cvs-20190113/
18-
RUN ls -l /source/scamper-cvs-20190113/scamper
21+
WORKDIR /source/scamper-cvs-20190113/
22+
RUN ./configure
1923
RUN make
2024
RUN make install
21-
22-
RUN chmod 4755 /usr/local/bin/scamper
25+
RUN ldconfig
2326

2427
COPY --from=build /go/bin/traceroute-caller /
2528

caller.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,57 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"log"
78
"time"
89

10+
"github.com/m-lab/go/flagx"
11+
"github.com/m-lab/go/rtx"
12+
13+
"github.com/m-lab/go/prometheusx"
914
"github.com/m-lab/traceroute-caller/connectionwatcher"
1015
"github.com/m-lab/traceroute-caller/scamper"
1116
)
1217

18+
var (
19+
scamperBin = flag.String("scamper.bin", "scamper", "path of scamper binary")
20+
scattachBin = flag.String("scamper.sc_attach", "sc_attach", "path of sc_attach binary")
21+
scwarts2jsonBin = flag.String("scamper.sc_warts2json", "sc_warts2json", "path of sc_warts2json binary")
22+
scamperCtrlSocket = flag.String("scamper.unixsocket", "/tmp/scamperctrl", "The name of the UNIX-domain socket that the scamper daemon should listen on")
23+
outputPath = flag.String("outputPath", "/var/spool/scamper", "path of output")
24+
25+
ctx, cancel = context.WithCancel(context.Background())
26+
)
27+
1328
// Sample cmd:
1429
// go build
1530
// ./traceroute-caller --outputPath scamper_output
1631
func main() {
17-
var outputPath string
18-
flag.StringVar(&outputPath, "outputPath", "/var/spool/scamper", "path of output")
1932
flag.Parse()
33+
rtx.Must(flagx.ArgsFromEnv(flag.CommandLine), "Could not get args from environment")
34+
35+
promSrv := prometheusx.MustServeMetrics()
36+
37+
daemon := scamper.Daemon{
38+
Binary: *scamperBin,
39+
AttachBinary: *scattachBin,
40+
Warts2JSONBinary: *scwarts2jsonBin,
41+
OutputPath: *outputPath,
42+
ControlSocket: *scamperCtrlSocket,
43+
}
44+
go daemon.MustStart(ctx)
2045

2146
connWatcher := connectionwatcher.New()
22-
for {
47+
for ctx.Err() == nil {
2348
closedCollection := connWatcher.GetClosedCollection()
2449
fmt.Printf("length of closed connections: %d\n", len(closedCollection))
2550
for _, conn := range closedCollection {
26-
log.Printf("PT start: %s %d", conn.Remote_ip, conn.Remote_port)
27-
go scamper.Run(conn, outputPath)
51+
log.Printf("PT start: %s %d", conn.RemoteIP, conn.RemotePort)
52+
go daemon.Trace(&conn, time.Now())
2853
}
2954
time.Sleep(5 * time.Second)
3055
}
56+
promSrv.Shutdown(ctx)
3157
}

caller_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/m-lab/go/prometheusx"
8+
9+
"github.com/m-lab/go/prometheusx/promtest"
10+
)
11+
12+
func TestMetrics(t *testing.T) {
13+
promtest.LintMetrics(t)
14+
}
15+
16+
func TestMain(t *testing.T) {
17+
// Verify that main doesn't crash, and that it does exit when the context is canceled.
18+
// TODO: verify more in this test.
19+
*prometheusx.ListenAddress = ":0"
20+
ctx, cancel = context.WithCancel(context.Background())
21+
cancel()
22+
main()
23+
}

connection/connection.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
)
88

99
type Connection struct {
10-
Remote_ip string
11-
Remote_port int
12-
Local_ip string
13-
Local_port int
14-
Cookie string
10+
RemoteIP string
11+
RemotePort int
12+
LocalIP string
13+
LocalPort int
14+
Cookie string
1515
}
1616

1717
// UUID returns uuid from cookie parsed from "ss -e" output.

connection/connection_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestUUID(t *testing.T) {
1111
conn := connection.Connection{Cookie: "1be3"}
1212
tmp, err := conn.UUID()
1313
s := strings.Split(tmp, "_")
14-
if err != nil || len(s) != 3 || s[2] != "0000000000001BE3" {
14+
if err != nil || s[len(s)-1] != "0000000000001BE3" {
1515
t.Error("Make uuid from cookie incorrect")
1616
}
1717
}

connectionwatcher/connectionwatcher.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
)
1717

1818
// The new test output filename is joint of hostname, server boot time, and socker TCO cookie.
19-
// like: pboothe2.nyc.corp.google.com_1548788619_00000000000084FF
20-
var IGNORE_IPV4_NETS = []string{"127.", "128.112.139.", "::ffff:127.0.0.1"}
19+
// like: myhost.example.com_1548788619_00000000000084FF
20+
var localIPv4 = []string{"127.", "128.112.139.", "::ffff:127.0.0.1"}
2121

2222
// parseIPAndPort returns a valid IP and port from "ss -e" output.
2323
func parseIPAndPort(input string) (string, int, error) {
@@ -29,7 +29,7 @@ func parseIPAndPort(input string) (string, int, error) {
2929
if IPStr[0] == '[' {
3030
IPStr = IPStr[1 : len(IPStr)-1]
3131
}
32-
for _, prefix := range IGNORE_IPV4_NETS {
32+
for _, prefix := range localIPv4 {
3333
if strings.HasPrefix(IPStr, prefix) {
3434
return "", 0, errors.New("ignore this IP address")
3535
}
@@ -78,7 +78,14 @@ func parseSSLine(line string) (*connection.Connection, error) {
7878
return nil, err
7979
}
8080

81-
output := &connection.Connection{Remote_ip: remoteIP, Remote_port: remotePort, Local_ip: localIP, Local_port: localPort, Cookie: cookie}
81+
output := &connection.Connection{
82+
RemoteIP: remoteIP,
83+
RemotePort: remotePort,
84+
LocalIP: localIP,
85+
LocalPort: localPort,
86+
Cookie: cookie,
87+
}
88+
8289
return output, nil
8390
}
8491

@@ -118,11 +125,11 @@ func (c *ConnectionWatcher) GetClosedCollection() []connection.Connection {
118125
c.getConnections()
119126
fmt.Printf("new connection size %d\n", len(c.connectionPool))
120127
var closed []connection.Connection
121-
for conn, _ := range oldConn {
122-
if !c.connectionPool[conn] && !c.recentIPCache.Has(conn.Remote_ip) {
128+
for conn := range oldConn {
129+
if !c.connectionPool[conn] && !c.recentIPCache.Has(conn.RemoteIP) {
123130
closed = append(closed, conn)
124-
log.Printf("Try to add " + conn.Remote_ip)
125-
c.recentIPCache.Add(conn.Remote_ip)
131+
log.Printf("Try to add " + conn.RemoteIP)
132+
c.recentIPCache.Add(conn.RemoteIP)
126133
}
127134
}
128135
return closed

connectionwatcher/connectionwatcher_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ func TestParseSSLine(t *testing.T) {
3434
t.Error("ss output not parsed correctly")
3535
}
3636
expected := &connection.Connection{
37-
Remote_ip: "2607:f8b0:400d:c0d::81",
38-
Remote_port: 5034,
39-
Local_ip: "2620:0:1003:416:a0ad:fd1a:62f:c862",
40-
Local_port: 58790,
41-
Cookie: "10f3d"}
37+
RemoteIP: "2607:f8b0:400d:c0d::81",
38+
RemotePort: 5034,
39+
LocalIP: "2620:0:1003:416:a0ad:fd1a:62f:c862",
40+
LocalPort: 58790,
41+
Cookie: "10f3d"}
4242
if !reflect.DeepEqual(conn, expected) {
4343
t.Errorf("Expected %v, got %v for parse ss line", expected, conn)
4444
}

ipcache/ipcache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// Do not traceroute to an IP more than once in this many seconds
12-
var IpCacheTimeout = flag.Duration("IpCacheTimeout", 120*time.Second, "Timeout duration in seconds for IPCache")
12+
var IPCacheTimeout = flag.Duration("IPCacheTimeout", 120*time.Second, "Timeout duration in seconds for IPCache")
1313

1414
type RecentIPCache struct {
1515
cache map[string]time.Time
@@ -29,7 +29,7 @@ func New(ctx context.Context) *RecentIPCache {
2929
return
3030
}
3131
for k, v := range m.cache {
32-
if now.Sub(v) > *IpCacheTimeout {
32+
if now.Sub(v) > *IPCacheTimeout {
3333
fmt.Println("try to delete " + k)
3434
m.mu.Lock()
3535
delete(m.cache, k)

ipcache/ipcache_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ package ipcache_test
22

33
import (
44
"context"
5-
"flag"
65
"testing"
76
"time"
87

98
"github.com/m-lab/traceroute-caller/ipcache"
109
)
1110

1211
func TestRecentIPCache(t *testing.T) {
13-
f := flag.Lookup("IpCacheTimeout")
14-
f.Value.Set("20")
12+
*ipcache.IPCacheTimeout = 2 * time.Second
1513

16-
tmp := ipcache.New(context.Background())
14+
ctx, cancel := context.WithCancel(context.Background())
15+
defer cancel()
16+
tmp := ipcache.New(ctx)
1717
tmp.Add("1.2.3.4")
1818
if !tmp.Has("1.2.3.4") {
1919
t.Error("cache not working correctly")
2020
}
2121

22-
time.Sleep(22 * time.Second)
22+
time.Sleep(4 * time.Second)
2323
if tmp.Has("1.2.3.4") {
2424
t.Error("cache not expire correctly")
2525
}

0 commit comments

Comments
 (0)