Documentation
¶
Overview ¶
Package grpc provides the gRPC server implementation for Agent communication. grpc 包提供用于 Agent 通信的 gRPC 服务器实现。
Index ¶
- Constants
- Variables
- func SetClusterNodeProvider(provider ClusterNodeProvider)
- func SetDiagnosticsService(svc *diagnostics.Service)
- func SetMonitorService(svc MonitorService)
- type ClusterNodeProvider
- type MonitorService
- type NodeWithMonitorConfig
- type Server
- func (s *Server) CommandStream(stream grpc.BidiStreamingServer[pb.CommandResponse, pb.CommandRequest]) error
- func (s *Server) GetDiagnosticsLogCursors(ctx context.Context, req *pb.DiagnosticsCursorRequest) (*pb.DiagnosticsCursorResponse, error)
- func (s *Server) GetPort() int
- func (s *Server) HandleDiscoverClusters(ctx context.Context, agentID string, params map[string]string) (*pb.CommandResponse, error)
- func (s *Server) HandleProcessEventReport(ctx context.Context, agentID string, report *pb.ProcessEventReport) error
- func (s *Server) HandleUpdateMonitorConfig(ctx context.Context, agentID string, config *monitor.MonitorConfig, ...) error
- func (s *Server) Heartbeat(ctx context.Context, req *pb.HeartbeatRequest) (*pb.HeartbeatResponse, error)
- func (s *Server) IsRunning() bool
- func (s *Server) LogStream(stream grpc.ClientStreamingServer[pb.LogEntry, pb.LogStreamResponse]) error
- func (s *Server) Register(ctx context.Context, req *pb.RegisterRequest) (*pb.RegisterResponse, error)
- func (s *Server) Start(ctx context.Context) error
- func (s *Server) Stop()
- type ServerConfig
Constants ¶
const ( // DefaultGRPCPort is the default port for gRPC server. // DefaultGRPCPort 是 gRPC 服务器的默认端口。 DefaultGRPCPort = 17890 // DefaultMaxRecvMsgSize is the default maximum receive message size (16MB). // DefaultMaxRecvMsgSize 是默认的最大接收消息大小(16MB)。 DefaultMaxRecvMsgSize = 16 * 1024 * 1024 // DefaultMaxSendMsgSize is the default maximum send message size (16MB). // DefaultMaxSendMsgSize 是默认的最大发送消息大小(16MB)。 DefaultMaxSendMsgSize = 16 * 1024 * 1024 // DefaultHeartbeatInterval is the default heartbeat interval for Agent configuration. // DefaultHeartbeatInterval 是 Agent 配置的默认心跳间隔。 DefaultHeartbeatInterval = 10 )
Default configuration values for gRPC server gRPC 服务器的默认配置值
Variables ¶
var ( // ErrServerNotRunning indicates the server is not running. // ErrServerNotRunning 表示服务器未运行。 ErrServerNotRunning = errors.New("grpc: server is not running") // ErrServerAlreadyRunning indicates the server is already running. // ErrServerAlreadyRunning 表示服务器已在运行。 ErrServerAlreadyRunning = errors.New("grpc: server is already running") // ErrInvalidTLSConfig indicates invalid TLS configuration. // ErrInvalidTLSConfig 表示无效的 TLS 配置。 ErrInvalidTLSConfig = errors.New("grpc: invalid TLS configuration") )
Errors for gRPC server operations gRPC 服务器操作的错误定义
Functions ¶
func SetClusterNodeProvider ¶
func SetClusterNodeProvider(provider ClusterNodeProvider)
SetClusterNodeProvider sets the cluster node provider for gRPC handlers. SetClusterNodeProvider 设置 gRPC 处理器的集群节点提供者。
func SetDiagnosticsService ¶
func SetDiagnosticsService(svc *diagnostics.Service)
SetDiagnosticsService sets the diagnostics service for gRPC handlers. SetDiagnosticsService 设置 gRPC 处理器的诊断服务。
func SetMonitorService ¶
func SetMonitorService(svc MonitorService)
SetMonitorService sets the monitor service for gRPC handlers. SetMonitorService 设置 gRPC 处理器的监控服务。
Types ¶
type ClusterNodeProvider ¶
type ClusterNodeProvider interface {
GetNodeByHostAndInstallDirAndRole(ctx context.Context, hostID uint, installDir, role string) (clusterID, nodeID uint, found bool, err error)
// GetNodesByHostID returns all nodes on a specific host with their cluster's monitor config.
// GetNodesByHostID 返回特定主机上的所有节点及其集群的监控配置。
GetNodesByHostID(ctx context.Context, hostID uint) ([]*NodeWithMonitorConfig, error)
// UpdateNodeProcessStatus updates the process PID and status for a node.
// UpdateNodeProcessStatus 更新节点的进程 PID 和状态。
UpdateNodeProcessStatus(ctx context.Context, nodeID uint, pid int, status string) error
// RefreshClusterStatusFromNodes recalculates cluster status from its nodes (e.g. after heartbeat).
// RefreshClusterStatusFromNodes 根据节点状态重新计算集群状态(如心跳更新节点后)。
RefreshClusterStatusFromNodes(ctx context.Context, clusterID uint)
// GetClusterNodeDisplayInfo returns cluster name and node display "主机名 - 角色" for audit resource name.
// GetClusterNodeDisplayInfo 返回集群名及节点展示「主机名 - 角色」,用于审计资源名称。
GetClusterNodeDisplayInfo(ctx context.Context, clusterID, nodeID uint) (clusterName, nodeDisplay string)
}
ClusterNodeProvider provides cluster node information. ClusterNodeProvider 提供集群节点信息。
type MonitorService ¶
type MonitorService interface {
GetConfig(ctx context.Context, clusterID uint) (*monitor.MonitorConfig, error)
RecordEventFromReport(ctx context.Context, clusterID, nodeID, hostID uint, eventType monitor.ProcessEventType, pid int, processName, installDir, role string, details map[string]string) error
}
MonitorService provides monitor-related operations for gRPC handlers. MonitorService 为 gRPC 处理器提供监控相关操作。
type NodeWithMonitorConfig ¶
type NodeWithMonitorConfig struct {
ClusterID uint `json:"cluster_id"`
NodeID uint `json:"node_id"`
InstallDir string `json:"install_dir"`
Role string `json:"role"`
ProcessPID int `json:"process_pid"`
MonitorConfig *monitor.MonitorConfig `json:"monitor_config"`
}
NodeWithMonitorConfig represents a node with its cluster's monitor config. NodeWithMonitorConfig 表示带有集群监控配置的节点。
type Server ¶
type Server struct {
pb.UnimplementedAgentServiceServer
// contains filtered or unexported fields
}
Server represents the gRPC server for Agent communication. Server 表示用于 Agent 通信的 gRPC 服务器。 Requirements: 8.1 - Implements gRPC server with TLS support and interceptors.
func NewServer ¶
func NewServer(config *ServerConfig, agentManager *agent.Manager, hostService *host.Service, auditRepo *audit.Repository, logger *zap.Logger) *Server
NewServer creates a new gRPC server instance. NewServer 创建一个新的 gRPC 服务器实例。 Requirements: 8.1 - Creates gRPC server with configuration.
func (*Server) CommandStream ¶
func (s *Server) CommandStream(stream grpc.BidiStreamingServer[pb.CommandResponse, pb.CommandRequest]) error
CommandStream handles bidirectional streaming for command dispatch and result reporting. CommandStream 处理用于命令分发和结果上报的双向流。 Requirements: 1.5, 8.6 - Implements bidirectional stream for command dispatching.
func (*Server) GetDiagnosticsLogCursors ¶
func (s *Server) GetDiagnosticsLogCursors(ctx context.Context, req *pb.DiagnosticsCursorRequest) (*pb.DiagnosticsCursorResponse, error)
GetDiagnosticsLogCursors returns all diagnostics log cursors for a given agent. GetDiagnosticsLogCursors 返回某个 Agent 的所有诊断日志游标。
func (*Server) HandleDiscoverClusters ¶
func (s *Server) HandleDiscoverClusters(ctx context.Context, agentID string, params map[string]string) (*pb.CommandResponse, error)
HandleDiscoverClusters handles DISCOVER_CLUSTERS command. HandleDiscoverClusters 处理 DISCOVER_CLUSTERS 命令。 Requirements: 1.3, 1.7 - Trigger agent discovery Task 11.1
func (*Server) HandleProcessEventReport ¶
func (s *Server) HandleProcessEventReport(ctx context.Context, agentID string, report *pb.ProcessEventReport) error
HandleProcessEventReport handles ProcessEventReport from agent. HandleProcessEventReport 处理来自 Agent 的 ProcessEventReport。 Requirements: 3.4, 3.5 - Receive and process process events Task 11.3
func (*Server) HandleUpdateMonitorConfig ¶
func (s *Server) HandleUpdateMonitorConfig(ctx context.Context, agentID string, config *monitor.MonitorConfig, processes []*monitor.TrackedProcessInfo) error
HandleUpdateMonitorConfig handles UPDATE_MONITOR_CONFIG command. HandleUpdateMonitorConfig 处理 UPDATE_MONITOR_CONFIG 命令。 Requirements: 5.4, 7.5 - Push monitor config to agent Task 11.2
func (*Server) Heartbeat ¶
func (s *Server) Heartbeat(ctx context.Context, req *pb.HeartbeatRequest) (*pb.HeartbeatResponse, error)
Heartbeat handles Agent heartbeat requests. Heartbeat 处理 Agent 心跳请求。 Requirements: 1.3, 3.3 - Processes heartbeat, updates host resource usage.
func (*Server) LogStream ¶
func (s *Server) LogStream(stream grpc.ClientStreamingServer[pb.LogEntry, pb.LogStreamResponse]) error
LogStream handles log streaming from Agents. LogStream 处理来自 Agent 的日志流。 Requirements: 10.2, 10.3 - Receives Agent logs and stores to audit log.
func (*Server) Register ¶
func (s *Server) Register(ctx context.Context, req *pb.RegisterRequest) (*pb.RegisterResponse, error)
Register handles Agent registration requests. Register 处理 Agent 注册请求。 Requirements: 1.1, 3.2 - Handles Agent registration, matches host IP, updates Agent status.
type ServerConfig ¶
type ServerConfig struct {
// Port is the port number for the gRPC server.
// Port 是 gRPC 服务器的端口号。
Port int
// TLSEnabled indicates whether TLS is enabled.
// TLSEnabled 表示是否启用 TLS。
TLSEnabled bool
// CertFile is the path to the TLS certificate file.
// CertFile 是 TLS 证书文件的路径。
CertFile string
// KeyFile is the path to the TLS key file.
// KeyFile 是 TLS 密钥文件的路径。
KeyFile string
// CAFile is the path to the CA certificate file for client verification.
// CAFile 是用于客户端验证的 CA 证书文件路径。
CAFile string
// MaxRecvMsgSize is the maximum receive message size in bytes.
// MaxRecvMsgSize 是最大接收消息大小(字节)。
MaxRecvMsgSize int
// MaxSendMsgSize is the maximum send message size in bytes.
// MaxSendMsgSize 是最大发送消息大小(字节)。
MaxSendMsgSize int
// HeartbeatInterval is the heartbeat interval to send to Agents (seconds).
// HeartbeatInterval 是发送给 Agent 的心跳间隔(秒)。
HeartbeatInterval int
}
ServerConfig holds configuration for the gRPC server. ServerConfig 保存 gRPC 服务器的配置。 Requirements: 8.1 - Configures gRPC server options including TLS and interceptors.