@@ -8,7 +8,7 @@ import (
"github.com/naiba/nezha/model"
"github.com/naiba/nezha/pkg/utils"
pb "github.com/naiba/nezha/proto"
"github.com/naiba/nezha/service/dao "
"github.com/naiba/nezha/service/singleton "
)
type NezhaHandler struct {
@@ -23,25 +23,25 @@ func (s *NezhaHandler) ReportTask(c context.Context, r *pb.TaskResult) (*pb.Rece
}
if r . GetType ( ) == model . TaskTypeCommand {
// 处理上报的计划任务
dao . CronLock . RLock ( )
defer dao . CronLock . RUnlock ( )
cr := dao . Crons [ r . GetId ( ) ]
singleton . CronLock . RLock ( )
defer singleton . CronLock . RUnlock ( )
cr := singleton . Crons [ r . GetId ( ) ]
if cr != nil {
dao . ServerLock . RLock ( )
defer dao . ServerLock . RUnlock ( )
singleton . ServerLock . RLock ( )
defer singleton . ServerLock . RUnlock ( )
if cr . PushSuccessful && r . GetSuccessful ( ) {
dao . SendNotification ( fmt . Sprintf ( "[任务成功] %s ,服务器:%s, 日志: \n%s" , cr . Name , dao . ServerList [ clientID ] . Name , r . GetData ( ) ) , false )
singleton . SendNotification ( fmt . Sprintf ( "[任务成功] %s ,服务器:%s, 日志: \n%s" , cr . Name , singleton . ServerList [ clientID ] . Name , r . GetData ( ) ) , false )
}
if ! r . GetSuccessful ( ) {
dao . SendNotification ( fmt . Sprintf ( "[任务失败] %s ,服务器:%s, 日志: \n%s" , cr . Name , dao . ServerList [ clientID ] . Name , r . GetData ( ) ) , false )
singleton . SendNotification ( fmt . Sprintf ( "[任务失败] %s ,服务器:%s, 日志: \n%s" , cr . Name , singleton . ServerList [ clientID ] . Name , r . GetData ( ) ) , false )
}
dao . DB . Model ( cr ) . Updates ( model . Cron {
singleton . DB . Model ( cr ) . Updates ( model . Cron {
LastExecutedAt : time . Now ( ) . Add ( time . Second * - 1 * time . Duration ( r . GetDelay ( ) ) ) ,
LastResult : r . GetSuccessful ( ) ,
} )
}
} else if model . IsServiceSentinelNeeded ( r . GetType ( ) ) {
dao . ServiceSentinelShared . Dispatch ( dao . ReportData {
singleton . ServiceSentinelShared . Dispatch ( singleton . ReportData {
Data : r ,
Reporter : clientID ,
} )
@@ -56,14 +56,14 @@ func (s *NezhaHandler) RequestTask(h *pb.Host, stream pb.NezhaService_RequestTas
return err
}
closeCh := make ( chan error )
dao . ServerLock . RLock ( )
singleton . ServerLock . RLock ( )
// 修复不断的请求 task 但是没有 return 导致内存泄漏
if dao . ServerList [ clientID ] . TaskClose != nil {
close ( dao . ServerList [ clientID ] . TaskClose )
if singleton . ServerList [ clientID ] . TaskClose != nil {
close ( singleton . ServerList [ clientID ] . TaskClose )
}
dao . ServerList [ clientID ] . TaskStream = stream
dao . ServerList [ clientID ] . TaskClose = closeCh
dao . ServerLock . RUnlock ( )
singleton . ServerList [ clientID ] . TaskStream = stream
singleton . ServerList [ clientID ] . TaskClose = closeCh
singleton . ServerLock . RUnlock ( )
return <- closeCh
}
@@ -74,15 +74,15 @@ func (s *NezhaHandler) ReportSystemState(c context.Context, r *pb.State) (*pb.Re
return nil , err
}
state := model . PB2State ( r )
dao . ServerLock . RLock ( )
defer dao . ServerLock . RUnlock ( )
dao . ServerList [ clientID ] . LastActive = time . Now ( )
dao . ServerList [ clientID ] . State = & state
singleton . ServerLock . RLock ( )
defer singleton . ServerLock . RUnlock ( )
singleton . ServerList [ clientID ] . LastActive = time . Now ( )
singleton . ServerList [ clientID ] . State = & state
// 如果从未记录过,先打点,等到小时时间点时入库
if dao . ServerList [ clientID ] . PrevHourlyTransferIn == 0 || dao . ServerList [ clientID ] . PrevHourlyTransferOut == 0 {
dao . ServerList [ clientID ] . PrevHourlyTransferIn = int64 ( state . NetInTransfer )
dao . ServerList [ clientID ] . PrevHourlyTransferOut = int64 ( state . NetOutTransfer )
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
@@ -95,26 +95,26 @@ func (s *NezhaHandler) ReportSystemInfo(c context.Context, r *pb.Host) (*pb.Rece
return nil , err
}
host := model . PB2Host ( r )
dao . ServerLock . RLock ( )
defer dao . ServerLock . RUnlock ( )
if dao . Conf . EnableIPChangeNotification &&
( ( dao . Conf . Cover == model . ConfigCoverAll && ! dao . Conf . IgnoredIPNotificationServerIDs [ clientID ] ) ||
( dao . Conf . Cover == model . ConfigCoverIgnoreAll && dao . Conf . IgnoredIPNotificationServerIDs [ clientID ] ) ) &&
dao . ServerList [ clientID ] . Host != nil &&
dao . ServerList [ clientID ] . Host . IP != "" &&
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 != "" &&
dao . ServerList [ clientID ] . Host . IP != host . IP {
dao . SendNotification ( fmt . Sprintf (
singleton . ServerList [ clientID ] . Host . IP != host . IP {
singleton . SendNotification ( fmt . Sprintf (
"[IP变更] %s , 旧IP: %s, 新IP: %s。" ,
dao . ServerList [ clientID ] . Name , utils . IPDesensitize ( dao . ServerList [ clientID ] . Host . IP ) , utils . IPDesensitize ( host . IP ) ) , true )
singleton . ServerList [ clientID ] . Name , utils . IPDesensitize ( singleton . ServerList [ clientID ] . Host . IP ) , utils . IPDesensitize ( host . IP ) ) , true )
}
// 判断是否是机器重启,如果是机器重启要录入最后记录的流量里面
if dao . ServerList [ clientID ] . Host . BootTime < host . BootTime {
dao . ServerList [ clientID ] . PrevHourlyTransferIn = dao . ServerList [ clientID ] . PrevHourlyTransferIn - int64 ( dao . ServerList [ clientID ] . State . NetInTransfer )
dao . ServerList [ clientID ] . PrevHourlyTransferOut = dao . ServerList [ clientID ] . PrevHourlyTransferOut - int64 ( dao . ServerList [ clientID ] . State . NetOutTransfer )
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 )
}
dao . ServerList [ clientID ] . Host = & host
singleton . ServerList [ clientID ] . Host = & host
return & pb . Receipt { Proced : true } , nil
}