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
|
|
|
)
|
|
|
|
|
2015-08-06 11:54:48 +00: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 16:05:14 +00:00
|
|
|
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-08-05 14:35:18 +00:00
|
|
|
sysInfo.cgroupBlkioInfo = checkCgroupBlkioInfo(quiet)
|
|
|
|
sysInfo.cgroupCpusetInfo = checkCgroupCpusetInfo(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
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:29:10 +00:00
|
|
|
// checkCgroupMem reads the memory information from the memory cgroup mount point.
|
|
|
|
func checkCgroupMem(quiet bool) cgroupMemInfo {
|
2015-06-17 02:36:20 +00:00
|
|
|
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-08-06 22:29:10 +00:00
|
|
|
return cgroupMemInfo{}
|
2015-05-14 16:05:14 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:29:10 +00:00
|
|
|
swapLimit := cgroupEnabled(mountPoint, "memory.memsw.limit_in_bytes")
|
|
|
|
if !quiet && !swapLimit {
|
2015-06-17 02:36:20 +00:00
|
|
|
logrus.Warn("Your kernel does not support swap memory limit.")
|
2015-05-14 16:05:14 +00:00
|
|
|
}
|
2015-09-23 06:02:45 +00:00
|
|
|
memoryReservation := cgroupEnabled(mountPoint, "memory.soft_limit_in_bytes")
|
|
|
|
if !quiet && !memoryReservation {
|
|
|
|
logrus.Warn("Your kernel does not support memory reservation.")
|
|
|
|
}
|
2015-08-06 22:29:10 +00:00
|
|
|
oomKillDisable := cgroupEnabled(mountPoint, "memory.oom_control")
|
|
|
|
if !quiet && !oomKillDisable {
|
2015-06-17 02:36:20 +00:00
|
|
|
logrus.Warnf("Your kernel does not support oom control.")
|
2015-06-17 01:19:11 +00:00
|
|
|
}
|
2015-08-06 22:29:10 +00:00
|
|
|
memorySwappiness := cgroupEnabled(mountPoint, "memory.swappiness")
|
|
|
|
if !quiet && !memorySwappiness {
|
2015-07-12 07:46:33 +00:00
|
|
|
logrus.Warnf("Your kernel does not support memory swappiness.")
|
|
|
|
}
|
2015-08-19 15:56:55 +00:00
|
|
|
kernelMemory := cgroupEnabled(mountPoint, "memory.kmem.limit_in_bytes")
|
|
|
|
if !quiet && !kernelMemory {
|
|
|
|
logrus.Warnf("Your kernel does not support kernel memory limit.")
|
|
|
|
}
|
2015-06-17 02:36:20 +00:00
|
|
|
|
2015-08-06 22:29:10 +00:00
|
|
|
return cgroupMemInfo{
|
2015-09-23 06:02:45 +00:00
|
|
|
MemoryLimit: true,
|
|
|
|
SwapLimit: swapLimit,
|
|
|
|
MemoryReservation: memoryReservation,
|
|
|
|
OomKillDisable: oomKillDisable,
|
|
|
|
MemorySwappiness: memorySwappiness,
|
|
|
|
KernelMemory: kernelMemory,
|
2015-08-06 22:29:10 +00:00
|
|
|
}
|
2015-06-17 02:36:20 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:29:10 +00:00
|
|
|
// checkCgroupCPU reads the cpu information from the cpu cgroup mount point.
|
|
|
|
func checkCgroupCPU(quiet bool) cgroupCPUInfo {
|
2015-06-17 02:36:20 +00:00
|
|
|
mountPoint, err := cgroups.FindCgroupMountpoint("cpu")
|
|
|
|
if err != nil {
|
|
|
|
if !quiet {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
2015-08-06 22:29:10 +00:00
|
|
|
return cgroupCPUInfo{}
|
2015-06-17 01:19:11 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:29:10 +00:00
|
|
|
cpuShares := cgroupEnabled(mountPoint, "cpu.shares")
|
|
|
|
if !quiet && !cpuShares {
|
2015-08-05 14:35:18 +00:00
|
|
|
logrus.Warn("Your kernel does not support cgroup cpu shares")
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:29:10 +00:00
|
|
|
cpuCfsPeriod := cgroupEnabled(mountPoint, "cpu.cfs_period_us")
|
|
|
|
if !quiet && !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-08-06 22:29:10 +00:00
|
|
|
cpuCfsQuota := cgroupEnabled(mountPoint, "cpu.cfs_quota_us")
|
|
|
|
if !quiet && !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-08-06 22:29:10 +00:00
|
|
|
return cgroupCPUInfo{
|
|
|
|
CPUShares: cpuShares,
|
|
|
|
CPUCfsPeriod: cpuCfsPeriod,
|
|
|
|
CPUCfsQuota: cpuCfsQuota,
|
|
|
|
}
|
2015-06-17 02:36:20 +00:00
|
|
|
}
|
2015-05-14 16:05:14 +00:00
|
|
|
|
2015-08-06 22:29:10 +00:00
|
|
|
// checkCgroupBlkioInfo reads the blkio information from the blkio cgroup mount point.
|
|
|
|
func checkCgroupBlkioInfo(quiet bool) cgroupBlkioInfo {
|
2015-08-05 14:35:18 +00:00
|
|
|
mountPoint, err := cgroups.FindCgroupMountpoint("blkio")
|
|
|
|
if err != nil {
|
|
|
|
if !quiet {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
2015-08-06 22:29:10 +00:00
|
|
|
return cgroupBlkioInfo{}
|
2015-08-05 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:29:10 +00:00
|
|
|
w := cgroupEnabled(mountPoint, "blkio.weight")
|
|
|
|
if !quiet && !w {
|
2015-08-05 14:35:18 +00:00
|
|
|
logrus.Warn("Your kernel does not support cgroup blkio weight")
|
|
|
|
}
|
2015-08-06 22:29:10 +00:00
|
|
|
return cgroupBlkioInfo{BlkioWeight: w}
|
2015-08-05 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:29:10 +00:00
|
|
|
// checkCgroupCpusetInfo reads the cpuset information from the cpuset cgroup mount point.
|
|
|
|
func checkCgroupCpusetInfo(quiet bool) cgroupCpusetInfo {
|
2015-09-08 18:40:55 +00:00
|
|
|
mountPoint, err := cgroups.FindCgroupMountpoint("cpuset")
|
2015-08-05 14:35:18 +00:00
|
|
|
if err != nil {
|
|
|
|
if !quiet {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
2015-08-06 22:29:10 +00:00
|
|
|
return cgroupCpusetInfo{}
|
2015-08-05 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 18:40:55 +00:00
|
|
|
cpus, err := ioutil.ReadFile(path.Join(mountPoint, "cpuset.cpus"))
|
|
|
|
if err != nil {
|
|
|
|
return cgroupCpusetInfo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
mems, err := ioutil.ReadFile(path.Join(mountPoint, "cpuset.mems"))
|
|
|
|
if err != nil {
|
|
|
|
return cgroupCpusetInfo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cgroupCpusetInfo{
|
|
|
|
Cpuset: true,
|
|
|
|
Cpus: strings.TrimSpace(string(cpus)),
|
|
|
|
Mems: strings.TrimSpace(string(mems)),
|
|
|
|
}
|
2015-08-05 14:35:18 +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
|
|
|
}
|