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), + } +}