2015-05-14 16:05:14 +00:00
|
|
|
package sysinfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-07-16 23:00:55 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
2015-05-14 16:05:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new SysInfo, using the filesystem to detect which features the kernel supports.
|
|
|
|
func New(quiet bool) *SysInfo {
|
|
|
|
sysInfo := &SysInfo{}
|
2015-06-17 02:36:20 +00:00
|
|
|
sysInfo.cgroupMemInfo = checkCgroupMem(quiet)
|
2015-07-22 10:31:34 +00:00
|
|
|
sysInfo.cgroupCPUInfo = checkCgroupCPU(quiet)
|
2015-05-14 16:05:14 +00:00
|
|
|
|
2015-06-17 02:36:20 +00:00
|
|
|
_, err := cgroups.FindCgroupMountpoint("devices")
|
|
|
|
sysInfo.CgroupDevicesEnabled = err == nil
|
2015-05-14 16:05:14 +00:00
|
|
|
|
2015-06-17 02:36:20 +00:00
|
|
|
sysInfo.IPv4ForwardingDisabled = !readProcBool("/proc/sys/net/ipv4/ip_forward")
|
|
|
|
sysInfo.BridgeNfCallIptablesDisabled = !readProcBool("/proc/sys/net/bridge/bridge-nf-call-iptables")
|
2015-07-22 10:31:34 +00:00
|
|
|
sysInfo.BridgeNfCallIP6tablesDisabled = !readProcBool("/proc/sys/net/bridge/bridge-nf-call-ip6tables")
|
2015-06-17 02:36:20 +00:00
|
|
|
|
|
|
|
// Check if AppArmor is supported.
|
|
|
|
if _, err := os.Stat("/sys/kernel/security/apparmor"); !os.IsNotExist(err) {
|
|
|
|
sysInfo.AppArmor = true
|
2015-05-14 16:05:14 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 02:36:20 +00:00
|
|
|
return sysInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCgroupMem(quiet bool) *cgroupMemInfo {
|
|
|
|
info := &cgroupMemInfo{}
|
|
|
|
mountPoint, err := cgroups.FindCgroupMountpoint("memory")
|
|
|
|
if err != nil {
|
2015-05-14 16:05:14 +00:00
|
|
|
if !quiet {
|
2015-06-17 02:36:20 +00:00
|
|
|
logrus.Warnf("Your kernel does not support cgroup memory limit: %v", err)
|
2015-05-14 16:05:14 +00:00
|
|
|
}
|
2015-06-19 01:44:23 +00:00
|
|
|
return info
|
2015-05-14 16:05:14 +00:00
|
|
|
}
|
2015-06-17 02:36:20 +00:00
|
|
|
info.MemoryLimit = true
|
2015-05-14 16:05:14 +00:00
|
|
|
|
2015-06-17 02:36:20 +00:00
|
|
|
info.SwapLimit = cgroupEnabled(mountPoint, "memory.memsw.limit_in_bytes")
|
|
|
|
if !quiet && !info.SwapLimit {
|
|
|
|
logrus.Warn("Your kernel does not support swap memory limit.")
|
2015-05-14 16:05:14 +00:00
|
|
|
}
|
2015-06-17 02:36:20 +00:00
|
|
|
info.OomKillDisable = cgroupEnabled(mountPoint, "memory.oom_control")
|
|
|
|
if !quiet && !info.OomKillDisable {
|
|
|
|
logrus.Warnf("Your kernel does not support oom control.")
|
2015-06-17 01:19:11 +00:00
|
|
|
}
|
2015-07-12 07:46:33 +00:00
|
|
|
info.MemorySwappiness = cgroupEnabled(mountPoint, "memory.swappiness")
|
|
|
|
if !quiet && !info.MemorySwappiness {
|
|
|
|
logrus.Warnf("Your kernel does not support memory swappiness.")
|
|
|
|
}
|
2015-06-17 02:36:20 +00:00
|
|
|
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
2015-07-22 10:31:34 +00:00
|
|
|
func checkCgroupCPU(quiet bool) *cgroupCPUInfo {
|
|
|
|
info := &cgroupCPUInfo{}
|
2015-06-17 02:36:20 +00:00
|
|
|
mountPoint, err := cgroups.FindCgroupMountpoint("cpu")
|
|
|
|
if err != nil {
|
|
|
|
if !quiet {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
2015-06-19 01:44:23 +00:00
|
|
|
return info
|
2015-06-17 01:19:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 10:31:34 +00:00
|
|
|
info.CPUCfsPeriod = cgroupEnabled(mountPoint, "cpu.cfs_period_us")
|
|
|
|
if !quiet && !info.CPUCfsPeriod {
|
2015-06-17 02:36:20 +00:00
|
|
|
logrus.Warn("Your kernel does not support cgroup cfs period")
|
2015-05-14 16:05:14 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 10:31:34 +00:00
|
|
|
info.CPUCfsQuota = cgroupEnabled(mountPoint, "cpu.cfs_quota_us")
|
|
|
|
if !quiet && !info.CPUCfsQuota {
|
2015-06-17 02:36:20 +00:00
|
|
|
logrus.Warn("Your kernel does not support cgroup cfs quotas")
|
2015-05-14 16:05:14 +00:00
|
|
|
}
|
2015-06-17 02:36:20 +00:00
|
|
|
return info
|
|
|
|
}
|
2015-05-14 16:05:14 +00:00
|
|
|
|
2015-06-17 02:36:20 +00:00
|
|
|
func cgroupEnabled(mountPoint, name string) bool {
|
|
|
|
_, err := os.Stat(path.Join(mountPoint, name))
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readProcBool(path string) bool {
|
|
|
|
val, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(val)) == "1"
|
2015-05-14 16:05:14 +00:00
|
|
|
}
|