| 1 |
/* |
| 2 |
* Copyright (C) 2018 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
const _ = cockpit.gettext; |
| 8 |
|
| 9 |
export const cpu_ram_info = () => |
| 10 |
cockpit.spawn(["cat", "/proc/meminfo", "/proc/cpuinfo"]) |
| 11 |
.then(text => { |
| 12 |
const info = { }; |
| 13 |
const memtotal_match = text.match(/MemTotal:[^0-9]*([0-9]+) [kK]B/); |
| 14 |
const total_kb = memtotal_match && parseInt(memtotal_match[1], 10); |
| 15 |
if (total_kb) |
| 16 |
info.memory = total_kb * 1024; |
| 17 |
|
| 18 |
const available_match = text.match(/MemAvailable:[^0-9]*([0-9]+) [kK]B/); |
| 19 |
const available_kb = available_match && parseInt(available_match[1], 10); |
| 20 |
if (available_kb) |
| 21 |
info.available_memory = available_kb * 1024; |
| 22 |
|
| 23 |
const swap_match = text.match(/SwapTotal:[^0-9]*([0-9]+) [kK]B/); |
| 24 |
const swap_total_kb = swap_match && parseInt(swap_match[1], 10); |
| 25 |
if (swap_total_kb) |
| 26 |
info.swap = swap_total_kb * 1024; |
| 27 |
|
| 28 |
let model_match = text.match(/^model name\s*:\s*(.*)$/m); |
| 29 |
if (!model_match) |
| 30 |
model_match = text.match(/^cpu\s*:\s*(.*)$/m); // PowerPC |
| 31 |
if (!model_match) |
| 32 |
model_match = text.match(/^vendor_id\s*:\s*(.*)$/m); // s390x |
| 33 |
if (!model_match) |
| 34 |
model_match = text.match(/^Model Name\s*:\s*(.*)$/m); // LoongArch |
| 35 |
if (model_match) |
| 36 |
info.cpu_model = model_match[1]; |
| 37 |
|
| 38 |
info.cpus = 0; |
| 39 |
const re = /^(processor|cpu number)\s*:/gm; |
| 40 |
while (re.test(text)) |
| 41 |
info.cpus += 1; |
| 42 |
return info; |
| 43 |
}); |
| 44 |
|
| 45 |
// https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf |
| 46 |
const chassis_types = [ |
| 47 |
undefined, |
| 48 |
_("Other"), |
| 49 |
_("Unknown"), |
| 50 |
_("Desktop"), |
| 51 |
_("Low profile desktop"), |
| 52 |
_("Pizza box"), |
| 53 |
_("Mini tower"), |
| 54 |
_("Tower"), |
| 55 |
_("Portable"), |
| 56 |
_("Laptop"), |
| 57 |
_("Notebook"), |
| 58 |
_("Handheld"), |
| 59 |
_("Docking station"), |
| 60 |
_("All-in-one"), |
| 61 |
_("Sub-Notebook"), |
| 62 |
_("Space-saving computer"), |
| 63 |
_("Lunch box"), /* 0x10 */ |
| 64 |
_("Main server chassis"), |
| 65 |
_("Expansion chassis"), |
| 66 |
_("Sub-Chassis"), |
| 67 |
_("Bus expansion chassis"), |
| 68 |
_("Peripheral chassis"), |
| 69 |
_("RAID chassis"), |
| 70 |
_("Rack mount chassis"), |
| 71 |
_("Sealed-case PC"), |
| 72 |
_("Multi-system chassis"), |
| 73 |
_("Compact PCI"), /* 0x1A */ |
| 74 |
_("Advanced TCA"), |
| 75 |
_("Blade"), |
| 76 |
_("Blade enclosure"), |
| 77 |
_("Tablet"), |
| 78 |
_("Convertible"), |
| 79 |
_("Detachable"), /* 0x20 */ |
| 80 |
_("IoT gateway"), |
| 81 |
_("Embedded PC"), |
| 82 |
_("Mini PC"), |
| 83 |
_("Stick PC"), |
| 84 |
]; |
| 85 |
|
| 86 |
function parseDMIFields(text) { |
| 87 |
const info = {}; |
| 88 |
text.split("\n").forEach(line => { |
| 89 |
const sep = line.indexOf(':'); |
| 90 |
if (sep <= 0) |
| 91 |
return; |
| 92 |
const file = line.slice(0, sep); |
| 93 |
const key = file.slice(file.lastIndexOf('/') + 1); |
| 94 |
let value = line.slice(sep + 1); |
| 95 |
|
| 96 |
// clean up after lazy OEMs |
| 97 |
if (value.match(/to be filled by o\.?e\.?m\.?/i)) |
| 98 |
value = ""; |
| 99 |
|
| 100 |
info[key] = value; |
| 101 |
|
| 102 |
if (key === "chassis_type") |
| 103 |
info[key + "_str"] = chassis_types[parseInt(value)] || chassis_types[2]; // fall back to "Unknown" |
| 104 |
}); |
| 105 |
return info; |
| 106 |
} |
| 107 |
|
| 108 |
export function dmi_info() { |
| 109 |
// the grep often/usually exits with 2, that's okay as long as we find *some* information |
| 110 |
return cockpit.script("grep -r . /sys/class/dmi/id || true", null, |
| 111 |
{ err: "message", superuser: "try" }) |
| 112 |
.then((output) => parseDMIFields(output)); |
| 113 |
} |
| 114 |
|
| 115 |
// decode a binary Uint8Array with a trailing null byte |
| 116 |
function decode_proc_str(s) { |
| 117 |
return new TextDecoder().decode(s.slice(0, -1)); |
| 118 |
} |
| 119 |
|
| 120 |
export function devicetree_info() { |
| 121 |
let model; |
| 122 |
let serial; |
| 123 |
|
| 124 |
return Promise.all([ |
| 125 |
// these succeed with content === null if files are absent |
| 126 |
cockpit.file("/proc/device-tree/model", { binary: true }).read() |
| 127 |
.then(content => { model = content ? decode_proc_str(content) : null }), |
| 128 |
cockpit.file("/proc/device-tree/serial-number", { binary: true }).read() |
| 129 |
.then(content => { serial = content ? decode_proc_str(content) : null }), |
| 130 |
]) |
| 131 |
.then(() => ({ model, serial })); |
| 132 |
} |
| 133 |
|
| 134 |
/* we expect udev db paragraphs like this: |
| 135 |
* |
| 136 |
P: /devices/virtual/mem/null |
| 137 |
N: null |
| 138 |
E: DEVMODE=0666 |
| 139 |
E: DEVNAME=/dev/null |
| 140 |
E: SUBSYSTEM=mem |
| 141 |
*/ |
| 142 |
|
| 143 |
const udevPathRE = /^P: (.*)$/; |
| 144 |
const udevPropertyRE = /^E: (\w+)=(.*)$/; |
| 145 |
|
| 146 |
function parseUdevDB(text) { |
| 147 |
const info = {}; |
| 148 |
text.split("\n\n").forEach(paragraph => { |
| 149 |
let syspath = null; |
| 150 |
const props = {}; |
| 151 |
|
| 152 |
paragraph = paragraph.trim(); |
| 153 |
if (!paragraph) |
| 154 |
return; |
| 155 |
|
| 156 |
paragraph.split("\n").forEach(line => { |
| 157 |
let match = line.match(udevPathRE); |
| 158 |
if (match) { |
| 159 |
syspath = match[1]; |
| 160 |
} else { |
| 161 |
match = line.match(udevPropertyRE); |
| 162 |
if (match) |
| 163 |
props[match[1]] = match[2]; |
| 164 |
} |
| 165 |
}); |
| 166 |
|
| 167 |
if (syspath) |
| 168 |
info[syspath] = props; |
| 169 |
else |
| 170 |
console.log("udev database paragraph is missing P:", paragraph); |
| 171 |
}); |
| 172 |
return info; |
| 173 |
} |
| 174 |
|
| 175 |
export function udev_info() { |
| 176 |
return cockpit.spawn(["udevadm", "info", "--export-db"], { err: "message" }) |
| 177 |
.then(output => parseUdevDB(output)); |
| 178 |
} |
| 179 |
|
| 180 |
const memoryRE = /^([ \w]+): (.*)/; |
| 181 |
|
| 182 |
// Process the dmidecode output and create a mapping of locator to DIMM properties |
| 183 |
function parseMemoryInfo(text) { |
| 184 |
const info = {}; |
| 185 |
text.split("\n\n").forEach(paragraph => { |
| 186 |
let locator = null; |
| 187 |
let bankLocator = null; |
| 188 |
const props = {}; |
| 189 |
paragraph = paragraph.trim(); |
| 190 |
if (!paragraph) |
| 191 |
return; |
| 192 |
|
| 193 |
paragraph.split("\n").forEach(line => { |
| 194 |
line = line.trim(); |
| 195 |
const match = line.match(memoryRE); |
| 196 |
if (match) |
| 197 |
props[match[1]] = match[2]; |
| 198 |
}); |
| 199 |
|
| 200 |
locator = props.Locator; |
| 201 |
bankLocator = props['Bank Locator']; |
| 202 |
if (locator) |
| 203 |
info[bankLocator + locator] = props; |
| 204 |
}); |
| 205 |
return processMemory(info); |
| 206 |
} |
| 207 |
|
| 208 |
// Select the useful properties to display |
| 209 |
function processMemory(info) { |
| 210 |
const memoryArray = []; |
| 211 |
|
| 212 |
for (const dimm in info) { |
| 213 |
const memoryProperty = info[dimm]; |
| 214 |
|
| 215 |
let memorySize = memoryProperty.Size || _("Unknown"); |
| 216 |
if (memorySize.includes("MB")) { |
| 217 |
const memorySizeValue = parseInt(memorySize, 10); |
| 218 |
memorySize = cockpit.format(_("$0 GiB"), memorySizeValue / 1024); |
| 219 |
} |
| 220 |
|
| 221 |
let memoryTechnology = memoryProperty["Memory technology"]; |
| 222 |
if (!memoryTechnology || memoryTechnology == "<OUT OF SPEC>") |
| 223 |
memoryTechnology = _("Unknown"); |
| 224 |
|
| 225 |
let memoryRank = memoryProperty.Rank || _("Unknown"); |
| 226 |
if (memoryRank == 1) |
| 227 |
memoryRank = _("Single rank"); |
| 228 |
if (memoryRank == 2) |
| 229 |
memoryRank = _("Dual rank"); |
| 230 |
|
| 231 |
memoryArray.push({ |
| 232 |
locator: (memoryProperty['Bank Locator'] + ': ' + memoryProperty.Locator) || _("Unknown"), |
| 233 |
technology: memoryTechnology, |
| 234 |
type: memoryProperty.Type || _("Unknown"), |
| 235 |
size: memorySize, |
| 236 |
state: memoryProperty["Total Width"] == "Unknown" ? _("Absent") : _("Present"), |
| 237 |
rank: memoryRank, |
| 238 |
speed: memoryProperty.Speed || _("Unknown") |
| 239 |
}); |
| 240 |
} |
| 241 |
|
| 242 |
return memoryArray; |
| 243 |
} |
| 244 |
|
| 245 |
export function memory_info() { |
| 246 |
return cockpit.spawn(["dmidecode", "-t", "memory"], { environ: ["LC_ALL=C"], err: "message", superuser: "try" }) |
| 247 |
.then(output => parseMemoryInfo(output)); |
| 248 |
} |