From 14cb6a4607e82a878b6a213a9b8b36aa5663d093 Mon Sep 17 00:00:00 2001 From: nazunalika Date: Wed, 29 Dec 2021 19:05:10 -0700 Subject: [PATCH] add mild suse support --- modules/paste/sysinfo.go | 146 +++++++++++++++++++++++++++--------- modules/setting/template.go | 118 ++++++++++++++++++++++++++++- modules/utility/system.go | 42 ++++++++++- 3 files changed, 264 insertions(+), 42 deletions(-) diff --git a/modules/paste/sysinfo.go b/modules/paste/sysinfo.go index f62a730..47b2978 100644 --- a/modules/paste/sysinfo.go +++ b/modules/paste/sysinfo.go @@ -11,10 +11,13 @@ import ( "text/template" "github.com/rocky-linux/rpaste/modules/setting" + "github.com/rocky-linux/rpaste/modules/utility" ) var ( - buf bytes.Buffer + buf bytes.Buffer + osID string + sysInfoReturn string ) type SystemInfo struct { @@ -45,8 +48,10 @@ type SystemInfo struct { YumConf string LastTwentyPackages string EFISupport string + AppArmorStatus string } +// Distro agnostic functions func osRelease() string { app := "uniq" args := []string{"/etc/os-release"} @@ -82,22 +87,6 @@ func desktopInstalled() string { return values } -func selinuxStatus() string { - app1 := "getenforce" - app2 := "grep -v '^#' /etc/sysconfig/selinux" - cmd1, _ := exec.Command(app1).CombinedOutput() - cmd2, _ := exec.Command("/bin/sh", "-c", app2).CombinedOutput() - combined := string(cmd1) + string(cmd2) - return combined -} - -func selinuxErrors() string { - // I'm not proud of this - app := "journalctl --since yesterday |grep avc: | grep -Eo comm='[^ ]+' | sort |uniq -c |sort -rn" - cmd, _ := exec.Command("/bin/sh", "-c", app).CombinedOutput() - return string(cmd) -} - func cpuInfo() string { // I'm not proud of this app := "grep 'model name' /proc/cpuinfo | awk -F: '{print $2}' | uniq -c | sed -re 's/^ +//'" @@ -179,6 +168,33 @@ func drmInfo() string { return string(cmd) } +func dmesgTail() string { + // I'm not proud of this + // Maybe I can implement a tail-like function + app := "dmesg | tail" + cmd, _ := exec.Command("/bin/sh", "-c", app).CombinedOutput() + return string(cmd) +} + +func lastTenReboots() string { + app := "last" + args := []string{ + "-x", + "-n10", + "reboot", + "runlevel", + } + cmd, _ := exec.Command(app, args...).CombinedOutput() + return string(cmd) +} + +func efiSupport() string { + app := "efibootmgr" + args := []string{"-v"} + cmd, _ := exec.Command(app, args...).CombinedOutput() + return string(cmd) +} + func xorgModules() string { // I'm not proud of this app := "grep LoadModule /var/log/Xorg.0.log ~/.local/share/xorg/Xorg.0.log | cut -d '\"' -f 2 | xargs" @@ -201,23 +217,23 @@ func xorgErrors() string { return string(cmd) } -func dmesgTail() string { - // I'm not proud of this - // Maybe I can implement a tail-like function - app := "dmesg | tail" - cmd, _ := exec.Command("/bin/sh", "-c", app).CombinedOutput() - return string(cmd) +// End agnostic functions + +// Red Hat (EL, Fedora) specific functions that may not work elsewhere +// using these on non-rh distros should be used with caution. +func selinuxStatus() string { + app1 := "getenforce" + app2 := "grep -v '^#' /etc/sysconfig/selinux" + cmd1, _ := exec.Command(app1).CombinedOutput() + cmd2, _ := exec.Command("/bin/sh", "-c", app2).CombinedOutput() + combined := string(cmd1) + string(cmd2) + return combined } -func lastTenReboots() string { - app := "last" - args := []string{ - "-x", - "-n10", - "reboot", - "runlevel", - } - cmd, _ := exec.Command(app, args...).CombinedOutput() +func selinuxErrors() string { + // I'm not proud of this + app := "journalctl --since yesterday |grep avc: | grep -Eo comm='[^ ]+' | sort |uniq -c |sort -rn" + cmd, _ := exec.Command("/bin/sh", "-c", app).CombinedOutput() return string(cmd) } @@ -250,14 +266,36 @@ func lastTwentyPkgs() string { return string(cmd) } -func efiSupport() string { - app := "efibootmgr" - args := []string{"-v"} - cmd, _ := exec.Command(app, args...).CombinedOutput() +// End Red Hat functions + +// SuSE specific functions +// End SuSE + +// Debian/Ubuntu specific functions +// End Debian + +// AppArmor, affects Debian/Ubuntu/SuSE +func appArmorStatus() string { + app := "aa-status" + cmd, _ := exec.Command(app).CombinedOutput() return string(cmd) } +// End AppArmor + func SysInfoGather() string { + osID = utility.GetGenericDistroID() + switch osID { + case "redhat": + sysInfoReturn = redHatSysInfoGather() + case "suse": + sysInfoReturn = suseSysInfoGather() + } + + return sysInfoReturn +} + +func redHatSysInfoGather() string { sysInfoData := SystemInfo{ OsRelease: osRelease(), DesktopEnvs: desktopEnvs(), @@ -289,7 +327,41 @@ func SysInfoGather() string { } t := template.New("sysinfo") - t, _ = t.Parse(setting.SysInfoTemplate) + t, _ = t.Parse(setting.RedHatSysInfoTemplate) + t.Execute(&buf, sysInfoData) + sysInfoResults := buf.String() + return sysInfoResults +} + +func suseSysInfoGather() string { + sysInfoData := SystemInfo{ + OsRelease: osRelease(), + DesktopEnvs: desktopEnvs(), + DesktopInstalled: desktopInstalled(), + AppArmorStatus: appArmorStatus(), + CPUInfo: cpuInfo(), + Support64bit: support64Bit(), + VirtSupport: virtSupport(), + LoadAverage: loadAverage(), + MemUsage: memUsage(), + TopCPUHogs: topCPUHogs(), + TopMemHogs: topMemHogs(), + DiskUsage: diskUsage(), + BlockDevs: blockDevs(), + PciDevs: pciDevs(), + USBDevs: usbDevs(), + DRMInfo: drmInfo(), + XorgModules: xorgModules(), + GLSupport: glSupport(), + XorgErrors: xorgErrors(), + DmesgTail: dmesgTail(), + LastTenReboots: lastTenReboots(), + LastTwentyPackages: lastTwentyPkgs(), + EFISupport: efiSupport(), + } + + t := template.New("sysinfo") + t, _ = t.Parse(setting.SuseSysInfoTemplate) t.Execute(&buf, sysInfoData) sysInfoResults := buf.String() return sysInfoResults diff --git a/modules/setting/template.go b/modules/setting/template.go index 8a68cdc..c33577e 100644 --- a/modules/setting/template.go +++ b/modules/setting/template.go @@ -1,6 +1,6 @@ package setting -var SysInfoTemplate = `################################################################################ +var RedHatSysInfoTemplate = `################################################################################ # OS Release # {{.OsRelease}} @@ -127,3 +127,119 @@ var SysInfoTemplate = `######################################################### # {{.EFISupport}} ` + +var SuseSysInfoTemplate = `################################################################################ +# OS Release +# +{{.OsRelease}} + +################################################################################ +# Desktop Environments +# +{{.DesktopEnvs}} + +################################################################################ +# Desktop Installed +# +{{.DesktopInstalled}} + +################################################################################ +# AppArmor Status +# +{{.AppArmorStatus}} + +################################################################################ +# CPU Info +# +{{.CPUInfo}} + +################################################################################ +# 64-bit Support +# +{{.Support64bit}} + +################################################################################ +# Virtualization Support +# +{{.VirtSupport}} + +################################################################################ +# Load Average +# +{{.LoadAverage}} + +################################################################################ +# Memory usage +# +{{.MemUsage}} + +################################################################################ +# Top 5 CPU hogs +# +{{.TopCPUHogs}} + +################################################################################ +# Top 5 memory hogs +# +{{.TopMemHogs}} + +################################################################################ +# Disk space usage +# +{{.DiskUsage}} + +################################################################################ +# Block Devices +# +{{.BlockDevs}} + +################################################################################ +# PCI devices +# +{{.PciDevs}} + +################################################################################ +# USB devices +# +{{.USBDevs}} + +################################################################################ +# DRM Information +# +{{.DRMInfo}} + +################################################################################ +# Xorg modules +# +{{.XorgModules}} + +################################################################################ +# GL Support +# +{{.GLSupport}} + +################################################################################ +# Xorg errors +# +{{.XorgErrors}} + +################################################################################ +# Kernel Buffer Tail +# +{{.DmesgTail}} + +################################################################################ +# Last few reboots +# +{{.LastTenReboots}} + +################################################################################ +# Last 20 packages +# +{{.LastTwentyPackages}} + +################################################################################ +# EFI Support +# +{{.EFISupport}} +` diff --git a/modules/utility/system.go b/modules/utility/system.go index 23569dd..c8a7fc5 100644 --- a/modules/utility/system.go +++ b/modules/utility/system.go @@ -8,8 +8,8 @@ import ( ) // Not all of these are going to be used, but just in case. This is more of a -// reference just for me. I want to be able to support more than just EL for -// example and send to the "right" paste bins or some generic one. +// reference just for me. If more features are eventually added to this utility +// it may make sense to eventually use these. var ( regexPrettyName = regexp.MustCompile(`^PRETTY_NAME=(.*)$`) regexID = regexp.MustCompile(`^ID=(.*)$`) @@ -22,9 +22,12 @@ var ( SystemVersion string SystemPrettyName string pasteBin string + osReleaseID string + simpleOSID string ) -func SetPasteOnOS() string { +// Gets the distro id only +func GetDistroID() string { file, err := os.Open("/etc/os-release") if err != nil { return "" @@ -37,15 +40,46 @@ func SetPasteOnOS() string { } } + return VendorName +} + +// Sets the pastebin based on the OS +func SetPasteOnOS() string { + osReleaseID = GetDistroID() + // The order should be: config -> this - switch VendorName { + switch osReleaseID { case "rocky": pasteBin = "rpaste" case "redhat", "fedora", "centos", "almalinux", "scientific": pasteBin = "fpaste" + case "opensuse-leap", "opensuse-tumbleweed", "sles": + // this will be on rpaste for now + pasteBin = "rpaste" default: pasteBin = "rpaste" } return pasteBin } + +// This is because there are several derivatives and some distros come from +// another. For example, rocky, rhel, alma, centos, fedora, they all generally +// operate the same (eg, sysinfo should work on all of them). But suse, this +// isn't that simple, unfortunately. +func GetGenericDistroID() string { + osReleaseID = GetDistroID() + + switch osReleaseID { + case "rocky", "redhat", "fedora", "centos", "almalinux", "scientific": + simpleOSID = "redhat" + case "opensuse-leap", "opensuse-tumbleweed", "sles": + simpleOSID = "suse" + case "debian", "ubuntu": + simpleOSID = "debian" + default: + simpleOSID = "unknown" + } + + return simpleOSID +}