2016-09-07 19:08:51 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2016-09-07 19:08:51 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/container"
|
2017-07-19 10:20:13 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-09-07 19:08:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Resolve Network SandboxID in case the container reuse another container's network stack
|
|
|
|
func (daemon *Daemon) getNetworkSandboxID(c *container.Container) (string, error) {
|
|
|
|
curr := c
|
|
|
|
for curr.HostConfig.NetworkMode.IsContainer() {
|
|
|
|
containerID := curr.HostConfig.NetworkMode.ConnectedContainer()
|
|
|
|
connected, err := daemon.GetContainer(containerID)
|
|
|
|
if err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return "", errors.Wrapf(err, "Could not get container for %s", containerID)
|
2016-09-07 19:08:51 -04:00
|
|
|
}
|
|
|
|
curr = connected
|
|
|
|
}
|
|
|
|
return curr.NetworkSettings.SandboxID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) getNetworkStats(c *container.Container) (map[string]types.NetworkStats, error) {
|
|
|
|
sandboxID, err := daemon.getNetworkSandboxID(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := daemon.netController.SandboxByID(sandboxID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lnstats, err := sb.Statistics()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stats := make(map[string]types.NetworkStats)
|
2016-09-21 06:36:52 -04:00
|
|
|
// Convert libnetwork nw stats into api stats
|
2016-09-07 19:08:51 -04:00
|
|
|
for ifName, ifStats := range lnstats {
|
|
|
|
stats[ifName] = types.NetworkStats{
|
|
|
|
RxBytes: ifStats.RxBytes,
|
|
|
|
RxPackets: ifStats.RxPackets,
|
|
|
|
RxErrors: ifStats.RxErrors,
|
|
|
|
RxDropped: ifStats.RxDropped,
|
|
|
|
TxBytes: ifStats.TxBytes,
|
|
|
|
TxPackets: ifStats.TxPackets,
|
|
|
|
TxErrors: ifStats.TxErrors,
|
|
|
|
TxDropped: ifStats.TxDropped,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stats, nil
|
|
|
|
}
|