2020-08-27 10:37:32 +03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2022-03-07 19:45:49 +03:00
|
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
|
|
|
"github.com/shirou/gopsutil/v3/disk"
|
|
|
|
"github.com/shirou/gopsutil/v3/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.
|
2022-03-25 09:06:47 +03:00
|
|
|
const maxCollectionValues = 300
|
2020-08-27 10:37:32 +03:00
|
|
|
|
|
|
|
func collectCPUUtilization() {
|
2022-03-17 03:34:44 +03:00
|
|
|
if len(metrics.CPUUtilizations) > maxCollectionValues {
|
|
|
|
metrics.CPUUtilizations = metrics.CPUUtilizations[1:]
|
2020-08-27 10:37:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-09 20:49:17 +03:00
|
|
|
// 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}
|
2022-03-17 03:34:44 +03:00
|
|
|
metrics.CPUUtilizations = append(metrics.CPUUtilizations, metricValue)
|
|
|
|
cpuUsage.Set(metricValue.Value)
|
2020-08-27 10:37:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func collectRAMUtilization() {
|
2022-03-17 03:34:44 +03:00
|
|
|
if len(metrics.RAMUtilizations) > maxCollectionValues {
|
|
|
|
metrics.RAMUtilizations = metrics.RAMUtilizations[1:]
|
2020-08-27 10:37:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
memoryUsage, _ := mem.VirtualMemory()
|
2022-03-17 03:34:44 +03:00
|
|
|
metricValue := TimestampedValue{time.Now(), memoryUsage.UsedPercent}
|
|
|
|
metrics.RAMUtilizations = append(metrics.RAMUtilizations, metricValue)
|
2020-08-27 10:37:32 +03:00
|
|
|
}
|
2020-10-02 22:18:08 +03:00
|
|
|
|
|
|
|
func collectDiskUtilization() {
|
|
|
|
path := "./"
|
|
|
|
diskUse, _ := disk.Usage(path)
|
|
|
|
|
2022-03-17 03:34:44 +03:00
|
|
|
if len(metrics.DiskUtilizations) > maxCollectionValues {
|
|
|
|
metrics.DiskUtilizations = metrics.DiskUtilizations[1:]
|
2020-10-02 22:18:08 +03:00
|
|
|
}
|
|
|
|
|
2022-03-17 03:34:44 +03:00
|
|
|
metricValue := TimestampedValue{time.Now(), diskUse.UsedPercent}
|
|
|
|
metrics.DiskUtilizations = append(metrics.DiskUtilizations, metricValue)
|
2020-10-02 22:18:08 +03:00
|
|
|
}
|