owncast/metrics/hardware.go
Eng Zer Jun 08bda166fe
Replace github.com/shirou/gopsutil/v3 with v4 (#3956)
* 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>
2024-10-11 12:13:33 -07:00

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)
}