nezha/cmd/agent/main.go

51 lines
1.1 KiB
Go
Raw Normal View History

2019-12-05 09:36:58 -05:00
package main
import (
"context"
2019-12-07 05:14:40 -05:00
"fmt"
2019-12-05 09:36:58 -05:00
"log"
2019-12-07 05:14:40 -05:00
"time"
2019-12-05 09:36:58 -05:00
"google.golang.org/grpc"
2019-12-05 10:42:20 -05:00
pb "github.com/p14yground/nezha/proto"
2019-12-07 05:14:40 -05:00
"github.com/p14yground/nezha/service/monitor"
2019-12-08 03:59:58 -05:00
"github.com/p14yground/nezha/service/rpc"
2019-12-05 09:36:58 -05:00
)
func main() {
2019-12-08 03:59:58 -05:00
auth := rpc.AuthHandler{
2019-12-05 09:36:58 -05:00
AppKey: "naiba",
2019-12-07 05:14:40 -05:00
AppSecret: "123456",
2019-12-05 09:36:58 -05:00
}
conn, err := grpc.Dial(":5555", grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth))
if err != nil {
panic(err)
}
defer conn.Close()
2019-12-05 10:42:20 -05:00
client := pb.NewNezhaServiceClient(conn)
2019-12-07 05:14:40 -05:00
ctx := context.Background()
resp, err := client.Register(ctx, monitor.GetHost().PB())
if err != nil {
log.Printf("client.Register err: %v", err)
}
log.Printf("Register resp: %s", resp)
hc, err := client.Heartbeat(ctx, &pb.Beat{
Timestamp: fmt.Sprintf("%v", time.Now()),
})
if err != nil {
log.Printf("client.Register err: %v", err)
}
log.Printf("Register resp: %s", hc)
2019-12-05 09:36:58 -05:00
2019-12-05 10:42:20 -05:00
for i := 0; i < 3; i++ {
2019-12-07 05:14:40 -05:00
resp, err := client.ReportState(ctx, monitor.GetState(3).PB())
2019-12-05 10:42:20 -05:00
if err != nil {
2019-12-07 05:14:40 -05:00
log.Printf("client.ReportState err: %v", err)
2019-12-05 10:42:20 -05:00
}
2019-12-07 05:14:40 -05:00
log.Printf("ReportState resp: %s", resp)
2019-12-05 10:42:20 -05:00
}
2019-12-05 09:36:58 -05:00
}