diff --git a/agent_test.go b/agent_test.go index 2cca3cb..d8c0607 100644 --- a/agent_test.go +++ b/agent_test.go @@ -1,158 +1,157 @@ package main import ( - "errors" - "io/ioutil" - "net/http" - "net/http/httptest" - "testing" + "errors" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" - . "github.com/smartystreets/goconvey/convey" + . "github.com/smartystreets/goconvey/convey" ) func TestNewAgent(t *testing.T) { - Convey("Setting up a new agent on Ubuntu", t, func() { - f := new(MockReader) - f.On("FileExists", "/etc/apt").Return(true) - r := new(MockRunner) - r.On("Run", "whoami", []string{}).Return("root", nil) - agent := NewAgent() - runner = r - config_json, _ := ioutil.ReadFile("config.json") - f.On("ReadFile", "config.json").Return(config_json, nil) - f.On("ReadFile", "/opt/sysward/bin/uid").Return([]byte("uid123"), nil) - f.On("FileExists", "/usr/lib/update-notifier/apt-check").Return(true) - f.On("FileExists", "/etc/apt").Return(true) - f.On("FileExists", "/usr/bin/python").Return(true) - f.On("FileExists", "/usr/lib/python2.7/dist-packages/apt/__init__.py").Return(true).Maybe() - f.On("FileExists", "/usr/lib/python3/dist-packages/apt/__init__.py").Return(true).Maybe() - f.On("ReadFile", "config.json").Return(config_json, nil) - r.On("Run", "python", []string{"trex.py"}).Return("", nil) - fileReader = f - agent.Startup() - So(agent.runner, ShouldHaveSameTypeAs, SyswardRunner{}) - So(agent.fileReader, ShouldHaveSameTypeAs, SyswardFileReader{}) - So(agent.packageManager, ShouldHaveSameTypeAs, DebianPackageManager{}) - }) + Convey("Setting up a new agent on Ubuntu", t, func() { + f := new(MockReader) + f.On("FileExists", "/etc/apt").Return(true) + r := new(MockRunner) + r.On("Run", "whoami", []string{}).Return("root", nil) + agent := NewAgent() + runner = r + config_json, _ := ioutil.ReadFile("config.json") + f.On("ReadFile", "config.json").Return(config_json, nil) + f.On("ReadFile", "/opt/sysward/bin/uid").Return([]byte("uid123"), nil) + f.On("FileExists", "/usr/lib/update-notifier/apt-check").Return(true) + f.On("FileExists", "/etc/apt").Return(true) + f.On("FileExists", "/usr/bin/python").Return(true) + f.On("FileExists", "/usr/lib/python2.7/dist-packages/apt/__init__.py").Return(true).Maybe() + f.On("FileExists", "/usr/lib/python3/dist-packages/apt/__init__.py").Return(true).Maybe() + f.On("ReadFile", "config.json").Return(config_json, nil) + r.On("Run", "python", []string{"trex.py"}).Return("", nil) + fileReader = f + agent.Startup() + So(agent.runner, ShouldHaveSameTypeAs, SyswardRunner{}) + So(agent.fileReader, ShouldHaveSameTypeAs, SyswardFileReader{}) + So(agent.packageManager, ShouldHaveSameTypeAs, DebianPackageManager{}) + }) } func TestAgentCronInstall(t *testing.T) { - Convey("Cron job must be installed", t, func() { - agent := NewAgent() - f := new(MockReader) - Convey("Cron is already installed, upstart config doesnt exist", func() { - f.On("FileExists", "/etc/crontab").Return(true) - f.On("ReadFile", "/etc/crontab").Return([]byte("bin && ./sysward"), nil) - f.On("FileExists", "/etc/init/sysward-agent.conf").Return(false) - fileReader = f - agent.InstallCron() - f.Mock.AssertExpectations(t) - }) - - // TODO: Interface out the cron installation - Convey("Cron is not installed, upstart config exists", func() { - r := new(MockRunner) - w := new(MockWriter) - f.On("FileExists", "/etc/crontab").Return(true) - w.On("AppendToFile", "/etc/crontab", "*/5 * * * * root cd /opt/sysward/bin && ./sysward >> /dev/null\n").Return() - r.On("Run", "/sbin/stop", []string{"sysward-agent"}).Return("", nil) - r.On("Run", "rm", []string{"-rf", "/etc/init/sysward-agent.conf"}).Return("", nil) - f.On("ReadFile", "/etc/crontab").Return([]byte(""), nil) - f.On("FileExists", "/etc/init/sysward-agent.conf").Return(true) - fileReader = f - fileWriter = w - runner = r - agent.InstallCron() - f.Mock.AssertExpectations(t) - r.Mock.AssertExpectations(t) - w.Mock.AssertExpectations(t) - }) - }) + Convey("Cron job must be installed", t, func() { + agent := NewAgent() + f := new(MockReader) + Convey("Cron is already installed, upstart config doesnt exist", func() { + f.On("FileExists", "/etc/crontab").Return(true) + f.On("ReadFile", "/etc/crontab").Return([]byte("bin && ./sysward"), nil) + f.On("FileExists", "/etc/init/sysward-agent.conf").Return(false) + fileReader = f + agent.InstallCron() + f.Mock.AssertExpectations(t) + }) + + // TODO: Interface out the cron installation + Convey("Cron is not installed, upstart config exists", func() { + r := new(MockRunner) + w := new(MockWriter) + f.On("FileExists", "/etc/crontab").Return(true) + w.On("AppendToFile", "/etc/crontab", "*/5 * * * * root cd /opt/sysward/bin && ./sysward >> /dev/null\n").Return() + r.On("Run", "/sbin/stop", []string{"sysward-agent"}).Return("", nil) + r.On("Run", "rm", []string{"-rf", "/etc/init/sysward-agent.conf"}).Return("", nil) + f.On("ReadFile", "/etc/crontab").Return([]byte(""), nil) + f.On("FileExists", "/etc/init/sysward-agent.conf").Return(true) + fileReader = f + fileWriter = w + runner = r + agent.InstallCron() + f.Mock.AssertExpectations(t) + r.Mock.AssertExpectations(t) + w.Mock.AssertExpectations(t) + }) + }) } func TestAgentStartup(t *testing.T) { - Convey("Agent startup should verify root and check pre-req packages", t, func() { - handler := func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) - } - server := httptest.NewServer(http.HandlerFunc(handler)) - defer server.Close() - r := new(MockRunner) - f := new(MockReader) - r.On("Run", "whoami", []string{}).Return("root", nil) - config_json, _ := ioutil.ReadFile("config.json") - f.On("FileExists", "/usr/lib/update-notifier/apt-check").Return(true) - f.On("FileExists", "/etc/apt").Return(true) - f.On("ReadFile", "config.json").Return(config_json, nil) - agent := NewAgent() - runner = r - fileReader = f - agent.Startup() - api = SyswardApi{httpClient: http.Client{}} - f.Mock.AssertExpectations(t) - r.Mock.AssertExpectations(t) - }) + Convey("Agent startup should verify root and check pre-req packages", t, func() { + handler := func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + } + server := httptest.NewServer(http.HandlerFunc(handler)) + defer server.Close() + r := new(MockRunner) + f := new(MockReader) + r.On("Run", "whoami", []string{}).Return("root", nil) + config_json, _ := ioutil.ReadFile("config.json") + f.On("FileExists", "/etc/apt").Return(true) + f.On("ReadFile", "config.json").Return(config_json, nil) + agent := NewAgent() + runner = r + fileReader = f + agent.Startup() + api = SyswardApi{httpClient: http.Client{}} + f.Mock.AssertExpectations(t) + r.Mock.AssertExpectations(t) + }) } func TestIfAgentIsRunning(t *testing.T) { - packageManager = DebianPackageManager{} - Convey("Checking if the agent is running", t, func() { - - Convey("Agent is running", func() { - r := new(MockRunner) - r.On("Run", "ps", []string{"ax"}).Return("./sysward\n./sysward", nil) - runner = r - So(CheckIfAgentIsRunning, ShouldPanic) - r.Mock.AssertExpectations(t) - }) - - Convey("Agent isn't running", func() { - r := new(MockRunner) - // We're testing the case where its the only process running - r.On("Run", "ps", []string{"ax"}).Return("./sysward\n", nil) - runner = r - So(CheckIfAgentIsRunning, ShouldNotPanic) - r.Mock.AssertExpectations(t) - }) - - }) - - runner = SyswardRunner{} + packageManager = DebianPackageManager{} + Convey("Checking if the agent is running", t, func() { + + Convey("Agent is running", func() { + r := new(MockRunner) + r.On("Run", "ps", []string{"ax"}).Return("./sysward\n./sysward", nil) + runner = r + So(CheckIfAgentIsRunning, ShouldPanic) + r.Mock.AssertExpectations(t) + }) + + Convey("Agent isn't running", func() { + r := new(MockRunner) + // We're testing the case where its the only process running + r.On("Run", "ps", []string{"ax"}).Return("./sysward\n", nil) + runner = r + So(CheckIfAgentIsRunning, ShouldNotPanic) + r.Mock.AssertExpectations(t) + }) + + }) + + runner = SyswardRunner{} } func TestAgentRun(t *testing.T) { - r := new(MockRunner) - r.On("Run", "whoami", []string{}).Return("root", nil) - r.On("Run", "lsb_release", []string{"-d"}).Return("Description: Ubuntu 14.04 LTS", nil) - r.On("Run", "grep", []string{"MemTotal", "/proc/meminfo"}).Return("MemTotal: 1017764 kB", nil) - r.On("Run", "grep", []string{"name", "/proc/cpuinfo"}).Return("model name : Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz", nil) - runner = r - - f := new(MockReader) - f.On("ReadFile", "/sys/class/dmi/id/product_uuid").Return([]byte("UUID"), nil) - - a := new(MockSyswardApi) - a.On("GetJobs").Return("") - - pm := new(MockPackageManager) - pm.On("UpdatePackageLists").Return(nil) - pm.On("UpdateCounts").Return(Updates{Regular: 0, Security: 0}) - pm.On("BuildPackageList").Return([]OsPackage{}) - pm.On("GetSourcesList").Return([]Source{}) - pm.On("BuildInstalledPackageList").Return([]string{}) - - packageManager = pm - - config_json, _ := ioutil.ReadFile("config.json") - f.On("FileExists", "/usr/lib/update-notifier/apt-check").Return(true) - f.On("FileExists", "/var/run/reboot-required").Return(true) - f.On("ReadFile", "config.json").Return(config_json, nil) - f.On("ReadFile", "/opt/sysward/bin/uid").Return([]byte("uid123"), nil) - f.On("FileExists", "/opt/sysward/bin/uid").Return(true) - f.On("FileExists", "/usr/bin/python").Return(true) - f.On("FileExists", "/usr/lib/python2.7/dist-packages/apt/__init__.py").Return(true).Maybe() - f.On("FileExists", "/usr/lib/python3/dist-packages/apt/__init__.py").Return(true).Maybe() - f.On("ReadFile", "/etc/os-release").Return([]byte(` + r := new(MockRunner) + r.On("Run", "whoami", []string{}).Return("root", nil) + r.On("Run", "lsb_release", []string{"-d"}).Return("Description: Ubuntu 14.04 LTS", nil) + r.On("Run", "grep", []string{"MemTotal", "/proc/meminfo"}).Return("MemTotal: 1017764 kB", nil) + r.On("Run", "grep", []string{"name", "/proc/cpuinfo"}).Return("model name : Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz", nil) + runner = r + + f := new(MockReader) + f.On("ReadFile", "/sys/class/dmi/id/product_uuid").Return([]byte("UUID"), nil) + + a := new(MockSyswardApi) + a.On("GetJobs").Return("") + + pm := new(MockPackageManager) + pm.On("UpdatePackageLists").Return(nil) + pm.On("UpdateCounts").Return(Updates{Regular: 0, Security: 0}) + pm.On("BuildPackageList").Return([]OsPackage{}) + pm.On("GetSourcesList").Return([]Source{}) + pm.On("BuildInstalledPackageList").Return([]string{}) + + packageManager = pm + + config_json, _ := ioutil.ReadFile("config.json") + f.On("FileExists", "/usr/lib/update-notifier/apt-check").Return(true) + f.On("FileExists", "/var/run/reboot-required").Return(true) + f.On("ReadFile", "config.json").Return(config_json, nil) + f.On("ReadFile", "/opt/sysward/bin/uid").Return([]byte("uid123"), nil) + f.On("FileExists", "/opt/sysward/bin/uid").Return(true) + f.On("FileExists", "/usr/bin/python").Return(true) + f.On("FileExists", "/usr/lib/python2.7/dist-packages/apt/__init__.py").Return(true).Maybe() + f.On("FileExists", "/usr/lib/python3/dist-packages/apt/__init__.py").Return(true).Maybe() + f.On("ReadFile", "/etc/os-release").Return([]byte(` NAME="Ubuntu" VERSION="20.04.6 LTS (Focal Fossa)" ID=ubuntu @@ -166,26 +165,26 @@ PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-poli VERSION_CODENAME=focal UBUNTU_CODENAME=focal `), nil) - fileReader = f - - agentData := AgentData{ - Packages: packageManager.BuildPackageList(), - SystemUpdates: packageManager.UpdateCounts(), - OperatingSystem: getOsInformation(), - Sources: packageManager.GetSourcesList(), - InstalledPackages: packageManager.BuildInstalledPackageList(), - RebootRequired: true, - } - - a.On("CheckIn", agentData).Return(errors.New("foo")) - api = a - - Convey("Agent run should checkin, and gather system information", t, func() { - agent := Agent{} - agent.Run() - //r.Mock.AssertExpectations(t) - //f.Mock.AssertExpectations(t) - a.Mock.AssertExpectations(t) - //pm.Mock.AssertExpectations(t) - }) + fileReader = f + + agentData := AgentData{ + Packages: packageManager.BuildPackageList(), + SystemUpdates: packageManager.UpdateCounts(), + OperatingSystem: getOsInformation(), + Sources: packageManager.GetSourcesList(), + InstalledPackages: packageManager.BuildInstalledPackageList(), + RebootRequired: true, + } + + a.On("CheckIn", agentData).Return(errors.New("foo")) + api = a + + Convey("Agent run should checkin, and gather system information", t, func() { + agent := Agent{} + agent.Run() + //r.Mock.AssertExpectations(t) + //f.Mock.AssertExpectations(t) + a.Mock.AssertExpectations(t) + //pm.Mock.AssertExpectations(t) + }) } diff --git a/operating_system.go b/operating_system.go index 72faccd..63f92e6 100644 --- a/operating_system.go +++ b/operating_system.go @@ -1,215 +1,215 @@ package main import ( - "fmt" - "net" - "os" - "runtime" - "strings" + "fmt" + "net" + "os" + "runtime" + "strings" - "github.com/sysward/sysward-agent/logging" + "github.com/sysward/sysward-agent/logging" ) func rebootRequired() bool { - if agent.linux == "debian" { - return fileReader.FileExists("/var/run/reboot-required") - } - 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 + if agent.linux == "debian" { + return fileReader.FileExists("/var/run/reboot-required") + } + 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 + 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) - } - - } + 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 + // 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, - } + 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, ":") - x := strings.TrimSpace(t[1]) - return x + out, _ := runner.Run("grep", "MemTotal", "/proc/meminfo") + t := strings.Split(out, ":") + x := strings.TrimSpace(t[1]) + return x } func getCPUName() string { - out, _ := runner.Run("grep", "name", "/proc/cpuinfo") - t := strings.Split(out, ":") - return strings.TrimSpace(t[1]) + out, _ := runner.Run("grep", "name", "/proc/cpuinfo") + t := strings.Split(out, ":") + 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 + 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 }