|
@@ -1,6 +1,7 @@
|
|
package usageinfo
|
|
package usageinfo
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "log"
|
|
"math"
|
|
"math"
|
|
"os/exec"
|
|
"os/exec"
|
|
"runtime"
|
|
"runtime"
|
|
@@ -18,7 +19,9 @@ import (
|
|
|
|
|
|
const query_cpuproc_command = "ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10"
|
|
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 = "top -d1 | sed '4q;d' | awk '{print $(NF-1)}'"
|
|
|
|
+const query_freemem_command_darwin = "top -d | sed '4q;d' | awk '{print $(NF-1)}'"
|
|
const query_phymem_command = "sysctl hw.physmem | awk '{print $NF}'"
|
|
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
|
|
//Get CPU Usage in percentage
|
|
func GetCPUUsage() float64 {
|
|
func GetCPUUsage() float64 {
|
|
@@ -35,7 +38,7 @@ func GetCPUUsage() float64 {
|
|
usage = 0
|
|
usage = 0
|
|
}
|
|
}
|
|
usage = s
|
|
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
|
|
//Get CPU first 10 processes uses most CPU resources
|
|
cmd := exec.Command("bash", "-c", query_cpuproc_command)
|
|
cmd := exec.Command("bash", "-c", query_cpuproc_command)
|
|
out, err := cmd.CombinedOutput()
|
|
out, err := cmd.CombinedOutput()
|
|
@@ -61,19 +64,22 @@ func GetCPUUsage() float64 {
|
|
queryNCPUCommand := ""
|
|
queryNCPUCommand := ""
|
|
if runtime.GOOS == "linux" {
|
|
if runtime.GOOS == "linux" {
|
|
queryNCPUCommand = "nproc"
|
|
queryNCPUCommand = "nproc"
|
|
- } else if runtime.GOOS == "freebsd" {
|
|
|
|
|
|
+ } else if runtime.GOOS == "freebsd" || runtime.GOOS == "darwin" {
|
|
queryNCPUCommand = "sysctl hw.ncpu | awk '{print $NF}'"
|
|
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()
|
|
out, err = cmd.CombinedOutput()
|
|
if err != nil {
|
|
if err != nil {
|
|
return usageCounter
|
|
return usageCounter
|
|
}
|
|
}
|
|
-
|
|
|
|
// Divide total CPU usage by processes by total CPU core count
|
|
// 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 {
|
|
if err != nil {
|
|
coreCount = 1
|
|
coreCount = 1
|
|
}
|
|
}
|
|
@@ -160,10 +166,16 @@ func GetNumericRAMUsage() (int64, int64) {
|
|
return usedRam, totalRam
|
|
return usedRam, totalRam
|
|
}
|
|
}
|
|
|
|
|
|
- } else if runtime.GOOS == "freebsd" {
|
|
|
|
|
|
+ } else if runtime.GOOS == "freebsd" || runtime.GOOS == "darwin" {
|
|
|
|
|
|
// Get usused memory size (free)
|
|
// Get usused memory size (free)
|
|
- cmd := exec.Command("bash", "-c", query_freemem_command)
|
|
|
|
|
|
+ var cmd *exec.Cmd
|
|
|
|
+ if runtime.GOOS == "freebsd" {
|
|
|
|
+ cmd = exec.Command("bash", "-c", query_freemem_command)
|
|
|
|
+ } else if runtime.GOOS == "darwin" {
|
|
|
|
+ cmd = exec.Command("bash", "-c", query_freemem_command_darwin)
|
|
|
|
+ }
|
|
|
|
+
|
|
freeMemByteArr, err := cmd.CombinedOutput()
|
|
freeMemByteArr, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
if err != nil {
|
|
return usedRam, totalRam
|
|
return usedRam, totalRam
|
|
@@ -173,7 +185,11 @@ func GetNumericRAMUsage() (int64, int64) {
|
|
freeMemSize, err := strconv.ParseFloat(strings.ReplaceAll(string(freeMemStr), "M", ""), 10)
|
|
freeMemSize, err := strconv.ParseFloat(strings.ReplaceAll(string(freeMemStr), "M", ""), 10)
|
|
|
|
|
|
// Get phy memory size
|
|
// Get phy memory size
|
|
- cmd = exec.Command("bash", "-c", query_phymem_command)
|
|
|
|
|
|
+ if runtime.GOOS == "freebsd" {
|
|
|
|
+ cmd = exec.Command("bash", "-c", query_phymem_command)
|
|
|
|
+ } else if runtime.GOOS == "darwin" {
|
|
|
|
+ cmd = exec.Command("bash", "-c", query_phymem_command_darwin)
|
|
|
|
+ }
|
|
phyMemByteArr, err := cmd.CombinedOutput()
|
|
phyMemByteArr, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
if err != nil {
|
|
return usedRam, totalRam
|
|
return usedRam, totalRam
|
|
@@ -194,7 +210,7 @@ func GetNumericRAMUsage() (int64, int64) {
|
|
|
|
|
|
totalRam = int64(total)
|
|
totalRam = int64(total)
|
|
usedRam = int64(used)
|
|
usedRam = int64(used)
|
|
-
|
|
|
|
|
|
+ log.Println(totalRam, usedRam)
|
|
return usedRam, totalRam
|
|
return usedRam, totalRam
|
|
}
|
|
}
|
|
return -1, -1
|
|
return -1, -1
|
|
@@ -263,10 +279,15 @@ func GetRAMUsage() (string, string, float64) {
|
|
return usedRam, totalRam, usedPercentage
|
|
return usedRam, totalRam, usedPercentage
|
|
}
|
|
}
|
|
|
|
|
|
- } else if runtime.GOOS == "freebsd" {
|
|
|
|
|
|
+ } else if runtime.GOOS == "freebsd" || runtime.GOOS == "darwin" {
|
|
|
|
|
|
// Get usused memory size (free)
|
|
// Get usused memory size (free)
|
|
- cmd := exec.Command("bash", "-c", query_freemem_command)
|
|
|
|
|
|
+ var cmd *exec.Cmd
|
|
|
|
+ if runtime.GOOS == "freebsd" {
|
|
|
|
+ cmd = exec.Command("bash", "-c", query_freemem_command)
|
|
|
|
+ } else if runtime.GOOS == "darwin" {
|
|
|
|
+ cmd = exec.Command("bash", "-c", query_freemem_command_darwin)
|
|
|
|
+ }
|
|
freeMemByteArr, err := cmd.CombinedOutput()
|
|
freeMemByteArr, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
if err != nil {
|
|
return usedRam, totalRam, usedPercentage
|
|
return usedRam, totalRam, usedPercentage
|
|
@@ -274,9 +295,13 @@ func GetRAMUsage() (string, string, float64) {
|
|
freeMemStr := string(freeMemByteArr)
|
|
freeMemStr := string(freeMemByteArr)
|
|
freeMemStr = strings.ReplaceAll(freeMemStr, "\n", "")
|
|
freeMemStr = strings.ReplaceAll(freeMemStr, "\n", "")
|
|
freeMemSize, err := strconv.ParseFloat(strings.ReplaceAll(string(freeMemStr), "M", ""), 10)
|
|
freeMemSize, err := strconv.ParseFloat(strings.ReplaceAll(string(freeMemStr), "M", ""), 10)
|
|
-
|
|
|
|
|
|
+ log.Println("freeMem", freeMemStr)
|
|
// Get phy memory size
|
|
// Get phy memory size
|
|
- cmd = exec.Command("bash", "-c", query_phymem_command)
|
|
|
|
|
|
+ if runtime.GOOS == "freebsd" {
|
|
|
|
+ cmd = exec.Command("bash", "-c", query_phymem_command)
|
|
|
|
+ } else if runtime.GOOS == "darwin" {
|
|
|
|
+ cmd = exec.Command("bash", "-c", query_phymem_command_darwin)
|
|
|
|
+ }
|
|
phyMemByteArr, err := cmd.CombinedOutput()
|
|
phyMemByteArr, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
if err != nil {
|
|
return usedRam, totalRam, usedPercentage
|
|
return usedRam, totalRam, usedPercentage
|
|
@@ -284,6 +309,7 @@ func GetRAMUsage() (string, string, float64) {
|
|
|
|
|
|
phyMemStr := string(phyMemByteArr)
|
|
phyMemStr := string(phyMemByteArr)
|
|
phyMemStr = strings.ReplaceAll(phyMemStr, "\n", "")
|
|
phyMemStr = strings.ReplaceAll(phyMemStr, "\n", "")
|
|
|
|
+ log.Println("phyMemStr", phyMemStr)
|
|
|
|
|
|
// phyMemSize in MB
|
|
// phyMemSize in MB
|
|
phyMemSizeFloat, err := strconv.ParseFloat(phyMemStr, 10)
|
|
phyMemSizeFloat, err := strconv.ParseFloat(phyMemStr, 10)
|
|
@@ -295,6 +321,7 @@ func GetRAMUsage() (string, string, float64) {
|
|
usedRAMSizeFloat := phyMemSizeFloat - freeMemSize
|
|
usedRAMSizeFloat := phyMemSizeFloat - freeMemSize
|
|
usedRAMSizeFloat = math.Floor(usedRAMSizeFloat)
|
|
usedRAMSizeFloat = math.Floor(usedRAMSizeFloat)
|
|
usedRam = strconv.FormatFloat(usedRAMSizeFloat, 'f', -1, 64) + "MB"
|
|
usedRam = strconv.FormatFloat(usedRAMSizeFloat, 'f', -1, 64) + "MB"
|
|
|
|
+ log.Println("usedRam", usedRam)
|
|
|
|
|
|
usedPercentage = usedRAMSizeFloat / phyMemSizeFloat * 100
|
|
usedPercentage = usedRAMSizeFloat / phyMemSizeFloat * 100
|
|
|
|
|