|
@@ -36,7 +36,6 @@ func GetCPUUsage() float64 {
|
|
|
}
|
|
|
usage = s
|
|
|
} 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()
|
|
@@ -91,6 +90,116 @@ func GetCPUUsage() float64 {
|
|
|
return usage
|
|
|
}
|
|
|
|
|
|
+//Get RAM Usage in Numeric values
|
|
|
+func GetNumericRAMUsage() (int64, int64) {
|
|
|
+ usedRam := int64(-1)
|
|
|
+ totalRam := int64(-1)
|
|
|
+ if runtime.GOOS == "windows" {
|
|
|
+ cmd := exec.Command("system/hardware/windows/RAMUsage.exe")
|
|
|
+ out, err := cmd.CombinedOutput()
|
|
|
+ if err != nil {
|
|
|
+ return -1, -1
|
|
|
+ }
|
|
|
+ raminfo := strings.Split(strings.TrimSpace(string(out)), ",")
|
|
|
+ if len(raminfo) == 3 {
|
|
|
+
|
|
|
+ //The returned value is something like this
|
|
|
+ //7639 MB,16315 MB,0.468219429972418
|
|
|
+ tmp := strings.Split(raminfo[0], " ")[0]
|
|
|
+ used, err := strconv.ParseInt(tmp, 10, 64)
|
|
|
+ if err != nil {
|
|
|
+ return -1, -1
|
|
|
+ }
|
|
|
+
|
|
|
+ tmp = strings.Split(raminfo[1], " ")[0]
|
|
|
+ total, err := strconv.ParseInt(tmp, 10, 64)
|
|
|
+ if err != nil {
|
|
|
+ return -1, -1
|
|
|
+ }
|
|
|
+
|
|
|
+ usedRam = used * 1024 * 1024 //From MB to Bytes
|
|
|
+ totalRam = total * 1024 * 1024 //From MB to Bytes
|
|
|
+
|
|
|
+ return usedRam, totalRam
|
|
|
+ } else {
|
|
|
+ return -1, -1
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if runtime.GOOS == "linux" {
|
|
|
+ cmd := exec.Command("bash", "-c", "free -m | grep Mem:")
|
|
|
+ out, err := cmd.CombinedOutput()
|
|
|
+ if err != nil {
|
|
|
+ return usedRam, totalRam
|
|
|
+ }
|
|
|
+
|
|
|
+ //If the output contain more than one Memory info, only use the first one
|
|
|
+ if strings.Contains(string(out), "\n") {
|
|
|
+ out = []byte(strings.Split(string(out), "\n")[0])
|
|
|
+ }
|
|
|
+
|
|
|
+ //Trim of double space to space
|
|
|
+ for strings.Contains(string(out), " ") {
|
|
|
+ out = []byte(strings.ReplaceAll(string(out), " ", " "))
|
|
|
+ }
|
|
|
+
|
|
|
+ data := strings.Split(string(out), " ")
|
|
|
+ if len(data) > 3 {
|
|
|
+ used, err := strconv.ParseInt(data[2], 10, 64)
|
|
|
+ if err != nil {
|
|
|
+ return -1, -1
|
|
|
+ }
|
|
|
+
|
|
|
+ total, err := strconv.ParseInt(data[1], 10, 64)
|
|
|
+ if err != nil {
|
|
|
+ return -1, -1
|
|
|
+ }
|
|
|
+
|
|
|
+ usedRam = used * 1024 * 1024
|
|
|
+ totalRam = total * 1024 * 1024
|
|
|
+
|
|
|
+ return usedRam, totalRam
|
|
|
+ }
|
|
|
+
|
|
|
+ } 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
|
|
|
+ }
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+ phyMemStr := string(phyMemByteArr)
|
|
|
+ phyMemStr = strings.ReplaceAll(phyMemStr, "\n", "")
|
|
|
+
|
|
|
+ // phyMemSize in MB
|
|
|
+ phyMemSizeFloat, err := strconv.ParseFloat(phyMemStr, 10)
|
|
|
+ phyMemSizeFloat = math.Floor(phyMemSizeFloat)
|
|
|
+ total := phyMemSizeFloat
|
|
|
+
|
|
|
+ // Used memory
|
|
|
+ usedRAMSizeFloat := phyMemSizeFloat - freeMemSize
|
|
|
+ usedRAMSizeFloat = math.Floor(usedRAMSizeFloat)
|
|
|
+ used := usedRAMSizeFloat
|
|
|
+
|
|
|
+ totalRam = int64(total)
|
|
|
+ usedRam = int64(used)
|
|
|
+
|
|
|
+ return usedRam, totalRam
|
|
|
+ }
|
|
|
+ return -1, -1
|
|
|
+}
|
|
|
+
|
|
|
//Get RAM usage, return used / total / used percentage
|
|
|
func GetRAMUsage() (string, string, float64) {
|
|
|
usedRam := "Unknown"
|