nezha/cmd/dashboard/main.go

211 lines
6.0 KiB
Go
Raw Normal View History

2019-12-05 09:36:58 -05:00
package main
import (
"bytes"
"context"
"fmt"
"log"
2019-12-08 03:59:58 -05:00
"time"
2019-12-05 09:36:58 -05:00
"github.com/ory/graceful"
2019-12-08 03:59:58 -05:00
"github.com/patrickmn/go-cache"
"github.com/robfig/cron/v3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
2019-12-05 10:42:20 -05: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"
2022-01-08 22:54:14 -05:00
"github.com/naiba/nezha/service/singleton"
2019-12-05 09:36:58 -05:00
)
2019-12-08 03:59:58 -05:00
func init() {
shanghai, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
// 初始化 dao 包
2022-03-18 11:57:30 -04:00
singleton.Init()
2022-01-08 22:54:14 -05:00
singleton.Conf = &model.Config{}
singleton.Cron = cron.New(cron.WithSeconds(), cron.WithLocation(shanghai))
singleton.Crons = make(map[uint64]*model.Cron)
singleton.ServerList = make(map[uint64]*model.Server)
singleton.SecretToID = make(map[string]uint64)
2022-01-08 22:54:14 -05:00
err = singleton.Conf.Read("data/config.yaml")
2019-12-08 03:59:58 -05:00
if err != nil {
panic(err)
}
2022-01-08 22:54:14 -05:00
singleton.DB, err = gorm.Open(sqlite.Open("data/sqlite.db"), &gorm.Config{
CreateBatchSize: 200,
})
2019-12-05 09:36:58 -05:00
if err != nil {
panic(err)
}
2022-01-08 22:54:14 -05:00
if singleton.Conf.Debug {
singleton.DB = singleton.DB.Debug()
2019-12-08 10:18:29 -05:00
}
2022-01-08 22:54:14 -05:00
if singleton.Conf.GRPCPort == 0 {
singleton.Conf.GRPCPort = 5555
}
2022-01-08 22:54:14 -05:00
singleton.Cache = cache.New(5*time.Minute, 10*time.Minute)
initSystem()
2019-12-08 10:18:29 -05:00
}
func initSystem() {
2022-01-08 22:54:14 -05:00
singleton.DB.AutoMigrate(model.Server{}, model.User{},
model.Notification{}, model.AlertRule{}, model.Monitor{},
model.MonitorHistory{}, model.Cron{}, model.Transfer{})
2022-01-08 22:54:14 -05:00
singleton.LoadNotifications()
loadServers() //加载服务器列表
loadCrons() //加载计划任务
// 清理 服务请求记录 和 流量记录 的旧数据
2022-01-08 22:54:14 -05:00
_, err := singleton.Cron.AddFunc("0 30 3 * * *", cleanMonitorHistory)
2021-07-18 22:37:12 -04:00
if err != nil {
panic(err)
}
// 流量记录打点
2022-01-08 22:54:14 -05:00
_, err = singleton.Cron.AddFunc("0 0 * * * *", recordTransferHourlyUsage)
2021-07-18 22:37:12 -04:00
if err != nil {
panic(err)
}
}
func recordTransferHourlyUsage() {
2022-01-08 22:54:14 -05:00
singleton.ServerLock.Lock()
defer singleton.ServerLock.Unlock()
now := time.Now()
nowTrimSeconds := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.Local)
var txs []model.Transfer
2022-01-08 22:54:14 -05:00
for id, server := range singleton.ServerList {
tx := model.Transfer{
ServerID: id,
In: server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn),
Out: server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut),
}
2021-11-06 06:40:29 -04:00
if tx.In == 0 && tx.Out == 0 {
continue
}
server.PrevHourlyTransferIn = int64(server.State.NetInTransfer)
server.PrevHourlyTransferOut = int64(server.State.NetOutTransfer)
tx.CreatedAt = nowTrimSeconds
txs = append(txs, tx)
}
2021-11-06 06:40:29 -04:00
if len(txs) == 0 {
return
}
2022-01-08 22:54:14 -05:00
log.Println("NEZHA>> Cron 流量统计入库", len(txs), singleton.DB.Create(txs).Error)
}
func cleanMonitorHistory() {
2021-07-19 08:27:21 -04:00
// 清理无效数据
2022-01-08 22:54:14 -05:00
singleton.DB.Unscoped().Delete(&model.MonitorHistory{}, "created_at < ? OR monitor_id NOT IN (SELECT `id` FROM monitors)", time.Now().AddDate(0, 0, -30))
singleton.DB.Unscoped().Delete(&model.Transfer{}, "server_id NOT IN (SELECT `id` FROM servers)")
2021-07-19 08:27:21 -04:00
// 计算可清理流量记录的时长
var allServerKeep time.Time
specialServerKeep := make(map[uint64]time.Time)
var specialServerIDs []uint64
var alerts []model.AlertRule
2022-01-08 22:54:14 -05:00
singleton.DB.Find(&alerts)
for i := 0; i < len(alerts); i++ {
for j := 0; j < len(alerts[i].Rules); j++ {
// 是不是流量记录规则
if !alerts[i].Rules[j].IsTransferDurationRule() {
continue
}
dataCouldRemoveBefore := alerts[i].Rules[j].GetTransferDurationStart()
// 判断规则影响的机器范围
if alerts[i].Rules[j].Cover == model.RuleCoverAll {
// 更新全局可以清理的数据点
if allServerKeep.IsZero() || allServerKeep.After(dataCouldRemoveBefore) {
allServerKeep = dataCouldRemoveBefore
}
} else {
// 更新特定机器可以清理数据点
for id := range alerts[i].Rules[j].Ignore {
if specialServerKeep[id].IsZero() || specialServerKeep[id].After(dataCouldRemoveBefore) {
specialServerKeep[id] = dataCouldRemoveBefore
specialServerIDs = append(specialServerIDs, id)
}
}
}
}
}
for id, couldRemove := range specialServerKeep {
2022-01-08 22:54:14 -05:00
singleton.DB.Unscoped().Delete(&model.Transfer{}, "server_id = ? AND created_at < ?", id, couldRemove)
}
2021-07-14 12:07:02 -04:00
if allServerKeep.IsZero() {
2022-01-08 22:54:14 -05:00
singleton.DB.Unscoped().Delete(&model.Transfer{}, "server_id NOT IN (?)", specialServerIDs)
2021-07-14 12:07:02 -04:00
} else {
2022-01-08 22:54:14 -05:00
singleton.DB.Unscoped().Delete(&model.Transfer{}, "server_id NOT IN (?) AND created_at < ?", specialServerIDs, allServerKeep)
2021-07-14 12:07:02 -04:00
}
}
func loadServers() {
2019-12-09 03:02:49 -05:00
var servers []model.Server
2022-01-08 22:54:14 -05:00
singleton.DB.Find(&servers)
2019-12-09 03:02:49 -05:00
for _, s := range servers {
2019-12-09 05:14:31 -05:00
innerS := s
2020-12-19 23:18:27 -05:00
innerS.Host = &model.Host{}
innerS.State = &model.HostState{}
2022-01-08 22:54:14 -05:00
singleton.ServerList[innerS.ID] = &innerS
singleton.SecretToID[innerS.Secret] = innerS.ID
2019-12-09 03:02:49 -05:00
}
2022-01-08 22:54:14 -05:00
singleton.ReSortServer()
2019-12-08 03:59:58 -05:00
}
2019-12-05 09:36:58 -05:00
func loadCrons() {
var crons []model.Cron
2022-01-08 22:54:14 -05:00
singleton.DB.Find(&crons)
var err error
errMsg := new(bytes.Buffer)
for i := 0; i < len(crons); i++ {
cr := crons[i]
crIgnoreMap := make(map[uint64]bool)
for j := 0; j < len(cr.Servers); j++ {
crIgnoreMap[cr.Servers[j]] = true
}
2022-01-08 22:54:14 -05:00
cr.CronJobID, err = singleton.Cron.AddFunc(cr.Scheduler, singleton.CronTrigger(cr))
if err == nil {
2022-01-08 22:54:14 -05:00
singleton.Crons[cr.ID] = &cr
} else {
if errMsg.Len() == 0 {
errMsg.WriteString("调度失败的计划任务:[")
}
errMsg.WriteString(fmt.Sprintf("%d,", cr.ID))
}
}
if errMsg.Len() > 0 {
msg := errMsg.String()
2022-01-08 22:54:14 -05:00
singleton.SendNotification(msg[:len(msg)-1]+"] 这些任务将无法正常执行,请进入后点重新修改保存。", false)
}
2022-01-08 22:54:14 -05:00
singleton.Cron.Start()
}
2019-12-08 03:59:58 -05:00
func main() {
2021-07-16 06:09:50 -04:00
cleanMonitorHistory()
2022-01-08 22:54:14 -05:00
go rpc.ServeRPC(singleton.Conf.GRPCPort)
serviceSentinelDispatchBus := make(chan model.Monitor)
go rpc.DispatchTask(serviceSentinelDispatchBus)
go rpc.DispatchKeepalive()
2022-01-08 22:54:14 -05:00
go singleton.AlertSentinelStart()
singleton.NewServiceSentinel(serviceSentinelDispatchBus)
srv := controller.ServeWeb(singleton.Conf.HTTPPort)
graceful.Graceful(func() error {
return srv.ListenAndServe()
}, func(c context.Context) error {
2021-11-06 06:40:29 -04:00
log.Println("NEZHA>> Graceful::START")
recordTransferHourlyUsage()
log.Println("NEZHA>> Graceful::END")
srv.Shutdown(c)
return nil
})
2019-12-05 09:36:58 -05:00
}