add hypervisor detection

This commit is contained in:
Pete Fraedrich
2020-04-24 13:44:18 -04:00
parent f229963a0e
commit 3e24f97cf5
2 changed files with 26 additions and 0 deletions
+2
View File
@@ -9,6 +9,7 @@ import (
var hc = &http.Client{Timeout: 120 * time.Millisecond}
// Clouds type
type Clouds struct {
Aws string
Azure string
@@ -19,6 +20,7 @@ type Clouds struct {
Vr string
}
// Detect function
func Detect() string {
if runtime.GOOS != "darwin" {
var c Clouds
+24
View File
@@ -1,6 +1,8 @@
package libdetectcloud
import (
"regexp"
"github.com/digitalocean/go-smbios/smbios"
)
@@ -13,6 +15,15 @@ type BIOS struct {
Hypervisor string
}
var hypervisors = map[string][]string{
"VMWare": []string{"vmware", "vm ware"},
"Xen": []string{"xen"},
"Hyper-V": []string{"microsoft", "hyperv", "hyper-v", "hyper v"},
"VirtualBox": []string{"virtual box", "virtual-box", "virtualbox"},
"KVM": []string{"virt-manager", "virt manager", "virtmanager", "hvm", "kvm"},
"Parallels": []string{"parallels"},
}
//BIOSInfo function gets information from the SMBIOS
func BIOSInfo() (*BIOS, error) {
rc, _, err := smbios.Stream()
@@ -39,5 +50,18 @@ func BIOSInfo() (*BIOS, error) {
ProductName: info[1],
SerialNumber: info[3],
}
for h, s := range hypervisors {
for _, i := range s {
re, err := regexp.Compile(`.*` + i + `.*`)
if err != nil {
return b, nil
}
if re.MatchString(b.Manufacturer) || re.MatchString(b.ProductName) {
b.IsVM = true
b.Hypervisor = h
return b, nil
}
}
}
return b, nil
}