cgroup.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package cgroup
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "regexp"
  7. "strings"
  8. "time"
  9. "github.com/coroot/coroot-node-agent/common"
  10. "github.com/coroot/coroot-node-agent/flags"
  11. klog "github.com/sirupsen/logrus"
  12. )
  13. var (
  14. cgRoot = *flags.CgroupRoot
  15. baseCgroupPath = ""
  16. dockerIdRegexp = regexp.MustCompile(`([a-z0-9]{64})`)
  17. crioIdRegexp = regexp.MustCompile(`crio-([a-z0-9]{64})`)
  18. containerdIdRegexp = regexp.MustCompile(`cri-containerd[-:]([a-z0-9]{64})`)
  19. lxcIdRegexp = regexp.MustCompile(`/lxc/([^/]+)`)
  20. systemSliceIdRegexp = regexp.MustCompile(`(/(system|runtime)\.slice/([^/]+))`)
  21. )
  22. type Version uint8
  23. const (
  24. V1 Version = iota
  25. V2
  26. )
  27. type ContainerType uint8
  28. const (
  29. ContainerTypeUnknown ContainerType = iota
  30. ContainerTypeStandaloneProcess
  31. ContainerTypeDocker
  32. ContainerTypeCrio
  33. ContainerTypeContainerd
  34. ContainerTypeLxc
  35. ContainerTypeSystemdService
  36. ContainerTypeSandbox
  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 (cg *Cgroup) FileSystemPath() string {
  78. if cg == nil {
  79. return ""
  80. }
  81. if cg.Version == V1 {
  82. return path.Join(cgRoot, "cpu", cg.subsystems["cpu"])
  83. }
  84. return path.Join(cgRoot, cg.subsystems[""])
  85. }
  86. func NewFromProcessCgroupFile(filePath string) (*Cgroup, error) {
  87. data, err := os.ReadFile(filePath)
  88. if err != nil {
  89. return nil, err
  90. }
  91. cg := &Cgroup{
  92. subsystems: map[string]string{},
  93. }
  94. for _, line := range strings.Split(string(data), "\n") {
  95. parts := strings.SplitN(line, ":", 3)
  96. if len(parts) < 3 {
  97. continue
  98. }
  99. for _, cgType := range strings.Split(parts[1], ",") {
  100. cg.subsystems[cgType] = path.Join(baseCgroupPath, parts[2])
  101. }
  102. }
  103. if p := cg.subsystems["name=systemd"]; p != "" {
  104. cg.Id = p
  105. cg.Version = V1
  106. } else if p = cg.subsystems["cpu"]; p != "" {
  107. cg.Id = p
  108. cg.Version = V1
  109. } else {
  110. cg.Id = cg.subsystems[""]
  111. cg.Version = V2
  112. }
  113. if cg.ContainerType, cg.ContainerId, err = containerByCgroup(cg.Id); err != nil {
  114. return nil, err
  115. }
  116. return cg, nil
  117. }
  118. func containerByCgroup(path string) (ContainerType, string, error) {
  119. parts := strings.Split(strings.TrimLeft(path, "/"), "/")
  120. if len(parts) < 2 {
  121. return ContainerTypeStandaloneProcess, "", nil
  122. }
  123. prefix := parts[0]
  124. if prefix == "user.slice" || prefix == "init.scope" {
  125. return ContainerTypeStandaloneProcess, "", nil
  126. }
  127. if prefix == "docker" || (prefix == "system.slice" && strings.HasPrefix(parts[1], "docker-")) {
  128. matches := dockerIdRegexp.FindStringSubmatch(path)
  129. if matches == nil {
  130. return ContainerTypeUnknown, "", fmt.Errorf("invalid docker cgroup %s", path)
  131. }
  132. return ContainerTypeDocker, matches[1], nil
  133. }
  134. if strings.Contains(path, "kubepods") {
  135. crioMatches := crioIdRegexp.FindStringSubmatch(path)
  136. if crioMatches != nil {
  137. return ContainerTypeCrio, crioMatches[1], nil
  138. }
  139. if strings.Contains(path, "crio-conmon-") {
  140. return ContainerTypeUnknown, "", nil
  141. }
  142. containerdMatches := containerdIdRegexp.FindStringSubmatch(path)
  143. if containerdMatches != nil {
  144. return ContainerTypeContainerd, containerdMatches[1], nil
  145. }
  146. matches := dockerIdRegexp.FindStringSubmatch(path)
  147. if matches == nil {
  148. return ContainerTypeSandbox, "", nil
  149. }
  150. return ContainerTypeDocker, matches[1], nil
  151. }
  152. if prefix == "lxc" {
  153. matches := lxcIdRegexp.FindStringSubmatch(path)
  154. if matches == nil {
  155. return ContainerTypeUnknown, "", fmt.Errorf("invalid lxc cgroup %s", path)
  156. }
  157. return ContainerTypeLxc, matches[1], nil
  158. }
  159. if prefix == "system.slice" || prefix == "runtime.slice" {
  160. matches := systemSliceIdRegexp.FindStringSubmatch(path)
  161. if matches == nil {
  162. return ContainerTypeUnknown, "", fmt.Errorf("invalid systemd cgroup %s", path)
  163. }
  164. return ContainerTypeSystemdService, strings.Replace(matches[1], "\\x2d", "-", -1), nil
  165. }
  166. return ContainerTypeUnknown, "", fmt.Errorf("unknown container: %s", path)
  167. }