mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 17:59:28 +08:00
remove global variable
This commit is contained in:
@@ -26,21 +26,25 @@ import (
|
|||||||
psutilNet "github.com/shirou/gopsutil/v4/net"
|
psutilNet "github.com/shirou/gopsutil/v4/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Version = "0.1.2"
|
|
||||||
|
|
||||||
var containerStatsMap = make(map[string]*container.PrevContainerStats)
|
var containerStatsMap = make(map[string]*container.PrevContainerStats)
|
||||||
var containerStatsMutex = &sync.Mutex{}
|
|
||||||
|
|
||||||
type Agent struct {
|
type Agent struct {
|
||||||
port string
|
port string
|
||||||
pubKey []byte
|
pubKey []byte
|
||||||
sem chan struct{}
|
sem chan struct{}
|
||||||
|
containerStatsMutex *sync.Mutex
|
||||||
|
diskIoStats system.DiskIoStats
|
||||||
|
netIoStats system.NetIoStats
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAgent(pubKey []byte, port string) *Agent {
|
func NewAgent(pubKey []byte, port string) *Agent {
|
||||||
return &Agent{
|
return &Agent{
|
||||||
pubKey: pubKey,
|
pubKey: pubKey,
|
||||||
sem: make(chan struct{}, 15),
|
sem: make(chan struct{}, 15),
|
||||||
|
port: port,
|
||||||
|
containerStatsMutex: &sync.Mutex{},
|
||||||
|
diskIoStats: system.DiskIoStats{},
|
||||||
|
netIoStats: system.NetIoStats{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,24 +56,10 @@ func (a *Agent) releaseSemaphore() {
|
|||||||
<-a.sem
|
<-a.sem
|
||||||
}
|
}
|
||||||
|
|
||||||
var diskIoStats = system.DiskIoStats{
|
|
||||||
Read: 0,
|
|
||||||
Write: 0,
|
|
||||||
Time: time.Now(),
|
|
||||||
Filesystem: "",
|
|
||||||
}
|
|
||||||
|
|
||||||
var netIoStats = system.NetIoStats{
|
|
||||||
BytesRecv: 0,
|
|
||||||
BytesSent: 0,
|
|
||||||
Time: time.Now(),
|
|
||||||
Name: "",
|
|
||||||
}
|
|
||||||
|
|
||||||
// client for docker engine api
|
// client for docker engine api
|
||||||
var dockerClient = newDockerClient()
|
var dockerClient = newDockerClient()
|
||||||
|
|
||||||
func getSystemStats() (*system.SystemInfo, *system.SystemStats) {
|
func (a *Agent) getSystemStats() (*system.SystemInfo, *system.SystemStats) {
|
||||||
systemStats := &system.SystemStats{}
|
systemStats := &system.SystemStats{}
|
||||||
|
|
||||||
// cpu percent
|
// cpu percent
|
||||||
@@ -98,18 +88,18 @@ func getSystemStats() (*system.SystemInfo, *system.SystemStats) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// disk i/o
|
// disk i/o
|
||||||
if io, err := disk.IOCounters(diskIoStats.Filesystem); err == nil {
|
if io, err := disk.IOCounters(a.diskIoStats.Filesystem); err == nil {
|
||||||
for _, d := range io {
|
for _, d := range io {
|
||||||
// add to systemStats
|
// add to systemStats
|
||||||
secondsElapsed := time.Since(diskIoStats.Time).Seconds()
|
secondsElapsed := time.Since(a.diskIoStats.Time).Seconds()
|
||||||
readPerSecond := float64(d.ReadBytes-diskIoStats.Read) / secondsElapsed
|
readPerSecond := float64(d.ReadBytes-a.diskIoStats.Read) / secondsElapsed
|
||||||
systemStats.DiskRead = bytesToMegabytes(readPerSecond)
|
systemStats.DiskRead = bytesToMegabytes(readPerSecond)
|
||||||
writePerSecond := float64(d.WriteBytes-diskIoStats.Write) / secondsElapsed
|
writePerSecond := float64(d.WriteBytes-a.diskIoStats.Write) / secondsElapsed
|
||||||
systemStats.DiskWrite = bytesToMegabytes(writePerSecond)
|
systemStats.DiskWrite = bytesToMegabytes(writePerSecond)
|
||||||
// update diskIoStats
|
// update diskIoStats
|
||||||
diskIoStats.Time = time.Now()
|
a.diskIoStats.Time = time.Now()
|
||||||
diskIoStats.Read = d.ReadBytes
|
a.diskIoStats.Read = d.ReadBytes
|
||||||
diskIoStats.Write = d.WriteBytes
|
a.diskIoStats.Write = d.WriteBytes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,15 +116,15 @@ func getSystemStats() (*system.SystemInfo, *system.SystemStats) {
|
|||||||
bytesRecv += v.BytesRecv
|
bytesRecv += v.BytesRecv
|
||||||
}
|
}
|
||||||
// add to systemStats
|
// add to systemStats
|
||||||
secondsElapsed := time.Since(netIoStats.Time).Seconds()
|
secondsElapsed := time.Since(a.netIoStats.Time).Seconds()
|
||||||
sentPerSecond := float64(bytesSent-netIoStats.BytesSent) / secondsElapsed
|
sentPerSecond := float64(bytesSent-a.netIoStats.BytesSent) / secondsElapsed
|
||||||
recvPerSecond := float64(bytesRecv-netIoStats.BytesRecv) / secondsElapsed
|
recvPerSecond := float64(bytesRecv-a.netIoStats.BytesRecv) / secondsElapsed
|
||||||
systemStats.NetworkSent = bytesToMegabytes(sentPerSecond)
|
systemStats.NetworkSent = bytesToMegabytes(sentPerSecond)
|
||||||
systemStats.NetworkRecv = bytesToMegabytes(recvPerSecond)
|
systemStats.NetworkRecv = bytesToMegabytes(recvPerSecond)
|
||||||
// update netIoStats
|
// update netIoStats
|
||||||
netIoStats.BytesSent = bytesSent
|
a.netIoStats.BytesSent = bytesSent
|
||||||
netIoStats.BytesRecv = bytesRecv
|
a.netIoStats.BytesRecv = bytesRecv
|
||||||
netIoStats.Time = time.Now()
|
a.netIoStats.Time = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
systemInfo := &system.SystemInfo{
|
systemInfo := &system.SystemInfo{
|
||||||
@@ -191,7 +181,7 @@ func (a *Agent) getDockerStats() ([]*container.ContainerStats, error) {
|
|||||||
// note: can't use Created field because it's not updated on restart
|
// note: can't use Created field because it's not updated on restart
|
||||||
if strings.HasSuffix(ctr.Status, "seconds") {
|
if strings.HasSuffix(ctr.Status, "seconds") {
|
||||||
// if so, remove old container data
|
// if so, remove old container data
|
||||||
deleteContainerStatsSync(ctr.IdShort)
|
a.deleteContainerStatsSync(ctr.IdShort)
|
||||||
}
|
}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
@@ -204,7 +194,7 @@ func (a *Agent) getDockerStats() ([]*container.ContainerStats, error) {
|
|||||||
closeIdleConnections(err)
|
closeIdleConnections(err)
|
||||||
} else {
|
} else {
|
||||||
// otherwise delete container from map
|
// otherwise delete container from map
|
||||||
deleteContainerStatsSync(ctr.IdShort)
|
a.deleteContainerStatsSync(ctr.IdShort)
|
||||||
}
|
}
|
||||||
// retry once
|
// retry once
|
||||||
cstats, err = a.getContainerStats(ctr)
|
cstats, err = a.getContainerStats(ctr)
|
||||||
@@ -253,8 +243,8 @@ func (a *Agent) getContainerStats(ctr *container.Container) (*container.Containe
|
|||||||
}
|
}
|
||||||
usedMemory := statsJson.MemoryStats.Usage - memCache
|
usedMemory := statsJson.MemoryStats.Usage - memCache
|
||||||
|
|
||||||
containerStatsMutex.Lock()
|
a.containerStatsMutex.Lock()
|
||||||
defer containerStatsMutex.Unlock()
|
defer a.containerStatsMutex.Unlock()
|
||||||
|
|
||||||
// add empty values if they doesn't exist in map
|
// add empty values if they doesn't exist in map
|
||||||
stats, initialized := containerStatsMap[ctr.IdShort]
|
stats, initialized := containerStatsMap[ctr.IdShort]
|
||||||
@@ -301,14 +291,14 @@ func (a *Agent) getContainerStats(ctr *container.Container) (*container.Containe
|
|||||||
}
|
}
|
||||||
|
|
||||||
// delete container stats from map using mutex
|
// delete container stats from map using mutex
|
||||||
func deleteContainerStatsSync(id string) {
|
func (a *Agent) deleteContainerStatsSync(id string) {
|
||||||
containerStatsMutex.Lock()
|
a.containerStatsMutex.Lock()
|
||||||
defer containerStatsMutex.Unlock()
|
defer a.containerStatsMutex.Unlock()
|
||||||
delete(containerStatsMap, id)
|
delete(containerStatsMap, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Agent) gatherStats() *system.SystemData {
|
func (a *Agent) gatherStats() *system.SystemData {
|
||||||
systemInfo, systemStats := getSystemStats()
|
systemInfo, systemStats := a.getSystemStats()
|
||||||
stats := &system.SystemData{
|
stats := &system.SystemData{
|
||||||
Stats: systemStats,
|
Stats: systemStats,
|
||||||
Info: systemInfo,
|
Info: systemInfo,
|
||||||
@@ -346,13 +336,13 @@ func (a *Agent) startServer(addr string, pubKey []byte) {
|
|||||||
func (a *Agent) Run() {
|
func (a *Agent) Run() {
|
||||||
|
|
||||||
if filesystem, exists := os.LookupEnv("FILESYSTEM"); exists {
|
if filesystem, exists := os.LookupEnv("FILESYSTEM"); exists {
|
||||||
diskIoStats.Filesystem = filesystem
|
a.diskIoStats.Filesystem = filesystem
|
||||||
} else {
|
} else {
|
||||||
diskIoStats.Filesystem = findDefaultFilesystem()
|
a.diskIoStats.Filesystem = findDefaultFilesystem()
|
||||||
}
|
}
|
||||||
|
|
||||||
initializeDiskIoStats()
|
a.initializeDiskIoStats()
|
||||||
initializeNetIoStats()
|
a.initializeNetIoStats()
|
||||||
|
|
||||||
a.startServer(a.port, a.pubKey)
|
a.startServer(a.port, a.pubKey)
|
||||||
}
|
}
|
||||||
@@ -395,17 +385,17 @@ func skipNetworkInterface(v *psutilNet.IOCountersStat) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializeDiskIoStats() {
|
func (a *Agent) initializeDiskIoStats() {
|
||||||
if io, err := disk.IOCounters(diskIoStats.Filesystem); err == nil {
|
if io, err := disk.IOCounters(a.diskIoStats.Filesystem); err == nil {
|
||||||
for _, d := range io {
|
for _, d := range io {
|
||||||
diskIoStats.Time = time.Now()
|
a.diskIoStats.Time = time.Now()
|
||||||
diskIoStats.Read = d.ReadBytes
|
a.diskIoStats.Read = d.ReadBytes
|
||||||
diskIoStats.Write = d.WriteBytes
|
a.diskIoStats.Write = d.WriteBytes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializeNetIoStats() {
|
func (a *Agent) initializeNetIoStats() {
|
||||||
if netIO, err := psutilNet.IOCounters(true); err == nil {
|
if netIO, err := psutilNet.IOCounters(true); err == nil {
|
||||||
bytesSent := uint64(0)
|
bytesSent := uint64(0)
|
||||||
bytesRecv := uint64(0)
|
bytesRecv := uint64(0)
|
||||||
@@ -417,9 +407,9 @@ func initializeNetIoStats() {
|
|||||||
bytesSent += v.BytesSent
|
bytesSent += v.BytesSent
|
||||||
bytesRecv += v.BytesRecv
|
bytesRecv += v.BytesRecv
|
||||||
}
|
}
|
||||||
netIoStats.BytesSent = bytesSent
|
a.netIoStats.BytesSent = bytesSent
|
||||||
netIoStats.BytesRecv = bytesRecv
|
a.netIoStats.BytesRecv = bytesRecv
|
||||||
netIoStats.Time = time.Now()
|
a.netIoStats.Time = time.Now()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user