feat: ip change notification close #31

This commit is contained in:
naiba 2021-01-13 22:30:28 +08:00
parent c4f6837def
commit 0ce8017875
8 changed files with 35 additions and 9 deletions

View File

@ -166,6 +166,7 @@ func receiveCommand(hc pb.NezhaService_HeartbeatClient) error {
} }
func reportState() { func reportState() {
var lastReportHostInfo time.Time
var err error var err error
defer log.Printf("reportState exit %v => %v", time.Now(), err) defer log.Printf("reportState exit %v => %v", time.Now(), err)
for { for {
@ -176,6 +177,10 @@ func reportState() {
log.Printf("reportState error %v", err) log.Printf("reportState error %v", err)
time.Sleep(delayWhenError) time.Sleep(delayWhenError)
} }
if lastReportHostInfo.Before(time.Now().Add(-10 * time.Minute)) {
lastReportHostInfo = time.Now()
client.Register(ctx, monitor.GetHost().PB())
}
} }
} }
} }

View File

@ -261,10 +261,11 @@ func (ma *memberAPI) logout(c *gin.Context) {
} }
type settingForm struct { type settingForm struct {
Title string Title string
Admin string Admin string
Theme string Theme string
CustomCode string CustomCode string
EnableIPChangeNotification string
} }
func (ma *memberAPI) updateSetting(c *gin.Context) { func (ma *memberAPI) updateSetting(c *gin.Context) {
@ -276,6 +277,7 @@ func (ma *memberAPI) updateSetting(c *gin.Context) {
}) })
return return
} }
dao.Conf.EnableIPChangeNotification = sf.EnableIPChangeNotification == "on"
dao.Conf.Site.Brand = sf.Title dao.Conf.Site.Brand = sf.Title
dao.Conf.Site.Theme = sf.Theme dao.Conf.Site.Theme = sf.Theme
dao.Conf.Site.CustomCode = sf.CustomCode dao.Conf.Site.CustomCode = sf.CustomCode

View File

@ -10,7 +10,6 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
// Config ..
type Config struct { type Config struct {
Debug bool Debug bool
Site struct { Site struct {
@ -24,7 +23,8 @@ type Config struct {
ClientID string ClientID string
ClientSecret string ClientSecret string
} }
HTTPPort uint HTTPPort uint
EnableIPChangeNotification bool
v *viper.Viper v *viper.Viper
} }

View File

@ -106,6 +106,8 @@ function addOrEditServer(server) {
modal.find('.positive.button').html(server ? '修改<i class="edit icon"></i>' : '添加<i class="add icon"></i>') modal.find('.positive.button').html(server ? '修改<i class="edit icon"></i>' : '添加<i class="add icon"></i>')
modal.find('input[name=id]').val(server ? server.ID : null) modal.find('input[name=id]').val(server ? server.ID : null)
modal.find('input[name=name]').val(server ? server.Name : 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) { if (server) {
modal.find('.secret.field').attr('style', '') modal.find('.secret.field').attr('style', '')
modal.find('input[name=secret]').val(server.Secret) modal.find('input[name=secret]').val(server.Secret)

View File

@ -9,7 +9,7 @@
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.1/dist/semantic.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.1/dist/semantic.min.js"></script>
<script src="/static/semantic-ui-alerts.min.js"></script> <script src="/static/semantic-ui-alerts.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.min.js"></script>
<script src="/static/main.js?v202101082103"></script> <script src="/static/main.js?v202101132218"></script>
</body> </body>
</html> </html>

View File

@ -22,6 +22,12 @@
<label>自定义代码(style、script 都可以)</label> <label>自定义代码(style、script 都可以)</label>
<textarea name="CustomCode">{{.Conf.Site.CustomCode}}</textarea> <textarea name="CustomCode">{{.Conf.Site.CustomCode}}</textarea>
</div> </div>
<div class="field">
<div class="ui nf-ssl checkbox">
<input name="EnableIPChangeNotification" type="checkbox" tabindex="0" class="hidden">
<label>开启 IP 变更提醒</label>
</div>
</div>
<button class="ui button" type="submit">保存</button> <button class="ui button" type="submit">保存</button>
</form> </form>
</div> </div>
@ -40,5 +46,6 @@
}) })
return false; return false;
}) })
$('.checkbox').checkbox()
</script> </script>
{{end}} {{end}}

View File

@ -158,7 +158,7 @@ func checkStatus() {
} }
if flag { if flag {
message := fmt.Sprintf("报警规则:%s服务器%s(%s)%s逮到咯快去看看", alert.Name, server.Name, server.Host.IP, desc) 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() notificationsLock.RLock()
defer notificationsLock.RUnlock() defer notificationsLock.RUnlock()
for i := 0; i < len(notifications); i++ { for i := 0; i < len(notifications); i++ {

View File

@ -2,11 +2,13 @@ package rpc
import ( import (
"context" "context"
"fmt"
"log" "log"
"time" "time"
"github.com/naiba/nezha/model" "github.com/naiba/nezha/model"
pb "github.com/naiba/nezha/proto" pb "github.com/naiba/nezha/proto"
"github.com/naiba/nezha/service/alertmanager"
"github.com/naiba/nezha/service/dao" "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) host := model.PB2Host(r)
dao.ServerLock.RLock() dao.ServerLock.RLock()
defer dao.ServerLock.RUnlock() 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 dao.ServerList[clientID].Host = &host
return &pb.Receipt{Proced: true}, nil return &pb.Receipt{Proced: true}, nil
} }