main.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package main
  2. import (
  3. "bytes"
  4. "net/http"
  5. _ "net/http/pprof"
  6. "os"
  7. "runtime"
  8. "strings"
  9. "github.com/coroot/coroot-node-agent/common"
  10. "github.com/coroot/coroot-node-agent/containers"
  11. "github.com/coroot/coroot-node-agent/flags"
  12. "github.com/coroot/coroot-node-agent/logs"
  13. "github.com/coroot/coroot-node-agent/node"
  14. "github.com/coroot/coroot-node-agent/proc"
  15. "github.com/coroot/coroot-node-agent/profiling"
  16. "github.com/coroot/coroot-node-agent/prom"
  17. "github.com/coroot/coroot-node-agent/tracing"
  18. "github.com/prometheus/client_golang/prometheus"
  19. "github.com/prometheus/client_golang/prometheus/promhttp"
  20. "golang.org/x/mod/semver"
  21. "golang.org/x/sys/unix"
  22. "golang.org/x/time/rate"
  23. "k8s.io/klog/v2"
  24. )
  25. var (
  26. version = "unknown"
  27. )
  28. const minSupportedKernelVersion = "4.16"
  29. func uname() (string, string, error) {
  30. runtime.LockOSThread()
  31. defer runtime.UnlockOSThread()
  32. f, err := os.Open("/proc/1/ns/uts")
  33. if err != nil {
  34. return "", "", err
  35. }
  36. defer f.Close()
  37. self, err := os.Open("/proc/self/ns/uts")
  38. if err != nil {
  39. return "", "", err
  40. }
  41. defer self.Close()
  42. defer func() {
  43. unix.Setns(int(self.Fd()), unix.CLONE_NEWUTS)
  44. }()
  45. err = unix.Setns(int(f.Fd()), unix.CLONE_NEWUTS)
  46. if err != nil {
  47. return "", "", err
  48. }
  49. var utsname unix.Utsname
  50. if err := unix.Uname(&utsname); err != nil {
  51. return "", "", err
  52. }
  53. hostname := string(bytes.Split(utsname.Nodename[:], []byte{0})[0])
  54. kernelVersion := string(bytes.Split(utsname.Release[:], []byte{0})[0])
  55. return hostname, kernelVersion, nil
  56. }
  57. func machineID() string {
  58. for _, p := range []string{"/etc/machine-id", "/var/lib/dbus/machine-id", "/sys/devices/virtual/dmi/id/product_uuid"} {
  59. payload, err := os.ReadFile(proc.HostPath(p))
  60. if err != nil {
  61. klog.Warningln("failed to read machine-id:", err)
  62. continue
  63. }
  64. id := strings.TrimSpace(strings.Replace(string(payload), "-", "", -1))
  65. klog.Infoln("machine-id: ", id)
  66. return id
  67. }
  68. return ""
  69. }
  70. func systemUUID() string {
  71. payload, err := os.ReadFile(proc.HostPath("/sys/devices/virtual/dmi/id/product_uuid"))
  72. if err != nil {
  73. klog.Warningln("failed to read system-uuid:", err)
  74. return ""
  75. }
  76. return strings.TrimSpace(string(payload))
  77. }
  78. func whitelistNodeExternalNetworks() {
  79. netdevs, err := node.NetDevices()
  80. if err != nil {
  81. klog.Warningln("failed to get network interfaces:", err)
  82. return
  83. }
  84. for _, iface := range netdevs {
  85. for _, p := range iface.IPPrefixes {
  86. if p.IP().IsLoopback() || common.IsIpPrivate(p.IP()) {
  87. continue
  88. }
  89. // if the node has an external network IP, whitelist that network
  90. common.ConnectionFilter.WhitelistPrefix(p)
  91. }
  92. }
  93. }
  94. func main() {
  95. klog.LogToStderr(false)
  96. klog.SetOutput(&RateLimitedLogOutput{limiter: rate.NewLimiter(rate.Limit(*flags.LogPerSecond), *flags.LogBurst)})
  97. klog.Infoln("agent version:", version)
  98. hostname, kv, err := uname()
  99. if err != nil {
  100. klog.Exitln("failed to get uname:", err)
  101. }
  102. klog.Infoln("hostname:", hostname)
  103. klog.Infoln("kernel version:", kv)
  104. ver := common.KernelMajorMinor(kv)
  105. if ver == "" {
  106. klog.Exitln("invalid kernel version:", kv)
  107. }
  108. if semver.Compare("v"+ver, "v"+minSupportedKernelVersion) == -1 {
  109. klog.Exitf("the minimum Linux kernel version required is %s or later", minSupportedKernelVersion)
  110. }
  111. whitelistNodeExternalNetworks()
  112. machineId := machineID()
  113. systemUuid := systemUUID()
  114. tracing.Init(machineId, hostname, version)
  115. logs.Init(machineId, hostname, version)
  116. registry := prometheus.NewRegistry()
  117. registerer := prometheus.WrapRegistererWith(prometheus.Labels{"machine_id": machineId, "system_uuid": systemUuid}, registry)
  118. registerer.MustRegister(info("node_agent_info", version))
  119. if err := registerer.Register(node.NewCollector(hostname, kv)); err != nil {
  120. klog.Exitln(err)
  121. }
  122. processInfoCh := profiling.Init(machineId, hostname)
  123. cr, err := containers.NewRegistry(registerer, kv, processInfoCh)
  124. if err != nil {
  125. klog.Exitln(err)
  126. }
  127. defer cr.Close()
  128. profiling.Start()
  129. defer profiling.Stop()
  130. if err := prom.StartAgent(machineId); err != nil {
  131. klog.Exitln(err)
  132. }
  133. http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorLog: logger{}, Registry: registerer}))
  134. klog.Infoln("listening on:", *flags.ListenAddress)
  135. klog.Errorln(http.ListenAndServe(*flags.ListenAddress, nil))
  136. }
  137. func info(name, version string) prometheus.Collector {
  138. g := prometheus.NewGauge(prometheus.GaugeOpts{
  139. Name: name,
  140. ConstLabels: prometheus.Labels{"version": version},
  141. })
  142. g.Set(1)
  143. return g
  144. }
  145. type logger struct{}
  146. func (l logger) Println(v ...interface{}) {
  147. klog.Errorln(v...)
  148. }
  149. type RateLimitedLogOutput struct {
  150. limiter *rate.Limiter
  151. }
  152. func (o *RateLimitedLogOutput) Write(data []byte) (int, error) {
  153. if !o.limiter.Allow() {
  154. return len(data), nil
  155. }
  156. return os.Stderr.Write(data)
  157. }