watchdog-go #2
|
@ -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
|
||||
}
|
|
@ -2,6 +2,7 @@ package watchdog
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/shirou/gopsutil/v3/disk"
|
||||
"github.com/shirou/gopsutil/v3/host"
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
|
@ -37,6 +38,8 @@ func Run() {
|
|||
NewUsedCpu(config),
|
||||
}
|
||||
|
||||
collectors = append(collectors, diskCollector(config)...)
|
||||
|
||||
collectorMap := map[time.Duration][]MetricsCollector{}
|
||||
|
||||
for _, collector := range collectors {
|
||||
|
@ -106,3 +109,18 @@ func buildConfig() Config {
|
|||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue