Bladeren bron

Merge branch 'master' of HyperXraft/arozos into master

LGTM
TC 4 jaren geleden
bovenliggende
commit
775495270c

+ 232 - 0
mod/info/hardwareinfo/sysinfo_freebsd.go

@@ -0,0 +1,232 @@
+// +build freebsd
+
+package hardwareinfo
+
+import (
+	"encoding/json"
+	"log"
+	"net/http"
+	"os/exec"
+	"strconv"
+	"strings"
+)
+
+/*
+	System Info
+	author: HyperXraft
+	date:	2021-02-18
+
+	This module get the CPU information on different platform using
+	native terminal commands on FreeBSD platform
+
+	DEFINITIONS
+	===========
+	CPUModel: Refers to the Marketing name of the CPU, e.g. Intel Xeon E7 8890
+	CPUHardware: Refers to the CPUID name, e.g. GenuineIntel-6-3A-9
+	CPUArch: Refers to the ISA of the CPU, e.g. aarch64
+	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}'"
+
+// GetCPUFreq() -> String
+// Returns the CPU frequency in the terms of MHz
+func GetCPUFreq() string {
+	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
+	}
+
+	freqStr := strings.ReplaceAll(string(freqByteArr), "GHz", "")
+	freqStr = strings.ReplaceAll(freqStr, "\n", "")
+	freqStr = strings.ReplaceAll(freqStr, " ", "")
+	freqFloat, _ := strconv.ParseFloat(freqStr, 8)
+	freqFloat = freqFloat * 1000
+	freqStrMHz := strconv.FormatFloat(freqFloat, 'f', -1, 64)
+
+	return freqStrMHz
+}
+
+// GetCPUModel -> String
+// Returns the CPU model name string
+func GetCPUModel() string {
+	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 string(modelStr)
+}
+
+// GetCPUHardware -> String
+// Returns the CPU ID string
+func GetCPUHardware() string {
+	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 string(hwStr)
+}
+
+// GetCPUArch -> String
+// Returns the CPU architecture string
+func GetCPUArch() string {
+	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 string(archStr)
+}
+
+// Inherited code from sysinfo_window.go
+func GetCPUInfo(w http.ResponseWriter, r *http.Request) {
+	CPUInfo := CPUInfo{
+		Freq:        GetCPUFreq(),
+		Hardware:    GetCPUHardware(),
+		Instruction: GetCPUArch(),
+		Model:       GetCPUModel(),
+		Revision:    "unknown",
+	}
+
+	var jsonData []byte
+	jsonData, err := json.Marshal(CPUInfo)
+	if err != nil {
+		log.Println(err)
+	}
+	sendTextResponse(w, string(jsonData))
+}
+
+// Inherited code from sysinfo.go
+func Ifconfig(w http.ResponseWriter, r *http.Request) {
+	cmdin := query_netinfo_command
+	cmd := exec.Command("bash", "-c", cmdin)
+	networkInterfaces, err := cmd.CombinedOutput()
+	if err != nil {
+		networkInterfaces = []byte{}
+	}
+
+	nic := strings.Split(string(networkInterfaces), "\n")
+
+	var arr []string
+	for _, info := range nic {
+		thisInfo := string(info)
+		arr = append(arr, thisInfo)
+	}
+
+	var jsonData []byte
+	jsonData, err = json.Marshal(arr)
+	if err != nil {
+		log.Println(err)
+	}
+	sendTextResponse(w, string(jsonData))
+}
+
+// Inherited code from sysinfo.go
+func GetDriveStat(w http.ResponseWriter, r *http.Request) {
+	//Get drive status using df command
+	cmdin := `df -k | sed -e /Filesystem/d`
+	cmd := exec.Command("bash", "-c", cmdin)
+	dev, err := cmd.CombinedOutput()
+	if err != nil {
+		dev = []byte{}
+	}
+
+	drives := strings.Split(string(dev), "\n")
+
+	if len(drives) == 0 {
+		sendErrorResponse(w, "Invalid disk information")
+		return
+	}
+
+	var arr []LogicalDisk
+	for _, driveInfo := range drives {
+		if driveInfo == "" {
+			continue
+		}
+		for strings.Contains(driveInfo, "  ") {
+			driveInfo = strings.Replace(driveInfo, "  ", " ", -1)
+		}
+		driveInfoChunk := strings.Split(driveInfo, " ")
+		tmp, _ := strconv.Atoi(driveInfoChunk[3])
+		freespaceInByte := int64(tmp)
+
+		LogicalDisk := LogicalDisk{
+			DriveLetter: driveInfoChunk[5],
+			FileSystem:  driveInfoChunk[0],
+			FreeSpace:   strconv.FormatInt(freespaceInByte*1024, 10), //df show disk space in 1KB blocks
+		}
+		arr = append(arr, LogicalDisk)
+	}
+
+	var jsonData []byte
+	jsonData, err = json.Marshal(arr)
+	if err != nil {
+		log.Println(err)
+	}
+	sendTextResponse(w, string(jsonData))
+
+}
+
+// GetUSB(ResponseWriter, HttpRequest) -> nil
+// 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
+	cmd := exec.Command("bash", "-c", cmdin)
+	usbd, err := cmd.CombinedOutput()
+	if err != nil {
+		usbd = []byte{}
+	}
+
+	usbDrives := strings.Split(string(usbd), "\n")
+
+	var arr []string
+	for _, info := range usbDrives {
+		arr = append(arr, info)
+	}
+
+	var jsonData []byte
+	jsonData, err = json.Marshal(arr)
+	if err != nil {
+		log.Println(err)
+	}
+	sendTextResponse(w, string(jsonData))
+}
+
+// GetRamInfo(w ResponseWriter, r *HttpRequest) -> nil
+// Takes in http.ResponseWriter w and *http.Request r,
+// 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)
+	out, _ := cmd.CombinedOutput()
+
+	strOut := string(out)
+	strOut = strings.ReplaceAll(strOut, "\n", "")
+	ramSize, _ := strconv.ParseInt(strOut, 10, 64)
+	ramSizeInt := ramSize
+
+	var jsonData []byte
+	jsonData, err := json.Marshal(ramSizeInt)
+	if err != nil {
+		log.Println(err)
+	}
+	sendTextResponse(w, string(jsonData))
+}

+ 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

+ 37 - 0
mod/network/wifi/wifi_freebsd.go

@@ -0,0 +1,37 @@
+// +build freebsd
+package wifi
+
+/*
+	This interface is left to be developed in the future when I installed a WiFi adapter on TrueNAS server
+
+*/
+import "errors"
+
+//Toggle WiFi On Off. Only allow on sudo mode
+func (w *WiFiManager) SetInterfacePower(wlanInterface string, on bool) error {
+	return errors.New("Platform not supported")
+}
+
+func (w *WiFiManager) GetInterfacePowerStatuts(wlanInterface string) (bool, error) {
+	return false, errors.New("Platform not supported")
+}
+
+func (w *WiFiManager) ScanNearbyWiFi(interfaceName string) ([]WiFiInfo, error) {
+	return []WiFiInfo{}, errors.New("Platform not supported")
+}
+
+func (w *WiFiManager) GetWirelessInterfaces() ([]string, error) {
+	return []string{}, nil
+}
+
+func (w *WiFiManager) ConnectWiFi(ssid string, password string, connType string, identity string) (*WiFiConnectionResult, error) {
+	return &WiFiConnectionResult{}, errors.New("Platform not supported")
+}
+
+func (w *WiFiManager) GetConnectedWiFi() (string, string, error) {
+	return "", "", errors.New("Platform not supported")
+}
+
+func (w *WiFiManager) RemoveWifi(ssid string) error {
+	return errors.New("Platform not supported")
+}

+ 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