cgroup.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package cgroup
  2. import (
  3. "fmt"
  4. "github.com/coroot/coroot-node-agent/common"
  5. "github.com/coroot/coroot-node-agent/flags"
  6. "io/ioutil"
  7. "k8s.io/klog/v2"
  8. "os"
  9. "path"
  10. "regexp"
  11. "strings"
  12. "time"
  13. )
  14. var (
  15. cgRoot = *flags.CgroupRoot
  16. baseCgroupPath = ""
  17. dockerIdRegexp = regexp.MustCompile(`([a-z0-9]{64})`)
  18. crioIdRegexp = regexp.MustCompile(`crio-([a-z0-9]{64})`)
  19. containerdIdRegexp = regexp.MustCompile(`cri-containerd-([a-z0-9]{64})`)
  20. lxcIdRegexp = regexp.MustCompile(`/lxc/([^/]+)`)
  21. systemSliceIdRegexp = regexp.MustCompile(`(/system\.slice/([^/]+))`)
  22. )
  23. type Version uint8
  24. const (
  25. V1 Version = iota
  26. V2
  27. )
  28. type ContainerType uint8
  29. const (
  30. ContainerTypeUnknown ContainerType = iota
  31. ContainerTypeStandaloneProcess
  32. ContainerTypeDocker
  33. ContainerTypeCrio
  34. ContainerTypeContainerd
  35. ContainerTypeLxc
  36. ContainerTypeSystemdService
  37. )
  38. func (t ContainerType) String() string {
  39. switch t {
  40. case ContainerTypeStandaloneProcess:
  41. return "standalone"
  42. case ContainerTypeDocker:
  43. return "docker"
  44. case ContainerTypeCrio:
  45. return "crio"
  46. case ContainerTypeContainerd:
  47. return "cri-containerd"
  48. case ContainerTypeLxc:
  49. return "lxc"
  50. case ContainerTypeSystemdService:
  51. return "systemd"
  52. default:
  53. return "unknown"
  54. }
  55. }
  56. type Cgroup struct {
  57. Id string
  58. Version Version
  59. ContainerType ContainerType
  60. ContainerId string
  61. subsystems map[string]string
  62. }
  63. func (cg *Cgroup) CreatedAt() time.Time {
  64. p := path.Join(cgRoot, cg.subsystems[""]) //v2
  65. if cg.Version == V1 {
  66. p = path.Join(cgRoot, "cpu", cg.subsystems["cpu"])
  67. }
  68. fi, err := os.Stat(p)
  69. if err != nil {
  70. if !common.IsNotExist(err) {
  71. klog.Errorln(err)
  72. }
  73. return time.Time{}
  74. }
  75. return fi.ModTime()
  76. }
  77. func NewFromProcessCgroupFile(filePath string) (*Cgroup, error) {
  78. data, err := ioutil.ReadFile(filePath)
  79. if err != nil {
  80. return nil, err
  81. }
  82. cg := &Cgroup{
  83. subsystems: map[string]string{},
  84. }
  85. for _, line := range strings.Split(string(data), "\n") {
  86. parts := strings.SplitN(line, ":", 3)
  87. if len(parts) < 3 {
  88. continue
  89. }
  90. for _, cgType := range strings.Split(parts[1], ",") {
  91. cg.subsystems[cgType] = path.Join(baseCgroupPath, parts[2])
  92. }
  93. }
  94. if p := cg.subsystems["cpu"]; p != "" {
  95. cg.Id = p
  96. cg.Version = V1
  97. } else {
  98. cg.Id = cg.subsystems[""]
  99. cg.Version = V2
  100. }
  101. if cg.ContainerType, cg.ContainerId, err = containerByCgroup(cg.Id); err != nil {
  102. return nil, err
  103. }
  104. return cg, nil
  105. }
  106. func containerByCgroup(path string) (ContainerType, string, error) {
  107. parts := strings.Split(strings.TrimLeft(path, "/"), "/")
  108. if len(parts) < 2 {
  109. return ContainerTypeStandaloneProcess, "", nil
  110. }
  111. prefix := parts[0]
  112. if prefix == "user.slice" || prefix == "init.scope" {
  113. return ContainerTypeStandaloneProcess, "", nil
  114. }
  115. if prefix == "docker" || (prefix == "system.slice" && strings.HasPrefix(parts[1], "docker-")) {
  116. matches := dockerIdRegexp.FindStringSubmatch(path)
  117. if matches == nil {
  118. return ContainerTypeUnknown, "", fmt.Errorf("invalid docker cgroup %s", path)
  119. }
  120. return ContainerTypeDocker, matches[1], nil
  121. }
  122. if prefix == "kubepods" || prefix == "kubepods.slice" {
  123. crioMatches := crioIdRegexp.FindStringSubmatch(path)
  124. if crioMatches != nil {
  125. return ContainerTypeCrio, crioMatches[1], nil
  126. }
  127. containerdMatches := containerdIdRegexp.FindStringSubmatch(path)
  128. if containerdMatches != nil {
  129. return ContainerTypeContainerd, containerdMatches[1], nil
  130. }
  131. matches := dockerIdRegexp.FindStringSubmatch(path)
  132. if matches == nil {
  133. return ContainerTypeUnknown, "", fmt.Errorf("invalid docker cgroup %s", path)
  134. }
  135. return ContainerTypeDocker, matches[1], nil
  136. }
  137. if prefix == "lxc" {
  138. matches := lxcIdRegexp.FindStringSubmatch(path)
  139. if matches == nil {
  140. return ContainerTypeUnknown, "", fmt.Errorf("invalid lxc cgroup %s", path)
  141. }
  142. return ContainerTypeLxc, matches[1], nil
  143. }
  144. if prefix == "system.slice" {
  145. matches := systemSliceIdRegexp.FindStringSubmatch(path)
  146. if matches == nil {
  147. return ContainerTypeUnknown, "", fmt.Errorf("invalid systemd cgroup %s", path)
  148. }
  149. return ContainerTypeSystemdService, matches[1], nil
  150. }
  151. return ContainerTypeUnknown, "", fmt.Errorf("unknown container: %s", path)
  152. }