cgroup.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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["name=systemd"]; p != "" {
  95. cg.Id = p
  96. cg.Version = V1
  97. } else if p = cg.subsystems["cpu"]; p != "" {
  98. cg.Id = p
  99. cg.Version = V1
  100. } else {
  101. cg.Id = cg.subsystems[""]
  102. cg.Version = V2
  103. }
  104. if cg.ContainerType, cg.ContainerId, err = containerByCgroup(cg.Id); err != nil {
  105. return nil, err
  106. }
  107. return cg, nil
  108. }
  109. func containerByCgroup(path string) (ContainerType, string, error) {
  110. parts := strings.Split(strings.TrimLeft(path, "/"), "/")
  111. if len(parts) < 2 {
  112. return ContainerTypeStandaloneProcess, "", nil
  113. }
  114. prefix := parts[0]
  115. if prefix == "user.slice" || prefix == "init.scope" {
  116. return ContainerTypeStandaloneProcess, "", nil
  117. }
  118. if prefix == "docker" || (prefix == "system.slice" && strings.HasPrefix(parts[1], "docker-")) {
  119. matches := dockerIdRegexp.FindStringSubmatch(path)
  120. if matches == nil {
  121. return ContainerTypeUnknown, "", fmt.Errorf("invalid docker cgroup %s", path)
  122. }
  123. return ContainerTypeDocker, matches[1], nil
  124. }
  125. if strings.Contains(path, "kubepods") {
  126. crioMatches := crioIdRegexp.FindStringSubmatch(path)
  127. if crioMatches != nil {
  128. return ContainerTypeCrio, crioMatches[1], nil
  129. }
  130. if strings.Contains(path, "crio-conmon-") {
  131. return ContainerTypeUnknown, "", nil
  132. }
  133. containerdMatches := containerdIdRegexp.FindStringSubmatch(path)
  134. if containerdMatches != nil {
  135. return ContainerTypeContainerd, containerdMatches[1], nil
  136. }
  137. matches := dockerIdRegexp.FindStringSubmatch(path)
  138. if matches == nil {
  139. return ContainerTypeSandbox, "", nil
  140. }
  141. return ContainerTypeDocker, matches[1], nil
  142. }
  143. if prefix == "lxc" {
  144. matches := lxcIdRegexp.FindStringSubmatch(path)
  145. if matches == nil {
  146. return ContainerTypeUnknown, "", fmt.Errorf("invalid lxc cgroup %s", path)
  147. }
  148. return ContainerTypeLxc, matches[1], nil
  149. }
  150. if prefix == "system.slice" || prefix == "runtime.slice" {
  151. matches := systemSliceIdRegexp.FindStringSubmatch(path)
  152. if matches == nil {
  153. return ContainerTypeUnknown, "", fmt.Errorf("invalid systemd cgroup %s", path)
  154. }
  155. return ContainerTypeSystemdService, strings.Replace(matches[1], "\\x2d", "-", -1), nil
  156. }
  157. return ContainerTypeUnknown, "", fmt.Errorf("unknown container: %s", path)
  158. }