info.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "github.com/docker/docker/pkg/parsers/operatingsystem"
  22. "github.com/google/cadvisor/fs"
  23. info "github.com/google/cadvisor/info/v1"
  24. "github.com/google/cadvisor/utils/cloudinfo"
  25. "github.com/google/cadvisor/utils/sysfs"
  26. "github.com/google/cadvisor/utils/sysinfo"
  27. "k8s.io/klog"
  28. "golang.org/x/sys/unix"
  29. )
  30. const hugepagesDirectory = "/sys/kernel/mm/hugepages/"
  31. 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.")
  32. 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.")
  33. func getInfoFromFiles(filePaths string) string {
  34. if len(filePaths) == 0 {
  35. return ""
  36. }
  37. for _, file := range strings.Split(filePaths, ",") {
  38. id, err := ioutil.ReadFile(file)
  39. if err == nil {
  40. return strings.TrimSpace(string(id))
  41. }
  42. }
  43. klog.Warningf("Couldn't collect info from any of the files in %q", filePaths)
  44. return ""
  45. }
  46. func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.MachineInfo, error) {
  47. rootFs := "/"
  48. if !inHostNamespace {
  49. rootFs = "/rootfs"
  50. }
  51. cpuinfo, err := ioutil.ReadFile(filepath.Join(rootFs, "/proc/cpuinfo"))
  52. if err != nil {
  53. return nil, err
  54. }
  55. clockSpeed, err := GetClockSpeed(cpuinfo)
  56. if err != nil {
  57. return nil, err
  58. }
  59. memoryCapacity, err := GetMachineMemoryCapacity()
  60. if err != nil {
  61. return nil, err
  62. }
  63. hugePagesInfo, err := GetHugePagesInfo(hugepagesDirectory)
  64. if err != nil {
  65. return nil, err
  66. }
  67. filesystems, err := fsInfo.GetGlobalFsInfo()
  68. if err != nil {
  69. klog.Errorf("Failed to get global filesystem information: %v", err)
  70. }
  71. diskMap, err := sysinfo.GetBlockDeviceInfo(sysFs)
  72. if err != nil {
  73. klog.Errorf("Failed to get disk map: %v", err)
  74. }
  75. netDevices, err := sysinfo.GetNetworkDevices(sysFs)
  76. if err != nil {
  77. klog.Errorf("Failed to get network devices: %v", err)
  78. }
  79. topology, numCores, err := GetTopology(sysFs, string(cpuinfo))
  80. if err != nil {
  81. klog.Errorf("Failed to get topology information: %v", err)
  82. }
  83. systemUUID, err := sysinfo.GetSystemUUID(sysFs)
  84. if err != nil {
  85. klog.Errorf("Failed to get system UUID: %v", err)
  86. }
  87. realCloudInfo := cloudinfo.NewRealCloudInfo()
  88. cloudProvider := realCloudInfo.GetCloudProvider()
  89. instanceType := realCloudInfo.GetInstanceType()
  90. instanceID := realCloudInfo.GetInstanceID()
  91. machineInfo := &info.MachineInfo{
  92. NumCores: numCores,
  93. CpuFrequency: clockSpeed,
  94. MemoryCapacity: memoryCapacity,
  95. HugePages: hugePagesInfo,
  96. DiskMap: diskMap,
  97. NetworkDevices: netDevices,
  98. Topology: topology,
  99. MachineID: getInfoFromFiles(filepath.Join(rootFs, *machineIdFilePath)),
  100. SystemUUID: systemUUID,
  101. BootID: getInfoFromFiles(filepath.Join(rootFs, *bootIdFilePath)),
  102. CloudProvider: cloudProvider,
  103. InstanceType: instanceType,
  104. InstanceID: instanceID,
  105. }
  106. for i := range filesystems {
  107. fs := filesystems[i]
  108. inodes := uint64(0)
  109. if fs.Inodes != nil {
  110. inodes = *fs.Inodes
  111. }
  112. 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})
  113. }
  114. return machineInfo, nil
  115. }
  116. func ContainerOsVersion() string {
  117. os, err := operatingsystem.GetOperatingSystem()
  118. if err != nil {
  119. os = "Unknown"
  120. }
  121. return os
  122. }
  123. func KernelVersion() string {
  124. uname := &unix.Utsname{}
  125. if err := unix.Uname(uname); err != nil {
  126. return "Unknown"
  127. }
  128. return string(uname.Release[:bytes.IndexByte(uname.Release[:], 0)])
  129. }