mirror of
https://github.com/owncast/owncast.git
synced 2024-11-28 19:19:06 +03:00
e042c85f88
* Add metrics for disk usage * Add admin API for hardware metrics * Fix error message alert
48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package metrics
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/shirou/gopsutil/cpu"
|
|
"github.com/shirou/gopsutil/disk"
|
|
"github.com/shirou/gopsutil/mem"
|
|
)
|
|
|
|
// Max number of metrics we want to keep.
|
|
const maxCollectionValues = 500
|
|
|
|
func collectCPUUtilization() {
|
|
if len(Metrics.CPUUtilizations) > maxCollectionValues {
|
|
Metrics.CPUUtilizations = Metrics.CPUUtilizations[1:]
|
|
}
|
|
|
|
v, err := cpu.Percent(0, false)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
metricValue := timestampedValue{time.Now(), int(v[0])}
|
|
Metrics.CPUUtilizations = append(Metrics.CPUUtilizations, metricValue)
|
|
}
|
|
|
|
func collectRAMUtilization() {
|
|
if len(Metrics.RAMUtilizations) > maxCollectionValues {
|
|
Metrics.RAMUtilizations = Metrics.RAMUtilizations[1:]
|
|
}
|
|
|
|
memoryUsage, _ := mem.VirtualMemory()
|
|
metricValue := timestampedValue{time.Now(), int(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(), int(diskUse.UsedPercent)}
|
|
Metrics.DiskUtilizations = append(Metrics.DiskUtilizations, metricValue)
|
|
}
|