add mild suse support

This commit is contained in:
nazunalika 2021-12-29 19:05:10 -07:00
parent b48e3c289d
commit 14cb6a4607
Signed by: label
GPG Key ID: 6735C0E1BD65D048
3 changed files with 264 additions and 42 deletions

View File

@ -11,10 +11,13 @@ import (
"text/template" "text/template"
"github.com/rocky-linux/rpaste/modules/setting" "github.com/rocky-linux/rpaste/modules/setting"
"github.com/rocky-linux/rpaste/modules/utility"
) )
var ( var (
buf bytes.Buffer buf bytes.Buffer
osID string
sysInfoReturn string
) )
type SystemInfo struct { type SystemInfo struct {
@ -45,8 +48,10 @@ type SystemInfo struct {
YumConf string YumConf string
LastTwentyPackages string LastTwentyPackages string
EFISupport string EFISupport string
AppArmorStatus string
} }
// Distro agnostic functions
func osRelease() string { func osRelease() string {
app := "uniq" app := "uniq"
args := []string{"/etc/os-release"} args := []string{"/etc/os-release"}
@ -82,22 +87,6 @@ func desktopInstalled() string {
return values 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 { func cpuInfo() string {
// I'm not proud of this // I'm not proud of this
app := "grep 'model name' /proc/cpuinfo | awk -F: '{print $2}' | uniq -c | sed -re 's/^ +//'" 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) 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 { func xorgModules() string {
// I'm not proud of this // 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" 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) return string(cmd)
} }
func dmesgTail() string { // End agnostic functions
// I'm not proud of this
// Maybe I can implement a tail-like function // Red Hat (EL, Fedora) specific functions that may not work elsewhere
app := "dmesg | tail" // using these on non-rh distros should be used with caution.
cmd, _ := exec.Command("/bin/sh", "-c", app).CombinedOutput() func selinuxStatus() string {
return string(cmd) 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 { func selinuxErrors() string {
app := "last" // I'm not proud of this
args := []string{ app := "journalctl --since yesterday |grep avc: | grep -Eo comm='[^ ]+' | sort |uniq -c |sort -rn"
"-x", cmd, _ := exec.Command("/bin/sh", "-c", app).CombinedOutput()
"-n10",
"reboot",
"runlevel",
}
cmd, _ := exec.Command(app, args...).CombinedOutput()
return string(cmd) return string(cmd)
} }
@ -250,14 +266,36 @@ func lastTwentyPkgs() string {
return string(cmd) return string(cmd)
} }
func efiSupport() string { // End Red Hat functions
app := "efibootmgr"
args := []string{"-v"} // SuSE specific functions
cmd, _ := exec.Command(app, args...).CombinedOutput() // 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) return string(cmd)
} }
// End AppArmor
func SysInfoGather() string { func SysInfoGather() string {
osID = utility.GetGenericDistroID()
switch osID {
case "redhat":
sysInfoReturn = redHatSysInfoGather()
case "suse":
sysInfoReturn = suseSysInfoGather()
}
return sysInfoReturn
}
func redHatSysInfoGather() string {
sysInfoData := SystemInfo{ sysInfoData := SystemInfo{
OsRelease: osRelease(), OsRelease: osRelease(),
DesktopEnvs: desktopEnvs(), DesktopEnvs: desktopEnvs(),
@ -289,7 +327,41 @@ func SysInfoGather() string {
} }
t := template.New("sysinfo") 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) t.Execute(&buf, sysInfoData)
sysInfoResults := buf.String() sysInfoResults := buf.String()
return sysInfoResults return sysInfoResults

View File

@ -1,6 +1,6 @@
package setting package setting
var SysInfoTemplate = `################################################################################ var RedHatSysInfoTemplate = `################################################################################
# OS Release # OS Release
# #
{{.OsRelease}} {{.OsRelease}}
@ -127,3 +127,119 @@ var SysInfoTemplate = `#########################################################
# #
{{.EFISupport}} {{.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}}
`

View File

@ -8,8 +8,8 @@ import (
) )
// Not all of these are going to be used, but just in case. This is more of a // 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 // reference just for me. If more features are eventually added to this utility
// example and send to the "right" paste bins or some generic one. // it may make sense to eventually use these.
var ( var (
regexPrettyName = regexp.MustCompile(`^PRETTY_NAME=(.*)$`) regexPrettyName = regexp.MustCompile(`^PRETTY_NAME=(.*)$`)
regexID = regexp.MustCompile(`^ID=(.*)$`) regexID = regexp.MustCompile(`^ID=(.*)$`)
@ -22,9 +22,12 @@ var (
SystemVersion string SystemVersion string
SystemPrettyName string SystemPrettyName string
pasteBin 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") file, err := os.Open("/etc/os-release")
if err != nil { if err != nil {
return "" 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 // The order should be: config -> this
switch VendorName { switch osReleaseID {
case "rocky": case "rocky":
pasteBin = "rpaste" pasteBin = "rpaste"
case "redhat", "fedora", "centos", "almalinux", "scientific": case "redhat", "fedora", "centos", "almalinux", "scientific":
pasteBin = "fpaste" pasteBin = "fpaste"
case "opensuse-leap", "opensuse-tumbleweed", "sles":
// this will be on rpaste for now
pasteBin = "rpaste"
default: default:
pasteBin = "rpaste" pasteBin = "rpaste"
} }
return pasteBin 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
}