-
Notifications
You must be signed in to change notification settings - Fork 23
/
distro.go
120 lines (109 loc) · 3.95 KB
/
distro.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
// distro.go has functions for identifying Linux distribution type and version.
// Currently handles some CentOS, Ubuntu, and Debian versions.
package collector
import (
"regexp"
"strings"
except "github.com/banyanops/collector/except"
)
// DistroMap is a reference that maps each pretty name to the corresponding distribution name.
var DistroMap = map[string]string{
"Ubuntu 16.10": "UBUNTU-yakkety",
"Ubuntu 16.04": "UBUNTU-xenial",
"Ubuntu 15.10": "UBUNTU-wily",
"Ubuntu 15.04": "UBUNTU-vivid",
"Ubuntu 14.10": "UBUNTU-utopic",
"Ubuntu Utopic Unicorn (development branch)": "UBUNTU-utopic",
"Ubuntu 13.10": "UBUNTU-saucy",
"Ubuntu 13.04": "UBUNTU-raring",
"Ubuntu 12.10": "UBUNTU-quantal",
"Ubuntu 11.10": "UBUNTU-oneiric",
"Ubuntu 11.04": "UBUNTU-natty",
"Ubuntu 10.10": "UBUNTU-maverick",
"CentOS Linux 7 (Core)": "REDHAT-7Server",
"Debian GNU/Linux 7 (wheezy)": "DEBIAN-wheezy",
"Debian 6.0.10": "DEBIAN-squeeze",
"Debian GNU/Linux 8 (jessie)": "DEBIAN-jessie",
"Debian GNU/Linux jessie/sid": "DEBIAN-jessie",
"Debian GNU/Linux stretch/sid": "DEBIAN-stretch-sid",
}
// distroRegexp is a map of compiled regular expressions indexed by names.
var distroRegexp = make(map[regexpPattern]*regexp.Regexp)
type regexpPattern int
const (
rel6z regexpPattern = iota
rel5z
)
func init() {
type elem struct {
name regexpPattern
pattern string
}
patternList := []elem{
elem{name: rel6z, pattern: `release 6\.([\d]+)`},
elem{name: rel5z, pattern: `release 5\.([\d]+)`},
}
for _, p := range patternList {
r, err := regexp.Compile(p.pattern)
if err != nil {
except.Fail(err, p.name, p.pattern)
}
distroRegexp[p.name] = r
}
}
// getDistroID takes a distribution "pretty name" as input and returns the corresponding
// distribution ID, or "Unknown" if no match can be found.
func getDistroID(distroName string) string {
if id, ok := DistroMap[distroName]; ok {
return id
}
//Exceptions to the rule: There are many such cases, so bucketing them together
if strings.HasPrefix(distroName, `Ubuntu 16.10`) {
return "UBUNTU-yakkety"
}
if strings.HasPrefix(distroName, `Ubuntu 16.04`) {
return "UBUNTU-xenial"
}
if strings.HasPrefix(distroName, `Ubuntu 14.04`) {
return "UBUNTU-trusty"
}
if strings.HasPrefix(distroName, `Ubuntu precise`) {
return "UBUNTU-precise"
}
if strings.HasPrefix(distroName, `Ubuntu 12.04`) {
return "UBUNTU-precise"
}
if strings.HasPrefix(distroName, `Ubuntu 10.04`) {
return "UBUNTU-lucid"
}
if strings.HasPrefix(distroName, `CentOS release 5`) ||
strings.HasPrefix(distroName, `Red Hat Enterprise Linux Server release 5`) ||
strings.HasPrefix(distroName, `Red Hat Enterprise Linux Server 5`) {
m := distroRegexp[rel5z].FindStringSubmatch(distroName)
if len(m) > 1 {
return "REDHAT-5Server-5." + m[1]
}
return "REDHAT-5Server"
}
if strings.HasPrefix(distroName, `CentOS release 6`) ||
strings.HasPrefix(distroName, `Red Hat Enterprise Linux Server release 6`) ||
strings.HasPrefix(distroName, `Red Hat Enterprise Linux Server 6`) {
m := distroRegexp[rel6z].FindStringSubmatch(distroName)
if len(m) > 1 {
return "REDHAT-6Server-6." + m[1]
}
return "REDHAT-6Server"
}
if strings.HasPrefix(distroName, `Red Hat Enterprise Linux Server release 7`) ||
strings.HasPrefix(distroName, `Red Hat Enterprise Linux Server 7`) {
return "REDHAT-7Server"
}
if strings.HasPrefix(distroName, `Ubuntu Vivid`) {
return "UBUNTU-vivid"
}
if strings.HasPrefix(distroName, `Ubuntu Wily`) {
return "UBUNTU-wily"
}
except.Warn("DISTRO %s UNKNOWN", distroName)
return "Unknown"
}