瀏覽代碼

Merge branch 'macOS-systeminfo' of tmp/arozos into master

LGTM
TC 4 年之前
父節點
當前提交
68d6557e0d
共有 3 個文件被更改,包括 299 次插入8 次删除
  1. 1 1
      mod/info/hardwareinfo/sysinfo.go
  2. 235 0
      mod/info/hardwareinfo/sysinfo_darwin.go
  3. 63 7
      mod/info/usageinfo/usageinfo.go

+ 1 - 1
mod/info/hardwareinfo/sysinfo.go

@@ -1,4 +1,4 @@
-// +build linux darwin
+// +build linux
 
 package hardwareinfo
 

+ 235 - 0
mod/info/hardwareinfo/sysinfo_darwin.go

@@ -0,0 +1,235 @@
+// +build darwin
+
+package hardwareinfo
+
+import (
+	"encoding/json"
+	"log"
+	"net/http"
+	"os/exec"
+	"strconv"
+	"strings"
+)
+
+/*
+	System Info
+	original author: HyperXraft
+	modified by: Alanyeung
+	original date:	2021-02-18
+	modified by: 2021-07-25
+
+	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
+*/
+
+//usbinfo from https://apple.stackexchange.com/questions/170105/list-usb-devices-on-osx-command-line
+const unknown_string = "??? "
+const query_frequency_command = "sysctl machdep.cpu.brand_string | awk '{print $NF}'"
+const query_cpumodel_command = "sysctl machdep.cpu.brand_string | awk '{for(i=1;++i<=NF-3;) printf $i\" \"; print $(NF-2)}'"
+const query_cpuarch_command = "sysctl hw.machine | awk '{print $NF}'"
+const query_cpuhardware_command = "sysctl machdep.cpu.stepping | awk '{print $NF}'"
+const query_netinfo_command = "networksetup -listallhardwareports"
+const query_usbinfo_command = "ioreg -p IOUSB -w0 | sed 's/[^o]*o //; s/@.*$//' | grep -v '^Root.*'"
+const query_memsize_command = "sysctl hw.memsize | 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[8],
+			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))
+}

+ 63 - 7
mod/info/usageinfo/usageinfo.go

@@ -18,7 +18,9 @@ import (
 
 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_freemem_command_darwin = "ps -A -o %mem | awk '{mem += $1} END {print mem}'"
 const query_phymem_command = "sysctl hw.physmem | awk '{print $NF}'"
+const query_phymem_command_darwin = "sysctl hw.memsize | awk '{print $NF}'"
 
 //Get CPU Usage in percentage
 func GetCPUUsage() float64 {
@@ -35,7 +37,7 @@ func GetCPUUsage() float64 {
 			usage = 0
 		}
 		usage = s
-	} else if runtime.GOOS == "linux" || runtime.GOOS == "freebsd" {
+	} else if runtime.GOOS == "linux" || runtime.GOOS == "freebsd" || runtime.GOOS == "darwin" {
 		//Get CPU first 10 processes uses most CPU resources
 		cmd := exec.Command("bash", "-c", query_cpuproc_command)
 		out, err := cmd.CombinedOutput()
@@ -61,19 +63,22 @@ func GetCPUUsage() float64 {
 		queryNCPUCommand := ""
 		if runtime.GOOS == "linux" {
 			queryNCPUCommand = "nproc"
-		} else if runtime.GOOS == "freebsd" {
+		} else if runtime.GOOS == "freebsd" || runtime.GOOS == "darwin" {
 			queryNCPUCommand = "sysctl hw.ncpu | awk '{print $NF}'"
 		}
 
-		// Get CPU core count
-		cmd = exec.Command(queryNCPUCommand)
+		// Get CPU core count (freebsd way)
+		if runtime.GOOS == "freebsd" {
+			cmd = exec.Command(queryNCPUCommand)
+		} else if runtime.GOOS == "darwin" {
+			cmd = exec.Command("bash", "-c", queryNCPUCommand)
+		}
 		out, err = cmd.CombinedOutput()
 		if err != nil {
 			return usageCounter
 		}
-
 		// Divide total CPU usage by processes by total CPU core count
-		coreCount, err := strconv.Atoi(string(out))
+		coreCount, err := strconv.Atoi(strings.TrimSpace(string(out)))
 		if err != nil {
 			coreCount = 1
 		}
@@ -164,6 +169,7 @@ func GetNumericRAMUsage() (int64, int64) {
 
 		// Get usused memory size (free)
 		cmd := exec.Command("bash", "-c", query_freemem_command)
+
 		freeMemByteArr, err := cmd.CombinedOutput()
 		if err != nil {
 			return usedRam, totalRam
@@ -194,7 +200,29 @@ func GetNumericRAMUsage() (int64, int64) {
 
 		totalRam = int64(total)
 		usedRam = int64(used)
+		return usedRam, totalRam
+	} else if runtime.GOOS == "darwin" {
+		cmd := exec.Command("bash", "-c", query_freemem_command_darwin)
+		freeMemStr, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam
+		}
+		cmd = exec.Command("bash", "-c", query_phymem_command_darwin)
+		phyMemStr, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam
+		}
 
+		freeMem, err := strconv.ParseFloat(strings.TrimSpace(string(freeMemStr)), 10)
+		if err != nil {
+			return usedRam, totalRam
+		}
+		phyMem, err := strconv.ParseInt(strings.TrimSpace(string(phyMemStr)), 10, 64)
+		if err != nil {
+			return usedRam, totalRam
+		}
+		totalRam = phyMem
+		usedRam = int64(float64(phyMem) - float64(phyMem)*freeMem)
 		return usedRam, totalRam
 	}
 	return -1, -1
@@ -274,7 +302,6 @@ func GetRAMUsage() (string, string, float64) {
 		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()
@@ -298,6 +325,35 @@ func GetRAMUsage() (string, string, float64) {
 
 		usedPercentage = usedRAMSizeFloat / phyMemSizeFloat * 100
 
+		return usedRam, totalRam, usedPercentage
+	} else if runtime.GOOS == "darwin" {
+		cmd := exec.Command("bash", "-c", query_freemem_command_darwin)
+		freeMemStr, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam, usedPercentage
+		}
+		cmd = exec.Command("bash", "-c", query_phymem_command_darwin)
+		phyMemStr, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam, usedPercentage
+		}
+		freeMemSizeFloat, err := strconv.ParseFloat(strings.TrimSpace(string(freeMemStr)), 10)
+		if err != nil {
+			return usedRam, totalRam, usedPercentage
+		}
+		phyMemSizeFloat, err := strconv.ParseFloat(strings.TrimSpace(string(phyMemStr)), 10)
+		if err != nil {
+			return usedRam, totalRam, usedPercentage
+		}
+		phyMemSizeFloat = phyMemSizeFloat / 1048576
+		phyMemSizeFloat = math.Floor(phyMemSizeFloat)
+		totalRam = strconv.FormatFloat(phyMemSizeFloat, 'f', -1, 64) + "MB"
+
+		usedRAMSizeFloat := float64(phyMemSizeFloat) - float64(phyMemSizeFloat)*(1-(freeMemSizeFloat/100))
+		usedRAMSizeFloat = math.Floor(usedRAMSizeFloat)
+		usedRam = strconv.FormatFloat(usedRAMSizeFloat, 'f', -1, 64) + "MB"
+
+		usedPercentage = freeMemSizeFloat
 		return usedRam, totalRam, usedPercentage
 	}