github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/daemon/stats_unix.go (about)

     1  // +build !windows
     2  
     3  package daemon
     4  
     5  import (
     6  	"github.com/docker/docker/api/types"
     7  	"github.com/docker/docker/container"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  // Resolve Network SandboxID in case the container reuse another container's network stack
    12  func (daemon *Daemon) getNetworkSandboxID(c *container.Container) (string, error) {
    13  	curr := c
    14  	for curr.HostConfig.NetworkMode.IsContainer() {
    15  		containerID := curr.HostConfig.NetworkMode.ConnectedContainer()
    16  		connected, err := daemon.GetContainer(containerID)
    17  		if err != nil {
    18  			return "", errors.Wrapf(err, "Could not get container for %s", containerID)
    19  		}
    20  		curr = connected
    21  	}
    22  	return curr.NetworkSettings.SandboxID, nil
    23  }
    24  
    25  func (daemon *Daemon) getNetworkStats(c *container.Container) (map[string]types.NetworkStats, error) {
    26  	sandboxID, err := daemon.getNetworkSandboxID(c)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	sb, err := daemon.netController.SandboxByID(sandboxID)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	lnstats, err := sb.Statistics()
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	stats := make(map[string]types.NetworkStats)
    42  	// Convert libnetwork nw stats into api stats
    43  	for ifName, ifStats := range lnstats {
    44  		stats[ifName] = types.NetworkStats{
    45  			RxBytes:   ifStats.RxBytes,
    46  			RxPackets: ifStats.RxPackets,
    47  			RxErrors:  ifStats.RxErrors,
    48  			RxDropped: ifStats.RxDropped,
    49  			TxBytes:   ifStats.TxBytes,
    50  			TxPackets: ifStats.TxPackets,
    51  			TxErrors:  ifStats.TxErrors,
    52  			TxDropped: ifStats.TxDropped,
    53  		}
    54  	}
    55  
    56  	return stats, nil
    57  }