monagent_route.go 3.4 KB
Newer Older
Z
zhj-luo 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (c) 2023 OceanBase
 * OCP Express is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

O
ob-robot 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
package monagent

import (
	"context"
	"net/http"
	"os"

	"github.com/gin-gonic/gin"
	adapter "github.com/gwatts/gin-adapter"
	log "github.com/sirupsen/logrus"

	"github.com/oceanbase/obagent/api/common"
	"github.com/oceanbase/obagent/config"
	http2 "github.com/oceanbase/obagent/lib/http"
	"github.com/oceanbase/obagent/lib/system"
	"github.com/oceanbase/obagent/stat"
)

func InitMonitorAgentRoutes(router *gin.Engine, localRouter *gin.Engine) {
	router.GET("/metrics/stat", adapter.Wrap(stat.PromHandler))

	v1 := router.Group("/api/v1")
	v1.Use(common.PostHandlers())

	v1.POST("/module/config/update", common.UpdateConfigPropertiesHandler)
	v1.POST("/module/config/notify", common.NotifyConfigPropertiesHandler)
	v1.POST("/module/config/validate", common.ValidateConfigPropertiesHandler)

	v1.GET("/time", common.TimeHandler)
	v1.GET("/info", common.InfoHandler)
	v1.GET("/git-info", common.GitInfoHandler)
	v1.POST("/status", monitorStatusHandler)
	v1.GET("/status", monitorStatusHandler)

	initMonagentLocalRoutes(localRouter)
}

func initMonagentLocalRoutes(localRouter *gin.Engine) {
	common.InitPprofRouter(localRouter)

	localRouter.GET("/metrics/stat", adapter.Wrap(stat.PromHandler))

	group := localRouter.Group("/api/v1")
	group.GET("/time", common.TimeHandler)
	group.GET("/info", common.InfoHandler)
	group.POST("/info", common.InfoHandler)
	group.GET("/git-info", common.GitInfoHandler)
	group.POST("/status", monitorStatusHandler)
	group.GET("/status", monitorStatusHandler)
	group.POST("/module/config/update", common.UpdateConfigPropertiesHandler)
	group.POST("/module/config/notify", common.NotifyConfigPropertiesHandler)
}

func UseLocalMonitorMiddleware(r *gin.Engine) {
	r.Use(
		common.HttpStatMiddleware,
		gin.CustomRecovery(common.Recovery), // gin's crash-free middleware
		common.PreHandlers("/api/v1/module/config/update", "/api/v1/module/config/validate"),
		common.PostHandlers("/debug/pprof", "/debug/fgprof", "/metrics/", "/api/v1/log/alarms"),
	)
}

func UseMonitorMiddleware(r *gin.Engine) {
	r.Use(
		common.HttpStatMiddleware,
		gin.CustomRecovery(common.Recovery), // gin's crash-free middleware
		common.PreHandlers("/api/v1/module/config/update", "/api/v1/module/config/validate"),
		common.MonitorAgentPostHandler,
	)
}

func RegisterPipelineRoute(ctx context.Context, r *gin.Engine, url string, fh func(http.Handler) http.Handler) {
	log.WithContext(ctx).Infof("register route %s", url)
	r.GET(url, adapter.Wrap(fh))
}

var libProcess system.Process = system.ProcessImpl{}

func monitorStatusHandler(c *gin.Context) {
	ports := make([]int, 0)

	pid := os.Getpid()
	processInfo, err := libProcess.GetProcessInfoByPid(int32(pid))
	if err != nil {
		log.Errorf("StatusHandler get processInfo failed, pid:%s", pid)
	} else {
		ports = processInfo.Ports
	}
	var info = http2.Status{
		State:   http2.Running,
		Version: config.AgentVersion,
		Pid:     pid,
		StartAt: common.StartAt,
		Ports:   ports,
	}
	common.SendResponse(c, info, nil)
}