nezha/service/rpc/nezha.go

115 lines
3.2 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"
2021-01-16 01:11:51 -05:00
"strings"
2020-10-24 09:29:05 -04:00
"time"
2019-12-07 05:14:40 -05:00
2020-11-10 21:07:45 -05:00
"github.com/naiba/nezha/model"
pb "github.com/naiba/nezha/proto"
"github.com/naiba/nezha/service/alertmanager"
2020-11-10 21:07:45 -05:00
"github.com/naiba/nezha/service/dao"
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
if _, err = s.Auth.Check(c); err != nil {
return nil, err
}
if r.GetType() == model.MonitorTypeHTTPGET {
2021-01-16 01:11:51 -05:00
// SSL 证书报警
var last model.MonitorHistory
if err := dao.DB.Where("monitor_id = ?", r.GetId()).Order("id DESC").First(&last).Error; err == nil {
2021-01-16 01:11:51 -05:00
var errMsg string
if strings.HasPrefix(r.GetData(), "SSL证书错误") {
// 证书错误提醒
errMsg = r.GetData()
} else {
var splits = strings.Split(r.GetData(), "|")
// 证书变更提醒
if last.Data != "" && last.Data != splits[0] {
errMsg = fmt.Sprintf(
"SSL证书变更%s%s。",
last.Data, splits[0])
}
expires, err := time.Parse("2006-01-02 15:04:05 -0700 MST", splits[1])
// 证书过期提醒
if err == nil && expires.Before(time.Now().AddDate(0, 0, 7)) {
errMsg = fmt.Sprintf(
"SSL证书将在七天内过期过期时间%s。",
expires.Format("2006-01-02 15:04:05"))
}
}
if errMsg != "" {
var monitor model.Monitor
dao.DB.First(&monitor, "id = ?", last.MonitorID)
2021-01-16 01:11:51 -05:00
alertmanager.SendNotification(fmt.Sprintf("服务监控:%s %s", monitor.Name, errMsg))
}
}
}
// 存入历史记录
mh := model.PB2MonitorHistory(r)
if err := dao.DB.Create(&mh).Error; err != nil {
return nil, err
}
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)
dao.ServerLock.Lock()
dao.ServerList[clientID].TaskStream = stream
dao.ServerList[clientID].TaskClose = closeCh
dao.ServerLock.Unlock()
2019-12-10 04:57:57 -05:00
select {
case err = <-closeCh:
return err
2019-12-09 03:02:49 -05:00
}
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)
dao.ServerLock.RLock()
defer dao.ServerLock.RUnlock()
dao.ServerList[clientID].LastActive = time.Now()
dao.ServerList[clientID].State = &state
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)
2019-12-10 04:57:57 -05:00
dao.ServerLock.RLock()
defer dao.ServerLock.RUnlock()
if dao.Conf.EnableIPChangeNotification &&
dao.ServerList[clientID].Host != nil &&
dao.ServerList[clientID].Host.IP != "" &&
host.IP != "" &&
dao.ServerList[clientID].Host.IP != host.IP {
alertmanager.SendNotification(fmt.Sprintf(
2021-01-16 01:11:51 -05:00
"IP变更提醒 服务器:%s 旧IP%s新IP%s。",
dao.ServerList[clientID].Name, dao.ServerList[clientID].Host.IP, host.IP))
}
2019-12-13 04:56:14 -05:00
dao.ServerList[clientID].Host = &host
2019-12-07 05:14:40 -05:00
return &pb.Receipt{Proced: true}, nil
}