2015-05-14 12:05:14 -04:00
|
|
|
package sysinfo
|
|
|
|
|
|
|
|
import (
|
2016-01-20 22:20:59 -05:00
|
|
|
"fmt"
|
2015-05-14 12:05:14 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2016-01-11 14:44:34 -05:00
|
|
|
"syscall"
|
2015-05-14 12:05:14 -04:00
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2016-01-11 14:44:34 -05:00
|
|
|
const (
|
|
|
|
// SeccompModeFilter refers to the syscall argument SECCOMP_MODE_FILTER.
|
|
|
|
SeccompModeFilter = uintptr(2)
|
|
|
|
)
|
|
|
|
|
2016-01-20 22:20:59 -05:00
|
|
|
func findCgroupMountpoints() (map[string]string, error) {
|
2016-10-06 12:28:08 -04:00
|
|
|
cgMounts, err := cgroups.GetCgroupMounts(false)
|
2016-01-20 22:20:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse cgroup information: %v", err)
|
|
|
|
}
|
|
|
|
mps := make(map[string]string)
|
|
|
|
for _, m := range cgMounts {
|
|
|
|
for _, ss := range m.Subsystems {
|
|
|
|
mps[ss] = m.Mountpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mps, nil
|
|
|
|
}
|
|
|
|
|
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{}
|
2016-01-20 22:20:59 -05:00
|
|
|
cgMounts, err := findCgroupMountpoints()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Failed to parse cgroup information: %v", err)
|
|
|
|
} else {
|
|
|
|
sysInfo.cgroupMemInfo = checkCgroupMem(cgMounts, quiet)
|
|
|
|
sysInfo.cgroupCPUInfo = checkCgroupCPU(cgMounts, quiet)
|
|
|
|
sysInfo.cgroupBlkioInfo = checkCgroupBlkioInfo(cgMounts, quiet)
|
|
|
|
sysInfo.cgroupCpusetInfo = checkCgroupCpusetInfo(cgMounts, quiet)
|
2015-12-15 14:15:43 -05:00
|
|
|
sysInfo.cgroupPids = checkCgroupPids(quiet)
|
2016-01-20 22:20:59 -05:00
|
|
|
}
|
2015-05-14 12:05:14 -04:00
|
|
|
|
2016-01-20 22:20:59 -05:00
|
|
|
_, ok := cgMounts["devices"]
|
|
|
|
sysInfo.CgroupDevicesEnabled = ok
|
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")
|
2016-02-26 13:47:43 -05:00
|
|
|
sysInfo.BridgeNFCallIPTablesDisabled = !readProcBool("/proc/sys/net/bridge/bridge-nf-call-iptables")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-11 14:44:34 -05:00
|
|
|
// Check if Seccomp is supported, via CONFIG_SECCOMP.
|
|
|
|
if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_GET_SECCOMP, 0, 0); err != syscall.EINVAL {
|
|
|
|
// Make sure the kernel has CONFIG_SECCOMP_FILTER.
|
|
|
|
if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_SET_SECCOMP, SeccompModeFilter, 0); err != syscall.EINVAL {
|
|
|
|
sysInfo.Seccomp = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2016-01-20 22:20:59 -05:00
|
|
|
func checkCgroupMem(cgMounts map[string]string, quiet bool) cgroupMemInfo {
|
|
|
|
mountPoint, ok := cgMounts["memory"]
|
|
|
|
if !ok {
|
2015-05-14 12:05:14 -04:00
|
|
|
if !quiet {
|
2016-06-11 13:42:38 -04:00
|
|
|
logrus.Warn("Your kernel does not support cgroup memory limit")
|
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-09-23 02:02:45 -04:00
|
|
|
memoryReservation := cgroupEnabled(mountPoint, "memory.soft_limit_in_bytes")
|
|
|
|
if !quiet && !memoryReservation {
|
|
|
|
logrus.Warn("Your kernel does not support memory reservation.")
|
|
|
|
}
|
2015-08-06 18:29:10 -04:00
|
|
|
oomKillDisable := cgroupEnabled(mountPoint, "memory.oom_control")
|
|
|
|
if !quiet && !oomKillDisable {
|
2016-06-11 13:42:38 -04:00
|
|
|
logrus.Warn("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 {
|
2016-06-11 13:42:38 -04:00
|
|
|
logrus.Warn("Your kernel does not support memory swappiness.")
|
2015-07-12 03:46:33 -04:00
|
|
|
}
|
2015-08-19 11:56:55 -04:00
|
|
|
kernelMemory := cgroupEnabled(mountPoint, "memory.kmem.limit_in_bytes")
|
|
|
|
if !quiet && !kernelMemory {
|
2016-06-11 13:42:38 -04:00
|
|
|
logrus.Warn("Your kernel does not support kernel memory limit.")
|
2015-08-19 11:56:55 -04:00
|
|
|
}
|
2015-06-16 22:36:20 -04:00
|
|
|
|
2015-08-06 18:29:10 -04:00
|
|
|
return cgroupMemInfo{
|
2015-09-23 02:02:45 -04:00
|
|
|
MemoryLimit: true,
|
|
|
|
SwapLimit: swapLimit,
|
|
|
|
MemoryReservation: memoryReservation,
|
|
|
|
OomKillDisable: oomKillDisable,
|
|
|
|
MemorySwappiness: memorySwappiness,
|
|
|
|
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.
|
2016-01-20 22:20:59 -05:00
|
|
|
func checkCgroupCPU(cgMounts map[string]string, quiet bool) cgroupCPUInfo {
|
|
|
|
mountPoint, ok := cgMounts["cpu"]
|
|
|
|
if !ok {
|
2015-06-16 22:36:20 -04:00
|
|
|
if !quiet {
|
2016-06-11 13:42:38 -04:00
|
|
|
logrus.Warn("Unable to find cpu cgroup in mounts")
|
2015-06-16 22:36:20 -04:00
|
|
|
}
|
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.
|
2016-01-20 22:20:59 -05:00
|
|
|
func checkCgroupBlkioInfo(cgMounts map[string]string, quiet bool) cgroupBlkioInfo {
|
|
|
|
mountPoint, ok := cgMounts["blkio"]
|
|
|
|
if !ok {
|
2015-08-05 10:35:18 -04:00
|
|
|
if !quiet {
|
2016-06-11 13:42:38 -04:00
|
|
|
logrus.Warn("Unable to find blkio cgroup in mounts")
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
2015-08-06 18:29:10 -04:00
|
|
|
return cgroupBlkioInfo{}
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2015-06-11 20:34:20 -04:00
|
|
|
weight := cgroupEnabled(mountPoint, "blkio.weight")
|
|
|
|
if !quiet && !weight {
|
2015-08-05 10:35:18 -04:00
|
|
|
logrus.Warn("Your kernel does not support cgroup blkio weight")
|
|
|
|
}
|
2015-06-11 20:34:20 -04:00
|
|
|
|
|
|
|
weightDevice := cgroupEnabled(mountPoint, "blkio.weight_device")
|
|
|
|
if !quiet && !weightDevice {
|
|
|
|
logrus.Warn("Your kernel does not support cgroup blkio weight_device")
|
|
|
|
}
|
2015-07-08 07:06:48 -04:00
|
|
|
|
|
|
|
readBpsDevice := cgroupEnabled(mountPoint, "blkio.throttle.read_bps_device")
|
|
|
|
if !quiet && !readBpsDevice {
|
|
|
|
logrus.Warn("Your kernel does not support cgroup blkio throttle.read_bps_device")
|
|
|
|
}
|
|
|
|
|
|
|
|
writeBpsDevice := cgroupEnabled(mountPoint, "blkio.throttle.write_bps_device")
|
|
|
|
if !quiet && !writeBpsDevice {
|
|
|
|
logrus.Warn("Your kernel does not support cgroup blkio throttle.write_bps_device")
|
|
|
|
}
|
2015-07-08 07:06:48 -04:00
|
|
|
readIOpsDevice := cgroupEnabled(mountPoint, "blkio.throttle.read_iops_device")
|
|
|
|
if !quiet && !readIOpsDevice {
|
|
|
|
logrus.Warn("Your kernel does not support cgroup blkio throttle.read_iops_device")
|
|
|
|
}
|
|
|
|
|
|
|
|
writeIOpsDevice := cgroupEnabled(mountPoint, "blkio.throttle.write_iops_device")
|
|
|
|
if !quiet && !writeIOpsDevice {
|
|
|
|
logrus.Warn("Your kernel does not support cgroup blkio throttle.write_iops_device")
|
|
|
|
}
|
2015-06-11 20:34:20 -04:00
|
|
|
return cgroupBlkioInfo{
|
2015-07-08 07:06:48 -04:00
|
|
|
BlkioWeight: weight,
|
|
|
|
BlkioWeightDevice: weightDevice,
|
|
|
|
BlkioReadBpsDevice: readBpsDevice,
|
|
|
|
BlkioWriteBpsDevice: writeBpsDevice,
|
|
|
|
BlkioReadIOpsDevice: readIOpsDevice,
|
|
|
|
BlkioWriteIOpsDevice: writeIOpsDevice,
|
2015-06-11 20:34:20 -04:00
|
|
|
}
|
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.
|
2016-01-20 22:20:59 -05:00
|
|
|
func checkCgroupCpusetInfo(cgMounts map[string]string, quiet bool) cgroupCpusetInfo {
|
|
|
|
mountPoint, ok := cgMounts["cpuset"]
|
|
|
|
if !ok {
|
2015-08-05 10:35:18 -04:00
|
|
|
if !quiet {
|
2016-06-11 13:42:38 -04:00
|
|
|
logrus.Warn("Unable to find cpuset cgroup in mounts")
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
2015-08-06 18:29:10 -04:00
|
|
|
return cgroupCpusetInfo{}
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2015-09-08 14:40:55 -04: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 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2015-12-15 14:15:43 -05:00
|
|
|
// checkCgroupPids reads the pids information from the pids cgroup mount point.
|
|
|
|
func checkCgroupPids(quiet bool) cgroupPids {
|
|
|
|
_, err := cgroups.FindCgroupMountpoint("pids")
|
|
|
|
if err != nil {
|
|
|
|
if !quiet {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
|
|
|
return cgroupPids{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cgroupPids{
|
|
|
|
PidsLimit: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|