Files
beszel-ipv6/internal/entities/systemd/systemd.go
Shelby Tucker 40b3951615 [Feature] Basic systemd service monitoring (#1153)
* basic systemd service monitoring

* update to work after /internal rename

* monitor systemd service cpu and memory usage

---------

Co-authored-by: henrygd <hank@henrygd.me>
2025-11-10 15:29:21 -05:00

35 lines
907 B
Go

package systemd
import (
"runtime"
"time"
)
// Service represents a single systemd service with its stats.
type Service struct {
Name string `json:"n" cbor:"0,keyasint"`
Status string `json:"s" cbor:"1,keyasint"`
Cpu float64 `json:"c" cbor:"2,keyasint"`
Mem float64 `json:"m" cbor:"3,keyasint"`
PrevCpuUsage uint64 `json:"-"`
PrevReadTime time.Time `json:"-"`
}
// CalculateCPUPercent calculates the CPU usage percentage for the service.
func (s *Service) CalculateCPUPercent(cpuUsage uint64) {
if s.PrevReadTime.IsZero() {
s.Cpu = 0
} else {
duration := time.Since(s.PrevReadTime).Nanoseconds()
if duration > 0 {
coreCount := int64(runtime.NumCPU())
duration *= coreCount
cpuPercent := float64(cpuUsage-s.PrevCpuUsage) / float64(duration)
s.Cpu = cpuPercent * 100
}
}
s.PrevCpuUsage = cpuUsage
s.PrevReadTime = time.Now()
}