nezha/service/rpc/nezha.go

142 lines
4.9 KiB
Go
Raw Normal View History

2019-12-08 03:59:58 -05:00
package rpc
2019-12-07 05:14:40 -05:00
import (
"context"
"fmt"
2020-10-24 09:29:05 -04:00
"time"
2019-12-07 05:14:40 -05:00
"github.com/jinzhu/copier"
"github.com/nicksnyder/go-i18n/v2/i18n"
2020-11-10 21:07:45 -05:00
"github.com/naiba/nezha/model"
pb "github.com/naiba/nezha/proto"
2022-01-08 22:54:14 -05:00
"github.com/naiba/nezha/service/singleton"
2019-12-07 05:14:40 -05:00
)
type NezhaHandler struct {
Auth *AuthHandler
}
func (s *NezhaHandler) ReportTask(c context.Context, r *pb.TaskResult) (*pb.Receipt, error) {
2019-12-09 05:14:31 -05:00
var err error
var clientID uint64
if clientID, err = s.Auth.Check(c); err != nil {
return nil, err
}
2021-09-27 09:18:09 -04:00
if r.GetType() == model.TaskTypeCommand {
// 处理上报的计划任务
2022-01-08 22:54:14 -05:00
singleton.CronLock.RLock()
defer singleton.CronLock.RUnlock()
cr := singleton.Crons[r.GetId()]
if cr != nil {
2022-01-08 22:54:14 -05:00
singleton.ServerLock.RLock()
defer singleton.ServerLock.RUnlock()
// 保存当前服务器状态信息
curServer := model.Server{}
copier.Copy(&curServer, singleton.ServerList[clientID])
if cr.PushSuccessful && r.GetSuccessful() {
singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("[%s] %s, %s\n%s", singleton.Localizer.MustLocalize(
&i18n.LocalizeConfig{
MessageID: "ScheduledTaskExecutedSuccessfully",
},
), cr.Name, singleton.ServerList[clientID].Name, r.GetData()), nil, &curServer)
}
if !r.GetSuccessful() {
singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("[%s] %s, %s\n%s", singleton.Localizer.MustLocalize(
&i18n.LocalizeConfig{
MessageID: "ScheduledTaskExecutedFailed",
},
), cr.Name, singleton.ServerList[clientID].Name, r.GetData()), nil, &curServer)
}
2022-01-08 22:54:14 -05:00
singleton.DB.Model(cr).Updates(model.Cron{
LastExecutedAt: time.Now().Add(time.Second * -1 * time.Duration(r.GetDelay())),
LastResult: r.GetSuccessful(),
})
}
2021-09-27 09:18:09 -04:00
} else if model.IsServiceSentinelNeeded(r.GetType()) {
2022-01-08 22:54:14 -05:00
singleton.ServiceSentinelShared.Dispatch(singleton.ReportData{
2021-09-27 09:18:09 -04:00
Data: r,
Reporter: clientID,
})
}
2019-12-07 05:14:40 -05:00
return &pb.Receipt{Proced: true}, nil
}
func (s *NezhaHandler) RequestTask(h *pb.Host, stream pb.NezhaService_RequestTaskServer) error {
2021-01-08 08:04:50 -05:00
var clientID uint64
2019-12-09 05:14:31 -05:00
var err error
if clientID, err = s.Auth.Check(stream.Context()); err != nil {
2019-12-07 05:14:40 -05:00
return err
}
2019-12-10 04:57:57 -05:00
closeCh := make(chan error)
2022-01-08 22:54:14 -05:00
singleton.ServerLock.RLock()
// 修复不断的请求 task 但是没有 return 导致内存泄漏
2022-01-08 22:54:14 -05:00
if singleton.ServerList[clientID].TaskClose != nil {
close(singleton.ServerList[clientID].TaskClose)
}
2022-01-08 22:54:14 -05:00
singleton.ServerList[clientID].TaskStream = stream
singleton.ServerList[clientID].TaskClose = closeCh
singleton.ServerLock.RUnlock()
return <-closeCh
2019-12-07 05:14:40 -05:00
}
func (s *NezhaHandler) ReportSystemState(c context.Context, r *pb.State) (*pb.Receipt, error) {
var clientID uint64
var err error
if clientID, err = s.Auth.Check(c); err != nil {
return nil, err
}
state := model.PB2State(r)
2022-01-08 22:54:14 -05:00
singleton.ServerLock.RLock()
defer singleton.ServerLock.RUnlock()
singleton.ServerList[clientID].LastActive = time.Now()
singleton.ServerList[clientID].State = &state
// 如果从未记录过,先打点,等到小时时间点时入库
2022-01-08 22:54:14 -05:00
if singleton.ServerList[clientID].PrevHourlyTransferIn == 0 || singleton.ServerList[clientID].PrevHourlyTransferOut == 0 {
singleton.ServerList[clientID].PrevHourlyTransferIn = int64(state.NetInTransfer)
singleton.ServerList[clientID].PrevHourlyTransferOut = int64(state.NetOutTransfer)
}
return &pb.Receipt{Proced: true}, nil
}
func (s *NezhaHandler) ReportSystemInfo(c context.Context, r *pb.Host) (*pb.Receipt, error) {
2021-01-08 08:04:50 -05:00
var clientID uint64
2019-12-09 05:14:31 -05:00
var err error
if clientID, err = s.Auth.Check(c); err != nil {
2019-12-07 05:14:40 -05:00
return nil, err
}
2019-12-13 04:56:14 -05:00
host := model.PB2Host(r)
2022-01-08 22:54:14 -05:00
singleton.ServerLock.RLock()
defer singleton.ServerLock.RUnlock()
if singleton.Conf.EnableIPChangeNotification &&
((singleton.Conf.Cover == model.ConfigCoverAll && !singleton.Conf.IgnoredIPNotificationServerIDs[clientID]) ||
(singleton.Conf.Cover == model.ConfigCoverIgnoreAll && singleton.Conf.IgnoredIPNotificationServerIDs[clientID])) &&
singleton.ServerList[clientID].Host != nil &&
singleton.ServerList[clientID].Host.IP != "" &&
host.IP != "" &&
2022-01-08 22:54:14 -05:00
singleton.ServerList[clientID].Host.IP != host.IP {
singleton.SendNotification(singleton.Conf.IPChangeNotificationTag,
fmt.Sprintf(
"[%s] %s, %s => %s",
singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "IPChanged",
}),
singleton.ServerList[clientID].Name, singleton.IPDesensitize(singleton.ServerList[clientID].Host.IP),
singleton.IPDesensitize(host.IP),
),
nil)
}
2021-07-16 06:09:50 -04:00
// 判断是否是机器重启,如果是机器重启要录入最后记录的流量里面
2022-01-08 22:54:14 -05:00
if singleton.ServerList[clientID].Host.BootTime < host.BootTime {
singleton.ServerList[clientID].PrevHourlyTransferIn = singleton.ServerList[clientID].PrevHourlyTransferIn - int64(singleton.ServerList[clientID].State.NetInTransfer)
singleton.ServerList[clientID].PrevHourlyTransferOut = singleton.ServerList[clientID].PrevHourlyTransferOut - int64(singleton.ServerList[clientID].State.NetOutTransfer)
2021-07-16 06:09:50 -04:00
}
2022-01-08 22:54:14 -05:00
singleton.ServerList[clientID].Host = &host
2019-12-07 05:14:40 -05:00
return &pb.Receipt{Proced: true}, nil
}