cgroup.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 NewFromProcessCgroupFile(filePath string) (*Cgroup, error) {
  78. data, err := os.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 strings.Contains(path, "kubepods") {
  123. crioMatches := crioIdRegexp.FindStringSubmatch(path)
  124. if crioMatches != nil {
  125. return ContainerTypeCrio, crioMatches[1], nil
  126. }
  127. if strings.Contains(path, "crio-conmon-") {
  128. return ContainerTypeUnknown, "", nil
  129. }
  130. containerdMatches := containerdIdRegexp.FindStringSubmatch(path)
  131. if containerdMatches != nil {
  132. return ContainerTypeContainerd, containerdMatches[1], nil
  133. }
  134. matches := dockerIdRegexp.FindStringSubmatch(path)
  135. if matches == nil {
  136. return ContainerTypeSandbox, "", nil
  137. }
  138. return ContainerTypeDocker, matches[1], nil
  139. }
  140. if prefix == "lxc" {
  141. matches := lxcIdRegexp.FindStringSubmatch(path)
  142. if matches == nil {
  143. return ContainerTypeUnknown, "", fmt.Errorf("invalid lxc cgroup %s", path)
  144. }
  145. return ContainerTypeLxc, matches[1], nil
  146. }
  147. if prefix == "system.slice" || prefix == "runtime.slice" {
  148. matches := systemSliceIdRegexp.FindStringSubmatch(path)
  149. if matches == nil {
  150. return ContainerTypeUnknown, "", fmt.Errorf("invalid systemd cgroup %s", path)
  151. }
  152. return ContainerTypeSystemdService, matches[1], nil
  153. }
  154. return ContainerTypeUnknown, "", fmt.Errorf("unknown container: %s", path)
  155. }