mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 12:40:07 +00:00
gRPC 上报系统信息
This commit is contained in:
@@ -2,33 +2,21 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
pb "github.com/p14yground/nezha/proto"
|
||||
"github.com/p14yground/nezha/service/handler"
|
||||
"github.com/p14yground/nezha/service/monitor"
|
||||
)
|
||||
|
||||
// Auth ..
|
||||
type Auth struct {
|
||||
AppKey string
|
||||
AppSecret string
|
||||
}
|
||||
|
||||
// GetRequestMetadata ..
|
||||
func (a *Auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
|
||||
return map[string]string{"app_key": a.AppKey, "app_secret": a.AppSecret}, nil
|
||||
}
|
||||
|
||||
// RequireTransportSecurity ..
|
||||
func (a *Auth) RequireTransportSecurity() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func main() {
|
||||
auth := Auth{
|
||||
auth := handler.AuthHandler{
|
||||
AppKey: "naiba",
|
||||
AppSecret: "nbsecret",
|
||||
AppSecret: "123456",
|
||||
}
|
||||
conn, err := grpc.Dial(":5555", grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth))
|
||||
if err != nil {
|
||||
@@ -36,13 +24,27 @@ func main() {
|
||||
}
|
||||
defer conn.Close()
|
||||
client := pb.NewNezhaServiceClient(conn)
|
||||
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)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
resp, err := client.ReportState(context.Background(), &pb.State{})
|
||||
resp, err := client.ReportState(ctx, monitor.GetState(3).PB())
|
||||
if err != nil {
|
||||
log.Fatalf("client.Search err: %v", err)
|
||||
log.Printf("client.ReportState err: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("resp: %s", resp)
|
||||
log.Printf("ReportState resp: %s", resp)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
pb "github.com/p14yground/nezha/proto"
|
||||
"github.com/p14yground/nezha/service/handler"
|
||||
)
|
||||
|
||||
// NezhaService ..
|
||||
type NezhaService struct {
|
||||
auth *Auth
|
||||
}
|
||||
|
||||
// ReportState ..
|
||||
func (s *NezhaService) ReportState(ctx context.Context, r *pb.State) (*pb.Receipt, error) {
|
||||
if err := s.auth.Check(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Printf("receive: %s\n", r)
|
||||
return &pb.Receipt{}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
server := grpc.NewServer()
|
||||
pb.RegisterNezhaServiceServer(server, &NezhaService{})
|
||||
pb.RegisterNezhaServiceServer(server, &handler.NezhaHandler{
|
||||
Auth: &handler.AuthHandler{
|
||||
AppKey: "naiba",
|
||||
AppSecret: "123456",
|
||||
},
|
||||
})
|
||||
|
||||
lis, err := net.Listen("tcp", ":5555")
|
||||
if err != nil {
|
||||
@@ -38,44 +25,3 @@ func main() {
|
||||
|
||||
server.Serve(lis)
|
||||
}
|
||||
|
||||
// Auth ..
|
||||
type Auth struct {
|
||||
appKey string
|
||||
appSecret string
|
||||
}
|
||||
|
||||
// Check ..
|
||||
func (a *Auth) Check(ctx context.Context) error {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
return status.Errorf(codes.Unauthenticated, "metadata.FromIncomingContext err")
|
||||
}
|
||||
|
||||
var (
|
||||
appKey string
|
||||
appSecret string
|
||||
)
|
||||
if value, ok := md["app_key"]; ok {
|
||||
appKey = value[0]
|
||||
}
|
||||
if value, ok := md["app_secret"]; ok {
|
||||
appSecret = value[0]
|
||||
}
|
||||
|
||||
if appKey != a.GetAppKey() || appSecret != a.GetAppSecret() {
|
||||
return status.Errorf(codes.Unauthenticated, "invalid token")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAppKey ..
|
||||
func (a *Auth) GetAppKey() string {
|
||||
return "naiba"
|
||||
}
|
||||
|
||||
// GetAppSecret ..
|
||||
func (a *Auth) GetAppSecret() string {
|
||||
return "nbsecret"
|
||||
}
|
||||
|
||||
@@ -1,53 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
"github.com/shirou/gopsutil/disk"
|
||||
"github.com/shirou/gopsutil/host"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
"github.com/shirou/gopsutil/net"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Host info
|
||||
hi, _ := host.Info()
|
||||
fmt.Printf("「HostInfo」 platform:%v platformVersion:%v kernelArch:%v virtualizationSystem:%v uptime:%v boottime:%v\n", hi.OS, hi.PlatformVersion, hi.KernelArch, hi.VirtualizationSystem, hi.Uptime, hi.BootTime)
|
||||
// Memory
|
||||
mv, _ := mem.VirtualMemory()
|
||||
ms, _ := mem.SwapMemory()
|
||||
fmt.Printf("「VirtualMemory」 Total: %v, Free:%v, UsedPercent:%f%%\n", mv.Total, mv.Free, mv.UsedPercent)
|
||||
fmt.Printf("「SwapMemory」 Total: %v, Free:%v, UsedPercent:%f%%\n", ms.Total, ms.Free, ms.UsedPercent)
|
||||
// Disk
|
||||
dparts, _ := disk.Partitions(false)
|
||||
for _, part := range dparts {
|
||||
fmt.Printf("「Disk」 %v\n", part)
|
||||
u, _ := disk.Usage(part.Mountpoint)
|
||||
fmt.Println("\t" + u.Path + "\t" + strconv.FormatFloat(u.UsedPercent, 'f', 2, 64) + "% full.")
|
||||
fmt.Println("\t\tTotal: " + strconv.FormatUint(u.Total/1024/1024/1024, 10) + " GiB")
|
||||
fmt.Println("\t\tFree: " + strconv.FormatUint(u.Free/1024/1024/1024, 10) + " GiB")
|
||||
fmt.Println("\t\tUsed: " + strconv.FormatUint(u.Used/1024/1024/1024, 10) + " GiB")
|
||||
}
|
||||
// CPU
|
||||
go func() {
|
||||
cp, _ := cpu.Percent(time.Second*2, false)
|
||||
ci, _ := cpu.Info()
|
||||
for i := 0; i < len(ci); i++ {
|
||||
fmt.Printf("「CPU」 %v core:%v step:%v", ci[i].ModelName, ci[i].Cores, ci[i].Stepping)
|
||||
}
|
||||
fmt.Printf(" percentIn2sec:%v%%\n", cp[0])
|
||||
}()
|
||||
// Network
|
||||
nc, _ := net.IOCounters(true)
|
||||
for _, ni := range nc {
|
||||
fmt.Printf("「Net」%v\n", ni)
|
||||
}
|
||||
select {}
|
||||
|
||||
}
|
||||
|
||||
func cmdExec() {
|
||||
|
||||
Reference in New Issue
Block a user