Merge pull request #2600 from dqminh/raw-container-process-stat

Allow raw container to retrieve process stats
This commit is contained in:
David Ashpole 2020-06-25 16:19:30 -07:00 committed by GitHub
commit 6f30891d89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -118,56 +118,58 @@ func (h *Handler) GetStats() (*info.ContainerStats, error) {
} }
// If we know the pid then get network stats from /proc/<pid>/net/dev // If we know the pid then get network stats from /proc/<pid>/net/dev
if h.pid == 0 { if h.pid > 0 {
return stats, nil if h.includedMetrics.Has(container.NetworkUsageMetrics) {
} netStats, err := networkStatsFromProc(h.rootFs, h.pid)
if h.includedMetrics.Has(container.NetworkUsageMetrics) { if err != nil {
netStats, err := networkStatsFromProc(h.rootFs, h.pid) klog.V(4).Infof("Unable to get network stats from pid %d: %v", h.pid, err)
if err != nil { } else {
klog.V(4).Infof("Unable to get network stats from pid %d: %v", h.pid, err) stats.Network.Interfaces = append(stats.Network.Interfaces, netStats...)
} else { }
stats.Network.Interfaces = append(stats.Network.Interfaces, netStats...)
}
}
if h.includedMetrics.Has(container.NetworkTcpUsageMetrics) {
t, err := tcpStatsFromProc(h.rootFs, h.pid, "net/tcp")
if err != nil {
klog.V(4).Infof("Unable to get tcp stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Tcp = t
} }
if h.includedMetrics.Has(container.NetworkTcpUsageMetrics) {
t, err := tcpStatsFromProc(h.rootFs, h.pid, "net/tcp")
if err != nil {
klog.V(4).Infof("Unable to get tcp stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Tcp = t
}
t6, err := tcpStatsFromProc(h.rootFs, h.pid, "net/tcp6") t6, err := tcpStatsFromProc(h.rootFs, h.pid, "net/tcp6")
if err != nil { if err != nil {
klog.V(4).Infof("Unable to get tcp6 stats from pid %d: %v", h.pid, err) klog.V(4).Infof("Unable to get tcp6 stats from pid %d: %v", h.pid, err)
} else { } else {
stats.Network.Tcp6 = t6 stats.Network.Tcp6 = t6
} }
}
if h.includedMetrics.Has(container.NetworkAdvancedTcpUsageMetrics) {
ta, err := advancedTCPStatsFromProc(h.rootFs, h.pid, "net/netstat", "net/snmp")
if err != nil {
klog.V(4).Infof("Unable to get advanced tcp stats from pid %d: %v", h.pid, err)
} else {
stats.Network.TcpAdvanced = ta
} }
} if h.includedMetrics.Has(container.NetworkAdvancedTcpUsageMetrics) {
if h.includedMetrics.Has(container.NetworkUdpUsageMetrics) { ta, err := advancedTCPStatsFromProc(h.rootFs, h.pid, "net/netstat", "net/snmp")
u, err := udpStatsFromProc(h.rootFs, h.pid, "net/udp") if err != nil {
if err != nil { klog.V(4).Infof("Unable to get advanced tcp stats from pid %d: %v", h.pid, err)
klog.V(4).Infof("Unable to get udp stats from pid %d: %v", h.pid, err) } else {
} else { stats.Network.TcpAdvanced = ta
stats.Network.Udp = u }
} }
if h.includedMetrics.Has(container.NetworkUdpUsageMetrics) {
u, err := udpStatsFromProc(h.rootFs, h.pid, "net/udp")
if err != nil {
klog.V(4).Infof("Unable to get udp stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Udp = u
}
u6, err := udpStatsFromProc(h.rootFs, h.pid, "net/udp6") u6, err := udpStatsFromProc(h.rootFs, h.pid, "net/udp6")
if err != nil { if err != nil {
klog.V(4).Infof("Unable to get udp6 stats from pid %d: %v", h.pid, err) klog.V(4).Infof("Unable to get udp6 stats from pid %d: %v", h.pid, err)
} else { } else {
stats.Network.Udp6 = u6 stats.Network.Udp6 = u6
}
} }
} }
// some process metrics are per container ( number of processes, number of
// file descriptors etc.) and not required a proper container's
// root PID (systemd services don't have the root PID atm)
if h.includedMetrics.Has(container.ProcessMetrics) { if h.includedMetrics.Has(container.ProcessMetrics) {
paths := h.cgroupManager.GetPaths() paths := h.cgroupManager.GetPaths()
path, ok := paths["cpu"] path, ok := paths["cpu"]
@ -298,13 +300,15 @@ func processStatsFromProcs(rootFs string, cgroupPath string, rootPid int) (info.
} }
} }
} }
ulimits := processRootProcUlimits(rootFs, rootPid)
processStats := info.ProcessStats{ processStats := info.ProcessStats{
ProcessCount: uint64(len(pids)), ProcessCount: uint64(len(pids)),
FdCount: fdCount, FdCount: fdCount,
SocketCount: socketCount, SocketCount: socketCount,
Ulimits: ulimits, }
if rootPid > 0 {
processStats.Ulimits = processRootProcUlimits(rootFs, rootPid)
} }
return processStats, nil return processStats, nil