Commit 22f35fd5 authored by James Lamb's avatar James Lamb

add netfilter counter support

parent dfff8af4
......@@ -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)
......
......@@ -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{
......
......@@ -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)
if err != nil {
return nil, err
}
maxfile := "/proc/sys/net/netfilter/nf_conntrack_max"
max, err := common.ReadLines(maxfile)
count, err := common.ReadInts(countfile)
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
stats := make([]NetFilterStat, 0, 1)
max, err := common.ReadInts(maxfile)
if err != nil {
return nil, err
}
stats := NetFilterStat{
ConnTrackCount: count,
ConnTrackMax: max,
payload := NetFilterStat{
ConnTrackCount: count[0],
ConnTrackMax: max[0],
}
stats = append(stats, payload)
return stats, nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment