/
zhendosina
/
obshell
Обзор
Документация
Войти
/
zhendosina
/
obshell
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
ob/agent/api/info_handler.go
170 строк
5 KB
Junkrat77
PullRequest: 801 optz: prefer querying ob version from database over binary
17 апр 2026, 06:37
17 апр 2026, 06:37
9a158f5
Код
Авторство
О чём код?
/* * Copyright (c) 2024 OceanBase. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package api import ( "time" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" "github.com/oceanbase/obshell/ob/agent/api/common" "github.com/oceanbase/obshell/ob/agent/config" "github.com/oceanbase/obshell/ob/agent/constant" "github.com/oceanbase/obshell/ob/agent/global" "github.com/oceanbase/obshell/ob/agent/lib/binary" "github.com/oceanbase/obshell/ob/agent/lib/http" "github.com/oceanbase/obshell/ob/agent/meta" "github.com/oceanbase/obshell/ob/agent/repository/db/oceanbase" "github.com/oceanbase/obshell/ob/agent/secure" "github.com/oceanbase/obshell/ob/agent/service/task" ) var localTaskService = task.NewLocalTaskService() // TimeHandler returns the current time // // @ID getTime // @Summary get current time // @Description get current time // @Tags v1 // @Accept application/json // @Produce application/json // @Success 200 object http.OcsAgentResponse{data=time.Time} // @Router /api/v1/time [get] func TimeHandler(c *gin.Context) { common.SendResponse(c, time.Now(), nil) } // InfoHandler returns the agent info // // @ID getAgentInfo // @Summary get agent info // @Description get agent info // @Tags v1 // @Accept application/json // @Produce application/json // @Success 200 object http.OcsAgentResponse{data=meta.AgentStatus} // @Router /api/v1/info [get] func InfoHandler(s *http.State) gin.HandlerFunc { return func(c *gin.Context) { obVersion, _ := clusterService.GetObVersion() if obVersion == "" { obVersion, _, _ = binary.GetMyOBVersion() } agentStatus := meta.NewAgentStatus(meta.OCS_AGENT, global.Pid, s.GetState(), global.StartAt, global.HomePath, obVersion, meta.AGENT_PWD.Inited(), meta.IsObproxyAgent()) agentStatus.Architecture = global.Architecture common.SendResponse(c, agentStatus, nil) } } // GitInfoHandler returns the agent git info // // @ID getGitInfo // @Summary get git info // @Description get git info // @Tags v1 // @Accept application/json // @Produce application/json // @Success 200 object http.OcsAgentResponse{data=config.GitInfo} // @Router /api/v1/git-info [get] func GitInfoHandler(c *gin.Context) { common.SendResponse(c, config.GetGitInfo(), nil) } func GetAgentStatus(s *http.State, obQueryTimeout string) (http.AgentStatus, error) { isrunning, err := localTaskService.IsRunning() var status = http.AgentStatus{ State: s.GetState(), Pid: global.Pid, StartAt: global.StartAt, Version: constant.VERSION_RELEASE, UnderMaintenance: !isrunning, } if meta.OCS_AGENT != nil { status.Agent.AgentInfo = meta.OCS_AGENT.GetAgentInfo() status.Agent.Identity = meta.OCS_AGENT.GetIdentity() status.Port = meta.OCS_AGENT.GetPort() } status.SqlPort = meta.MYSQL_PORT if obQueryTimeout != "" { status.OBState = oceanbase.GetStateWithTimeout(obQueryTimeout) } else { // Use short timeout so status API returns quickly when OB is unavailable, // allowing daemon/client to see State=RUNNING and avoid blocking "obshell agent start". status.OBState = oceanbase.GetStateWithTimeout("10000000") } return status, err } // StatusHandler returns the agent status // // @ID getStatus // @Summary get agent status // @Description get agent status // @Tags v1 // @Accept application/json // @Produce application/json // @Success 200 object http.OcsAgentResponse{data=http.AgentStatus} // @Router /api/v1/status [get] func StatusHandler(s *http.State) gin.HandlerFunc { return func(c *gin.Context) { obQueryTimeout := c.Query("ob_query_timeout") status, err := GetAgentStatus(s, obQueryTimeout) common.SendResponse(c, status, err) } } // StatusHandler returns the agent status // // @ID getAllAgentsStatus // @Summary get all agent status // @Description get all agent status // @Tags v1 // @Accept application/json // @Produce application/json // @Success 200 object http.OcsAgentResponse{data=map[string]http.AgentStatus} // @Router /api/v1/agents/status [get] func GetAllAgentStatus(s *http.State) gin.HandlerFunc { return func(c *gin.Context) { obQueryTimeout := c.Query("ob_query_timeout") agentsStatus := make(map[string]http.AgentStatus) status, err := GetAgentStatus(s, obQueryTimeout) if err == nil { agentsStatus[meta.OCS_AGENT.String()] = status } agents, err := agentService.GetAllAgents() if err == nil { uri := constant.URI_API_V1 + constant.URI_STATUS for _, agent := range agents { if agent.Equal(meta.OCS_AGENT) { continue } status := http.AgentStatus{} err := secure.SendGetRequest(&agent, uri, nil, &status) if err == nil { agentsStatus[agent.String()] = status } else { log.WithContext(c).Warnf("Failed to get status of agent %s: %s", agent.String(), err.Error()) } } } common.SendResponse(c, agentsStatus, nil) } }