Skip to content

Commit

Permalink
agent(secondary)의 리소스 상태 수집 (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaehoon Kim committed Mar 31, 2021
1 parent 5c0fe59 commit aac07ac
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 319 deletions.
38 changes: 12 additions & 26 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,51 +1,37 @@
module github.com/Klevry/klevr

go 1.15
go 1.16

require (
github.com/NexClipper/logger v1.1.6
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/fanliao/go-promise v0.0.0-20141029170127-1890db352a72
github.com/gin-gonic/gin v1.6.3
github.com/go-logr/logr v0.2.1 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/go-openapi/spec v0.20.3 // indirect
github.com/go-playground/validator/v10 v10.3.0 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/golang/snappy v0.0.2 // indirect
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
github.com/gorilla/context v1.1.1
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-retryablehttp v0.6.8
github.com/jasonlvhit/gocron v0.0.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/mackerelio/go-osstat v0.1.0
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6
github.com/orcaman/concurrent-map v0.0.0-20210106121528-16402b402231
github.com/pkg/errors v0.9.1
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.20.12+incompatible
github.com/shirou/gopsutil v3.21.2+incompatible
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/streadway/amqp v1.0.0
github.com/stretchr/testify v1.6.1
github.com/swaggo/http-swagger v0.0.0-20200308142732-58ac5e232fba
github.com/stretchr/testify v1.7.0
github.com/swaggo/http-swagger v1.0.0
github.com/swaggo/swag v1.7.0
github.com/ugorji/go v1.1.8 // indirect
github.com/urfave/cli/v2 v2.3.0
golang.org/x/net v0.0.0-20210222171744-9060382bd457 // indirect
golang.org/x/sys v0.0.0-20210223212115-eede4237b368 // indirect
golang.org/x/tools v0.1.0 // indirect
google.golang.org/grpc v1.27.0
google.golang.org/protobuf v1.25.0
google.golang.org/grpc v1.36.0
google.golang.org/protobuf v1.26.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
k8s.io/apimachinery v0.19.2
k8s.io/klog/v2 v2.3.0 // indirect
k8s.io/apimachinery v0.20.5
sigs.k8s.io/yaml v1.2.0
xorm.io/builder v0.3.7
xorm.io/xorm v1.0.5
xorm.io/builder v0.3.9
xorm.io/xorm v1.0.7
)
195 changes: 77 additions & 118 deletions go.sum

Large diffs are not rendered by default.

34 changes: 29 additions & 5 deletions pkg/agent/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"encoding/json"
"net"
"runtime"
"time"

"github.com/Klevry/klevr/pkg/common"
"github.com/mackerelio/go-osstat/memory"

"github.com/NexClipper/logger"
"google.golang.org/grpc"
Expand All @@ -22,7 +24,7 @@ type server struct {
agentKey string
}

func (s *server) SendTask(ctx context.Context, in *pb.Message) (*pb.Message, error) {
func (s server) SendTask(ctx context.Context, in *pb.Message) (*pb.Message, error) {
logger.Debugf("Receive message body from client: %v", string(in.Task))

var t common.KlevrTask
Expand All @@ -42,10 +44,24 @@ func (s *server) SendTask(ctx context.Context, in *pb.Message) (*pb.Message, err
return &pb.Message{Task: b}, nil
}

func (s *server) StatusCheck(ctx context.Context, in *pb.Status) (*pb.Status, error) {
func (s server) StatusCheck(ctx context.Context, in *pb.Status) (*pb.Status, error) {
logger.Debugf("Receive message body from client: %v", in.Status)

return &pb.Status{Status: "OK"}, nil
disk := DiskUsage("/")
memory, _ := memory.Get()

agentStatus := &common.AgentStatus{
AgentKey: s.agentKey,
Resource: &common.Resource{
Core: runtime.NumCPU(),
Memory: int(memory.Total / MB),
Disk: int(disk.All / MB),
},
}

b := JsonMarshal(agentStatus)

return &pb.Status{Status: b}, nil
}

func (agent *KlevrAgent) SecondaryServer() {
Expand All @@ -71,7 +87,7 @@ func (agent *KlevrAgent) SecondaryServer() {

grpcServer := grpc.NewServer()

pb.RegisterTaskSendServer(grpcServer, &server{agentKey: agent.AgentKey})
pb.RegisterTaskSendServer(grpcServer, server{agentKey: agent.AgentKey})

if err := grpcServer.Serve(agent.connect); err != nil {
logger.Fatalf("failed to serve: %s", err)
Expand Down Expand Up @@ -112,8 +128,16 @@ func (agent *KlevrAgent) PrimaryStatusCheck() {
ctx, _ := context.WithTimeout(context.Background(), time.Second)
c := pb.NewTaskSendClient(conn)

_, resErr := c.StatusCheck(ctx, &pb.Status{})
s, resErr := c.StatusCheck(ctx, &pb.Status{})
if resErr == nil {
var agentStatus common.AgentStatus
json.Unmarshal(s.Status, &agentStatus)

logger.Debugf("AgentStatus: %v", agentStatus)

agent.Agents[i].Core = agentStatus.Core
agent.Agents[i].Memory = agentStatus.Memory
agent.Agents[i].Disk = agentStatus.Disk
agent.Agents[i].LastAliveCheckTime = &common.JSONTime{Time: time.Now().UTC()}
agent.Agents[i].IsActive = true
} else {
Expand Down
Loading

0 comments on commit aac07ac

Please sign in to comment.