github.com/google/cadvisor@v0.49.1/machine/info.go (about) 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 15 package machine 16 17 import ( 18 "bytes" 19 "flag" 20 "os" 21 "path/filepath" 22 "strings" 23 "time" 24 25 "golang.org/x/sys/unix" 26 27 "github.com/google/cadvisor/fs" 28 info "github.com/google/cadvisor/info/v1" 29 "github.com/google/cadvisor/nvm" 30 "github.com/google/cadvisor/utils/cloudinfo" 31 "github.com/google/cadvisor/utils/sysfs" 32 "github.com/google/cadvisor/utils/sysinfo" 33 34 "k8s.io/klog/v2" 35 ) 36 37 const hugepagesDirectory = "/sys/kernel/mm/hugepages/" 38 const memoryControllerPath = "/sys/devices/system/edac/mc/" 39 40 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.") 41 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.") 42 43 func getInfoFromFiles(filePaths string) string { 44 if len(filePaths) == 0 { 45 return "" 46 } 47 for _, file := range strings.Split(filePaths, ",") { 48 id, err := os.ReadFile(file) 49 if err == nil { 50 return strings.TrimSpace(string(id)) 51 } 52 } 53 klog.Warningf("Couldn't collect info from any of the files in %q", filePaths) 54 return "" 55 } 56 57 func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.MachineInfo, error) { 58 rootFs := "/" 59 if !inHostNamespace { 60 rootFs = "/rootfs" 61 } 62 63 cpuinfo, err := os.ReadFile(filepath.Join(rootFs, "/proc/cpuinfo")) 64 if err != nil { 65 return nil, err 66 } 67 clockSpeed, err := GetClockSpeed(cpuinfo) 68 if err != nil { 69 return nil, err 70 } 71 72 memoryCapacity, err := GetMachineMemoryCapacity() 73 if err != nil { 74 return nil, err 75 } 76 77 memoryByType, err := GetMachineMemoryByType(memoryControllerPath) 78 if err != nil { 79 return nil, err 80 } 81 82 swapCapacity, err := GetMachineSwapCapacity() 83 if err != nil { 84 return nil, err 85 } 86 87 nvmInfo, err := nvm.GetInfo() 88 if err != nil { 89 return nil, err 90 } 91 92 hugePagesInfo, err := sysinfo.GetHugePagesInfo(sysFs, hugepagesDirectory) 93 if err != nil { 94 return nil, err 95 } 96 97 filesystems, err := fsInfo.GetGlobalFsInfo() 98 if err != nil { 99 klog.Errorf("Failed to get global filesystem information: %v", err) 100 } 101 102 diskMap, err := sysinfo.GetBlockDeviceInfo(sysFs) 103 if err != nil { 104 klog.Errorf("Failed to get disk map: %v", err) 105 } 106 107 netDevices, err := sysinfo.GetNetworkDevices(sysFs) 108 if err != nil { 109 klog.Errorf("Failed to get network devices: %v", err) 110 } 111 112 topology, numCores, err := GetTopology(sysFs) 113 if err != nil { 114 klog.Errorf("Failed to get topology information: %v", err) 115 } 116 117 systemUUID, err := sysinfo.GetSystemUUID(sysFs) 118 if err != nil { 119 klog.Errorf("Failed to get system UUID: %v", err) 120 } 121 122 realCloudInfo := cloudinfo.NewRealCloudInfo() 123 cloudProvider := realCloudInfo.GetCloudProvider() 124 instanceType := realCloudInfo.GetInstanceType() 125 instanceID := realCloudInfo.GetInstanceID() 126 127 machineInfo := &info.MachineInfo{ 128 Timestamp: time.Now(), 129 CPUVendorID: GetCPUVendorID(cpuinfo), 130 NumCores: numCores, 131 NumPhysicalCores: GetPhysicalCores(cpuinfo), 132 NumSockets: GetSockets(cpuinfo), 133 CpuFrequency: clockSpeed, 134 MemoryCapacity: memoryCapacity, 135 MemoryByType: memoryByType, 136 SwapCapacity: swapCapacity, 137 NVMInfo: nvmInfo, 138 HugePages: hugePagesInfo, 139 DiskMap: diskMap, 140 NetworkDevices: netDevices, 141 Topology: topology, 142 MachineID: getInfoFromFiles(filepath.Join(rootFs, *machineIDFilePath)), 143 SystemUUID: systemUUID, 144 BootID: getInfoFromFiles(filepath.Join(rootFs, *bootIDFilePath)), 145 CloudProvider: cloudProvider, 146 InstanceType: instanceType, 147 InstanceID: instanceID, 148 } 149 150 for i := range filesystems { 151 fs := filesystems[i] 152 inodes := uint64(0) 153 if fs.Inodes != nil { 154 inodes = *fs.Inodes 155 } 156 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}) 157 } 158 159 return machineInfo, nil 160 } 161 162 func ContainerOsVersion() string { 163 os, err := getOperatingSystem() 164 if err != nil { 165 os = "Unknown" 166 } 167 return os 168 } 169 170 func KernelVersion() string { 171 uname := &unix.Utsname{} 172 173 if err := unix.Uname(uname); err != nil { 174 return "Unknown" 175 } 176 177 return string(uname.Release[:bytes.IndexByte(uname.Release[:], 0)]) 178 }