From 2e24ec71aa57ba56d827a3c9399719861d42c4fd Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 13 Mar 2024 01:50:50 +0900 Subject: [PATCH] =?UTF-8?q?watchdog-go:=20cpu=E6=83=85=E5=A0=B1=E3=82=92?= =?UTF-8?q?=E5=8F=96=E5=BE=97=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- watchdog-go/watchdog/cpu_collector.go | 51 +++++++++++++++++++ ...MemoryCollector.go => memory_collector.go} | 0 watchdog-go/watchdog/watchdog_go.go | 34 ++++++++----- 3 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 watchdog-go/watchdog/cpu_collector.go rename watchdog-go/watchdog/{MemoryCollector.go => memory_collector.go} (100%) diff --git a/watchdog-go/watchdog/cpu_collector.go b/watchdog-go/watchdog/cpu_collector.go new file mode 100644 index 0000000..bae054e --- /dev/null +++ b/watchdog-go/watchdog/cpu_collector.go @@ -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 +} diff --git a/watchdog-go/watchdog/MemoryCollector.go b/watchdog-go/watchdog/memory_collector.go similarity index 100% rename from watchdog-go/watchdog/MemoryCollector.go rename to watchdog-go/watchdog/memory_collector.go diff --git a/watchdog-go/watchdog/watchdog_go.go b/watchdog-go/watchdog/watchdog_go.go index 6f0c741..b6cc742 100644 --- a/watchdog-go/watchdog/watchdog_go.go +++ b/watchdog-go/watchdog/watchdog_go.go @@ -2,6 +2,7 @@ package watchdog import ( "fmt" + "github.com/shirou/gopsutil/v3/host" "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -18,17 +19,7 @@ func Run() { fmt.Println("start gRPC Client.") - load, err := ini.Load("config.ini") - 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"), - } + config := buildConfig() dial, err := grpc.Dial(config.Url, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { @@ -43,6 +34,7 @@ func Run() { NewUsedSwap(config), NewTotalMemory(config), NewTotalSwap(config), + NewUsedCpu(config), } collectorMap := map[time.Duration][]MetricsCollector{} @@ -75,7 +67,7 @@ func Run() { if err != nil { continue } - log.Printf("%s: %s", collect.Name, collect.Value) + log.Printf("%s: %s message: %s", collect.Name, collect.Value, collect.Message) metrics <- *collect } @@ -96,3 +88,21 @@ func Run() { func toId(value string) string { 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), + } +}