2019-12-05 09:36:58 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-30 12:15:41 -04:00
|
|
|
"os"
|
2021-07-14 11:53:37 -04:00
|
|
|
"context"
|
2023-11-02 09:06:34 -04:00
|
|
|
"fmt"
|
2022-04-27 11:51:45 -04:00
|
|
|
"log"
|
2024-08-02 07:41:39 -04:00
|
|
|
"time"
|
2024-09-02 10:13:13 -04:00
|
|
|
_ "time/tzdata"
|
2022-04-27 11:51:45 -04:00
|
|
|
|
2020-11-10 21:07:45 -05:00
|
|
|
"github.com/naiba/nezha/cmd/dashboard/controller"
|
|
|
|
"github.com/naiba/nezha/cmd/dashboard/rpc"
|
|
|
|
"github.com/naiba/nezha/model"
|
2024-08-02 07:41:39 -04:00
|
|
|
"github.com/naiba/nezha/proto"
|
2022-01-08 22:54:14 -05:00
|
|
|
"github.com/naiba/nezha/service/singleton"
|
2022-04-12 01:16:33 -04:00
|
|
|
"github.com/ory/graceful"
|
2023-11-02 09:06:34 -04:00
|
|
|
flag "github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DashboardCliParam struct {
|
|
|
|
Version bool // 当前版本号
|
|
|
|
ConfigFile string // 配置文件路径
|
|
|
|
DatebaseLocation string // Sqlite3 数据库文件路径
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
dashboardCliParam DashboardCliParam
|
2019-12-05 09:36:58 -05:00
|
|
|
)
|
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
func init() {
|
2023-11-02 09:06:34 -04:00
|
|
|
flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
|
|
|
|
flag.BoolVarP(&dashboardCliParam.Version, "version", "v", false, "查看当前版本号")
|
|
|
|
flag.StringVarP(&dashboardCliParam.ConfigFile, "config", "c", "data/config.yaml", "配置文件路径")
|
|
|
|
flag.StringVar(&dashboardCliParam.DatebaseLocation, "db", "data/sqlite.db", "Sqlite3数据库文件路径")
|
|
|
|
flag.Parse()
|
2019-12-08 10:18:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-18 20:59:04 -05:00
|
|
|
func initSystem() {
|
2022-04-12 01:16:33 -04:00
|
|
|
// 启动 singleton 包下的所有服务
|
|
|
|
singleton.LoadSingleton()
|
2021-01-18 20:59:04 -05:00
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
// 每天的3:30 对 监控记录 和 流量记录 进行清理
|
2022-04-12 01:16:33 -04:00
|
|
|
if _, err := singleton.Cron.AddFunc("0 30 3 * * *", singleton.CleanMonitorHistory); err != nil {
|
2021-07-18 22:37:12 -04:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-09-02 11:45:21 -04:00
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
// 每小时对流量记录进行打点
|
2022-04-12 01:16:33 -04:00
|
|
|
if _, err := singleton.Cron.AddFunc("0 0 * * * *", singleton.RecordTransferHourlyUsage); err != nil {
|
2021-07-18 22:37:12 -04:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-07-14 11:53:37 -04:00
|
|
|
}
|
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
func main() {
|
2023-11-02 09:06:34 -04:00
|
|
|
if dashboardCliParam.Version {
|
|
|
|
fmt.Println(singleton.Version)
|
2024-10-30 12:15:41 -04:00
|
|
|
os.Exit(0)
|
2023-11-02 09:06:34 -04:00
|
|
|
}
|
|
|
|
|
2024-10-30 12:15:41 -04:00
|
|
|
// 初始化 dao 包
|
|
|
|
singleton.InitConfigFromPath(dashboardCliParam.ConfigFile)
|
|
|
|
singleton.InitTimezoneAndCache()
|
|
|
|
singleton.InitDBFromPath(dashboardCliParam.DatebaseLocation)
|
|
|
|
singleton.InitLocalizer()
|
|
|
|
initSystem()
|
|
|
|
|
2024-07-14 00:47:36 -04:00
|
|
|
// TODO 使用 cmux 在同一端口服务 HTTP 和 gRPC
|
2022-04-12 01:16:33 -04:00
|
|
|
singleton.CleanMonitorHistory()
|
2022-01-08 22:54:14 -05:00
|
|
|
go rpc.ServeRPC(singleton.Conf.GRPCPort)
|
2022-04-11 10:51:02 -04:00
|
|
|
serviceSentinelDispatchBus := make(chan model.Monitor) // 用于传递服务监控任务信息的channel
|
2021-09-02 11:45:21 -04:00
|
|
|
go rpc.DispatchTask(serviceSentinelDispatchBus)
|
2021-11-10 23:49:54 -05:00
|
|
|
go rpc.DispatchKeepalive()
|
2022-01-08 22:54:14 -05:00
|
|
|
go singleton.AlertSentinelStart()
|
|
|
|
singleton.NewServiceSentinel(serviceSentinelDispatchBus)
|
|
|
|
srv := controller.ServeWeb(singleton.Conf.HTTPPort)
|
2024-08-02 07:41:39 -04:00
|
|
|
go dispatchReportInfoTask()
|
2023-11-02 09:06:34 -04:00
|
|
|
if err := graceful.Graceful(func() error {
|
2021-07-14 11:53:37 -04:00
|
|
|
return srv.ListenAndServe()
|
|
|
|
}, func(c context.Context) error {
|
2021-11-06 06:40:29 -04:00
|
|
|
log.Println("NEZHA>> Graceful::START")
|
2022-04-12 01:16:33 -04:00
|
|
|
singleton.RecordTransferHourlyUsage()
|
2021-11-06 06:40:29 -04:00
|
|
|
log.Println("NEZHA>> Graceful::END")
|
2021-07-14 11:53:37 -04:00
|
|
|
srv.Shutdown(c)
|
|
|
|
return nil
|
2023-11-02 09:06:34 -04:00
|
|
|
}); err != nil {
|
|
|
|
log.Printf("NEZHA>> ERROR: %v", err)
|
|
|
|
}
|
2019-12-05 09:36:58 -05:00
|
|
|
}
|
2024-08-02 07:41:39 -04:00
|
|
|
|
|
|
|
func dispatchReportInfoTask() {
|
|
|
|
time.Sleep(time.Second * 15)
|
|
|
|
singleton.ServerLock.RLock()
|
|
|
|
defer singleton.ServerLock.RUnlock()
|
|
|
|
for _, server := range singleton.ServerList {
|
|
|
|
if server == nil || server.TaskStream == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
server.TaskStream.Send(&proto.Task{
|
|
|
|
Type: model.TaskTypeReportHostInfo,
|
|
|
|
Data: "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|