From c9bc100bcd63f40a102c1b8168341add984cf767 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 11 Mar 2024 20:12:34 +0900 Subject: [PATCH 1/4] =?UTF-8?q?watchdog-go:=20Swap=E7=AD=89=E3=82=92?= =?UTF-8?q?=E7=9B=A3=E8=A6=96=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- watchdog-go/main.go | 107 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 97 insertions(+), 10 deletions(-) diff --git a/watchdog-go/main.go b/watchdog-go/main.go index 3e0df84..d7e2980 100644 --- a/watchdog-go/main.go +++ b/watchdog-go/main.go @@ -50,14 +50,22 @@ func main() { go func() { for { - metricArray := getMetrics(config) - for _, metric := range metricArray { + for _, metric := range perMinute(config) { metrics <- metric } time.Sleep(1 * time.Minute) } }() + go func() { + for { + for _, metric := range perDay(config) { + metrics <- metric + } + time.Sleep(24 * time.Hour) + } + }() + for metric := range metrics { _, err := client.Push(context.Background(), &metric) if err != nil { @@ -67,19 +75,32 @@ func main() { } } -func getMetrics(config Config) []watchdog.Metric { - v, _ := mem.VirtualMemory() - - return []watchdog.Metric{usedMemory(*v, config)} +func perMinute(config Config) []watchdog.Metric { + return []watchdog.Metric{ + *usedMemory(config), + *usedSwap(config), + } } -func usedMemory(stat mem.VirtualMemoryStat, config Config) watchdog.Metric { +func perDay(config Config) []watchdog.Metric { + return []watchdog.Metric{ + *totalMem(config), + *totalSwap(config), + } +} + +func usedMemory(config Config) *watchdog.Metric { + memory, err := mem.VirtualMemory() + if err != nil { + log.Println(err) + return nil + } name := config.ClientName + " Used Memory" - usedMemory := fmt.Sprintf("%g", stat.UsedPercent) + usedMemory := fmt.Sprintf("%g", memory.UsedPercent) - log.Printf("Mem: %s%%\n", usedMemory) + log.Printf("Used Mem: %s%%\n", usedMemory) - return watchdog.Metric{ + return &watchdog.Metric{ Name: name, ObjectId: toId(name), Domain: config.ClientDomain, @@ -90,6 +111,72 @@ func usedMemory(stat mem.VirtualMemoryStat, config Config) watchdog.Metric { } } +func usedSwap(config Config) *watchdog.Metric { + memory, err := mem.VirtualMemory() + if err != nil { + log.Println(err) + return nil + } + name := config.ClientName + " Used Swap" + usedSwap := fmt.Sprintf("%g", memory.UsedPercent) + + log.Printf("Used Swap: %s%%\n", usedSwap) + + return &watchdog.Metric{ + Name: name, + ObjectId: toId(name), + Domain: config.ClientDomain, + Status: watchdog.Status_OK, + Value: usedSwap, + Timestamp: timestamppb.Now(), + Message: "", + } +} + +func totalMem(config Config) *watchdog.Metric { + memory, err := mem.VirtualMemory() + if err != nil { + log.Println(err) + return nil + } + name := config.ClientName + " Total Mem" + totalMemory := fmt.Sprintf("%d", memory.Total) + + log.Printf("Total Mem: %s%%\n", totalMemory) + + return &watchdog.Metric{ + Name: name, + ObjectId: toId(name), + Domain: config.ClientDomain, + Status: watchdog.Status_OK, + Value: totalMemory, + Timestamp: timestamppb.Now(), + Message: "", + } +} + +func totalSwap(config Config) *watchdog.Metric { + memory, err := mem.SwapMemory() + if err != nil { + log.Println(err) + return nil + } + name := config.ClientName + " Total Swap" + totalMemory := fmt.Sprintf("%d", memory.Total) + + log.Printf("Total Swap: %s%%\n", totalMemory) + + return &watchdog.Metric{ + Name: name, + ObjectId: toId(name), + Domain: config.ClientDomain, + Status: watchdog.Status_OK, + Value: totalMemory, + Timestamp: timestamppb.Now(), + Message: "", + } +} + func toId(value string) string { return regex.ReplaceAllString(strings.ReplaceAll(value, " ", "-"), "") } From 6cdcb9e7828ad722709f22cc843d8c1742b6b6b4 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 11 Mar 2024 21:39:36 +0900 Subject: [PATCH 2/4] =?UTF-8?q?watchdog-go:=20=E6=A7=8B=E9=80=A0=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- watchdog-go/Taskfile.yml | 2 +- watchdog-go/go.mod | 6 +- watchdog-go/go.sum | 7 + watchdog-go/main.go | 177 +----------------- watchdog-go/watchdog/MemoryCollector.go | 141 ++++++++++++++ watchdog-go/watchdog/config.go | 7 + watchdog-go/watchdog/metrics_collector.go | 8 + .../unos => }/watchdog/push_metrics.pb.go | 0 .../watchdog/push_metrics_grpc.pb.go | 0 watchdog-go/watchdog/watchdog_go.go | 98 ++++++++++ 10 files changed, 267 insertions(+), 179 deletions(-) create mode 100644 watchdog-go/watchdog/MemoryCollector.go create mode 100644 watchdog-go/watchdog/config.go create mode 100644 watchdog-go/watchdog/metrics_collector.go rename watchdog-go/{git.usbharu.dev/usbharu/unos => }/watchdog/push_metrics.pb.go (100%) rename watchdog-go/{git.usbharu.dev/usbharu/unos => }/watchdog/push_metrics_grpc.pb.go (100%) create mode 100644 watchdog-go/watchdog/watchdog_go.go diff --git a/watchdog-go/Taskfile.yml b/watchdog-go/Taskfile.yml index 26f3188..b409399 100644 --- a/watchdog-go/Taskfile.yml +++ b/watchdog-go/Taskfile.yml @@ -13,6 +13,6 @@ tasks: generate: cmds: - - protoc -I ../unos-proto/src/main/proto --go_out=. ../unos-proto/src/main/proto/watchdog/push_metrics.proto --go_opt=module=$prefix --go-grpc_out=. + - protoc -I ../unos-proto/src/main/proto --go_out=. ../unos-proto/src/main/proto/watchdog/push_metrics.proto --go_opt=module=git.usbharu.dev/usbharu/unos --go-grpc_opt=module=git.usbharu.dev/usbharu/unos --go-grpc_out=. diff --git a/watchdog-go/go.mod b/watchdog-go/go.mod index 94d755e..9e660f1 100644 --- a/watchdog-go/go.mod +++ b/watchdog-go/go.mod @@ -10,10 +10,10 @@ require ( ) require ( - github.com/go-ole/go-ole v1.2.6 // indirect + github.com/go-ole/go-ole v1.3.0 // indirect github.com/golang/protobuf v1.5.4 // indirect - github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect - github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect + github.com/lufia/plan9stats v0.0.0-20240226150601-1dcf7310316a // indirect + github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect github.com/stretchr/testify v1.9.0 // indirect github.com/tklauser/go-sysconf v0.3.13 // indirect github.com/tklauser/numcpus v0.7.0 // indirect diff --git a/watchdog-go/go.sum b/watchdog-go/go.sum index ade3532..3a3992d 100644 --- a/watchdog-go/go.sum +++ b/watchdog-go/go.sum @@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= +github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= @@ -11,10 +13,14 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= +github.com/lufia/plan9stats v0.0.0-20240226150601-1dcf7310316a h1:3Bm7EwfUQUvhNeKIkUct/gl9eod1TcXuj8stxvi/GoI= +github.com/lufia/plan9stats v0.0.0-20240226150601-1dcf7310316a/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= +github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU= +github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= github.com/shirou/gopsutil/v3 v3.24.2 h1:kcR0erMbLg5/3LcInpw0X/rrPSqq4CDPyI6A6ZRC18Y= github.com/shirou/gopsutil/v3 v3.24.2/go.mod h1:tSg/594BcA+8UdQU2XcW803GWYgdtauFFPgJCJKZlVk= github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ= @@ -39,6 +45,7 @@ golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/watchdog-go/main.go b/watchdog-go/main.go index d7e2980..4215a98 100644 --- a/watchdog-go/main.go +++ b/watchdog-go/main.go @@ -1,182 +1,9 @@ package main import ( - "context" - "fmt" - "git.usbharu.dev/usbharu/unos/watchdog-go/git.usbharu.dev/usbharu/unos/watchdog" - "github.com/shirou/gopsutil/v3/mem" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" - "google.golang.org/protobuf/types/known/timestamppb" - "gopkg.in/ini.v1" - "log" - "regexp" - "strings" - "time" + "git.usbharu.dev/usbharu/unos/watchdog-go/watchdog" ) -type Config struct { - Url string - ClientName string - ClientDomain string -} - -var regex = regexp.MustCompile(`[^a-zA-Z0-9-_]`) - func main() { - 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("watchdog.internal"), - } - - dial, err := grpc.Dial(config.Url, grpc.WithTransportCredentials(insecure.NewCredentials())) - if err != nil { - log.Fatal(err) - return - } - - metrics := make(chan watchdog.Metric) - - client := watchdog.NewPushMetricsServiceClient(dial) - - go func() { - for { - for _, metric := range perMinute(config) { - metrics <- metric - } - time.Sleep(1 * time.Minute) - } - }() - - go func() { - for { - for _, metric := range perDay(config) { - metrics <- metric - } - time.Sleep(24 * time.Hour) - } - }() - - for metric := range metrics { - _, err := client.Push(context.Background(), &metric) - if err != nil { - log.Println(err) - continue - } - } -} - -func perMinute(config Config) []watchdog.Metric { - return []watchdog.Metric{ - *usedMemory(config), - *usedSwap(config), - } -} - -func perDay(config Config) []watchdog.Metric { - return []watchdog.Metric{ - *totalMem(config), - *totalSwap(config), - } -} - -func usedMemory(config Config) *watchdog.Metric { - memory, err := mem.VirtualMemory() - if err != nil { - log.Println(err) - return nil - } - name := config.ClientName + " Used Memory" - usedMemory := fmt.Sprintf("%g", memory.UsedPercent) - - log.Printf("Used Mem: %s%%\n", usedMemory) - - return &watchdog.Metric{ - Name: name, - ObjectId: toId(name), - Domain: config.ClientDomain, - Status: watchdog.Status_OK, - Value: usedMemory, - Timestamp: timestamppb.Now(), - Message: "", - } -} - -func usedSwap(config Config) *watchdog.Metric { - memory, err := mem.VirtualMemory() - if err != nil { - log.Println(err) - return nil - } - name := config.ClientName + " Used Swap" - usedSwap := fmt.Sprintf("%g", memory.UsedPercent) - - log.Printf("Used Swap: %s%%\n", usedSwap) - - return &watchdog.Metric{ - Name: name, - ObjectId: toId(name), - Domain: config.ClientDomain, - Status: watchdog.Status_OK, - Value: usedSwap, - Timestamp: timestamppb.Now(), - Message: "", - } -} - -func totalMem(config Config) *watchdog.Metric { - memory, err := mem.VirtualMemory() - if err != nil { - log.Println(err) - return nil - } - name := config.ClientName + " Total Mem" - totalMemory := fmt.Sprintf("%d", memory.Total) - - log.Printf("Total Mem: %s%%\n", totalMemory) - - return &watchdog.Metric{ - Name: name, - ObjectId: toId(name), - Domain: config.ClientDomain, - Status: watchdog.Status_OK, - Value: totalMemory, - Timestamp: timestamppb.Now(), - Message: "", - } -} - -func totalSwap(config Config) *watchdog.Metric { - memory, err := mem.SwapMemory() - if err != nil { - log.Println(err) - return nil - } - name := config.ClientName + " Total Swap" - totalMemory := fmt.Sprintf("%d", memory.Total) - - log.Printf("Total Swap: %s%%\n", totalMemory) - - return &watchdog.Metric{ - Name: name, - ObjectId: toId(name), - Domain: config.ClientDomain, - Status: watchdog.Status_OK, - Value: totalMemory, - Timestamp: timestamppb.Now(), - Message: "", - } -} - -func toId(value string) string { - return regex.ReplaceAllString(strings.ReplaceAll(value, " ", "-"), "") + watchdog.Run() } diff --git a/watchdog-go/watchdog/MemoryCollector.go b/watchdog-go/watchdog/MemoryCollector.go new file mode 100644 index 0000000..4cbc930 --- /dev/null +++ b/watchdog-go/watchdog/MemoryCollector.go @@ -0,0 +1,141 @@ +package watchdog + +import ( + "fmt" + "github.com/shirou/gopsutil/v3/mem" + "google.golang.org/protobuf/types/known/timestamppb" + "log" + "time" +) + +type UsedMemory struct { + config Config +} + +func NewUsedMemory(config Config) MetricsCollector { + return &UsedMemory{ + config, + } +} + +func (m UsedMemory) Collect() (*Metric, error) { + memory, err := mem.VirtualMemory() + if err != nil { + return nil, err + } + name := m.config.ClientName + " Used Memory" + usedMemory := fmt.Sprintf("%g", memory.UsedPercent) + + return &Metric{ + Name: name, + ObjectId: toId(name), + Domain: m.config.ClientDomain, + Status: Status_OK, + Value: usedMemory, + Timestamp: timestamppb.Now(), + Message: "", + }, nil +} + +func (m UsedMemory) Timer() time.Duration { + return 1 * time.Minute +} + +type UsedSwap struct { + config Config +} + +func NewUsedSwap(config Config) MetricsCollector { + return &UsedSwap{ + config: config, + } +} + +func (m UsedSwap) Collect() (*Metric, error) { + memory, err := mem.SwapMemory() + if err != nil { + return nil, err + } + name := m.config.ClientName + " Used Swap" + usedMemory := fmt.Sprintf("%g", memory.UsedPercent) + + return &Metric{ + Name: name, + ObjectId: toId(name), + Domain: m.config.ClientDomain, + Status: Status_OK, + Value: usedMemory, + Timestamp: timestamppb.Now(), + Message: "", + }, nil +} + +func (m UsedSwap) Timer() time.Duration { + return 1 * time.Minute +} + +type TotalMemory struct { + config Config +} + +func NewTotalMemory(config Config) MetricsCollector { + return &TotalMemory{config: config} +} + +func (t TotalMemory) Collect() (*Metric, error) { + memory, err := mem.VirtualMemory() + if err != nil { + log.Println(err) + return nil, nil + } + name := t.config.ClientName + " Total Mem" + totalMemory := fmt.Sprintf("%d", memory.Total) + + log.Printf("Total Mem: %s%%\n", totalMemory) + + return &Metric{ + Name: name, + ObjectId: toId(name), + Domain: t.config.ClientDomain, + Status: Status_OK, + Value: totalMemory, + Timestamp: timestamppb.Now(), + Message: "", + }, nil +} + +func (t TotalMemory) Timer() time.Duration { + return 24 * time.Hour +} + +type TotalSwap struct { + config Config +} + +func NewTotalSwap(config Config) MetricsCollector { + return &TotalSwap{config: config} +} + +func (t TotalSwap) Collect() (*Metric, error) { + memory, err := mem.SwapMemory() + if err != nil { + log.Println(err) + return nil, nil + } + name := t.config.ClientName + " Total Swap" + totalMemory := fmt.Sprintf("%d", memory.Total) + + return &Metric{ + Name: name, + ObjectId: toId(name), + Domain: t.config.ClientDomain, + Status: Status_OK, + Value: totalMemory, + Timestamp: timestamppb.Now(), + Message: "", + }, nil +} + +func (t TotalSwap) Timer() time.Duration { + return 24 * time.Hour +} diff --git a/watchdog-go/watchdog/config.go b/watchdog-go/watchdog/config.go new file mode 100644 index 0000000..c08f7fa --- /dev/null +++ b/watchdog-go/watchdog/config.go @@ -0,0 +1,7 @@ +package watchdog + +type Config struct { + Url string + ClientName string + ClientDomain string +} diff --git a/watchdog-go/watchdog/metrics_collector.go b/watchdog-go/watchdog/metrics_collector.go new file mode 100644 index 0000000..2de694f --- /dev/null +++ b/watchdog-go/watchdog/metrics_collector.go @@ -0,0 +1,8 @@ +package watchdog + +import "time" + +type MetricsCollector interface { + Collect() (*Metric, error) + Timer() time.Duration +} diff --git a/watchdog-go/git.usbharu.dev/usbharu/unos/watchdog/push_metrics.pb.go b/watchdog-go/watchdog/push_metrics.pb.go similarity index 100% rename from watchdog-go/git.usbharu.dev/usbharu/unos/watchdog/push_metrics.pb.go rename to watchdog-go/watchdog/push_metrics.pb.go diff --git a/watchdog-go/git.usbharu.dev/usbharu/unos/watchdog/push_metrics_grpc.pb.go b/watchdog-go/watchdog/push_metrics_grpc.pb.go similarity index 100% rename from watchdog-go/git.usbharu.dev/usbharu/unos/watchdog/push_metrics_grpc.pb.go rename to watchdog-go/watchdog/push_metrics_grpc.pb.go diff --git a/watchdog-go/watchdog/watchdog_go.go b/watchdog-go/watchdog/watchdog_go.go new file mode 100644 index 0000000..6f0c741 --- /dev/null +++ b/watchdog-go/watchdog/watchdog_go.go @@ -0,0 +1,98 @@ +package watchdog + +import ( + "fmt" + "golang.org/x/net/context" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "gopkg.in/ini.v1" + "log" + "regexp" + "strings" + "time" +) + +var regex = regexp.MustCompile(`[^a-zA-Z0-9-_]`) + +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"), + } + + dial, err := grpc.Dial(config.Url, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + log.Fatal(err) + return + } + + client := NewPushMetricsServiceClient(dial) + + collectors := []MetricsCollector{ + NewUsedMemory(config), + NewUsedSwap(config), + NewTotalMemory(config), + NewTotalSwap(config), + } + + collectorMap := map[time.Duration][]MetricsCollector{} + + for _, collector := range collectors { + timer := collector.Timer() + + metricsCollectors, ok := collectorMap[timer] + + if ok { + collectorMap[timer] = append(metricsCollectors, collector) + } else { + collectorMap[timer] = []MetricsCollector{collector} + } + + } + + metrics := make(chan Metric) + + for duration, metricsCollectors := range collectorMap { + + duration := duration + metricsCollectors := metricsCollectors + go func() { + for { + + for _, collector := range metricsCollectors { + + collect, err := collector.Collect() + if err != nil { + continue + } + log.Printf("%s: %s", collect.Name, collect.Value) + metrics <- *collect + } + + time.Sleep(duration) + } + }() + } + + for metric := range metrics { + _, err := client.Push(context.Background(), &metric) + if err != nil { + log.Println(err) + continue + } + } +} + +func toId(value string) string { + return regex.ReplaceAllString(strings.ReplaceAll(value, " ", "-"), "") +} 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 3/4] =?UTF-8?q?watchdog-go:=20cpu=E6=83=85=E5=A0=B1?= =?UTF-8?q?=E3=82=92=E5=8F=96=E5=BE=97=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=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), + } +} From fa6ca8f7175e3d6db34706e361e448bade201280 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:54:28 +0900 Subject: [PATCH 4/4] =?UTF-8?q?watchdog-go:=20disk=E6=83=85=E5=A0=B1?= =?UTF-8?q?=E3=82=92=E5=8F=96=E5=BE=97=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- watchdog-go/watchdog/disk_collector.go | 81 ++++++++++++++++++++++++++ watchdog-go/watchdog/watchdog_go.go | 18 ++++++ 2 files changed, 99 insertions(+) create mode 100644 watchdog-go/watchdog/disk_collector.go diff --git a/watchdog-go/watchdog/disk_collector.go b/watchdog-go/watchdog/disk_collector.go new file mode 100644 index 0000000..f95c655 --- /dev/null +++ b/watchdog-go/watchdog/disk_collector.go @@ -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 +} diff --git a/watchdog-go/watchdog/watchdog_go.go b/watchdog-go/watchdog/watchdog_go.go index b6cc742..165d3aa 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/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 +}