watchdog-go #2
|
@ -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
|
||||
}
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue