-
Notifications
You must be signed in to change notification settings - Fork 3
/
operating_system.go
221 lines (198 loc) · 5.61 KB
/
operating_system.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"fmt"
"net"
"os"
"runtime"
"strings"
"github.com/sysward/sysward-agent/logging"
)
func rebootRequired() bool {
if agent.linux == "debian" {
return fileReader.FileExists("/var/run/reboot-required") || fileReader.FileExists("/var/run/reboot-required.pkgs")
}
if agent.linux == "centos" {
out, _ := runner.Run("needs-restarting", "-r")
return strings.Contains(out, "Reboot is required")
}
if agent.linux == "suse" {
out, _ := runner.Run("zypper", "ps", "-s")
return strings.Contains(out, "You may wish to restart these processes.")
}
return false
}
func getSystemUID() string {
if fileReader.FileExists("/opt/sysward/bin/uid") {
readUID, err := fileReader.ReadFile("/opt/sysward/bin/uid")
if err != nil {
logging.LogMsg(fmt.Sprintf("Error reading UID file: %s", err))
} else {
return string(readUID)
}
}
interface_list, _ := net.Interfaces()
var uuid []string
for _, ifdev := range interface_list {
addToList := true
for _, s := range uuid {
if strings.Contains(ifdev.Name, "dummy") {
addToList = false
continue
}
if os.Getenv("DEBUG") == "true" {
logging.LogMsg("Name: " + ifdev.Name + " <-> HWADDR: " + ifdev.HardwareAddr.String())
}
if s == ifdev.HardwareAddr.String() {
addToList = false
}
}
if addToList {
uuid = append(uuid, ifdev.HardwareAddr.String())
}
}
uid := strings.Join(uuid, ".")
if os.Getenv("DEBUG") == "true" {
logging.LogMsg("UID: " + uid)
}
trimmedUID := strings.TrimSpace(string(uid))
fw := SyswardFileWriter{}
os.Create("/opt/sysward/bin/uid")
fw.AppendToFile("/opt/sysward/bin/uid", trimmedUID)
return trimmedUID
}
func checkPreReqs() {
if agent.linux == "debian" {
//if !fileReader.FileExists("/usr/lib/update-notifier/apt-check") {
// fmt.Println("update notifier not found, installing")
// _, err := runner.Run("apt-get", "update")
// out, err := runner.Run("apt-get", "install", "update-notifier", "-y")
// if err != nil {
// panic(err)
// }
// logging.LogMsg(out)
//}
} else if agent.linux == "centos" {
if !fileReader.FileExists("/usr/bin/dnf") {
fmt.Println("dnf not found, installing")
release, err := fileReader.ReadFile("/etc/os-release")
if err != nil {
logging.LogMsg("Error reading /etc/os-release: " + err.Error())
}
if !strings.Contains(string(release), "Amazon Linux") {
out, err := runner.Run("yum", "install", "-y", "dnf")
if err != nil {
panic(err)
}
logging.LogMsg(out)
} else {
logging.LogMsg("Skipping dnf install, using amazon linux")
}
}
if !fileReader.FileExists("/usr/bin/needs-restarting") {
fmt.Println("needs-restarting not found, installing")
out, err := runner.Run("yum", "install", "-y", "yum-utils")
if err != nil {
panic(err)
}
logging.LogMsg(out)
}
if !fileReader.FileExists("/usr/bin/wget") {
fmt.Println("wget not found, installing")
out, err := runner.Run("yum", "install", "-y", "wget")
if err != nil {
panic(err)
}
logging.LogMsg(out)
}
if !fileReader.FileExists("/etc/yum/pluginconf.d/versionlock.conf") {
fmt.Println("yum-plugin-versionlock.noarch not found, installing")
out, err := runner.Run("yum", "install", "-y", "yum-plugin-versionlock.noarch")
if err != nil {
if strings.ContainsAny(out, "Unable to find a match: yum-plugin-versionlock.noarch") {
var err2 error
out, err2 = runner.Run("yum", "install", "-y", "python3-dnf-plugin-versionlock")
if err2 != nil {
panic(err)
}
} else {
panic(err)
}
}
logging.LogMsg(out)
}
}
}
func verifyRoot() string {
// cant use user.Current() because we're cross compiling and no cgo
usr, err := runner.Run("whoami")
if err != nil {
panic(fmt.Sprintf("Return: %s | Error: %s", usr, err))
}
user := strings.TrimSpace(string(usr))
if user != "root" {
panic("SysWard agent must be run as root or as sudo.")
}
logging.LogMsg("root verified")
return user
}
func getOsInformation() OperatingSystem {
osRelease, err := fileReader.ReadFile("/etc/os-release")
if err != nil {
panic(err)
}
osInfo := map[string]string{}
for _, line := range strings.Split(string(osRelease), "\n") {
if len(line) == 0 {
continue
}
kv := strings.Split(line, "=")
osInfo[kv[0]] = strings.Trim(kv[1], "\"")
}
hostname, err := os.Hostname()
cpu_information := CPUInformation{getCPUName(), runtime.NumCPU()}
memory_information := MemoryInformation{getTotalMemory()}
return OperatingSystem{
Name: osInfo["NAME"],
UID: getSystemUID(),
Version: osInfo["VERSION_ID"],
Interfaces: getInterfaceInformation(),
Hostname: hostname,
CPUInformation: cpu_information,
MemoryInformation: memory_information,
}
}
func getTotalMemory() string {
out, _ := runner.Run("grep", "MemTotal", "/proc/meminfo")
t := strings.Split(out, ":")
if len(t) < 2 {
return "0"
}
x := strings.TrimSpace(t[1])
return x
}
func getCPUName() string {
out, _ := runner.Run("grep", "name", "/proc/cpuinfo")
t := strings.Split(out, ":")
if len(t) < 2 {
return "Unknown"
}
return strings.TrimSpace(t[1])
}
func getInterfaceInformation() []Interface {
interface_list, _ := net.Interfaces()
iflist := make([]Interface, len(interface_list))
for index, ifdev := range interface_list {
ips, _ := ifdev.Addrs()
// skip interfaces that are from docker
if strings.Contains(ifdev.Name, "veth") {
logging.LogMsg(fmt.Sprintf("Skipping: %+v", ifdev))
continue
}
ip_list := make([]string, len(ips))
for c, ip := range ips {
ip_list[c] = ip.String()
}
iflist[index] = Interface{ifdev.Name, ip_list, ifdev.HardwareAddr.String()}
}
return iflist
}