watchdog-go: 構造化
This commit is contained in:
parent
c9bc100bcd
commit
6cdcb9e782
|
@ -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=.
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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=
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package watchdog
|
||||
|
||||
type Config struct {
|
||||
Url string
|
||||
ClientName string
|
||||
ClientDomain string
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package watchdog
|
||||
|
||||
import "time"
|
||||
|
||||
type MetricsCollector interface {
|
||||
Collect() (*Metric, error)
|
||||
Timer() time.Duration
|
||||
}
|
|
@ -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, " ", "-"), "")
|
||||
}
|
Loading…
Reference in New Issue