info.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package machine
  15. import (
  16. "bytes"
  17. "flag"
  18. "io/ioutil"
  19. "path/filepath"
  20. "strings"
  21. "time"
  22. "golang.org/x/sys/unix"
  23. "github.com/google/cadvisor/fs"
  24. info "github.com/google/cadvisor/info/v1"
  25. "github.com/google/cadvisor/nvm"
  26. "github.com/google/cadvisor/utils/cloudinfo"
  27. "github.com/google/cadvisor/utils/sysfs"
  28. "github.com/google/cadvisor/utils/sysinfo"
  29. "k8s.io/klog/v2"
  30. )
  31. const hugepagesDirectory = "/sys/kernel/mm/hugepages/"
  32. const memoryControllerPath = "/sys/devices/system/edac/mc/"
  33. var machineIDFilePath = flag.String("machine_id_file", "/etc/machine-id,/var/lib/dbus/machine-id", "Comma-separated list of files to check for machine-id. Use the first one that exists.")
  34. var bootIDFilePath = flag.String("boot_id_file", "/proc/sys/kernel/random/boot_id", "Comma-separated list of files to check for boot-id. Use the first one that exists.")
  35. func getInfoFromFiles(filePaths string) string {
  36. if len(filePaths) == 0 {
  37. return ""
  38. }
  39. for _, file := range strings.Split(filePaths, ",") {
  40. id, err := ioutil.ReadFile(file)
  41. if err == nil {
  42. return strings.TrimSpace(string(id))
  43. }
  44. }
  45. klog.Warningf("Couldn't collect info from any of the files in %q", filePaths)
  46. return ""
  47. }
  48. func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.MachineInfo, error) {
  49. rootFs := "/"
  50. if !inHostNamespace {
  51. rootFs = "/rootfs"
  52. }
  53. cpuinfo, err := ioutil.ReadFile(filepath.Join(rootFs, "/proc/cpuinfo"))
  54. if err != nil {
  55. return nil, err
  56. }
  57. clockSpeed, err := GetClockSpeed(cpuinfo)
  58. if err != nil {
  59. return nil, err
  60. }
  61. memoryCapacity, err := GetMachineMemoryCapacity()
  62. if err != nil {
  63. return nil, err
  64. }
  65. memoryByType, err := GetMachineMemoryByType(memoryControllerPath)
  66. if err != nil {
  67. return nil, err
  68. }
  69. nvmInfo, err := nvm.GetInfo()
  70. if err != nil {
  71. return nil, err
  72. }
  73. hugePagesInfo, err := sysinfo.GetHugePagesInfo(sysFs, hugepagesDirectory)
  74. if err != nil {
  75. return nil, err
  76. }
  77. filesystems, err := fsInfo.GetGlobalFsInfo()
  78. if err != nil {
  79. klog.Errorf("Failed to get global filesystem information: %v", err)
  80. }
  81. diskMap, err := sysinfo.GetBlockDeviceInfo(sysFs)
  82. if err != nil {
  83. klog.Errorf("Failed to get disk map: %v", err)
  84. }
  85. netDevices, err := sysinfo.GetNetworkDevices(sysFs)
  86. if err != nil {
  87. klog.Errorf("Failed to get network devices: %v", err)
  88. }
  89. topology, numCores, err := GetTopology(sysFs)
  90. if err != nil {
  91. klog.Errorf("Failed to get topology information: %v", err)
  92. }
  93. systemUUID, err := sysinfo.GetSystemUUID(sysFs)
  94. if err != nil {
  95. klog.Errorf("Failed to get system UUID: %v", err)
  96. }
  97. realCloudInfo := cloudinfo.NewRealCloudInfo()
  98. cloudProvider := realCloudInfo.GetCloudProvider()
  99. instanceType := realCloudInfo.GetInstanceType()
  100. instanceID := realCloudInfo.GetInstanceID()
  101. machineInfo := &info.MachineInfo{
  102. Timestamp: time.Now(),
  103. CPUVendorID: GetCPUVendorID(cpuinfo),
  104. NumCores: numCores,
  105. NumPhysicalCores: GetPhysicalCores(cpuinfo),
  106. NumSockets: GetSockets(cpuinfo),
  107. CpuFrequency: clockSpeed,
  108. MemoryCapacity: memoryCapacity,
  109. MemoryByType: memoryByType,
  110. NVMInfo: nvmInfo,
  111. HugePages: hugePagesInfo,
  112. DiskMap: diskMap,
  113. NetworkDevices: netDevices,
  114. Topology: topology,
  115. MachineID: getInfoFromFiles(filepath.Join(rootFs, *machineIDFilePath)),
  116. SystemUUID: systemUUID,
  117. BootID: getInfoFromFiles(filepath.Join(rootFs, *bootIDFilePath)),
  118. CloudProvider: cloudProvider,
  119. InstanceType: instanceType,
  120. InstanceID: instanceID,
  121. }
  122. for i := range filesystems {
  123. fs := filesystems[i]
  124. inodes := uint64(0)
  125. if fs.Inodes != nil {
  126. inodes = *fs.Inodes
  127. }
  128. machineInfo.Filesystems = append(machineInfo.Filesystems, info.FsInfo{Device: fs.Device, DeviceMajor: uint64(fs.Major), DeviceMinor: uint64(fs.Minor), Type: fs.Type.String(), Capacity: fs.Capacity, Inodes: inodes, HasInodes: fs.Inodes != nil})
  129. }
  130. return machineInfo, nil
  131. }
  132. func ContainerOsVersion() string {
  133. os, err := getOperatingSystem()
  134. if err != nil {
  135. os = "Unknown"
  136. }
  137. return os
  138. }
  139. func KernelVersion() string {
  140. uname := &unix.Utsname{}
  141. if err := unix.Uname(uname); err != nil {
  142. return "Unknown"
  143. }
  144. return string(uname.Release[:bytes.IndexByte(uname.Release[:], 0)])
  145. }