Allow to hide swap stats for mem plugin, fix swap calculation

This commit is contained in:
eleksir
2024-01-10 19:07:12 +03:00
parent bc9b8f9106
commit c969ac9e42
4 changed files with 49 additions and 12 deletions

View File

@@ -23,7 +23,10 @@ type MyConfig struct {
// LA Plugin // LA Plugin
LA bool `json:"la,omitempty"` LA bool `json:"la,omitempty"`
Mem bool `json:"mem,omitempty"` Mem struct {
Enabled bool `json:"enabled,omitempty"`
ShowSwap bool `json:"show_swap,omitempty"`
} `json:"mem,omitempty"`
Clock struct { Clock struct {
Enabled bool `json:"enabled,omitempty"` Enabled bool `json:"enabled,omitempty"`

View File

@@ -7,7 +7,10 @@
"la" : true, "la" : true,
// Memory stats // Memory stats
"mem" : true, "mem" : {
"enabled": true,
"show_swap": false
},
// Just clock // Just clock
"clock": { "clock": {

10
main.go
View File

@@ -70,7 +70,7 @@ func main() {
syscall.SIGUSR2) syscall.SIGUSR2)
// Populate memory stats // Populate memory stats
if Conf.Mem { if Conf.Mem.Enabled {
go UpdateMemStats() go UpdateMemStats()
} }
@@ -160,9 +160,15 @@ func main() {
j = append(j, b) j = append(j, b)
} }
if Conf.Mem { if Conf.Mem.Enabled {
var b I3BarOutBlock var b I3BarOutBlock
if Conf.Mem.ShowSwap {
b.FullText = fmt.Sprintf("M:%d%% SHM:%dM SW:%dM", Memory.Usedpct, Memory.Shared, Memory.Swap) b.FullText = fmt.Sprintf("M:%d%% SHM:%dM SW:%dM", Memory.Usedpct, Memory.Shared, Memory.Swap)
} else {
b.FullText = fmt.Sprintf("M:%d%% SHM:%dM", Memory.Usedpct, Memory.Shared)
}
j = append(j, b) j = append(j, b)
} }

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"log"
"time" "time"
"github.com/shirou/gopsutil/mem" "github.com/shirou/gopsutil/mem"
@@ -19,15 +20,39 @@ var Memory Mem
// UpdateMemStats parses mem info stats. // UpdateMemStats parses mem info stats.
func UpdateMemStats() { func UpdateMemStats() {
for { for {
v, _ := mem.VirtualMemory() v, err := mem.VirtualMemory()
if err != nil {
log.Printf("Unable to get memory statistics: %s", err)
time.Sleep(1 * time.Second)
continue
}
sw, err := mem.SwapMemory()
if err != nil {
log.Printf("Unable to get swap statistics: %s", err)
time.Sleep(1 * time.Second)
continue
}
if Conf.Mem.ShowSwap {
if Memory.Usedpct != int64(v.UsedPercent) || Memory.Shared != int64(v.Shared/1024/1024) || if Memory.Usedpct != int64(v.UsedPercent) || Memory.Shared != int64(v.Shared/1024/1024) ||
Memory.Swap != int64(v.SwapTotal-v.SwapFree) { Memory.Swap != int64(v.SwapTotal-v.SwapFree) {
Memory.Usedpct = int64(v.UsedPercent) Memory.Usedpct = int64(v.UsedPercent)
Memory.Shared = int64(v.Shared / 1024 / 1024) Memory.Shared = int64(v.Shared / 1024 / 1024)
Memory.Swap = int64(v.SwapTotal - v.SwapFree) Memory.Swap = int64(sw.Total - sw.Free)
UpdateReady <- true UpdateReady <- true
} }
} else {
if Memory.Usedpct != int64(v.UsedPercent) || Memory.Shared != int64(v.Shared/1024/1024) {
Memory.Usedpct = int64(v.UsedPercent)
Memory.Shared = int64(v.Shared / 1024 / 1024)
UpdateReady <- true
}
}
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
} }