watchdog-go: cpu情報を取得できるように

This commit is contained in:
usbharu 2024-03-13 01:50:50 +09:00
parent 6cdcb9e782
commit 2e24ec71aa
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
3 changed files with 73 additions and 12 deletions

View File

@ -0,0 +1,51 @@
package watchdog
import (
"fmt"
"github.com/shirou/gopsutil/v3/cpu"
"google.golang.org/protobuf/types/known/timestamppb"
"time"
)
type UsedCPU struct {
config Config
}
func NewUsedCpu(config Config) MetricsCollector {
return &UsedCPU{
config: config,
}
}
func (u UsedCPU) Collect() (*Metric, error) {
coreCounts, err := cpu.Counts(false)
if err != nil {
return nil, err
}
threadCounts, err := cpu.Counts(true)
if err != nil {
return nil, err
}
name := u.config.ClientName + " Used CPU"
percent, err := cpu.Percent(100+time.Millisecond, false)
if err != nil {
return nil, err
}
usedCpu := fmt.Sprintf("%g", percent[0])
cpuCore := fmt.Sprintf("%d Core %d Thread", coreCounts, threadCounts)
return &Metric{
Name: name,
ObjectId: toId(name),
Domain: u.config.ClientDomain,
Status: Status_OK,
Value: usedCpu,
Timestamp: timestamppb.Now(),
Message: cpuCore,
}, nil
}
func (u UsedCPU) Timer() time.Duration {
return 1 * time.Minute
}

View File

@ -2,6 +2,7 @@ package watchdog
import ( import (
"fmt" "fmt"
"github.com/shirou/gopsutil/v3/host"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
@ -18,17 +19,7 @@ func Run() {
fmt.Println("start gRPC Client.") fmt.Println("start gRPC Client.")
load, err := ini.Load("config.ini") config := buildConfig()
if err != nil {
log.Fatal(err)
return
}
config := Config{
Url: load.Section("Parent").Key("url").String(),
ClientName: load.Section("Watch").Key("name").MustString("Watch Dog Go"),
ClientDomain: load.Section("Watch").Key("domain").MustString("internal"),
}
dial, err := grpc.Dial(config.Url, grpc.WithTransportCredentials(insecure.NewCredentials())) dial, err := grpc.Dial(config.Url, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil { if err != nil {
@ -43,6 +34,7 @@ func Run() {
NewUsedSwap(config), NewUsedSwap(config),
NewTotalMemory(config), NewTotalMemory(config),
NewTotalSwap(config), NewTotalSwap(config),
NewUsedCpu(config),
} }
collectorMap := map[time.Duration][]MetricsCollector{} collectorMap := map[time.Duration][]MetricsCollector{}
@ -75,7 +67,7 @@ func Run() {
if err != nil { if err != nil {
continue continue
} }
log.Printf("%s: %s", collect.Name, collect.Value) log.Printf("%s: %s message: %s", collect.Name, collect.Value, collect.Message)
metrics <- *collect metrics <- *collect
} }
@ -96,3 +88,21 @@ func Run() {
func toId(value string) string { func toId(value string) string {
return regex.ReplaceAllString(strings.ReplaceAll(value, " ", "-"), "") return regex.ReplaceAllString(strings.ReplaceAll(value, " ", "-"), "")
} }
func buildConfig() Config {
load, err := ini.Load("config.ini")
if err != nil {
log.Fatal(err)
}
hostinfo, err := host.Info()
if err != nil {
log.Fatal(err)
}
return Config{
Url: load.Section("Parent").Key("url").String(),
ClientName: load.Section("Watch").Key("name").MustString(hostinfo.Hostname),
ClientDomain: load.Section("Watch").Key("domain").MustString(hostinfo.Hostname),
}
}