add detecting virtualization system on Linux.

pull/4/head
Shirou WAKAYAMA 11 years ago
parent 11569147c0
commit 1464099024

@ -14,6 +14,9 @@ type HostInfoStat struct {
Platform string `json:"platform"` // ex: ubuntu, linuxmint
PlatformFamily string `json:"platformFamily"` // ex: debian, rhel
PlatformVersion string `json:"platformVersion"`
VirtualizationSystem string `json:"virtualizationSystem"`
VirtualizationRole string `json:"virtualizationRole"` // guest or host
}
type UserStat struct {

@ -26,14 +26,16 @@ func HostInfo() (*HostInfoStat, error) {
}
ret.Hostname = hostname
out, err := exec.Command("uname", "-s").Output()
platform, family, version, err := GetPlatformInformation()
if err == nil {
ret.Platform = strings.ToLower(strings.TrimSpace(string(out)))
ret.Platform = platform
ret.PlatformFamily = family
ret.PlatformVersion = version
}
out, err = exec.Command("uname", "-r").Output()
system, role, err := GetVirtualization()
if err == nil {
ret.PlatformVersion = strings.ToLower(strings.TrimSpace(string(out)))
ret.VirtualizationSystem = system
ret.VirtualizationRole = role
}
values, err := doSysctrl("kern.boottime")
@ -101,3 +103,28 @@ func Users() ([]UserStat, error) {
return ret, nil
}
func GetPlatformInformation() (string, string, string, error) {
platform := ""
family := ""
version := ""
out, err := exec.Command("uname", "-s").Output()
if err == nil {
platform = strings.ToLower(strings.TrimSpace(string(out)))
}
out, err = exec.Command("uname", "-r").Output()
if err == nil {
version = strings.ToLower(strings.TrimSpace(string(out)))
}
return platform, family, version, nil
}
func GetVirtualization() (string, string, error) {
system := ""
role := ""
return system, role, nil
}

@ -32,12 +32,17 @@ func HostInfo() (*HostInfoStat, error) {
OS: runtime.GOOS,
}
platform, family, version, err := getPlatformInformation()
platform, family, version, err := GetPlatformInformation()
if err == nil {
ret.Platform = platform
ret.PlatformFamily = family
ret.PlatformVersion = version
}
system, role, err := GetVirtualization()
if err == nil {
ret.VirtualizationSystem = system
ret.VirtualizationRole = role
}
uptime, err := BootTime()
if err == nil {
ret.Uptime = uptime
@ -145,7 +150,7 @@ func getLSB() (*LSB, error) {
return ret, nil
}
func getPlatformInformation() (string, string, string, error) {
func GetPlatformInformation() (string, string, string, error) {
platform := ""
family := ""
version := ""
@ -252,3 +257,88 @@ func getRedhatishVersion(contents []string) (string, error) {
func getRedhatishPlatform(contents []string) (string, error) {
return "", nil
}
func GetVirtualization() (string, string, error) {
var system string
var role string
if pathExists("/proc/xen") {
system = "xen"
role = "guest" // assume guest
if pathExists("/proc/xen/capabilities") {
contents, err := readLines("/proc/xen/capabilities")
if err == nil {
if stringContains(contents, "control_d") {
role = "host"
}
}
}
}
if pathExists("/proc/modules") {
contents, err := readLines("/proc/modules")
if err == nil {
if stringContains(contents, "kvm") {
system = "kvm"
role = "host"
} else if stringContains(contents, "vboxdrv") {
system = "vbox"
role = "host"
} else if stringContains(contents, "vboxguest") {
system = "vbox"
role = "guest"
}
}
}
if pathExists("/proc/cpuinfo") {
contents, err := readLines("/proc/cpuinfo")
if err == nil {
if stringContains(contents, "QEMU Virtual CPU") ||
stringContains(contents, "Common KVM processor") ||
stringContains(contents, "Common 32-bit KVM processor") {
system = "kvm"
role = "guest"
}
}
}
if pathExists("/proc/bc/0") {
system = "openvz"
role = "host"
} else if pathExists("/proc/vz") {
system = "openvz"
role = "guest"
}
// not use dmidecode because it requires root
if pathExists("/proc/self/status") {
contents, err := readLines("/proc/self/status")
if err == nil {
if stringContains(contents, "s_context:") ||
stringContains(contents, "VxID:") {
system = "linux-vserver"
}
// TODO: guest or host
}
}
if pathExists("/proc/self/cgroup") {
contents, err := readLines("/proc/self/cgroup")
if err == nil {
if stringContains(contents, "lxc") ||
stringContains(contents, "docker") {
system = "lxc"
role = "guest"
} else if pathExists("/usr/bin/lxc-version") { // TODO: which
system = "lxc"
role = "host"
}
}
}
return system, role, nil
}

@ -49,7 +49,7 @@ func TestHostInfoStat_String(t *testing.T) {
OS: "linux",
Platform: "ubuntu",
}
e := `{"hostname":"test","uptime":3000,"procs":100,"os":"linux","platform":"ubuntu","platformFamily":"","platformVersion":""}`
e := `{"hostname":"test","uptime":3000,"procs":100,"os":"linux","platform":"ubuntu","platformFamily":"","platformVersion":"","virtualizationSystem":"","virtualizationRole":""}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("HostInfoStat string is invalid: %v", v)
}

Loading…
Cancel
Save