watchdog-go: disk情報を取得できるように
/ test (pull_request) Successful in 1s Details
/ build-docker-image (pull_request) Has been skipped Details

This commit is contained in:
usbharu 2024-03-13 10:54:28 +09:00
parent 2e24ec71aa
commit fa6ca8f717
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
2 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,81 @@
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
}

View File

@ -2,6 +2,7 @@ package watchdog
import ( import (
"fmt" "fmt"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host" "github.com/shirou/gopsutil/v3/host"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -37,6 +38,8 @@ func Run() {
NewUsedCpu(config), NewUsedCpu(config),
} }
collectors = append(collectors, diskCollector(config)...)
collectorMap := map[time.Duration][]MetricsCollector{} collectorMap := map[time.Duration][]MetricsCollector{}
for _, collector := range collectors { for _, collector := range collectors {
@ -106,3 +109,18 @@ func buildConfig() Config {
ClientDomain: load.Section("Watch").Key("domain").MustString(hostinfo.Hostname), ClientDomain: load.Section("Watch").Key("domain").MustString(hostinfo.Hostname),
} }
} }
func diskCollector(config Config) []MetricsCollector {
partitions, err := disk.Partitions(true)
if err != nil {
log.Println(err)
return []MetricsCollector{}
}
var collectors []MetricsCollector
for _, partition := range partitions {
collectors = append(collectors, NewUsedDisk(config, partition))
collectors = append(collectors, NewTotalDisk(config, partition))
}
return collectors
}