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 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 {
Enabled bool `json:"enabled,omitempty"`

View File

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

12
main.go
View File

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

View File

@@ -1,6 +1,7 @@
package main
import (
"log"
"time"
"github.com/shirou/gopsutil/mem"
@@ -19,14 +20,38 @@ var Memory Mem
// UpdateMemStats parses mem info stats.
func UpdateMemStats() {
for {
v, _ := mem.VirtualMemory()
v, err := mem.VirtualMemory()
if Memory.Usedpct != int64(v.UsedPercent) || Memory.Shared != int64(v.Shared/1024/1024) ||
Memory.Swap != int64(v.SwapTotal-v.SwapFree) {
Memory.Usedpct = int64(v.UsedPercent)
Memory.Shared = int64(v.Shared / 1024 / 1024)
Memory.Swap = int64(v.SwapTotal - v.SwapFree)
UpdateReady <- true
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) ||
Memory.Swap != int64(v.SwapTotal-v.SwapFree) {
Memory.Usedpct = int64(v.UsedPercent)
Memory.Shared = int64(v.Shared / 1024 / 1024)
Memory.Swap = int64(sw.Total - sw.Free)
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)