Prechádzať zdrojové kódy

Renamed constants for freebsd support

Summary
=======
1. Renamed constants from screaming case to lower case in usageinfo and hardwareinfo module.
2. Rechecked the comment in wifi_freebsd.go
HyperXraft 4 rokov pred
rodič
commit
56567c00e6

+ 27 - 31
mod/info/hardwareinfo/sysinfo_freebsd.go

@@ -27,24 +27,23 @@ import (
 	CPUFreq: Refers to the CPU frequency in terms of gigahertz, e.g. 0.8GHz
 */
 
-const UNKNOWN_STRING = "??? "
-const QUERY_FREQUENCY_COMMAND = "sysctl hw.model | awk '{print $NF}'"
-const QUERY_CPUMODEL_COMMAND = "sysctl hw.model | awk '{for(i=1;++i<=NF-3;) printf $i\" \"; print $(NF-2)}'"
-const QUERY_CPUARCH_COMMAND = "sysctl hw.machine_arch | awk '{print $NF}'"
-const QUERY_CPUHARDWARE_COMMAND = "sysctl kern.hwpmc.cpuid | awk '{print $NF}'"
-const QUERY_NETINFO_COMMAND = "ifconfig -a"
-const QUERY_USBINFO_COMMAND = "usbconfig"
-const QUERY_MEMSIZE_COMMAND = "sysctl hw.physmem | awk '{print $NF}'"
+const unknown_string = "??? "
+const query_frequency_command = "sysctl hw.model | awk '{print $NF}'"
+const query_cpumodel_command = "sysctl hw.model | awk '{for(i=1;++i<=NF-3;) printf $i\" \"; print $(NF-2)}'"
+const query_cpuarch_command = "sysctl hw.machine_arch | awk '{print $NF}'"
+const query_cpuhardware_command = "sysctl kern.hwpmc.cpuid | awk '{print $NF}'"
+const query_netinfo_command = "ifconfig -a"
+const query_usbinfo_command = "usbconfig"
+const query_memsize_command = "sysctl hw.physmem | awk '{print $NF}'"
 
 // GetCPUFreq() -> String
 // Returns the CPU frequency in the terms of MHz
 func GetCPUFreq() string {
-	cmd := QUERY_FREQUENCY_COMMAND             // Query CPUFreq w/ sysctl
-	shell := exec.Command("bash", "-c", cmd)   // Run command
-	freqByteArr, err := shell.CombinedOutput() // Response from cmdline
-	if err != nil {                            // If done w/ errors then
+	shell := exec.Command("bash", "-c", query_frequency_command) // Run command
+	freqByteArr, err := shell.CombinedOutput()                   // Response from cmdline
+	if err != nil {                                              // If done w/ errors then
 		log.Println(err)
-		return UNKNOWN_STRING
+		return unknown_string
 	}
 
 	freqStr := strings.ReplaceAll(string(freqByteArr), "GHz", "")
@@ -60,12 +59,11 @@ func GetCPUFreq() string {
 // GetCPUModel -> String
 // Returns the CPU model name string
 func GetCPUModel() string {
-	cmd := QUERY_CPUMODEL_COMMAND            // Query CPUModel w/ sysctl
-	shell := exec.Command("bash", "-c", cmd) // Run command
-	modelStr, err := shell.CombinedOutput()  // Response from cmdline
-	if err != nil {                          // If done w/ errors then
+	shell := exec.Command("bash", "-c", query_cpumodel_command) // Run command
+	modelStr, err := shell.CombinedOutput()                     // Response from cmdline
+	if err != nil {                                             // If done w/ errors then
 		log.Println(err)
-		return UNKNOWN_STRING
+		return unknown_string
 	}
 
 	return string(modelStr)
@@ -74,12 +72,11 @@ func GetCPUModel() string {
 // GetCPUHardware -> String
 // Returns the CPU ID string
 func GetCPUHardware() string {
-	cmd := QUERY_CPUHARDWARE_COMMAND         // Query CPUHW w/ sysctl
-	shell := exec.Command("bash", "-c", cmd) // Run command
-	hwStr, err := shell.CombinedOutput()     // Response from cmdline
-	if err != nil {                          // If done w/ errors then
+	shell := exec.Command("bash", "-c", query_cpuhardware_command) // Run command
+	hwStr, err := shell.CombinedOutput()                           // Response from cmdline
+	if err != nil {                                                // If done w/ errors then
 		log.Println(err)
-		return UNKNOWN_STRING
+		return unknown_string
 	}
 
 	return string(hwStr)
@@ -88,12 +85,11 @@ func GetCPUHardware() string {
 // GetCPUArch -> String
 // Returns the CPU architecture string
 func GetCPUArch() string {
-	cmd := QUERY_CPUARCH_COMMAND             // Query CPUArch w/ sysctl
-	shell := exec.Command("bash", "-c", cmd) // Run command
-	archStr, err := shell.CombinedOutput()   // Response from cmdline
-	if err != nil {                          // If done w/ errors then
+	shell := exec.Command("bash", "-c", query_cpuarch_command) // Run command
+	archStr, err := shell.CombinedOutput()                     // Response from cmdline
+	if err != nil {                                            // If done w/ errors then
 		log.Println(err)
-		return UNKNOWN_STRING
+		return unknown_string
 	}
 
 	return string(archStr)
@@ -119,7 +115,7 @@ func GetCPUInfo(w http.ResponseWriter, r *http.Request) {
 
 // Inherited code from sysinfo.go
 func Ifconfig(w http.ResponseWriter, r *http.Request) {
-	cmdin := QUERY_NETINFO_COMMAND
+	cmdin := query_netinfo_command
 	cmd := exec.Command("bash", "-c", cmdin)
 	networkInterfaces, err := cmd.CombinedOutput()
 	if err != nil {
@@ -192,7 +188,7 @@ func GetDriveStat(w http.ResponseWriter, r *http.Request) {
 // Takes in http.ResponseWriter w and *http.Request r,
 // Send TextResponse containing USB information extracted from shell in JSON
 func GetUSB(w http.ResponseWriter, r *http.Request) {
-	cmdin := QUERY_USBINFO_COMMAND
+	cmdin := query_usbinfo_command
 	cmd := exec.Command("bash", "-c", cmdin)
 	usbd, err := cmd.CombinedOutput()
 	if err != nil {
@@ -219,7 +215,7 @@ func GetUSB(w http.ResponseWriter, r *http.Request) {
 // Send TextResponse containing physical memory size
 // extracted from shell in JSON
 func GetRamInfo(w http.ResponseWriter, r *http.Request) {
-	cmd := exec.Command("bash", "-c", QUERY_MEMSIZE_COMMAND)
+	cmd := exec.Command("bash", "-c", query_memsize_command)
 	out, _ := cmd.CombinedOutput()
 
 	strOut := string(out)

+ 58 - 8
mod/info/usageinfo/usageinfo.go

@@ -1,6 +1,7 @@
 package usageinfo
 
 import (
+	"math"
 	"os/exec"
 	"runtime"
 	"strconv"
@@ -15,6 +16,10 @@ import (
 	native terminal commands
 */
 
+const query_cpuproc_command = "ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10"
+const query_freemem_command = "top -d1 | sed '4q;d' | awk '{print $(NF-1)}'"
+const query_phymem_command = "sysctl hw.physmem | awk '{print $NF}'"
+
 //Get CPU Usage in percentage
 func GetCPUUsage() float64 {
 	usage := float64(0)
@@ -30,9 +35,10 @@ func GetCPUUsage() float64 {
 			usage = 0
 		}
 		usage = s
-	} else if runtime.GOOS == "linux" {
-		//Get CPU processes
-		cmd := exec.Command("bash", "-c", "ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10")
+	} else if runtime.GOOS == "linux" || runtime.GOOS == "freebsd" {
+
+		//Get CPU first 10 processes uses most CPU resources
+		cmd := exec.Command("bash", "-c", query_cpuproc_command)
 		out, err := cmd.CombinedOutput()
 		if err != nil {
 			usage = 0
@@ -52,14 +58,22 @@ func GetCPUUsage() float64 {
 			}
 		}
 
-		//Get CPU Core Counts
-		cmd = exec.Command("nproc")
+		// Prepare queryNCPUCommnad for core count query
+		queryNCPUCommand := ""
+		if runtime.GOOS == "linux" {
+			queryNCPUCommand = "nproc"
+		} else if runtime.GOOS == "freebsd" {
+			queryNCPUCommand = "sysctl hw.ncpu | awk '{print $NF}'"
+		}
+
+		// Get CPU core count
+		cmd = exec.Command(queryNCPUCommand)
 		out, err = cmd.CombinedOutput()
 		if err != nil {
 			return usageCounter
 		}
 
-		//Divide the process usage by core count
+		// Divide total CPU usage by processes by total CPU core count
 		coreCount, err := strconv.Atoi(string(out))
 		if err != nil {
 			coreCount = 1
@@ -69,9 +83,9 @@ func GetCPUUsage() float64 {
 		if usage > float64(100) {
 			usage = 100
 		}
-	} else {
-		//Not supported
 
+	} else {
+		// CPU Usage Not supported on this platform
 	}
 
 	return usage
@@ -140,6 +154,42 @@ func GetRAMUsage() (string, string, float64) {
 			return usedRam, totalRam, usedPercentage
 		}
 
+	} else if runtime.GOOS == "freebsd" {
+
+		// Get usused memory size (free)
+		cmd := exec.Command("bash", "-c", query_freemem_command)
+		freeMemByteArr, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam, usedPercentage
+		}
+		freeMemStr := string(freeMemByteArr)
+		freeMemStr = strings.ReplaceAll(freeMemStr, "\n", "")
+		freeMemSize, err := strconv.ParseFloat(strings.ReplaceAll(string(freeMemStr), "M", ""), 10)
+
+		// Get phy memory size
+		cmd = exec.Command("bash", "-c", query_phymem_command)
+		phyMemByteArr, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam, usedPercentage
+		}
+
+		phyMemStr := string(phyMemByteArr)
+		phyMemStr = strings.ReplaceAll(phyMemStr, "\n", "")
+
+		// phyMemSize in MB
+		phyMemSizeFloat, err := strconv.ParseFloat(phyMemStr, 10)
+		phyMemSizeFloat = phyMemSizeFloat / 1048576
+		phyMemSizeFloat = math.Floor(phyMemSizeFloat)
+		totalRam = strconv.FormatFloat(phyMemSizeFloat, 'f', -1, 64) + "MB"
+
+		// Used memory
+		usedRAMSizeFloat := phyMemSizeFloat - freeMemSize
+		usedRAMSizeFloat = math.Floor(usedRAMSizeFloat)
+		usedRam = strconv.FormatFloat(usedRAMSizeFloat, 'f', -1, 64) + "MB"
+
+		usedPercentage = usedRAMSizeFloat / phyMemSizeFloat * 100
+
+		return usedRam, totalRam, usedPercentage
 	}
 
 	return usedRam, totalRam, usedPercentage

+ 1 - 1
mod/network/wifi/wifi_freebsd.go

@@ -2,7 +2,7 @@
 package wifi
 
 /*
-	This interface is left to be developed in the future when I have a macbook :P
+	This interface is left to be developed in the future when I installed a WiFi adapter on TrueNAS server
 
 */
 import "errors"

+ 3 - 0
subservice/WsTTY/build.sh

@@ -14,5 +14,8 @@ mv "${PWD##*/}" "${PWD##*/}_linux_arm64"
 echo "Building windows"
 #GOOS=windows GOARCH=amd64 go build
 
+echo "Building freebsd"
+# GOOS=freebsd GOARCH=amd64 go build
+# mv "${PWD##*/}" "${PWD##*/}_freebsd_amd64"
 
 echo "Completed"

+ 4 - 0
subservice/demo/build.sh

@@ -11,6 +11,10 @@ mv "${PWD##*/}" "${PWD##*/}_linux_arm"
 GOOS=linux GOARCH=arm64 go build
 mv "${PWD##*/}" "${PWD##*/}_linux_arm64"
 
+echo "Building freebsd"
+GOOS=freebsd GOARCH=amd64 go build
+mv "${PWD##*/}" "${PWD##*/}_freebsd_amd64"
+
 echo "Building windows"
 GOOS=windows GOARCH=amd64 go build