|
17 | 17 | package main |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "bufio" |
20 | 21 | "fmt" |
21 | 22 | "math/rand" |
22 | 23 | "os" |
| 24 | + "runtime" |
| 25 | + "strings" |
23 | 26 |
|
24 | 27 | resourceapi "k8s.io/api/resource/v1beta1" |
25 | 28 | "k8s.io/apimachinery/pkg/api/resource" |
@@ -84,3 +87,181 @@ func hash(s string) int64 { |
84 | 87 | } |
85 | 88 | return h |
86 | 89 | } |
| 90 | + |
| 91 | +// HostHardwareInfo represents hardware information available to admin access. |
| 92 | +type HostHardwareInfo struct { |
| 93 | + CPUInfo string |
| 94 | + MemInfo string |
| 95 | + KernelInfo string |
| 96 | + SystemInfo string |
| 97 | + NetworkInfo string |
| 98 | + StorageInfo string |
| 99 | +} |
| 100 | + |
| 101 | +// GetHostHardwareInfo gathers hardware information from the host system. |
| 102 | +func GetHostHardwareInfo() (*HostHardwareInfo, error) { |
| 103 | + info := &HostHardwareInfo{} |
| 104 | + |
| 105 | + // Get CPU information |
| 106 | + if cpuInfo, err := readFile("/proc/cpuinfo"); err == nil { |
| 107 | + info.CPUInfo = extractCPUModel(cpuInfo) |
| 108 | + } else { |
| 109 | + info.CPUInfo = fmt.Sprintf("CPU Info unavailable: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + // Get memory information |
| 113 | + if memInfo, err := readFile("/proc/meminfo"); err == nil { |
| 114 | + info.MemInfo = extractMemInfo(memInfo) |
| 115 | + } else { |
| 116 | + info.MemInfo = fmt.Sprintf("Memory Info unavailable: %v", err) |
| 117 | + } |
| 118 | + |
| 119 | + // Get kernel information |
| 120 | + if kernelInfo, err := readFile("/proc/version"); err == nil { |
| 121 | + info.KernelInfo = strings.TrimSpace(kernelInfo) |
| 122 | + } else { |
| 123 | + info.KernelInfo = fmt.Sprintf("Kernel Info unavailable: %v", err) |
| 124 | + } |
| 125 | + |
| 126 | + // Get system information (architecture, OS) |
| 127 | + info.SystemInfo = fmt.Sprintf("GOOS: %s, GOARCH: %s", runtime.GOOS, runtime.GOARCH) |
| 128 | + |
| 129 | + // Get network information |
| 130 | + if netInfo, err := getNetworkInfo(); err == nil { |
| 131 | + info.NetworkInfo = netInfo |
| 132 | + } else { |
| 133 | + info.NetworkInfo = fmt.Sprintf("Network Info unavailable: %v", err) |
| 134 | + } |
| 135 | + |
| 136 | + // Get storage information |
| 137 | + if storageInfo, err := getStorageInfo(); err == nil { |
| 138 | + info.StorageInfo = storageInfo |
| 139 | + } else { |
| 140 | + info.StorageInfo = fmt.Sprintf("Storage Info unavailable: %v", err) |
| 141 | + } |
| 142 | + |
| 143 | + return info, nil |
| 144 | +} |
| 145 | + |
| 146 | +func readFile(path string) (string, error) { |
| 147 | + data, err := os.ReadFile(path) |
| 148 | + if err != nil { |
| 149 | + return "", err |
| 150 | + } |
| 151 | + return string(data), nil |
| 152 | +} |
| 153 | + |
| 154 | +func extractCPUModel(cpuInfo string) string { |
| 155 | + scanner := bufio.NewScanner(strings.NewReader(cpuInfo)) |
| 156 | + cpuCount := 0 |
| 157 | + var modelName string |
| 158 | + |
| 159 | + for scanner.Scan() { |
| 160 | + line := scanner.Text() |
| 161 | + if strings.HasPrefix(line, "processor") { |
| 162 | + cpuCount++ |
| 163 | + } |
| 164 | + if strings.HasPrefix(line, "model name") { |
| 165 | + parts := strings.Split(line, ":") |
| 166 | + if len(parts) > 1 { |
| 167 | + modelName = strings.TrimSpace(parts[1]) |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + if modelName != "" { |
| 173 | + return fmt.Sprintf("%d x %s", cpuCount, modelName) |
| 174 | + } |
| 175 | + return fmt.Sprintf("%d CPU cores", cpuCount) |
| 176 | +} |
| 177 | + |
| 178 | +func extractMemInfo(memInfo string) string { |
| 179 | + scanner := bufio.NewScanner(strings.NewReader(memInfo)) |
| 180 | + var totalMem, availableMem string |
| 181 | + |
| 182 | + for scanner.Scan() { |
| 183 | + line := scanner.Text() |
| 184 | + if strings.HasPrefix(line, "MemTotal:") { |
| 185 | + parts := strings.Fields(line) |
| 186 | + if len(parts) >= 2 { |
| 187 | + totalMem = parts[1] + "kB" |
| 188 | + } |
| 189 | + } |
| 190 | + if strings.HasPrefix(line, "MemAvailable:") { |
| 191 | + parts := strings.Fields(line) |
| 192 | + if len(parts) >= 2 { |
| 193 | + availableMem = parts[1] + "kB" |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if totalMem != "" && availableMem != "" { |
| 199 | + return fmt.Sprintf("Total: %s, Available: %s", totalMem, availableMem) |
| 200 | + } |
| 201 | + return "Memory information parsing failed" |
| 202 | +} |
| 203 | + |
| 204 | +func getNetworkInfo() (string, error) { |
| 205 | + // Read network interfaces |
| 206 | + interfaces, err := readFile("/proc/net/dev") |
| 207 | + if err != nil { |
| 208 | + return "", err |
| 209 | + } |
| 210 | + |
| 211 | + scanner := bufio.NewScanner(strings.NewReader(interfaces)) |
| 212 | + var interfaceNames []string |
| 213 | + |
| 214 | + for scanner.Scan() { |
| 215 | + line := strings.TrimSpace(scanner.Text()) |
| 216 | + if strings.Contains(line, ":") && !strings.Contains(line, "Inter-|") && !strings.Contains(line, "face |") { |
| 217 | + parts := strings.Split(line, ":") |
| 218 | + if len(parts) > 0 { |
| 219 | + ifName := strings.TrimSpace(parts[0]) |
| 220 | + if ifName != "lo" { // Skip loopback |
| 221 | + interfaceNames = append(interfaceNames, ifName) |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + return fmt.Sprintf("Network Interfaces: %s", strings.Join(interfaceNames, ", ")), nil |
| 228 | +} |
| 229 | + |
| 230 | +func getStorageInfo() (string, error) { |
| 231 | + // Read mounted filesystems |
| 232 | + mounts, err := readFile("/proc/mounts") |
| 233 | + if err != nil { |
| 234 | + return "", err |
| 235 | + } |
| 236 | + |
| 237 | + scanner := bufio.NewScanner(strings.NewReader(mounts)) |
| 238 | + var rootFS string |
| 239 | + var mountCount int |
| 240 | + |
| 241 | + for scanner.Scan() { |
| 242 | + line := scanner.Text() |
| 243 | + fields := strings.Fields(line) |
| 244 | + if len(fields) >= 3 { |
| 245 | + mountPoint := fields[1] |
| 246 | + fsType := fields[2] |
| 247 | + |
| 248 | + if mountPoint == "/" { |
| 249 | + rootFS = fmt.Sprintf("Root FS: %s (%s)", fields[0], fsType) |
| 250 | + } |
| 251 | + |
| 252 | + // Count real filesystems (not virtual ones) |
| 253 | + if !strings.HasPrefix(fields[0], "/proc") && |
| 254 | + !strings.HasPrefix(fields[0], "/sys") && |
| 255 | + !strings.HasPrefix(fields[0], "/dev/pts") && |
| 256 | + fsType != "tmpfs" && fsType != "devtmpfs" && |
| 257 | + fsType != "cgroup" && fsType != "cgroup2" { |
| 258 | + mountCount++ |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + if rootFS != "" { |
| 264 | + return fmt.Sprintf("%s, Total mounts: %d", rootFS, mountCount), nil |
| 265 | + } |
| 266 | + return fmt.Sprintf("Storage mounts: %d", mountCount), nil |
| 267 | +} |
0 commit comments