2020-08-27 09:37:32 +02:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2022-03-07 17:45:49 +01:00
|
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
|
|
|
"github.com/shirou/gopsutil/v3/disk"
|
|
|
|
"github.com/shirou/gopsutil/v3/mem"
|
2020-12-22 04:42:47 +01:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-08-27 09:37:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Max number of metrics we want to keep.
|
2022-03-25 07:06:47 +01:00
|
|
|
const maxCollectionValues = 300
|
2020-08-27 09:37:32 +02:00
|
|
|
|
|
|
|
func collectCPUUtilization() {
|
2022-03-17 01:34:44 +01:00
|
|
|
if len(metrics.CPUUtilizations) > maxCollectionValues {
|
|
|
|
metrics.CPUUtilizations = metrics.CPUUtilizations[1:]
|
2020-08-27 09:37:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
v, err := cpu.Percent(0, false)
|
|
|
|
if err != nil {
|
2020-12-22 04:42:47 +01:00
|
|
|
log.Errorln(err)
|
|
|
|
return
|
2020-08-27 09:37:32 +02:00
|
|
|
}
|
|
|
|
|
2022-03-17 01:34:44 +01:00
|
|
|
metricValue := TimestampedValue{time.Now(), v[0]}
|
|
|
|
metrics.CPUUtilizations = append(metrics.CPUUtilizations, metricValue)
|
|
|
|
cpuUsage.Set(metricValue.Value)
|
2020-08-27 09:37:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func collectRAMUtilization() {
|
2022-03-17 01:34:44 +01:00
|
|
|
if len(metrics.RAMUtilizations) > maxCollectionValues {
|
|
|
|
metrics.RAMUtilizations = metrics.RAMUtilizations[1:]
|
2020-08-27 09:37:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memoryUsage, _ := mem.VirtualMemory()
|
2022-03-17 01:34:44 +01:00
|
|
|
metricValue := TimestampedValue{time.Now(), memoryUsage.UsedPercent}
|
|
|
|
metrics.RAMUtilizations = append(metrics.RAMUtilizations, metricValue)
|
2020-08-27 09:37:32 +02:00
|
|
|
}
|
2020-10-02 21:18:08 +02:00
|
|
|
|
|
|
|
func collectDiskUtilization() {
|
|
|
|
path := "./"
|
|
|
|
diskUse, _ := disk.Usage(path)
|
|
|
|
|
2022-03-17 01:34:44 +01:00
|
|
|
if len(metrics.DiskUtilizations) > maxCollectionValues {
|
|
|
|
metrics.DiskUtilizations = metrics.DiskUtilizations[1:]
|
2020-10-02 21:18:08 +02:00
|
|
|
}
|
|
|
|
|
2022-03-17 01:34:44 +01:00
|
|
|
metricValue := TimestampedValue{time.Now(), diskUse.UsedPercent}
|
|
|
|
metrics.DiskUtilizations = append(metrics.DiskUtilizations, metricValue)
|
2020-10-02 21:18:08 +02:00
|
|
|
}
|