From 22f35fd518c9944bbe084ddf11a52fa2670c2251 Mon Sep 17 00:00:00 2001 From: James Lamb Date: Tue, 8 Dec 2015 09:32:45 +1100 Subject: [PATCH] add netfilter counter support --- internal/common/common.go | 27 +++++++++++++++++++++++++++ net/net.go | 4 ++-- net/net_linux.go | 34 ++++++++++++++++------------------ 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/internal/common/common.go b/internal/common/common.go index aeadda6..3449cdd 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -141,6 +141,33 @@ func ByteToString(orig []byte) string { return string(orig[l:n]) } +// ReadInts reads contents from single line file and returns them as []int32. +func ReadInts(filename string) ([]int64, error) { + f, err := os.Open(filename) + if err != nil { + return []int64{}, err + } + defer f.Close() + + var ret []int64 + + r := bufio.NewReader(f) + + // The int files that this is concerned with should only be one liners. + line, err := r.ReadString('\n') + if err != nil { + return []int64{}, err + } + + i, err := strconv.ParseInt(strings.Trim(line, "\n"), 10, 32) + if err != nil { + return []int64{}, err + } + ret = append(ret, i) + + return ret, nil +} + // Parse to int32 without error func mustParseInt32(val string) int32 { vv, _ := strconv.ParseInt(val, 10, 32) diff --git a/net/net.go b/net/net.go index 2253310..feb5c38 100644 --- a/net/net.go +++ b/net/net.go @@ -65,8 +65,8 @@ type NetInterfaceStat struct { } type NetFilterStat struct { - ConnTrackCount int32 `json:"conntrackcount"` - ConnTrackMax int32 `json:"conntrackmax"` + ConnTrackCount int64 `json:"conntrackcount"` + ConnTrackMax int64 `json:"conntrackmax"` } var constMap = map[string]int{ diff --git a/net/net_linux.go b/net/net_linux.go index b12a8fc..058720a 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -164,29 +164,27 @@ func NetProtoCounters(protocols []string) ([]NetProtoCountersStat, error) { // NetFilterCounters returns iptables conntrack statistics // the currently in use conntrack count and the max. // If the file does not exist or is invalid it will return nil. -func NetFilterCounters() (NetFilterStat, error) { +func NetFilterCounters() ([]NetFilterStat, error) { countfile := "/proc/sys/net/netfilter/nf_conntrack_count" - count, err := common.ReadLines(count) + maxfile := "/proc/sys/net/netfilter/nf_conntrack_max" + + count, err := common.ReadInts(countfile) + + if err != nil { + return nil, err + } + stats := make([]NetFilterStat, 0, 1) + + max, err := common.ReadInts(maxfile) if err != nil { return nil, err } - maxfile := "/proc/sys/net/netfilter/nf_conntrack_max" - max, err := common.ReadLines(maxfile) - if err != nil { - return nil, err - } - if len(count) != 1 { - // format of file has changed - return nil, err - } - if len(max) != 1 { - // format of file has changed - return nil, err - } - stats := NetFilterStat{ - ConnTrackCount: count, - ConnTrackMax: max, + payload := NetFilterStat{ + ConnTrackCount: count[0], + ConnTrackMax: max[0], } + + stats = append(stats, payload) return stats, nil }