unos/watchdog-go/watchdog/disk_collector.go

82 lines
1.6 KiB
Go
Raw Permalink Normal View History

package watchdog
import (
"fmt"
"github.com/shirou/gopsutil/v3/disk"
"google.golang.org/protobuf/types/known/timestamppb"
"time"
)
type UsedDisk struct {
config Config
partitionStat disk.PartitionStat
}
func NewUsedDisk(config Config, stat disk.PartitionStat) MetricsCollector {
return &UsedDisk{
config: config,
partitionStat: stat,
}
}
func (u UsedDisk) Collect() (*Metric, error) {
usage, err := disk.Usage(u.partitionStat.Mountpoint)
if err != nil {
return nil, err
}
name := u.config.ClientName + " Used Disk " + u.partitionStat.Device
usedDisk := fmt.Sprintf("%g", usage.UsedPercent)
return &Metric{
Name: name,
ObjectId: toId(name),
Domain: u.config.ClientDomain,
Status: Status_OK,
Value: usedDisk,
Timestamp: timestamppb.Now(),
Message: "",
}, nil
}
func (u UsedDisk) Timer() time.Duration {
return 1 * time.Hour
}
type TotalDisk struct {
Config
disk.PartitionStat
}
func NewTotalDisk(config Config, stat disk.PartitionStat) MetricsCollector {
return &TotalDisk{
Config: config,
PartitionStat: stat,
}
}
func (t TotalDisk) Collect() (*Metric, error) {
usage, err := disk.Usage(t.Mountpoint)
if err != nil {
return nil, err
}
name := t.ClientName + " Total Disk " + t.Device
totalDisk := fmt.Sprintf("%d", usage.Total)
return &Metric{
Name: name,
ObjectId: toId(name),
Domain: t.ClientDomain,
Status: Status_OK,
Value: totalDisk,
Timestamp: timestamppb.Now(),
Message: "",
}, nil
}
func (t TotalDisk) Timer() time.Duration {
return 24 * time.Hour
}