Skip to content

Commit 40bda76

Browse files
author
ShiShujuan
committedAug 29, 2017
stats network traffic with all interfaces.
1 parent c2f8191 commit 40bda76

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed
 

‎container/libcontainer/helpers.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/golang/glog"
3232
"github.com/opencontainers/runc/libcontainer"
3333
"github.com/opencontainers/runc/libcontainer/cgroups"
34+
"reflect"
3435
)
3536

3637
type CgroupSubsystems struct {
@@ -137,12 +138,33 @@ func GetStats(cgroupManager cgroups.Manager, rootFs string, pid int, ignoreMetri
137138

138139
// For backwards compatibility.
139140
if len(stats.Network.Interfaces) > 0 {
140-
stats.Network.InterfaceStats = stats.Network.Interfaces[0]
141+
//stats.Network.InterfaceStats = stats.Network.Interfaces[0]
142+
stats.Network.InterfaceStats = sumNetworkStats(stats)
141143
}
142144

143145
return stats, nil
144146
}
145147

148+
func sumNetworkStats(stats *info.ContainerStats) info.InterfaceStats {
149+
interfaceStats := stats.Network.Interfaces[0]
150+
interfaceStats.Name = "ethall"
151+
t0 := reflect.TypeOf(interfaceStats)
152+
v0 := reflect.ValueOf(&interfaceStats).Elem()
153+
for i := 1; i < len(stats.Network.Interfaces); i++ {
154+
interfaceStatsOfi := stats.Network.Interfaces[i]
155+
vi := reflect.ValueOf(interfaceStatsOfi)
156+
157+
for k := 1; k < t0.NumField(); k++ {
158+
fieldName := t0.Field(k).Name
159+
value0 := v0.Field(k).Interface().(uint64)
160+
valuei := vi.Field(k).Interface().(uint64)
161+
field := v0.FieldByName(fieldName)
162+
field.SetUint(value0 + valuei)
163+
}
164+
}
165+
return interfaceStats
166+
}
167+
146168
func networkStatsFromProc(rootFs string, pid int) ([]info.InterfaceStats, error) {
147169
netStatsFile := path.Join(rootFs, "proc", strconv.Itoa(pid), "/net/dev")
148170

@@ -512,7 +534,8 @@ func setNetworkStats(libcontainerStats *libcontainer.Stats, ret *info.ContainerS
512534

513535
// Add to base struct for backwards compatibility.
514536
if len(ret.Network.Interfaces) > 0 {
515-
ret.Network.InterfaceStats = ret.Network.Interfaces[0]
537+
//ret.Network.InterfaceStats = ret.Network.Interfaces[0]
538+
ret.Network.InterfaceStats = sumNetworkStats(ret)
516539
}
517540
}
518541

0 commit comments

Comments
 (0)
Please sign in to comment.