4c3da2704f
* Add CPU and RAM usage alerting * Create basic troubleshooting document to point alerts at * Limit max number of hardware values collected * Save metric value with the point in time it was taken
36 lines
841 B
Go
36 lines
841 B
Go
package metrics
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/shirou/gopsutil/cpu"
|
|
"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 := value{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 := value{time.Now(), int(memoryUsage.UsedPercent)}
|
|
Metrics.RAMUtilizations = append(Metrics.RAMUtilizations, metricValue)
|
|
}
|