2015-05-14 16:05:14 +00:00
|
|
|
package sysinfo
|
|
|
|
|
|
|
|
import (
|
2016-01-21 03:20:59 +00:00
|
|
|
"fmt"
|
2015-05-14 16:05:14 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2016-01-11 19:44:34 +00:00
|
|
|
"syscall"
|
2015-05-14 16:05:14 +00:00
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2016-01-11 19:44:34 +00:00
|
|
|
const (
|
|
|
|
// SeccompModeFilter refers to the syscall argument SECCOMP_MODE_FILTER.
|
|
|
|
SeccompModeFilter = uintptr(2)
|
|
|
|
)
|
|
|
|
|
2016-01-21 03:20:59 +00:00
|
|
|
func findCgroupMountpoints() (map[string]string, error) {
|
2016-09-29 18:02:38 +00:00
|
|
|
cgMounts, err := cgroups.GetCgroupMounts(false)
|
2016-01-21 03:20:59 +00: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 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{}
|
2016-01-21 03:20:59 +00: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 19:15:43 +00:00
|
|
|
sysInfo.cgroupPids = checkCgroupPids(quiet)
|
2016-01-21 03:20:59 +00:00
|
|
|
}
|
2015-05-14 16:05:14 +00:00
|
|
|
|
2016-01-21 03:20:59 +00:00
|
|
|
_, ok := cgMounts["devices"]
|
|
|
|
sysInfo.CgroupDevicesEnabled = ok
|
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")
|
2016-02-26 18:47:43 +00: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-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
|
|
|
}
|
|
|
|
|
2016-01-11 19:44:34 +00: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-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.
|
2016-01-21 03:20:59 +00:00
|
|
|
func checkCgroupMem(cgMounts map[string]string, quiet bool) cgroupMemInfo {
|
|
|
|
mountPoint, ok := cgMounts["memory"]
|
|
|
|
if !ok {
|
2015-05-14 16:05:14 +00:00
|
|
|
if !quiet {
|
2016-06-11 17:42:38 +00:00
|
|
|
logrus.Warn("Your kernel does not support cgroup memory limit")
|
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 {
|
2016-11-29 15:38:07 +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 {
|
2016-11-29 15:38:07 +00:00
|
|
|
logrus.Warn("Your kernel does not support memory reservation")
|
2015-09-23 06:02:45 +00:00
|
|
|
}
|
2015-08-06 22:29:10 +00:00
|
|
|
oomKillDisable := cgroupEnabled(mountPoint, "memory.oom_control")
|
|
|
|
if !quiet && !oomKillDisable {
|
2016-11-29 15:38:07 +00:00
|
|
|
logrus.Warn("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 {
|
2016-11-29 15:38:07 +00:00
|
|
|
logrus.Warn("Your kernel does not support memory swappiness")
|
2015-07-12 07:46:33 +00:00
|
|
|
}
|
2015-08-19 15:56:55 +00:00
|
|
|
kernelMemory := cgroupEnabled(mountPoint, "memory.kmem.limit_in_bytes")
|
|
|
|
if !quiet && !kernelMemory {
|
2016-11-29 15:38:07 +00:00
|
|
|
logrus.Warn("Your kernel does not support kernel memory limit")
|
2015-08-19 15:56:55 +00:00
|
|
|
}
|
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.
|
2016-01-21 03:20:59 +00:00
|
|
|
func checkCgroupCPU(cgMounts map[string]string, quiet bool) cgroupCPUInfo {
|
|
|
|
mountPoint, ok := cgMounts["cpu"]
|
|
|
|
if !ok {
|
2015-06-17 02:36:20 +00:00
|
|
|
if !quiet {
|
2016-06-11 17:42:38 +00:00
|
|
|
logrus.Warn("Unable to find cpu cgroup in mounts")
|
2015-06-17 02:36:20 +00:00
|
|
|
}
|
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
|
|
|
}
|
2016-06-07 19:05:43 +00:00
|
|
|
|
|
|
|
cpuRealtimePeriod := cgroupEnabled(mountPoint, "cpu.rt_period_us")
|
|
|
|
if !quiet && !cpuRealtimePeriod {
|
|
|
|
logrus.Warn("Your kernel does not support cgroup rt period")
|
|
|
|
}
|
|
|
|
|
|
|
|
cpuRealtimeRuntime := cgroupEnabled(mountPoint, "cpu.rt_runtime_us")
|
|
|
|
if !quiet && !cpuRealtimeRuntime {
|
|
|
|
logrus.Warn("Your kernel does not support cgroup rt runtime")
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:29:10 +00:00
|
|
|
return cgroupCPUInfo{
|
2016-06-07 19:05:43 +00:00
|
|
|
CPUShares: cpuShares,
|
|
|
|
CPUCfsPeriod: cpuCfsPeriod,
|
|
|
|
CPUCfsQuota: cpuCfsQuota,
|
|
|
|
CPURealtimePeriod: cpuRealtimePeriod,
|
|
|
|
CPURealtimeRuntime: cpuRealtimeRuntime,
|
2015-08-06 22:29:10 +00:00
|
|
|
}
|
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.
|
2016-01-21 03:20:59 +00:00
|
|
|
func checkCgroupBlkioInfo(cgMounts map[string]string, quiet bool) cgroupBlkioInfo {
|
|
|
|
mountPoint, ok := cgMounts["blkio"]
|
|
|
|
if !ok {
|
2015-08-05 14:35:18 +00:00
|
|
|
if !quiet {
|
2016-06-11 17:42:38 +00:00
|
|
|
logrus.Warn("Unable to find blkio cgroup in mounts")
|
2015-08-05 14:35:18 +00:00
|
|
|
}
|
2015-08-06 22:29:10 +00:00
|
|
|
return cgroupBlkioInfo{}
|
2015-08-05 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 00:34:20 +00:00
|
|
|
weight := cgroupEnabled(mountPoint, "blkio.weight")
|
|
|
|
if !quiet && !weight {
|
2015-08-05 14:35:18 +00:00
|
|
|
logrus.Warn("Your kernel does not support cgroup blkio weight")
|
|
|
|
}
|
2015-06-12 00:34:20 +00:00
|
|
|
|
|
|
|
weightDevice := cgroupEnabled(mountPoint, "blkio.weight_device")
|
|
|
|
if !quiet && !weightDevice {
|
|
|
|
logrus.Warn("Your kernel does not support cgroup blkio weight_device")
|
|
|
|
}
|
2015-07-08 11:06:48 +00: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 11:06:48 +00: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-12 00:34:20 +00:00
|
|
|
return cgroupBlkioInfo{
|
2015-07-08 11:06:48 +00:00
|
|
|
BlkioWeight: weight,
|
|
|
|
BlkioWeightDevice: weightDevice,
|
|
|
|
BlkioReadBpsDevice: readBpsDevice,
|
|
|
|
BlkioWriteBpsDevice: writeBpsDevice,
|
|
|
|
BlkioReadIOpsDevice: readIOpsDevice,
|
|
|
|
BlkioWriteIOpsDevice: writeIOpsDevice,
|
2015-06-12 00:34:20 +00:00
|
|
|
}
|
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.
|
2016-01-21 03:20:59 +00:00
|
|
|
func checkCgroupCpusetInfo(cgMounts map[string]string, quiet bool) cgroupCpusetInfo {
|
|
|
|
mountPoint, ok := cgMounts["cpuset"]
|
|
|
|
if !ok {
|
2015-08-05 14:35:18 +00:00
|
|
|
if !quiet {
|
2016-06-11 17:42:38 +00:00
|
|
|
logrus.Warn("Unable to find cpuset cgroup in mounts")
|
2015-08-05 14:35:18 +00:00
|
|
|
}
|
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-12-15 19:15:43 +00: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-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
|
|
|
}
|