mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-28 13:48:49 +08:00
Merge pull request #603 from pierresouchay/host_id_fixed_on_linux
Have a real fixed HostID on Linux
This commit is contained in:
commit
48177ef5f8
@ -73,7 +73,10 @@ func InfoWithContext(ctx context.Context) (*InfoStat, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sysProductUUID := common.HostSys("class/dmi/id/product_uuid")
|
sysProductUUID := common.HostSys("class/dmi/id/product_uuid")
|
||||||
|
machineID := common.HostEtc("machine-id")
|
||||||
switch {
|
switch {
|
||||||
|
// In order to read this file, needs to be supported by kernel/arch and run as root
|
||||||
|
// so having fallback is important
|
||||||
case common.PathExists(sysProductUUID):
|
case common.PathExists(sysProductUUID):
|
||||||
lines, err := common.ReadLines(sysProductUUID)
|
lines, err := common.ReadLines(sysProductUUID)
|
||||||
if err == nil && len(lines) > 0 && lines[0] != "" {
|
if err == nil && len(lines) > 0 && lines[0] != "" {
|
||||||
@ -81,6 +84,16 @@ func InfoWithContext(ctx context.Context) (*InfoStat, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
fallthrough
|
fallthrough
|
||||||
|
// Fallback on GNU Linux systems with systemd, readable by everyone
|
||||||
|
case common.PathExists(machineID):
|
||||||
|
lines, err := common.ReadLines(machineID)
|
||||||
|
if err == nil && len(lines) > 0 && len(lines[0]) == 32 {
|
||||||
|
st := lines[0]
|
||||||
|
ret.HostID = fmt.Sprintf("%s-%s-%s-%s-%s", st[0:8], st[8:12], st[12:16], st[16:20], st[20:32])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
// Not stable between reboot, but better than nothing
|
||||||
default:
|
default:
|
||||||
values, err := common.DoSysctrl("kernel.random.boot_id")
|
values, err := common.DoSysctrl("kernel.random.boot_id")
|
||||||
if err == nil && len(values) == 1 && values[0] != "" {
|
if err == nil && len(values) == 1 && values[0] != "" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user