mirror of
https://github.com/owncast/owncast.git
synced 2024-11-27 17:59:21 +03:00
08bda166fe
* Replace `github.com/shirou/gopsutil/v3` to v4 PR #3925 only added the new v4 dependency into our `go.mod` file. The actual usages in the source code were not updated to use the new v4 version. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> * Tidy `go.mod` correctly One `require` section for direct dependencies; Another `require` section for indirect dependencies. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> --------- Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package metrics
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/shirou/gopsutil/v4/cpu"
|
|
"github.com/shirou/gopsutil/v4/disk"
|
|
"github.com/shirou/gopsutil/v4/mem"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Max number of metrics we want to keep.
|
|
const maxCollectionValues = 300
|
|
|
|
func collectCPUUtilization() {
|
|
if len(metrics.CPUUtilizations) > maxCollectionValues {
|
|
metrics.CPUUtilizations = metrics.CPUUtilizations[1:]
|
|
}
|
|
|
|
v, err := cpu.Percent(0, false)
|
|
if err != nil {
|
|
log.Errorln(err)
|
|
return
|
|
}
|
|
|
|
// Default to zero but try to use the cumulative values of all the CPUs
|
|
// if values exist.
|
|
value := 0.0
|
|
if len(v) > 0 {
|
|
value = v[0]
|
|
}
|
|
|
|
metricValue := TimestampedValue{time.Now(), value}
|
|
metrics.CPUUtilizations = append(metrics.CPUUtilizations, metricValue)
|
|
cpuUsage.Set(metricValue.Value)
|
|
}
|
|
|
|
func collectRAMUtilization() {
|
|
if len(metrics.RAMUtilizations) > maxCollectionValues {
|
|
metrics.RAMUtilizations = metrics.RAMUtilizations[1:]
|
|
}
|
|
|
|
memoryUsage, _ := mem.VirtualMemory()
|
|
metricValue := TimestampedValue{time.Now(), memoryUsage.UsedPercent}
|
|
metrics.RAMUtilizations = append(metrics.RAMUtilizations, metricValue)
|
|
}
|
|
|
|
func collectDiskUtilization() {
|
|
path := "./"
|
|
diskUse, _ := disk.Usage(path)
|
|
|
|
if len(metrics.DiskUtilizations) > maxCollectionValues {
|
|
metrics.DiskUtilizations = metrics.DiskUtilizations[1:]
|
|
}
|
|
|
|
metricValue := TimestampedValue{time.Now(), diskUse.UsedPercent}
|
|
metrics.DiskUtilizations = append(metrics.DiskUtilizations, metricValue)
|
|
}
|