2015-05-14 12:05:14 -04:00
|
|
|
package sysinfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-07-16 19:00:55 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
2015-05-14 12:05:14 -04:00
|
|
|
)
|
|
|
|
|
2015-08-06 07:54:48 -04:00
|
|
|
// New returns a new SysInfo, using the filesystem to detect which features
|
|
|
|
// the kernel supports. If `quiet` is `false` warnings are printed in logs
|
|
|
|
// whenever an error occurs or misconfigurations are present.
|
2015-05-14 12:05:14 -04:00
|
|
|
func New(quiet bool) *SysInfo {
|
|
|
|
sysInfo := &SysInfo{}
|
2015-06-16 22:36:20 -04:00
|
|
|
sysInfo.cgroupMemInfo = checkCgroupMem(quiet)
|
2015-07-22 06:31:34 -04:00
|
|
|
sysInfo.cgroupCPUInfo = checkCgroupCPU(quiet)
|
2015-08-05 10:35:18 -04:00
|
|
|
sysInfo.cgroupBlkioInfo = checkCgroupBlkioInfo(quiet)
|
|
|
|
sysInfo.cgroupCpusetInfo = checkCgroupCpusetInfo(quiet)
|
2015-05-14 12:05:14 -04:00
|
|
|
|
2015-06-16 22:36:20 -04:00
|
|
|
_, err := cgroups.FindCgroupMountpoint("devices")
|
|
|
|
sysInfo.CgroupDevicesEnabled = err == nil
|
2015-05-14 12:05:14 -04:00
|
|
|
|
2015-06-16 22:36:20 -04:00
|
|
|
sysInfo.IPv4ForwardingDisabled = !readProcBool("/proc/sys/net/ipv4/ip_forward")
|
|
|
|
sysInfo.BridgeNfCallIptablesDisabled = !readProcBool("/proc/sys/net/bridge/bridge-nf-call-iptables")
|
2015-07-22 06:31:34 -04:00
|
|
|
sysInfo.BridgeNfCallIP6tablesDisabled = !readProcBool("/proc/sys/net/bridge/bridge-nf-call-ip6tables")
|
2015-06-16 22:36:20 -04:00
|
|
|
|
|
|
|
// Check if AppArmor is supported.
|
|
|
|
if _, err := os.Stat("/sys/kernel/security/apparmor"); !os.IsNotExist(err) {
|
|
|
|
sysInfo.AppArmor = true
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|
|
|
|
|
2015-06-16 22:36:20 -04:00
|
|
|
return sysInfo
|
|
|
|
}
|
|
|
|
|
2015-08-06 18:29:10 -04:00
|
|
|
// checkCgroupMem reads the memory information from the memory cgroup mount point.
|
|
|
|
func checkCgroupMem(quiet bool) cgroupMemInfo {
|
2015-06-16 22:36:20 -04:00
|
|
|
mountPoint, err := cgroups.FindCgroupMountpoint("memory")
|
|
|
|
if err != nil {
|
2015-05-14 12:05:14 -04:00
|
|
|
if !quiet {
|
2015-06-16 22:36:20 -04:00
|
|
|
logrus.Warnf("Your kernel does not support cgroup memory limit: %v", err)
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|
2015-08-06 18:29:10 -04:00
|
|
|
return cgroupMemInfo{}
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 18:29:10 -04:00
|
|
|
swapLimit := cgroupEnabled(mountPoint, "memory.memsw.limit_in_bytes")
|
|
|
|
if !quiet && !swapLimit {
|
2015-06-16 22:36:20 -04:00
|
|
|
logrus.Warn("Your kernel does not support swap memory limit.")
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|
2015-08-06 18:29:10 -04:00
|
|
|
oomKillDisable := cgroupEnabled(mountPoint, "memory.oom_control")
|
|
|
|
if !quiet && !oomKillDisable {
|
2015-06-16 22:36:20 -04:00
|
|
|
logrus.Warnf("Your kernel does not support oom control.")
|
2015-06-16 21:19:11 -04:00
|
|
|
}
|
2015-08-06 18:29:10 -04:00
|
|
|
memorySwappiness := cgroupEnabled(mountPoint, "memory.swappiness")
|
|
|
|
if !quiet && !memorySwappiness {
|
2015-07-12 03:46:33 -04:00
|
|
|
logrus.Warnf("Your kernel does not support memory swappiness.")
|
|
|
|
}
|
2015-08-19 11:56:55 -04:00
|
|
|
kernelMemory := cgroupEnabled(mountPoint, "memory.kmem.limit_in_bytes")
|
|
|
|
if !quiet && !kernelMemory {
|
|
|
|
logrus.Warnf("Your kernel does not support kernel memory limit.")
|
|
|
|
}
|
2015-06-16 22:36:20 -04:00
|
|
|
|
2015-08-06 18:29:10 -04:00
|
|
|
return cgroupMemInfo{
|
|
|
|
MemoryLimit: true,
|
|
|
|
SwapLimit: swapLimit,
|
|
|
|
OomKillDisable: oomKillDisable,
|
|
|
|
MemorySwappiness: memorySwappiness,
|
2015-08-19 11:56:55 -04:00
|
|
|
KernelMemory: kernelMemory,
|
2015-08-06 18:29:10 -04:00
|
|
|
}
|
2015-06-16 22:36:20 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 18:29:10 -04:00
|
|
|
// checkCgroupCPU reads the cpu information from the cpu cgroup mount point.
|
|
|
|
func checkCgroupCPU(quiet bool) cgroupCPUInfo {
|
2015-06-16 22:36:20 -04:00
|
|
|
mountPoint, err := cgroups.FindCgroupMountpoint("cpu")
|
|
|
|
if err != nil {
|
|
|
|
if !quiet {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
2015-08-06 18:29:10 -04:00
|
|
|
return cgroupCPUInfo{}
|
2015-06-16 21:19:11 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 18:29:10 -04:00
|
|
|
cpuShares := cgroupEnabled(mountPoint, "cpu.shares")
|
|
|
|
if !quiet && !cpuShares {
|
2015-08-05 10:35:18 -04:00
|
|
|
logrus.Warn("Your kernel does not support cgroup cpu shares")
|
|
|
|
}
|
|
|
|
|
2015-08-06 18:29:10 -04:00
|
|
|
cpuCfsPeriod := cgroupEnabled(mountPoint, "cpu.cfs_period_us")
|
|
|
|
if !quiet && !cpuCfsPeriod {
|
2015-06-16 22:36:20 -04:00
|
|
|
logrus.Warn("Your kernel does not support cgroup cfs period")
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 18:29:10 -04:00
|
|
|
cpuCfsQuota := cgroupEnabled(mountPoint, "cpu.cfs_quota_us")
|
|
|
|
if !quiet && !cpuCfsQuota {
|
2015-06-16 22:36:20 -04:00
|
|
|
logrus.Warn("Your kernel does not support cgroup cfs quotas")
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|
2015-08-06 18:29:10 -04:00
|
|
|
return cgroupCPUInfo{
|
|
|
|
CPUShares: cpuShares,
|
|
|
|
CPUCfsPeriod: cpuCfsPeriod,
|
|
|
|
CPUCfsQuota: cpuCfsQuota,
|
|
|
|
}
|
2015-06-16 22:36:20 -04:00
|
|
|
}
|
2015-05-14 12:05:14 -04:00
|
|
|
|
2015-08-06 18:29:10 -04:00
|
|
|
// checkCgroupBlkioInfo reads the blkio information from the blkio cgroup mount point.
|
|
|
|
func checkCgroupBlkioInfo(quiet bool) cgroupBlkioInfo {
|
2015-08-05 10:35:18 -04:00
|
|
|
mountPoint, err := cgroups.FindCgroupMountpoint("blkio")
|
|
|
|
if err != nil {
|
|
|
|
if !quiet {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
2015-08-06 18:29:10 -04:00
|
|
|
return cgroupBlkioInfo{}
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 18:29:10 -04:00
|
|
|
w := cgroupEnabled(mountPoint, "blkio.weight")
|
|
|
|
if !quiet && !w {
|
2015-08-05 10:35:18 -04:00
|
|
|
logrus.Warn("Your kernel does not support cgroup blkio weight")
|
|
|
|
}
|
2015-08-06 18:29:10 -04:00
|
|
|
return cgroupBlkioInfo{BlkioWeight: w}
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 18:29:10 -04:00
|
|
|
// checkCgroupCpusetInfo reads the cpuset information from the cpuset cgroup mount point.
|
|
|
|
func checkCgroupCpusetInfo(quiet bool) cgroupCpusetInfo {
|
2015-08-05 10:35:18 -04:00
|
|
|
_, err := cgroups.FindCgroupMountpoint("cpuset")
|
|
|
|
if err != nil {
|
|
|
|
if !quiet {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
2015-08-06 18:29:10 -04:00
|
|
|
return cgroupCpusetInfo{}
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 18:29:10 -04:00
|
|
|
return cgroupCpusetInfo{Cpuset: true}
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2015-06-16 22:36:20 -04: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 12:05:14 -04:00
|
|
|
}
|