Redo: Capture host metrics from inside docker container via volume mount /:/rootfs/

This commit is contained in:
Kok San 2015-09-09 14:05:20 +08:00
parent ff2de0c715
commit c5b1a6d77e
7 changed files with 28 additions and 28 deletions

View File

@ -12,7 +12,7 @@ import (
)
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
filename := "/proc/stat"
filename := "/rootfs/proc/stat"
var lines = []string{}
if percpu {
var startIdx uint = 1
@ -43,7 +43,7 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
}
func CPUInfo() ([]CPUInfoStat, error) {
filename := "/proc/cpuinfo"
filename := "/rootfs/proc/cpuinfo"
lines, _ := common.ReadLines(filename)
var ret []CPUInfoStat

View File

@ -42,7 +42,7 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
}
func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
filename := "/proc/diskstats"
filename := "/rootfs/proc/diskstats"
lines, err := common.ReadLines(filename)
if err != nil {
return nil, err

View File

@ -280,12 +280,12 @@ func GetVirtualization() (string, string, error) {
var system string
var role string
if common.PathExists("/proc/xen") {
if common.PathExists("/rootfs/proc/xen") {
system = "xen"
role = "guest" // assume guest
if common.PathExists("/proc/xen/capabilities") {
contents, err := common.ReadLines("/proc/xen/capabilities")
if common.PathExists("/rootfs/proc/xen/capabilities") {
contents, err := common.ReadLines("/rootfs/proc/xen/capabilities")
if err == nil {
if common.StringContains(contents, "control_d") {
role = "host"
@ -293,8 +293,8 @@ func GetVirtualization() (string, string, error) {
}
}
}
if common.PathExists("/proc/modules") {
contents, err := common.ReadLines("/proc/modules")
if common.PathExists("/rootfs/proc/modules") {
contents, err := common.ReadLines("/rootfs/proc/modules")
if err == nil {
if common.StringContains(contents, "kvm") {
system = "kvm"
@ -309,8 +309,8 @@ func GetVirtualization() (string, string, error) {
}
}
if common.PathExists("/proc/cpuinfo") {
contents, err := common.ReadLines("/proc/cpuinfo")
if common.PathExists("/rootfs/proc/cpuinfo") {
contents, err := common.ReadLines("/rootfs/proc/cpuinfo")
if err == nil {
if common.StringContains(contents, "QEMU Virtual CPU") ||
common.StringContains(contents, "Common KVM processor") ||
@ -321,18 +321,18 @@ func GetVirtualization() (string, string, error) {
}
}
if common.PathExists("/proc/bc/0") {
if common.PathExists("/rootfs/proc/bc/0") {
system = "openvz"
role = "host"
} else if common.PathExists("/proc/vz") {
} else if common.PathExists("/rootfs/proc/vz") {
system = "openvz"
role = "guest"
}
// not use dmidecode because it requires root
if common.PathExists("/proc/self/status") {
contents, err := common.ReadLines("/proc/self/status")
if common.PathExists("/rootfs/proc/self/status") {
contents, err := common.ReadLines("/rootfs/proc/self/status")
if err == nil {
if common.StringContains(contents, "s_context:") ||
@ -343,8 +343,8 @@ func GetVirtualization() (string, string, error) {
}
}
if common.PathExists("/proc/self/cgroup") {
contents, err := common.ReadLines("/proc/self/cgroup")
if common.PathExists("/rootfs/proc/self/cgroup") {
contents, err := common.ReadLines("/rootfs/proc/self/cgroup")
if err == nil {
if common.StringContains(contents, "lxc") ||

View File

@ -9,7 +9,7 @@ import (
)
func LoadAvg() (*LoadAvgStat, error) {
filename := "/proc/loadavg"
filename := "/rootfs/proc/loadavg"
line, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err

View File

@ -11,7 +11,7 @@ import (
)
func VirtualMemory() (*VirtualMemoryStat, error) {
filename := "/proc/meminfo"
filename := "/rootfs/proc/meminfo"
lines, _ := common.ReadLines(filename)
ret := &VirtualMemoryStat{}
@ -67,7 +67,7 @@ func SwapMemory() (*SwapMemoryStat, error) {
} else {
ret.UsedPercent = 0
}
lines, _ := common.ReadLines("/proc/vmstat")
lines, _ := common.ReadLines("/rootfs/proc/vmstat")
for _, l := range lines {
fields := strings.Fields(l)
if len(fields) < 2 {

View File

@ -15,7 +15,7 @@ import (
// every network interface installed on the system is returned
// separately.
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
filename := "/proc/net/dev"
filename := "/rootfs/proc/net/dev"
lines, err := common.ReadLines(filename)
if err != nil {
return nil, err

View File

@ -181,7 +181,7 @@ func (p *Process) IsRunning() (bool, error) {
return true, common.NotImplementedError
}
// MemoryMaps get memory maps from /proc/(pid)/smaps
// MemoryMaps get memory maps from /rootfs/proc/(pid)/smaps
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
pid := p.Pid
var ret []MemoryMapsStat
@ -263,7 +263,7 @@ func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
** Internal functions
**/
// Get num_fds from /proc/(pid)/fd
// Get num_fds from /rootfs/proc/(pid)/fd
func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) {
pid := p.Pid
statPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "fd")
@ -296,7 +296,7 @@ func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) {
return numFDs, openfiles, nil
}
// Get cwd from /proc/(pid)/cwd
// Get cwd from /rootfs/proc/(pid)/cwd
func (p *Process) fillFromCwd() (string, error) {
pid := p.Pid
cwdPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "cwd")
@ -307,7 +307,7 @@ func (p *Process) fillFromCwd() (string, error) {
return string(cwd), nil
}
// Get exe from /proc/(pid)/exe
// Get exe from /rootfs/proc/(pid)/exe
func (p *Process) fillFromExe() (string, error) {
pid := p.Pid
exePath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "exe")
@ -318,7 +318,7 @@ func (p *Process) fillFromExe() (string, error) {
return string(exe), nil
}
// Get cmdline from /proc/(pid)/cmdline
// Get cmdline from /rootfs/proc/(pid)/cmdline
func (p *Process) fillFromCmdline() (string, error) {
pid := p.Pid
cmdPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "cmdline")
@ -336,7 +336,7 @@ func (p *Process) fillFromCmdline() (string, error) {
return strings.Join(ret, " "), nil
}
// Get IO status from /proc/(pid)/io
// Get IO status from /rootfs/proc/(pid)/io
func (p *Process) fillFromIO() (*IOCountersStat, error) {
pid := p.Pid
ioPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "io")
@ -375,7 +375,7 @@ func (p *Process) fillFromIO() (*IOCountersStat, error) {
return ret, nil
}
// Get memory info from /proc/(pid)/statm
// Get memory info from /rootfs/proc/(pid)/statm
func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) {
pid := p.Pid
memPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "statm")
@ -427,7 +427,7 @@ func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) {
return memInfo, memInfoEx, nil
}
// Get various status from /proc/(pid)/status
// Get various status from /rootfs/proc/(pid)/status
func (p *Process) fillFromStatus() error {
pid := p.Pid
statPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "status")