2020-08-27 10:37:32 +03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/cpu"
|
2020-10-02 22:18:08 +03:00
|
|
|
"github.com/shirou/gopsutil/disk"
|
2020-08-27 10:37:32 +03:00
|
|
|
"github.com/shirou/gopsutil/mem"
|
2020-12-22 06:42:47 +03:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-08-27 10:37:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2020-12-22 06:42:47 +03:00
|
|
|
log.Errorln(err)
|
|
|
|
return
|
2020-08-27 10:37:32 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 10:06:14 +03:00
|
|
|
metricValue := timestampedValue{time.Now(), int(v[0])}
|
2020-08-27 10:37:32 +03:00
|
|
|
Metrics.CPUUtilizations = append(Metrics.CPUUtilizations, metricValue)
|
2022-03-07 04:26:52 +03:00
|
|
|
cpuUsage.Set(float64(metricValue.Value))
|
2020-08-27 10:37:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func collectRAMUtilization() {
|
|
|
|
if len(Metrics.RAMUtilizations) > maxCollectionValues {
|
|
|
|
Metrics.RAMUtilizations = Metrics.RAMUtilizations[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
memoryUsage, _ := mem.VirtualMemory()
|
2020-10-02 10:06:14 +03:00
|
|
|
metricValue := timestampedValue{time.Now(), int(memoryUsage.UsedPercent)}
|
2020-08-27 10:37:32 +03:00
|
|
|
Metrics.RAMUtilizations = append(Metrics.RAMUtilizations, metricValue)
|
|
|
|
}
|
2020-10-02 22:18:08 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|