info.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. "fmt"
  19. "io/ioutil"
  20. "path/filepath"
  21. "strconv"
  22. "strings"
  23. "github.com/docker/docker/pkg/parsers/operatingsystem"
  24. "github.com/google/cadvisor/fs"
  25. info "github.com/google/cadvisor/info/v1"
  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"
  30. "golang.org/x/sys/unix"
  31. )
  32. const hugepagesDirectory = "/sys/kernel/mm/hugepages/"
  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. // GetHugePagesInfo returns information about pre-allocated huge pages
  49. func GetHugePagesInfo() ([]info.HugePagesInfo, error) {
  50. var hugePagesInfo []info.HugePagesInfo
  51. files, err := ioutil.ReadDir(hugepagesDirectory)
  52. if err != nil {
  53. // treat as non-fatal since kernels and machine can be
  54. // configured to disable hugepage support
  55. return hugePagesInfo, nil
  56. }
  57. for _, st := range files {
  58. nameArray := strings.Split(st.Name(), "-")
  59. pageSizeArray := strings.Split(nameArray[1], "kB")
  60. pageSize, err := strconv.ParseUint(string(pageSizeArray[0]), 10, 64)
  61. if err != nil {
  62. return hugePagesInfo, err
  63. }
  64. numFile := hugepagesDirectory + st.Name() + "/nr_hugepages"
  65. val, err := ioutil.ReadFile(numFile)
  66. if err != nil {
  67. return hugePagesInfo, err
  68. }
  69. var numPages uint64
  70. // we use sscanf as the file as a new-line that trips up ParseUint
  71. // it returns the number of tokens successfully parsed, so if
  72. // n != 1, it means we were unable to parse a number from the file
  73. n, err := fmt.Sscanf(string(val), "%d", &numPages)
  74. if err != nil || n != 1 {
  75. return hugePagesInfo, fmt.Errorf("could not parse file %v contents %q", numFile, string(val))
  76. }
  77. hugePagesInfo = append(hugePagesInfo, info.HugePagesInfo{
  78. NumPages: numPages,
  79. PageSize: pageSize,
  80. })
  81. }
  82. return hugePagesInfo, nil
  83. }
  84. func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.MachineInfo, error) {
  85. rootFs := "/"
  86. if !inHostNamespace {
  87. rootFs = "/rootfs"
  88. }
  89. cpuinfo, err := ioutil.ReadFile(filepath.Join(rootFs, "/proc/cpuinfo"))
  90. clockSpeed, err := GetClockSpeed(cpuinfo)
  91. if err != nil {
  92. return nil, err
  93. }
  94. memoryCapacity, err := GetMachineMemoryCapacity()
  95. if err != nil {
  96. return nil, err
  97. }
  98. hugePagesInfo, err := GetHugePagesInfo()
  99. if err != nil {
  100. return nil, err
  101. }
  102. filesystems, err := fsInfo.GetGlobalFsInfo()
  103. if err != nil {
  104. klog.Errorf("Failed to get global filesystem information: %v", err)
  105. }
  106. diskMap, err := sysinfo.GetBlockDeviceInfo(sysFs)
  107. if err != nil {
  108. klog.Errorf("Failed to get disk map: %v", err)
  109. }
  110. netDevices, err := sysinfo.GetNetworkDevices(sysFs)
  111. if err != nil {
  112. klog.Errorf("Failed to get network devices: %v", err)
  113. }
  114. topology, numCores, err := GetTopology(sysFs, string(cpuinfo))
  115. if err != nil {
  116. klog.Errorf("Failed to get topology information: %v", err)
  117. }
  118. systemUUID, err := sysinfo.GetSystemUUID(sysFs)
  119. if err != nil {
  120. klog.Errorf("Failed to get system UUID: %v", err)
  121. }
  122. realCloudInfo := cloudinfo.NewRealCloudInfo()
  123. cloudProvider := realCloudInfo.GetCloudProvider()
  124. instanceType := realCloudInfo.GetInstanceType()
  125. instanceID := realCloudInfo.GetInstanceID()
  126. machineInfo := &info.MachineInfo{
  127. NumCores: numCores,
  128. CpuFrequency: clockSpeed,
  129. MemoryCapacity: memoryCapacity,
  130. HugePages: hugePagesInfo,
  131. DiskMap: diskMap,
  132. NetworkDevices: netDevices,
  133. Topology: topology,
  134. MachineID: getInfoFromFiles(filepath.Join(rootFs, *machineIdFilePath)),
  135. SystemUUID: systemUUID,
  136. BootID: getInfoFromFiles(filepath.Join(rootFs, *bootIdFilePath)),
  137. CloudProvider: cloudProvider,
  138. InstanceType: instanceType,
  139. InstanceID: instanceID,
  140. }
  141. for i := range filesystems {
  142. fs := filesystems[i]
  143. inodes := uint64(0)
  144. if fs.Inodes != nil {
  145. inodes = *fs.Inodes
  146. }
  147. 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})
  148. }
  149. return machineInfo, nil
  150. }
  151. func ContainerOsVersion() string {
  152. os, err := operatingsystem.GetOperatingSystem()
  153. if err != nil {
  154. os = "Unknown"
  155. }
  156. return os
  157. }
  158. func KernelVersion() string {
  159. uname := &unix.Utsname{}
  160. if err := unix.Uname(uname); err != nil {
  161. return "Unknown"
  162. }
  163. return string(uname.Release[:bytes.IndexByte(uname.Release[:], 0)])
  164. }