Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Simplified connection group
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-krolik committed Jan 11, 2017
1 parent e64756e commit 64567f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 45 deletions.
4 changes: 2 additions & 2 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ var getters map[string]container.StatGetter = map[string]container.StatGetter{
"pids_stats": &cgroupfs.Pids{},
"cpuset_stats": &cgroupfs.CpuSet{},
"network": &network.Network{},
"tcp": &network.Tcp{},
"tcp6": &network.Tcp6{},
"tcp": &network.Tcp{StatsFile: "net/tcp"},
"tcp6": &network.Tcp{StatsFile: "net/tcp6"},
"filesystem": &fs.DiskUsageCollector{},
}

Expand Down
58 changes: 15 additions & 43 deletions container/network/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
log "github.com/Sirupsen/logrus"
)

type Tcp struct{}
type Tcp struct {
StatsFile string
}

func (t *Tcp) GetStats(stats *container.Statistics, opts container.GetStatOpt) error {
func (tcp *Tcp) GetStats(stats *container.Statistics, opts container.GetStatOpt) error {
pid, err := opts.GetIntValue("pid")
if err != nil {
return err
Expand All @@ -33,64 +35,34 @@ func (t *Tcp) GetStats(stats *container.Statistics, opts container.GetStatOpt) e
}

if !isHost {
path := filepath.Join(procfs, strconv.Itoa(pid))
stats.Connection.Tcp, err = TcpStatsFromProc(path)
if err != nil {
// only log error message
path := filepath.Join(procfs, strconv.Itoa(pid), tcp.StatsFile)

switch tcp.StatsFile {
case "net/tcp":
stats.Connection.Tcp, err = tcpStatsFromProc(path)
case "net/tcp6":
stats.Connection.Tcp6, err = tcpStatsFromProc(path)
default:
log.WithFields(log.Fields{
"module": "network",
"block": "GetStats",
}).Errorf("Unable to get network stats, pid %d: %s", pid, err)
}).Errorf("Unknown tcp stats file %s", tcp.StatsFile)
return err
}
}

return nil
}

type Tcp6 struct{}

func (t *Tcp6) GetStats(stats *container.Statistics, opts container.GetStatOpt) error {
pid, err := opts.GetIntValue("pid")
if err != nil {
return err
}

isHost, err := opts.GetBoolValue("is_host")
if err != nil {
return err
}

procfs, err := opts.GetStringValue("procfs")
if err != nil {
return err
}

if !isHost {
path := filepath.Join(procfs, strconv.Itoa(pid))
stats.Connection.Tcp6, err = Tcp6StatsFromProc(path)
if err != nil {
// only log error message
log.WithFields(log.Fields{
"module": "network",
"block": "GetStats",
}).Errorf("Unable to get network stats, pid %d: %s", pid, err)
}).Errorf("Unable to get network stats, pid %d, stats file %s: %s", pid, tcp.StatsFile, err)
}

}

return nil
}

// TcpStatsFromProc returns TCP statistics (e.g. the number of TCP connections in state `established`, `close`, etc.)
func TcpStatsFromProc(path string) (container.TcpStat, error) {
return tcpStatsFromProc(filepath.Join(path, "net/tcp"))
}

// Tcp6StatsFromProc returns TCP6 statistics (e.g. the number of TCP6 connections in state `established`, `close`, etc.)
func Tcp6StatsFromProc(path string) (container.TcpStat, error) {
return tcpStatsFromProc(filepath.Join(path, "net/tcp6"))
}

func tcpStatsFromProc(tcpStatsFile string) (container.TcpStat, error) {
tcpStats, err := scanTcpStats(tcpStatsFile)
if err != nil {
Expand Down

0 comments on commit 64567f6

Please sign in to comment.