2019-12-05 09:36:58 -05:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2021-01-18 20:59:04 -05:00
|
|
|
|
"fmt"
|
2019-12-08 03:59:58 -05:00
|
|
|
|
"time"
|
2019-12-05 09:36:58 -05:00
|
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
|
"github.com/patrickmn/go-cache"
|
2021-01-18 22:10:32 -05:00
|
|
|
|
"github.com/robfig/cron/v3"
|
2020-12-19 10:11:16 -05:00
|
|
|
|
"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"
|
2021-01-18 20:59:04 -05:00
|
|
|
|
pb "github.com/naiba/nezha/proto"
|
2020-12-19 23:18:27 -05:00
|
|
|
|
"github.com/naiba/nezha/service/alertmanager"
|
2020-11-10 21:07:45 -05:00
|
|
|
|
"github.com/naiba/nezha/service/dao"
|
2019-12-05 09:36:58 -05:00
|
|
|
|
)
|
|
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
|
func init() {
|
2021-01-18 22:10:32 -05:00
|
|
|
|
shanghai, err := time.LoadLocation("Asia/Shanghai")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化 dao 包
|
2019-12-20 10:58:09 -05:00
|
|
|
|
dao.Conf = &model.Config{}
|
2021-01-18 22:10:32 -05:00
|
|
|
|
dao.Cron = cron.New(cron.WithLocation(shanghai))
|
|
|
|
|
dao.Crons = make(map[uint64]*model.Cron)
|
|
|
|
|
dao.ServerList = make(map[uint64]*model.Server)
|
|
|
|
|
|
2019-12-20 10:58:09 -05:00
|
|
|
|
err = dao.Conf.Read("data/config.yaml")
|
2019-12-08 03:59:58 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2020-12-19 10:11:16 -05:00
|
|
|
|
dao.DB, err = gorm.Open(sqlite.Open("data/sqlite.db"), &gorm.Config{})
|
2019-12-05 09:36:58 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2019-12-08 10:18:29 -05:00
|
|
|
|
if dao.Conf.Debug {
|
|
|
|
|
dao.DB = dao.DB.Debug()
|
|
|
|
|
}
|
2019-12-08 03:59:58 -05:00
|
|
|
|
dao.Cache = cache.New(5*time.Minute, 10*time.Minute)
|
2021-01-18 22:10:32 -05:00
|
|
|
|
|
2021-01-18 20:59:04 -05:00
|
|
|
|
initSystem()
|
2019-12-08 10:18:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 20:59:04 -05:00
|
|
|
|
func initSystem() {
|
2021-01-15 11:45:49 -05:00
|
|
|
|
dao.DB.AutoMigrate(model.Server{}, model.User{},
|
|
|
|
|
model.Notification{}, model.AlertRule{}, model.Monitor{},
|
2021-01-18 20:59:04 -05:00
|
|
|
|
model.MonitorHistory{}, model.Cron{})
|
|
|
|
|
|
|
|
|
|
loadServers() //加载服务器列表
|
|
|
|
|
loadCrons() //加载计划任务
|
|
|
|
|
|
|
|
|
|
// 清理旧数据
|
|
|
|
|
dao.Cron.AddFunc("* 3 * * *", cleanMonitorHistory)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func cleanMonitorHistory() {
|
|
|
|
|
dao.DB.Delete(&model.MonitorHistory{}, "created_at < ?", time.Now().AddDate(0, -1, 0))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func loadServers() {
|
2019-12-09 03:02:49 -05:00
|
|
|
|
var servers []model.Server
|
|
|
|
|
dao.DB.Find(&servers)
|
|
|
|
|
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{}
|
2021-01-15 11:45:49 -05:00
|
|
|
|
innerS.State = &model.HostState{}
|
2021-01-08 08:04:50 -05:00
|
|
|
|
dao.ServerList[innerS.ID] = &innerS
|
2019-12-09 03:02:49 -05:00
|
|
|
|
}
|
2021-01-08 08:04:50 -05:00
|
|
|
|
dao.ReSortServer()
|
2019-12-08 03:59:58 -05:00
|
|
|
|
}
|
2019-12-05 09:36:58 -05:00
|
|
|
|
|
2021-01-18 20:59:04 -05:00
|
|
|
|
func loadCrons() {
|
|
|
|
|
var crons []model.Cron
|
|
|
|
|
dao.DB.Find(&crons)
|
|
|
|
|
var err error
|
|
|
|
|
for i := 0; i < len(crons); i++ {
|
|
|
|
|
cr := crons[i]
|
|
|
|
|
cr.CronID, err = dao.Cron.AddFunc(cr.Scheduler, func() {
|
|
|
|
|
dao.ServerLock.RLock()
|
|
|
|
|
defer dao.ServerLock.RUnlock()
|
|
|
|
|
for j := 0; j < len(cr.Servers); j++ {
|
|
|
|
|
if dao.ServerList[cr.Servers[j]].TaskStream != nil {
|
|
|
|
|
dao.ServerList[cr.Servers[j]].TaskStream.Send(&pb.Task{
|
|
|
|
|
Id: cr.ID,
|
|
|
|
|
Data: cr.Command,
|
|
|
|
|
Type: model.TaskTypeCommand,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
2021-01-23 05:15:11 -05:00
|
|
|
|
alertmanager.SendNotification(fmt.Sprintf("计划任务:%s,服务器:%d 离线,无法执行。", cr.Name, cr.Servers[j]), false)
|
2021-01-18 20:59:04 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
dao.Crons[cr.ID] = &cr
|
|
|
|
|
}
|
|
|
|
|
dao.Cron.Start()
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
|
func main() {
|
2020-10-24 09:44:03 -04:00
|
|
|
|
go controller.ServeWeb(dao.Conf.HTTPPort)
|
2019-12-10 05:05:02 -05:00
|
|
|
|
go rpc.ServeRPC(5555)
|
2021-01-16 05:04:47 -05:00
|
|
|
|
go rpc.DispatchTask(time.Minute * 3)
|
2021-01-05 20:35:04 -05:00
|
|
|
|
alertmanager.Start()
|
2019-12-05 09:36:58 -05:00
|
|
|
|
}
|