-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmi.go
63 lines (56 loc) · 1.77 KB
/
dmi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package cloudinfo
import (
"os"
"path/filepath"
"strings"
)
// DMI can be used to find various information about the machine
type DMI struct {
Cloud string `json:"cloud"`
Vendor string `json:"sys_vendor,omitempty"`
ProductName string `json:"product_name,omitempty"`
ProductUUID string `json:"product_uuid,omitempty"`
ProductVersion string `json:"product_version,omitempty"`
BoardAssetTag string `json:"board_asset_tag,omitempty"`
ChassisAssetTag string `json:"chassis_asset_tag,omitempty"`
}
func ReadDMI() (*DMI, error) {
dmi := &DMI{Cloud: "unknown"}
dmi.Vendor = readDMI("sys_vendor")
dmi.BoardAssetTag = readDMI("board_asset_tag") // a uuid on google, the instance id on Amazon EC2
dmi.ChassisAssetTag = readDMI("chassis_asset_tag") // "Amazon EC2" on aws, nothing on google
// product_name on aws: m5a.large on google: Google Compute Engine
dmi.ProductName = readDMI("product_name")
dmi.ProductUUID = readDMI("product_uuid")
dmi.ProductVersion = readDMI("product_version")
switch strings.ToLower(dmi.Vendor) {
case "amazon ec2":
dmi.Cloud = "aws"
case "google":
dmi.Cloud = "gcp"
case "scaleway":
dmi.Cloud = "scaleway"
case "xen":
// ProductVersion = 4.11.amazon
if strings.Contains(dmi.ProductVersion, "amazon") {
dmi.Cloud = "aws"
break
}
}
// if Cloud=unknown, run this on the machine & send it as an issue to add support to that cloud provider
// for foo in /sys/class/dmi/id/*; do echo $foo; cat $foo; done
return dmi, nil
}
func readDMI(name string) string {
res, err := os.ReadFile(filepath.Join("/sys/class/dmi/id", name))
if err != nil {
return ""
}
resStr := strings.TrimSpace(string(res))
switch resStr {
case "To be filled by O.E.M.", "Default string":
return ""
default:
return resStr
}
}