forked from SUSE/saptune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csp.go
169 lines (155 loc) · 4.31 KB
/
csp.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package system
import (
"io/ioutil"
"os"
"regexp"
)
// constant definitions
const (
// Cloud Service Provider short and long names
// microsoft-azure
CSPAzure = "azure"
CSPAzureLong = "Microsoft Azure"
// amazon-web-services
CSPAWS = "aws"
CSPAWSLong = "Amazon Web Services"
// GoogleCloud
CSPGoogle = "google"
CSPGoogleLong = "Google Cloud Platform"
// OracleCloud
CSPOVM = "ovm"
CSPOVMLong = "Oracle Cloud"
// Alibaba Cloud
CSPAlibaba = "alibaba"
CSPAlibabaLong = "Alibaba Cloud"
)
var dmiDir = "/sys/class/dmi"
// dmidecode key files
// /usr/sbin/dmidecode -s chassis-asset-tag
var dmiChassisAssetTag = "/sys/class/dmi/id/chassis_asset_tag"
// /usr/sbin/dmidecode -s board-vendor
var dmiBoardVendor = "/sys/class/dmi/id/board_vendor"
// /usr/sbin/dmidecode -s bios-vendor
var dmiBiosVendor = "/sys/class/dmi/id/bios_vendor"
// /usr/sbin/dmidecode -s bios-version
var dmiBiosVersion = "/sys/class/dmi/id/bios_version"
// /usr/sbin/dmidecode -s system-version
var dmiSystemVersion = "/sys/class/dmi/id/system_version"
// /usr/sbin/dmidecode -s sys-vendor
var dmiSysVendor = "/sys/class/dmi/id/sys_vendor"
// /usr/sbin/dmidecode -s system-manufacturer
var dmiSystemManufacturer = "/sys/class/dmi/id/system-manufacturer"
// CSP identifier
var isAzureCat = regexp.MustCompile(`.*(7783-7084-3265-9085-8269-3286-77|MSFT AZURE VM).*`)
var isAzure = regexp.MustCompile(`.*[mM]icrosoft [cC]orporation.*`)
var isAWS = regexp.MustCompile(`.*[aA]mazon.*`)
var isGoogle = regexp.MustCompile(`.*[gG]oogle.*`)
var isOVM = regexp.MustCompile(`.*OVM.*`)
var isAlibaba = regexp.MustCompile(`.*[aA]libaba.*`)
// GetDMIDecode
//func GetDMIDecode(key string) string {
// system_version = "dmidecode -s system-version"
//}
// GetCSP returns the identifier of the cloud service provider of the current
// running system or an empty string, if the system does not belong to a CSP
// use files in /sys/class/dmi/id/ instead of dmidecode command
func GetCSP() string {
csp := ""
if _, err := os.Stat(dmiDir); os.IsNotExist(err) {
InfoLog("directory '%s' does not exist", dmiDir)
return csp
}
// check for Azure
if content, err := ioutil.ReadFile(dmiChassisAssetTag); err == nil {
matches := isAzureCat.FindStringSubmatch(string(content))
if len(matches) != 0 {
csp = CSPAzure
}
}
if csp == "" {
// SystemManufacturer
if content, err := ioutil.ReadFile(dmiSystemManufacturer); err == nil {
// check for Azure
matches := isAzure.FindStringSubmatch(string(content))
if len(matches) != 0 {
csp = CSPAzure
}
// check for Google
matches = isGoogle.FindStringSubmatch(string(content))
if len(matches) != 0 {
csp = CSPGoogle
}
// check for Alibaba
matches = isAlibaba.FindStringSubmatch(string(content))
if len(matches) != 0 {
csp = CSPAlibaba
}
}
}
if csp == "" {
// BoardVendor
if content, err := ioutil.ReadFile(dmiBoardVendor); err == nil {
// check for AWS
matches := isAWS.FindStringSubmatch(string(content))
if len(matches) != 0 {
csp = CSPAWS
}
}
}
if csp == "" {
// BiosVersion
if content, err := ioutil.ReadFile(dmiBiosVersion); err == nil {
// check for AWS
matches := isAWS.FindStringSubmatch(string(content))
if len(matches) != 0 {
csp = CSPAWS
}
// check for Google
matches = isGoogle.FindStringSubmatch(string(content))
if len(matches) != 0 {
csp = CSPGoogle
}
// check for Oracle Cloud
matches = isOVM.FindStringSubmatch(string(content))
if len(matches) != 0 {
csp = CSPOVM
}
}
}
if csp == "" {
// BiosVendor
if content, err := ioutil.ReadFile(dmiBiosVendor); err == nil {
// check for Google
matches := isGoogle.FindStringSubmatch(string(content))
if len(matches) != 0 {
csp = CSPGoogle
}
// check for AWS
matches = isAWS.FindStringSubmatch(string(content))
if len(matches) != 0 {
csp = CSPAWS
}
}
}
if csp == "" {
// SystemVersion
if content, err := ioutil.ReadFile(dmiSystemVersion); err == nil {
// check for AWS
matches := isAWS.FindStringSubmatch(string(content))
if len(matches) != 0 {
csp = CSPAWS
}
}
}
if csp == "" {
// SysVendor
if content, err := ioutil.ReadFile(dmiSysVendor); err == nil {
// check for AWS
matches := isAWS.FindStringSubmatch(string(content))
if len(matches) != 0 {
csp = CSPAWS
}
}
}
return csp
}