Skip to content

Commit

Permalink
Do a conntrack -L before -E to capture existing connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Wilkie committed Sep 7, 2015
1 parent 444cf5b commit 1d92b7c
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion probe/endpoint/conntrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strings"
"sync"
"io"

"github.com/weaveworks/scope/common/exec"
)
Expand Down Expand Up @@ -59,6 +60,11 @@ type Flow struct {
Original, Reply, Independent *Meta `xml:"-"`
}

type conntrack struct {
XMLName xml.Name `xml:"conntrack"`
Flows []Flow `xml:"flow"`
}

// Conntracker uses the conntrack command to track network connections
type Conntracker struct {
sync.Mutex
Expand Down Expand Up @@ -105,7 +111,18 @@ var ConntrackModulePresent = func() bool {

// NB this is not re-entrant!
func (c *Conntracker) run(args ...string) {
args = append([]string{"-E", "-o", "xml"}, args...)
// Fork another conntrack, just to capture existing connections
// for which we don't get events
existingFlows, err := c.existingConnections(args...)
if err != nil {
log.Printf("conntrack existingConnections error: %v", err)
return
}
for _, flow := range existingFlows {
c.handleFlow(flow)
}

args = append([]string{"-E", "-o", "xml", "-p", "tcp"}, args...)
cmd := exec.Command("conntrack", args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
Expand Down Expand Up @@ -154,6 +171,31 @@ func (c *Conntracker) run(args ...string) {
}
}

func (c *Conntracker) existingConnections(args ...string) ([]Flow, error) {
var conntrack conntrack
args = append([]string{"-L", "-o", "xml", "-p", "tcp"}, args...)
cmd := exec.Command("conntrack", args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return conntrack.Flows, err
}
if err := cmd.Start(); err != nil {
return conntrack.Flows, err
}
defer func() {
if err := cmd.Wait(); err != nil {
log.Printf("conntrack existingConnections exit error: %v", err)
}
}()
if err := xml.NewDecoder(stdout).Decode(&conntrack); err != nil {
if err == io.EOF {
return conntrack.Flows, err
}
return conntrack.Flows, err
}
return conntrack.Flows, nil
}

// Stop stop stop
func (c *Conntracker) Stop() {
c.Lock()
Expand Down

0 comments on commit 1d92b7c

Please sign in to comment.