github.com/kinvolk/docker@v1.13.1/daemon/stats_unix.go (about)

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