monagent_server.go 3.8 KB
Newer Older
W
wangzelin.wzl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Copyright (c) 2021 OceanBase
// obagent 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.

package engine

import (
	"context"
	"net/http"
	"sync"

	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"

	"github.com/oceanbase/obagent/api/route"
	"github.com/oceanbase/obagent/api/web"
	"github.com/oceanbase/obagent/config"
C
chris-sun-star 已提交
26
	"github.com/oceanbase/obagent/utils"
W
wangzelin.wzl 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
)

var monitorAgentServer *MonitorAgentServer

func GetMonitorAgentServer() *MonitorAgentServer {
	return monitorAgentServer
}

type MonitorAgentServer struct {
	// original configs
	Config *config.MonitorAgentConfig
	// sever of monitor metrics, selfstat, monitor manager API
	Server *web.HttpServer
	// server of pprof
	AdminServer *web.HttpServer
C
chris-sun-star 已提交
42 43
	// server status map
	serverStatusMap sync.Map
W
wangzelin.wzl 已提交
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
}

// NewMonitorAgentServer init monagent server: init configs and logger, register routers
func NewMonitorAgentServer(conf *config.MonitorAgentConfig) *MonitorAgentServer {
	monagentServer := &MonitorAgentServer{
		Config: conf,
		Server: &web.HttpServer{
			Counter:         new(web.Counter),
			Router:          gin.Default(),
			BasicAuthorizer: new(web.BasicAuth),
			Server:          &http.Server{},
			Address:         conf.Server.Address,
		},
		AdminServer: &web.HttpServer{
			Counter:         new(web.Counter),
			Router:          gin.Default(),
			BasicAuthorizer: new(web.BasicAuth),
			Server:          &http.Server{},
			Address:         conf.Server.AdminAddress,
		},
	}
	monitorAgentServer = monagentServer
	return monitorAgentServer
}

// Run start mongagent servers: admin server, monitor server
func (server *MonitorAgentServer) Run() error {
C
chris-sun-star 已提交
71
	// check port available before start server
W
wangzelin.wzl 已提交
72
	go func() {
C
chris-sun-star 已提交
73
		server.serverStatusMap.Store("adminServer", false)
W
wangzelin.wzl 已提交
74 75 76 77 78
		ctx, cancel := context.WithCancel(context.Background())
		server.AdminServer.Cancel = cancel
		server.AdminServer.Run(ctx)
	}()
	go func() {
C
chris-sun-star 已提交
79
		server.serverStatusMap.Store("server", false)
W
wangzelin.wzl 已提交
80 81 82 83
		ctx, cancel := context.WithCancel(context.Background())
		server.Server.Cancel = cancel
		server.Server.Run(ctx)
	}()
C
chris-sun-star 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96

	for {
		adminServerStatus, _ := server.serverStatusMap.LoadOrStore("adminServer", true)
		serverStatus, _ := server.serverStatusMap.LoadOrStore("server", true)
		adminServerOk, convertAdminServerOk := utils.ConvertToBool(adminServerStatus)
		serverOk, convertServerOk := utils.ConvertToBool(serverStatus)
		if !(convertAdminServerOk && convertServerOk) {
			return errors.New("start monagent server failed, adminSer")
		}
		if !(adminServerOk && serverOk) {
			return errors.New("start monagent server failed, adminSer")
		}
	}
W
wangzelin.wzl 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	return nil
}

// registerRouter register routers such as adminServer router and moniter metrics router
func (server *MonitorAgentServer) RegisterRouter() error {
	if err := server.registerAdminServerRouter(); err != nil {
		return errors.Wrap(err, "monitor agent server register admin server router")
	}
	if err := server.registerServerRouter(); err != nil {
		return errors.Wrap(err, "monitor agent server register metrics server router")
	}
	return nil
}

// registerServerRouter routers for moniter metrics.
func (server *MonitorAgentServer) registerServerRouter() error {
	server.Server.UseCounter()
	route.InitMonagentRoutes(server.Server.Router)
	return nil
}

// registerAdminServerRouter routers for selfstat, pprof and so on.
func (server *MonitorAgentServer) registerAdminServerRouter() error {
	server.AdminServer.UseCounter()
	route.InitPprofRouter(server.AdminServer.Router)
	return nil
}