diff --git a/API_Device_RemoteMgmt_InterfaceTable.md b/API_Device_RemoteMgmt_InterfaceTable.md index ab04f83..19e3252 100644 --- a/API_Device_RemoteMgmt_InterfaceTable.md +++ b/API_Device_RemoteMgmt_InterfaceTable.md @@ -242,7 +242,11 @@ Response 200:`{"ok":true}` > > agent 启动 media-server 时会显式传入:`--config `,因此不依赖 media-server 内部默认配置。 > -> 路径说明:`agent.media_server_process` 下的 `exec_path/work_dir/configs_dir/pid_file` 支持绝对或相对路径;若为相对路径,则以 agent 启动参数 `--config ` 的**配置文件所在目录**为基准解析。 +> 路径说明: +> - `agent.media_server_process` 下的 `exec_path/work_dir/configs_dir/pid_file` 支持绝对或相对路径;若为相对路径,则以 agent 启动参数 `--config ` 的**配置文件所在目录**为基准解析。 +> - `agent.config_path` 建议使用**绝对路径**;若使用相对路径: +> - `PUT /v1/config` 写盘时,以 **agent 进程当前工作目录(CWD)** 为基准; +> - 通过 `/v1/media-server/start|restart`(缺省 config)启动时,会把该相对路径原样传给 media-server,最终以 **media_server_process.work_dir** 为基准解析。 ### 6.1 `POST /v1/media-server/start` 用途:启动本机 media-server(若已运行则幂等返回 ok;若已运行但 config 不同则 409)。 @@ -307,6 +311,20 @@ Response 200: - 501:未启用进程控制 - 500:停止失败 +### 6.4 `GET /v1/media-server/status` +用途:查询本机 media-server 是否在运行(以 agent 的 pidfile 记录为准)。 + +**Auth**:读接口(见 0.2) + +Response 200: +```json +{"ok":true,"running":true,"pid":1234,"config_path":"/etc/rk3588sys/config.json"} +``` + +失败: +- 501:未启用进程控制 +- 500:读取 pidfile/进程存活检测失败 + ## 7. 只读代理接口(agent 对外,推荐管理端统一走 agent) ### 7.1 `GET /v1/graphs` diff --git a/agent/internal/httpapi/server.go b/agent/internal/httpapi/server.go index 273efdf..dcc77d2 100644 --- a/agent/internal/httpapi/server.go +++ b/agent/internal/httpapi/server.go @@ -77,6 +77,7 @@ func New(agentCfg config.AgentConfig, baseDir string, ms *mediaserver.Client, st mux.HandleFunc("/v1/media-server/start", s.handleMediaStart) mux.HandleFunc("/v1/media-server/restart", s.handleMediaRestart) mux.HandleFunc("/v1/media-server/stop", s.handleMediaStop) + mux.HandleFunc("/v1/media-server/status", s.handleMediaStatus) mux.HandleFunc("/v1/graphs", s.handleGraphs) mux.HandleFunc("/v1/graphs/", s.handleGraphDetail) mux.HandleFunc("/v1/logs/recent", s.handleLogsRecent) @@ -384,6 +385,31 @@ func (s *Server) handleMediaStop(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, map[string]any{"ok": true, "running": st.Running, "pid": st.Pid, "config_path": st.ConfigPath}) } +func (s *Server) handleMediaStatus(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + errorJSON(w, http.StatusMethodNotAllowed, "method not allowed") + return + } + if !s.authorize(r, false) { + errorJSON(w, http.StatusUnauthorized, "unauthorized") + return + } + if s.proc == nil || !s.proc.Enabled() { + errorJSON(w, http.StatusNotImplemented, "not supported") + return + } + st, err := s.proc.Status() + if err != nil { + if errors.Is(err, procctl.ErrNotSupported) { + errorJSON(w, http.StatusNotImplemented, "not supported") + return + } + errorJSON(w, http.StatusInternalServerError, "internal error: "+err.Error()) + return + } + writeJSON(w, http.StatusOK, map[string]any{"ok": true, "running": st.Running, "pid": st.Pid, "config_path": st.ConfigPath}) +} + func readOptionalJSON[T any](w http.ResponseWriter, r *http.Request, maxBytes int64) (T, error) { var zero T if r.Body == nil { diff --git a/agent/internal/procctl/procctl.go b/agent/internal/procctl/procctl.go index 5a337c6..8feb648 100644 --- a/agent/internal/procctl/procctl.go +++ b/agent/internal/procctl/procctl.go @@ -58,6 +58,29 @@ func New(agentCfg config.AgentConfig, baseDir string) *Controller { func (c *Controller) Enabled() bool { return c != nil && c.proc.Enable } +func (c *Controller) Status() (Status, error) { + c.mu.Lock() + defer c.mu.Unlock() + + pf, err := c.readPidFile() + if err != nil { + return Status{}, err + } + if pf == nil { + return Status{Running: false}, nil + } + + alive, aerr := isAlive(pf.Pid) + if errors.Is(aerr, ErrNotSupported) { + return Status{}, ErrNotSupported + } + if !alive { + _ = os.Remove(c.proc.PidFile) + return Status{Running: false, Pid: pf.Pid, ConfigPath: pf.ConfigPath}, nil + } + return Status{Running: true, Pid: pf.Pid, ConfigPath: pf.ConfigPath}, nil +} + func (c *Controller) Start(configName string) (Status, error) { c.mu.Lock() defer c.mu.Unlock() diff --git a/agent/rk3588-agent b/agent/rk3588-agent index 0e11dd1..8d3c43d 100644 Binary files a/agent/rk3588-agent and b/agent/rk3588-agent differ