stats_unix.go 1.5 KB

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