add netfilter counter support

This commit is contained in:
James Lamb 2015-12-08 09:32:45 +11:00
parent dfff8af4df
commit 22f35fd518
3 changed files with 45 additions and 20 deletions

View File

@ -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)

View File

@ -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{

View File

@ -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
}