From 1b9669f35102fc73a179dc6d226c58b9e866d30e Mon Sep 17 00:00:00 2001 From: redanthrax Date: Fri, 17 Jun 2022 09:38:18 -0700 Subject: [PATCH] moar refactor --- agent/agent_linux.go | 124 +------------------------------ agent/software/software.go | 3 + agent/system/system_linux.go | 116 +++++++++++++++++++++++++++++ agent/tactical/tactical_linux.go | 4 + agent/utils.go | 15 ---- 5 files changed, 126 insertions(+), 136 deletions(-) create mode 100644 agent/software/software.go diff --git a/agent/agent_linux.go b/agent/agent_linux.go index d8831d6..4ce4eaf 100644 --- a/agent/agent_linux.go +++ b/agent/agent_linux.go @@ -12,140 +12,22 @@ https://license.tacticalrmm.com package agent import ( - "bufio" - "fmt" - "os" - "runtime" - "strconv" - "strings" - "time" - rmm "github.com/amidaware/rmmagent/shared" - ps "github.com/elastic/go-sysinfo" - "github.com/go-resty/resty/v2" - "github.com/jaypipes/ghw" "github.com/kardianos/service" - "github.com/shirou/gopsutil/v3/cpu" trmm "github.com/wh1te909/trmm-shared" ) -func (a *Agent) GetWMIInfo() map[string]interface{} { - wmiInfo := make(map[string]interface{}) - ips := make([]string, 0) - disks := make([]string, 0) - cpus := make([]string, 0) - gpus := make([]string, 0) - // local ips - host, err := ps.Host() - if err != nil { - a.Logger.Errorln("GetWMIInfo() ps.Host()", err) - } else { - for _, ip := range host.Info().IPs { - if strings.Contains(ip, "127.0.") || strings.Contains(ip, "::1/128") { - continue - } - ips = append(ips, ip) - } - } - wmiInfo["local_ips"] = ips - - // disks - block, err := ghw.Block(ghw.WithDisableWarnings()) - if err != nil { - a.Logger.Errorln("ghw.Block()", err) - } else { - for _, disk := range block.Disks { - if disk.IsRemovable || strings.Contains(disk.Name, "ram") { - continue - } - ret := fmt.Sprintf("%s %s %s %s %s %s", disk.Vendor, disk.Model, disk.StorageController, disk.DriveType, disk.Name, ByteCountSI(disk.SizeBytes)) - ret = strings.TrimSpace(strings.ReplaceAll(ret, "unknown", "")) - disks = append(disks, ret) - } - } - wmiInfo["disks"] = disks - - // cpus - cpuInfo, err := cpu.Info() - if err != nil { - a.Logger.Errorln("cpu.Info()", err) - } else { - if len(cpuInfo) > 0 { - if cpuInfo[0].ModelName != "" { - cpus = append(cpus, cpuInfo[0].ModelName) - } - } - } - wmiInfo["cpus"] = cpus - - // make/model - wmiInfo["make_model"] = "" - chassis, err := ghw.Chassis(ghw.WithDisableWarnings()) - if err != nil { - a.Logger.Errorln("ghw.Chassis()", err) - } else { - if chassis.Vendor != "" || chassis.Version != "" { - wmiInfo["make_model"] = fmt.Sprintf("%s %s", chassis.Vendor, chassis.Version) - } - } - - // gfx cards - - gpu, err := ghw.GPU(ghw.WithDisableWarnings()) - if err != nil { - a.Logger.Errorln("ghw.GPU()", err) - } else { - for _, i := range gpu.GraphicsCards { - if i.DeviceInfo != nil { - ret := fmt.Sprintf("%s %s", i.DeviceInfo.Vendor.Name, i.DeviceInfo.Product.Name) - gpus = append(gpus, ret) - } - - } - } - wmiInfo["gpus"] = gpus - - // temp hack for ARM cpu/make/model if rasp pi - var makeModel string - if strings.Contains(runtime.GOARCH, "arm") { - file, _ := os.Open("/proc/cpuinfo") - scanner := bufio.NewScanner(file) - for scanner.Scan() { - if strings.Contains(strings.ToLower(scanner.Text()), "raspberry") { - model := strings.Split(scanner.Text(), ":") - if len(model) == 2 { - makeModel = strings.TrimSpace(model[1]) - break - } - } - } - } - - if len(cpus) == 0 { - wmiInfo["cpus"] = []string{makeModel} - } - if makeModel != "" && (wmiInfo["make_model"] == "" || wmiInfo["make_model"] == "unknown unknown") { - wmiInfo["make_model"] = makeModel - } - if len(gpus) == 1 && gpus[0] == "unknown unknown" { - wmiInfo["gpus"] = "" - } - - return wmiInfo -} // windows only below TODO add into stub file -func (a *Agent) PlatVer() (string, error) { return "", nil } -func (a *Agent) SendSoftware() {} -func (a *Agent) UninstallCleanup() {} -func (a *Agent) RunMigrations() {} -func GetServiceStatus(name string) (string, error) { return "", nil } + + + func (a *Agent) GetPython(force bool) {} diff --git a/agent/software/software.go b/agent/software/software.go new file mode 100644 index 0000000..081df6b --- /dev/null +++ b/agent/software/software.go @@ -0,0 +1,3 @@ +package software + +func SendSoftware() {} \ No newline at end of file diff --git a/agent/system/system_linux.go b/agent/system/system_linux.go index 2e73315..89c5cfb 100644 --- a/agent/system/system_linux.go +++ b/agent/system/system_linux.go @@ -1,13 +1,18 @@ package system import ( + "bufio" "fmt" "os" + "runtime" "strings" "syscall" "time" "github.com/amidaware/rmmagent/agent/utils" + ps "github.com/elastic/go-sysinfo" + "github.com/jaypipes/ghw" + "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/process" psHost "github.com/shirou/gopsutil/v3/host" "github.com/wh1te909/trmm-shared" @@ -152,3 +157,114 @@ func RunScript(code string, shell string, args []string, timeout int) (stdout, s return out.Stdout, retError, out.Status.Exit, nil } + +func GetWMIInfo() map[string]interface{} { + wmiInfo := make(map[string]interface{}) + ips := make([]string, 0) + disks := make([]string, 0) + cpus := make([]string, 0) + gpus := make([]string, 0) + + // local ips + host, err := ps.Host() + if err != nil { + //a.Logger.Errorln("GetWMIInfo() ps.Host()", err) + } else { + for _, ip := range host.Info().IPs { + if strings.Contains(ip, "127.0.") || strings.Contains(ip, "::1/128") { + continue + } + ips = append(ips, ip) + } + } + wmiInfo["local_ips"] = ips + + // disks + block, err := ghw.Block(ghw.WithDisableWarnings()) + if err != nil { + //a.Logger.Errorln("ghw.Block()", err) + } else { + for _, disk := range block.Disks { + if disk.IsRemovable || strings.Contains(disk.Name, "ram") { + continue + } + ret := fmt.Sprintf("%s %s %s %s %s %s", disk.Vendor, disk.Model, disk.StorageController, disk.DriveType, disk.Name, utils.ByteCountSI(disk.SizeBytes)) + ret = strings.TrimSpace(strings.ReplaceAll(ret, "unknown", "")) + disks = append(disks, ret) + } + } + + wmiInfo["disks"] = disks + + // cpus + cpuInfo, err := cpu.Info() + if err != nil { + //a.Logger.Errorln("cpu.Info()", err) + } else { + if len(cpuInfo) > 0 { + if cpuInfo[0].ModelName != "" { + cpus = append(cpus, cpuInfo[0].ModelName) + } + } + } + wmiInfo["cpus"] = cpus + + // make/model + wmiInfo["make_model"] = "" + chassis, err := ghw.Chassis(ghw.WithDisableWarnings()) + if err != nil { + //a.Logger.Errorln("ghw.Chassis()", err) + } else { + if chassis.Vendor != "" || chassis.Version != "" { + wmiInfo["make_model"] = fmt.Sprintf("%s %s", chassis.Vendor, chassis.Version) + } + } + + // gfx cards + + gpu, err := ghw.GPU(ghw.WithDisableWarnings()) + if err != nil { + //a.Logger.Errorln("ghw.GPU()", err) + } else { + for _, i := range gpu.GraphicsCards { + if i.DeviceInfo != nil { + ret := fmt.Sprintf("%s %s", i.DeviceInfo.Vendor.Name, i.DeviceInfo.Product.Name) + gpus = append(gpus, ret) + } + + } + } + wmiInfo["gpus"] = gpus + + // temp hack for ARM cpu/make/model if rasp pi + var makeModel string + if strings.Contains(runtime.GOARCH, "arm") { + file, _ := os.Open("/proc/cpuinfo") + scanner := bufio.NewScanner(file) + for scanner.Scan() { + if strings.Contains(strings.ToLower(scanner.Text()), "raspberry") { + model := strings.Split(scanner.Text(), ":") + if len(model) == 2 { + makeModel = strings.TrimSpace(model[1]) + break + } + } + } + } + + if len(cpus) == 0 { + wmiInfo["cpus"] = []string{makeModel} + } + if makeModel != "" && (wmiInfo["make_model"] == "" || wmiInfo["make_model"] == "unknown unknown") { + wmiInfo["make_model"] = makeModel + } + if len(gpus) == 1 && gpus[0] == "unknown unknown" { + wmiInfo["gpus"] = "" + } + + return wmiInfo +} + +func PlatVer() (string, error) { return "", nil } + +func GetServiceStatus(name string) (string, error) { return "", nil } \ No newline at end of file diff --git a/agent/tactical/tactical_linux.go b/agent/tactical/tactical_linux.go index 76a0934..1ad3c5e 100644 --- a/agent/tactical/tactical_linux.go +++ b/agent/tactical/tactical_linux.go @@ -168,3 +168,7 @@ func RecoverMesh(agentID string) { system.CmdV2(opts) SyncMeshNodeID() } + +func UninstallCleanup() {} + +func RunMigrations() {} \ No newline at end of file diff --git a/agent/utils.go b/agent/utils.go index df49ff7..b95ecc2 100644 --- a/agent/utils.go +++ b/agent/utils.go @@ -238,21 +238,6 @@ func Unzip(src, dest string) error { return nil } -// https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/ -func ByteCountSI(b uint64) string { - const unit = 1024 - if b < unit { - return fmt.Sprintf("%d B", b) - } - div, exp := int64(unit), 0 - for n := b / unit; n >= unit; n /= unit { - div *= unit - exp++ - } - return fmt.Sprintf("%.1f %cB", - float64(b)/float64(div), "kMGTPE"[exp]) -} - func randRange(min, max int) int { rand.Seed(time.Now().UnixNano()) return rand.Intn(max-min) + min