diff options
-rw-r--r-- | libpod/container.go | 2 | ||||
-rw-r--r-- | libpod/info.go | 27 | ||||
-rw-r--r-- | libpod/stats.go | 34 |
3 files changed, 50 insertions, 13 deletions
diff --git a/libpod/container.go b/libpod/container.go index 0e3b8b17f..42f13a992 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -632,7 +632,7 @@ func (c *Container) NamespacePath(ns LinuxNS) (string, error) { // CGroupPath returns a cgroups "path" for a given container. func (c *Container) CGroupPath() cgroups.Path { - return cgroups.StaticPath(filepath.Join(c.config.CgroupParent, fmt.Sprintf("libpod-conmon-%s", c.ID()))) + return cgroups.StaticPath(filepath.Join(c.config.CgroupParent, fmt.Sprintf("libpod-conmon-%s/%s", c.ID(), c.ID()))) } // RootFsSize returns the root FS size of the container diff --git a/libpod/info.go b/libpod/info.go index ab2865e85..03919eb1a 100644 --- a/libpod/info.go +++ b/libpod/info.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "runtime" + "strconv" "time" "github.com/docker/docker/pkg/system" @@ -51,7 +52,31 @@ func (r *Runtime) hostInfo() (map[string]interface{}, error) { if err != nil { return nil, errors.Wrapf(err, "error parsing system uptime") } - info["uptime"] = upDuration.String() + + hoursFound := false + var timeBuffer bytes.Buffer + var hoursBuffer bytes.Buffer + for _, elem := range upDuration.String() { + timeBuffer.WriteRune(elem) + if elem == 'h' || elem == 'm' { + timeBuffer.WriteRune(' ') + if elem == 'h' { + hoursFound = true + } + } + if !hoursFound { + hoursBuffer.WriteRune(elem) + } + } + + info["uptime"] = timeBuffer.String() + if hoursFound { + hours, err := strconv.ParseFloat(hoursBuffer.String(), 64) + if err == nil { + days := hours / 24 + info["uptime"] = fmt.Sprintf("%s (Approximately %.2f days)", info["uptime"], days) + } + } host, err := os.Hostname() if err != nil { diff --git a/libpod/stats.go b/libpod/stats.go index faccc2366..f4694923c 100644 --- a/libpod/stats.go +++ b/libpod/stats.go @@ -6,8 +6,10 @@ import ( "time" "github.com/containerd/cgroups" - "github.com/opencontainers/runc/libcontainer" + "github.com/containernetworking/plugins/pkg/ns" + "github.com/cri-o/ocicni/pkg/ocicni" "github.com/pkg/errors" + "github.com/vishvananda/netlink" ) // ContainerStats contains the statistics information for a running container @@ -55,6 +57,11 @@ func (c *Container) GetContainerStats(previousStats *ContainerStats) (*Container return stats, errors.Wrapf(err, "unable to determine container state") } + netStats, err := getContainerNetIO(c) + if err != nil { + return nil, err + } + previousCPU := previousStats.CPUNano previousSystem := previousStats.SystemNano stats.CPU = calculateCPUPercent(cgroupStats, previousCPU, previousSystem) @@ -63,13 +70,14 @@ func (c *Container) GetContainerStats(previousStats *ContainerStats) (*Container stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100 stats.PIDs = 0 if conState == ContainerStateRunning { - stats.PIDs = cgroupStats.Pids.Current - 1 + stats.PIDs = cgroupStats.Pids.Current } stats.BlockInput, stats.BlockOutput = calculateBlockIO(cgroupStats) stats.CPUNano = cgroupStats.CPU.Usage.Total stats.SystemNano = cgroupStats.CPU.Usage.Kernel - // TODO Figure out where to get the Netout stuff. - //stats.NetInput, stats.NetOutput = getContainerNetIO(cgroupStats) + stats.NetInput = netStats.TxBytes + stats.NetOutput = netStats.RxBytes + return stats, nil } @@ -90,13 +98,17 @@ func getMemLimit(cgroupLimit uint64) uint64 { return cgroupLimit } -// Returns the total number of bytes transmitted and received for the given container stats -func getContainerNetIO(stats *libcontainer.Stats) (received uint64, transmitted uint64) { //nolint - for _, iface := range stats.Interfaces { - received += iface.RxBytes - transmitted += iface.TxBytes - } - return +func getContainerNetIO(ctr *Container) (*netlink.LinkStatistics, error) { + var netStats *netlink.LinkStatistics + err := ns.WithNetNSPath(ctr.state.NetNS.Path(), func(_ ns.NetNS) error { + link, err := netlink.LinkByName(ocicni.DefaultInterfaceName) + if err != nil { + return err + } + netStats = link.Attrs().Statistics + return nil + }) + return netStats, err } func calculateCPUPercent(stats *cgroups.Metrics, previousCPU, previousSystem uint64) float64 { |