mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-28 13:48:49 +08:00
add nf_conntrack statistics to net_linux to query iptables/netfilter conntrack limits
This commit is contained in:
parent
759e96ebaf
commit
dfff8af4df
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
*~
|
*~
|
||||||
#*
|
#*
|
||||||
_obj
|
_obj
|
||||||
*.tmp
|
*.tmp
|
||||||
|
.idea
|
||||||
|
@ -64,6 +64,11 @@ type NetInterfaceStat struct {
|
|||||||
Addrs []NetInterfaceAddr `json:"addrs"`
|
Addrs []NetInterfaceAddr `json:"addrs"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NetFilterStat struct {
|
||||||
|
ConnTrackCount int32 `json:"conntrackcount"`
|
||||||
|
ConnTrackMax int32 `json:"conntrackmax"`
|
||||||
|
}
|
||||||
|
|
||||||
var constMap = map[string]int{
|
var constMap = map[string]int{
|
||||||
"TCP": syscall.SOCK_STREAM,
|
"TCP": syscall.SOCK_STREAM,
|
||||||
"UDP": syscall.SOCK_DGRAM,
|
"UDP": syscall.SOCK_DGRAM,
|
||||||
|
@ -160,3 +160,33 @@ func NetProtoCounters(protocols []string) ([]NetProtoCountersStat, error) {
|
|||||||
}
|
}
|
||||||
return stats, nil
|
return stats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
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)
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
return stats, nil
|
||||||
|
}
|
||||||
|
@ -196,3 +196,23 @@ func TestNetConnections(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNetFilterCounters(t *testing.T) {
|
||||||
|
if ci := os.Getenv("CI"); ci != "" { // skip if test on drone.io
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := NetFilterCounters()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not get NetConnections: %v", err)
|
||||||
|
}
|
||||||
|
if len(v) == 0 {
|
||||||
|
t.Errorf("could not get NetConnections: %v", v)
|
||||||
|
}
|
||||||
|
for _, vv := range v {
|
||||||
|
if vv.ConnTrackMax == 0 {
|
||||||
|
t.Errorf("nf_conntrack_max needs to be greater than zero: %v", vv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user