add netfilter counter support

pull/122/head
James Lamb 10 years ago
parent dfff8af4df
commit 22f35fd518

@ -141,6 +141,33 @@ func ByteToString(orig []byte) string {
return string(orig[l:n]) 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 // Parse to int32 without error
func mustParseInt32(val string) int32 { func mustParseInt32(val string) int32 {
vv, _ := strconv.ParseInt(val, 10, 32) vv, _ := strconv.ParseInt(val, 10, 32)

@ -65,8 +65,8 @@ type NetInterfaceStat struct {
} }
type NetFilterStat struct { type NetFilterStat struct {
ConnTrackCount int32 `json:"conntrackcount"` ConnTrackCount int64 `json:"conntrackcount"`
ConnTrackMax int32 `json:"conntrackmax"` ConnTrackMax int64 `json:"conntrackmax"`
} }
var constMap = map[string]int{ var constMap = map[string]int{

@ -164,29 +164,27 @@ func NetProtoCounters(protocols []string) ([]NetProtoCountersStat, error) {
// NetFilterCounters returns iptables conntrack statistics // NetFilterCounters returns iptables conntrack statistics
// the currently in use conntrack count and the max. // the currently in use conntrack count and the max.
// If the file does not exist or is invalid it will return nil. // 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" 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 { if err != nil {
return nil, err return nil, err
} }
stats := make([]NetFilterStat, 0, 1)
maxfile := "/proc/sys/net/netfilter/nf_conntrack_max" max, err := common.ReadInts(maxfile)
max, err := common.ReadLines(maxfile)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(count) != 1 {
// format of file has changed payload := NetFilterStat{
return nil, err ConnTrackCount: count[0],
} ConnTrackMax: max[0],
if len(max) != 1 {
// format of file has changed
return nil, err
}
stats := NetFilterStat{
ConnTrackCount: count,
ConnTrackMax: max,
} }
stats = append(stats, payload)
return stats, nil return stats, nil
} }

Loading…
Cancel
Save