diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 4d92ae1..5aa7a22 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -166,6 +166,7 @@ func receiveCommand(hc pb.NezhaService_HeartbeatClient) error { } func reportState() { + var lastReportHostInfo time.Time var err error defer log.Printf("reportState exit %v => %v", time.Now(), err) for { @@ -176,6 +177,10 @@ func reportState() { log.Printf("reportState error %v", err) time.Sleep(delayWhenError) } + if lastReportHostInfo.Before(time.Now().Add(-10 * time.Minute)) { + lastReportHostInfo = time.Now() + client.Register(ctx, monitor.GetHost().PB()) + } } } } diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index 8d72f70..5b1d83f 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -261,10 +261,11 @@ func (ma *memberAPI) logout(c *gin.Context) { } type settingForm struct { - Title string - Admin string - Theme string - CustomCode string + Title string + Admin string + Theme string + CustomCode string + EnableIPChangeNotification string } func (ma *memberAPI) updateSetting(c *gin.Context) { @@ -276,6 +277,7 @@ func (ma *memberAPI) updateSetting(c *gin.Context) { }) return } + dao.Conf.EnableIPChangeNotification = sf.EnableIPChangeNotification == "on" dao.Conf.Site.Brand = sf.Title dao.Conf.Site.Theme = sf.Theme dao.Conf.Site.CustomCode = sf.CustomCode diff --git a/model/config.go b/model/config.go index 1982f8e..616f92a 100644 --- a/model/config.go +++ b/model/config.go @@ -10,7 +10,6 @@ import ( "gopkg.in/yaml.v2" ) -// Config .. type Config struct { Debug bool Site struct { @@ -24,7 +23,8 @@ type Config struct { ClientID string ClientSecret string } - HTTPPort uint + HTTPPort uint + EnableIPChangeNotification bool v *viper.Viper } diff --git a/resource/static/main.js b/resource/static/main.js index aa5c653..2e14494 100644 --- a/resource/static/main.js +++ b/resource/static/main.js @@ -106,6 +106,8 @@ function addOrEditServer(server) { modal.find('.positive.button').html(server ? '修改' : '添加') modal.find('input[name=id]').val(server ? server.ID : null) modal.find('input[name=name]').val(server ? server.Name : null) + modal.find('input[name=Tag]').val(server ? server.Tag : null) + modal.find('input[name=DisplayIndex]').val(server ? server.DisplayIndex : null) if (server) { modal.find('.secret.field').attr('style', '') modal.find('input[name=secret]').val(server.Secret) diff --git a/resource/template/common/footer.html b/resource/template/common/footer.html index e5842a1..6726aee 100644 --- a/resource/template/common/footer.html +++ b/resource/template/common/footer.html @@ -9,7 +9,7 @@ - + diff --git a/resource/template/dashboard/setting.html b/resource/template/dashboard/setting.html index a65ab55..3c1e821 100644 --- a/resource/template/dashboard/setting.html +++ b/resource/template/dashboard/setting.html @@ -22,6 +22,12 @@ +
+
+ + +
+
@@ -40,5 +46,6 @@ }) return false; }) + $('.checkbox').checkbox() {{end}} \ No newline at end of file diff --git a/service/alertmanager/alertmanager.go b/service/alertmanager/alertmanager.go index ad43299..e56725a 100644 --- a/service/alertmanager/alertmanager.go +++ b/service/alertmanager/alertmanager.go @@ -158,7 +158,7 @@ func checkStatus() { } if flag { message := fmt.Sprintf("报警规则:%s,服务器:%s(%s),%s,逮到咯,快去看看!", alert.Name, server.Name, server.Host.IP, desc) - go sendNotification(message) + go SendNotification(message) } } // 清理旧数据 @@ -169,7 +169,7 @@ func checkStatus() { } } -func sendNotification(desc string) { +func SendNotification(desc string) { notificationsLock.RLock() defer notificationsLock.RUnlock() for i := 0; i < len(notifications); i++ { diff --git a/service/rpc/nezha.go b/service/rpc/nezha.go index e12003c..3a06b0a 100644 --- a/service/rpc/nezha.go +++ b/service/rpc/nezha.go @@ -2,11 +2,13 @@ package rpc import ( "context" + "fmt" "log" "time" "github.com/naiba/nezha/model" pb "github.com/naiba/nezha/proto" + "github.com/naiba/nezha/service/alertmanager" "github.com/naiba/nezha/service/dao" ) @@ -60,6 +62,14 @@ func (s *NezhaHandler) Register(c context.Context, r *pb.Host) (*pb.Receipt, err host := model.PB2Host(r) dao.ServerLock.RLock() defer dao.ServerLock.RUnlock() + if dao.ServerList[clientID].Host != nil && + dao.ServerList[clientID].Host.IP != "" && + host.IP != "" && + dao.ServerList[clientID].Host.IP != host.IP { + alertmanager.SendNotification(fmt.Sprintf( + "服务器:%s IP变更提醒,旧IP:%s,新IP:%s。", + dao.ServerList[clientID].Name, dao.ServerList[clientID].Host.IP, host.IP)) + } dao.ServerList[clientID].Host = &host return &pb.Receipt{Proced: true}, nil }