2023-03-17 10:37:27 +01:00
|
|
|
//go:build !android
|
|
|
|
|
2021-08-27 11:34:38 +02:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-05-25 23:25:02 +02:00
|
|
|
"context"
|
2021-08-27 11:34:38 +02:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-07-15 14:43:50 +02:00
|
|
|
"regexp"
|
2021-08-27 11:34:38 +02:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2023-03-15 07:54:51 +01:00
|
|
|
|
2023-11-10 16:33:13 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-02-20 11:53:11 +01:00
|
|
|
"github.com/zcalusic/sysinfo"
|
2023-11-10 16:33:13 +01:00
|
|
|
|
2023-03-15 07:54:51 +01:00
|
|
|
"github.com/netbirdio/netbird/version"
|
2021-08-27 11:34:38 +02:00
|
|
|
)
|
|
|
|
|
2024-12-03 10:22:04 +01:00
|
|
|
var (
|
|
|
|
// it is override in tests
|
|
|
|
getSystemInfo = defaultSysInfoImplementation
|
|
|
|
)
|
2024-08-01 16:46:55 +02:00
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
// GetInfo retrieves and parses the system information
|
|
|
|
func GetInfo(ctx context.Context) *Info {
|
2021-08-27 11:34:38 +02:00
|
|
|
info := _getInfo()
|
2021-08-29 13:22:52 +02:00
|
|
|
for strings.Contains(info, "broken pipe") {
|
2021-08-27 11:34:38 +02:00
|
|
|
info = _getInfo()
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:40:02 +01:00
|
|
|
osStr := strings.ReplaceAll(info, "\n", "")
|
|
|
|
osStr = strings.ReplaceAll(osStr, "\r\n", "")
|
2021-08-27 11:34:38 +02:00
|
|
|
osInfo := strings.Split(osStr, " ")
|
2024-06-13 13:24:24 +02:00
|
|
|
|
|
|
|
osName, osVersion := readOsReleaseFile()
|
2021-08-27 11:34:38 +02:00
|
|
|
if osName == "" {
|
|
|
|
osName = osInfo[3]
|
|
|
|
}
|
2024-02-20 11:53:11 +01:00
|
|
|
|
2023-04-20 16:00:22 +02:00
|
|
|
systemHostname, _ := os.Hostname()
|
2024-02-20 11:53:11 +01:00
|
|
|
|
|
|
|
addrs, err := networkAddresses()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("failed to discover network addresses: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-12-03 10:22:04 +01:00
|
|
|
start := time.Now()
|
|
|
|
si := updateStaticInfo()
|
|
|
|
if time.Since(start) > 1*time.Second {
|
|
|
|
log.Warnf("updateStaticInfo took %s", time.Since(start))
|
2024-02-20 11:53:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gio := &Info{
|
|
|
|
Kernel: osInfo[0],
|
|
|
|
Platform: osInfo[2],
|
|
|
|
OS: osName,
|
2024-06-13 13:24:24 +02:00
|
|
|
OSVersion: osVersion,
|
2024-02-20 11:53:11 +01:00
|
|
|
Hostname: extractDeviceName(ctx, systemHostname),
|
|
|
|
GoOS: runtime.GOOS,
|
|
|
|
CPUs: runtime.NumCPU(),
|
|
|
|
WiretrusteeVersion: version.NetbirdVersion(),
|
|
|
|
UIVersion: extractUserAgent(ctx),
|
|
|
|
KernelVersion: osInfo[1],
|
|
|
|
NetworkAddresses: addrs,
|
2024-12-03 10:22:04 +01:00
|
|
|
SystemSerialNumber: si.SystemSerialNumber,
|
|
|
|
SystemProductName: si.SystemProductName,
|
|
|
|
SystemManufacturer: si.SystemManufacturer,
|
|
|
|
Environment: si.Environment,
|
2024-02-20 11:53:11 +01:00
|
|
|
}
|
2022-02-03 18:35:54 +01:00
|
|
|
|
2021-08-27 11:34:38 +02:00
|
|
|
return gio
|
|
|
|
}
|
|
|
|
|
|
|
|
func _getInfo() string {
|
|
|
|
cmd := exec.Command("uname", "-srio")
|
|
|
|
cmd.Stdin = strings.NewReader("some")
|
|
|
|
var out bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2023-11-10 16:33:13 +01:00
|
|
|
log.Warnf("getInfo: %s", err)
|
2021-08-27 11:34:38 +02:00
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
2024-12-03 10:22:04 +01:00
|
|
|
func sysInfo() (string, string, string) {
|
2024-07-15 14:43:50 +02:00
|
|
|
isascii := regexp.MustCompile("^[[:ascii:]]+$")
|
2024-12-03 10:22:04 +01:00
|
|
|
si := getSystemInfo()
|
2024-08-01 16:46:55 +02:00
|
|
|
serials := []string{si.ChassisSerial, si.ProductSerial}
|
|
|
|
serial := ""
|
|
|
|
|
|
|
|
for _, s := range serials {
|
|
|
|
if isascii.MatchString(s) {
|
|
|
|
serial = s
|
|
|
|
if s != "Default string" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if serial == "" && isascii.MatchString(si.BoardSerial) {
|
|
|
|
serial = si.BoardSerial
|
2024-07-02 13:19:08 +02:00
|
|
|
}
|
2024-08-01 16:46:55 +02:00
|
|
|
|
|
|
|
var name string
|
|
|
|
for _, n := range []string{si.ProductName, si.BoardName} {
|
|
|
|
if isascii.MatchString(n) {
|
|
|
|
name = n
|
|
|
|
break
|
|
|
|
}
|
2024-07-15 14:43:50 +02:00
|
|
|
}
|
2024-08-01 16:46:55 +02:00
|
|
|
|
|
|
|
var manufacturer string
|
|
|
|
if isascii.MatchString(si.ProductVendor) {
|
|
|
|
manufacturer = si.ProductVendor
|
2024-07-15 14:43:50 +02:00
|
|
|
}
|
2024-08-01 16:46:55 +02:00
|
|
|
return serial, name, manufacturer
|
2024-02-20 11:53:11 +01:00
|
|
|
}
|
2024-12-03 10:22:04 +01:00
|
|
|
|
|
|
|
func defaultSysInfoImplementation() SysInfo {
|
|
|
|
si := sysinfo.SysInfo{}
|
|
|
|
si.GetSysInfo()
|
|
|
|
return SysInfo{
|
|
|
|
ChassisSerial: si.Chassis.Serial,
|
|
|
|
ProductSerial: si.Product.Serial,
|
|
|
|
BoardSerial: si.Board.Serial,
|
|
|
|
ProductName: si.Product.Name,
|
|
|
|
BoardName: si.Board.Name,
|
|
|
|
ProductVendor: si.Product.Vendor,
|
|
|
|
}
|
|
|
|
}
|